Skip to content

Commit

Permalink
add new argument to not watch the copied folders (#28)
Browse files Browse the repository at this point in the history
* add new argument to not watch the copied folders

* add documentation for --no-watch
  • Loading branch information
codingpaula committed May 7, 2024
1 parent 01a6bcf commit 8768c5e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Options:
interval in which mutex is updated (default: 10 seconds)
- `--ignore-mutex`
ignore mutex (use with caution) (default: False)
- `--no-watch`
don't keep watching the copied folders for changes after the sync (default: False)

### Python

Expand All @@ -64,6 +66,7 @@ sync(
```

The `sync` call will block until the script is aborted.
Only if `watch=False` is used, the `sync` call will end after copying the folders to the target once.
The `Folder` class allows to set the `port` and an `on_change` bash command which is executed after a sync has been performed.
Via the `rsync_args` build method you can pass additional options to configure rsync.

Expand Down
3 changes: 2 additions & 1 deletion livesync/livesync.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ def main():
parser.add_argument('--on-change', type=str, help='command to be executed on remote host after any file change')
parser.add_argument('--mutex-interval', type=int, default=10, help='interval in which mutex is updated')
parser.add_argument('--ignore-mutex', action='store_true', help='ignore mutex (use with caution)')
parser.add_argument('--no-watch', action='store_true', help='do not watch for changes')
parser.add_argument('rsync_args', nargs=argparse.REMAINDER, help='arbitrary rsync parameters after "--"')
args = parser.parse_args()

folder = Folder(args.source, args.target, ssh_port=args.ssh_port, on_change=args.on_change)
folder.rsync_args(' '.join(args.rsync_args))
sync(folder, mutex_interval=args.mutex_interval, ignore_mutex=args.ignore_mutex)
sync(folder, mutex_interval=args.mutex_interval, ignore_mutex=args.ignore_mutex, watch=not args.no_watch)


if __name__ == '__main__':
Expand Down
31 changes: 17 additions & 14 deletions livesync/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def get_summary(folders: Iterable[Folder]) -> str:
return '\n'.join(folder.get_summary() for folder in folders).replace('"', '\'')


async def run_folder_tasks(folders: Iterable[Folder], mutex_interval: float, ignore_mutex: bool = False) -> None:
async def run_folder_tasks(
folders: Iterable[Folder],
mutex_interval: float, ignore_mutex: bool = False, watch: bool = True) -> None:
try:
if not ignore_mutex:
summary = get_summary(folders)
Expand All @@ -25,23 +27,24 @@ async def run_folder_tasks(folders: Iterable[Folder], mutex_interval: float, ign
print(f' {folder.source_path} --> {folder.target}', flush=True)
folder.sync()

for folder in folders:
print(f'Watch folder {folder.source_path}', flush=True)
asyncio.create_task(folder.watch())

while True:
if not ignore_mutex:
summary = get_summary(folders)
for mutex in mutexes.values():
if not mutex.set(summary):
break
await asyncio.sleep(mutex_interval)
if watch:
for folder in folders:
print(f'Watch folder {folder.source_path}', flush=True)
asyncio.create_task(folder.watch())

while True:
if not ignore_mutex:
summary = get_summary(folders)
for mutex in mutexes.values():
if not mutex.set(summary):
break
await asyncio.sleep(mutex_interval)
except Exception as e:
print(e)


def sync(*folders: Folder, mutex_interval: float = 10, ignore_mutex: bool = False) -> None:
def sync(*folders: Folder, mutex_interval: float = 10, ignore_mutex: bool = False, watch: bool = True) -> None:
try:
asyncio.run(run_folder_tasks(folders, mutex_interval, ignore_mutex=ignore_mutex))
asyncio.run(run_folder_tasks(folders, mutex_interval, ignore_mutex=ignore_mutex, watch=watch))
except KeyboardInterrupt:
print('Bye!')

0 comments on commit 8768c5e

Please sign in to comment.