Skip to content

Commit

Permalink
Support watch and bump the version.
Browse files Browse the repository at this point in the history
  • Loading branch information
lazywei committed Jan 6, 2017
1 parent 073d843 commit c890007
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 14 deletions.
19 changes: 14 additions & 5 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@

MIT License

Copyright (c) 2017, Chih-Wei Chang

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ This will install a command line tool `psync`, the usage of which is described b
. .. README
```
3. Run `psync watch` to watch any modification under the project root and perform sync automatically.
### Usage Demo
![Usage Demo](demo-usage.gif)
Expand Down
57 changes: 51 additions & 6 deletions psync/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
import os
import yaml
from psync import psync
from psync import watcher
from watchdog.observers import Observer


def get_project_root():
cwd = os.getcwd()
root = psync.project_root(start_from=cwd)

if root is None:
return False, None
else:
return True, root


def ask_for_configs():
Expand All @@ -29,15 +41,22 @@ def ask_for_configs():
ignores=ignores)


@click.command()
def main(args=None):
@click.group(invoke_without_command=True)
@click.pass_context
def cli(ctx):
"""Console script for psync"""
if ctx.invoked_subcommand is None:
perform_sync()
else:
pass

cwd = os.getcwd()
root = psync.project_root(start_from=cwd)

if root is None:
def perform_sync():
is_proj, root = get_project_root()

if not is_proj:
click.echo("You are not in a project (no .psync found)!")
cwd = os.getcwd()
gen_conf = click.prompt(
"Generate .psync to current directory ({}) [Y/n]?".format(cwd),
default="Y")
Expand Down Expand Up @@ -70,5 +89,31 @@ def main(args=None):
click.echo("--- Sync Finished ---")


@cli.command()
def watch():
import time
is_proj, root = get_project_root()

state = {"dirty": False}

if not is_proj:
click.echo("Run psync to generate .psync config file.")
else:
event_handler = watcher.AnyEventHandler(state)
observer = Observer()
observer.schedule(event_handler, root, recursive=True)
observer.start()
try:
while True:
if state["dirty"]:
click.echo("Detect modification. Perform sync.")
perform_sync()
state["dirty"] = False
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()


if __name__ == "__main__":
main()
cli()
13 changes: 13 additions & 0 deletions psync/watcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from watchdog.events import FileSystemEventHandler


class AnyEventHandler(FileSystemEventHandler):
def __init__(self, state):
super(AnyEventHandler, self).__init__()
# A dirty way to emit changes to the outside world.
# Should try to use other pure way to do this.
self.state = state

def on_any_event(self, event):
super(AnyEventHandler, self).on_any_event(event)
self.state["dirty"] = True
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
requirements = [
'Click>=6.0',
'PyYAML>=3.12',
'watchdog>=0.8.3',
]

test_requirements = [
Expand All @@ -17,8 +18,8 @@

setup(
name='project-sync',
version='0.1.0',
description="A simple tool for synchronizing local project (files)"
version='0.1.1',
description="A simple tool for synchronizing local project (files) "
"to remote server.",
long_description=readme,

Expand All @@ -34,7 +35,7 @@

entry_points={
'console_scripts': [
'psync=psync.cli:main'
'psync=psync.cli:cli'
]
},

Expand Down

0 comments on commit c890007

Please sign in to comment.