Skip to content

Commit

Permalink
Merge pull request #4 from uzh-dqbm-cmi/run_initial_sync
Browse files Browse the repository at this point in the history
Run initial sync at startup and add verbose flag
  • Loading branch information
lokijuhy authored Jun 25, 2021
2 parents b095e78 + 5a11fad commit 0b968e7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ This command will use the configuration you set for the project when you registe
**Starting**
* In order to run `code_sync`, you must have an ssh connection open in another window.
Once you've entered your password there, `code_sync` uses that connection.
* When you start this script, nothing will be synced until a file in the `local_dir` is touched. This is normal!
* The destination dir must exist already, but need not be empty.

**Stopping**
Expand Down
28 changes: 21 additions & 7 deletions code_sync/code_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
from typing import Dict


cmd_str = 'watchmedo shell-command --recursive --patterns="{local_dir}*" --command="rsync --filter=\':- .gitignore\' ' \
rsync_cmd_str = 'rsync --filter=\':- .gitignore\' ' \
'--exclude \'*.ipynb\' --exclude \'.git\' --delete-after -rz --port {port} {local_dir} ' \
'{target}:{remote_dir}" {local_dir}'
'{target}:{remote_dir}'

cmd_str = 'watchmedo shell-command --recursive --patterns="{local_dir}*" --command="{rsync_cmd}" {local_dir}'

epilog_str = '''
EXAMPLE USAGE
Expand All @@ -31,15 +33,26 @@
CONFIG_FILE_NAME = '.code_sync'


def code_sync(local_dir, remote_dir, target, port=22):
def code_sync(local_dir, remote_dir, target, port=22, verbose=False):
# clean up slashes
local_dir = os.path.expanduser(local_dir)
local_dir = os.path.join(local_dir, '')
remote_dir = os.path.join(remote_dir, '')

print(f"Starting code_sync between {local_dir} and {target}:{remote_dir} ...")
rsync_cmd = rsync_cmd_str.format(local_dir=local_dir, remote_dir=remote_dir, target=target, port=port)
print('Running startup sync:')
if verbose:
print('Running command: {}'.format(rsync_cmd))
subprocess.call(rsync_cmd, shell=True)
print(' ... startup sync complete.')

watchmedo_cmd = cmd_str.format(rsync_cmd=rsync_cmd, local_dir=local_dir)
print('Code-sync running')
if verbose:
print('Running command: {}'.format(watchmedo_cmd))
print('(^C to quit)')
cmd = cmd_str.format(local_dir=local_dir, remote_dir=remote_dir, target=target, port=port)
subprocess.call(cmd, shell=True)
subprocess.call(watchmedo_cmd, shell=True)


def get_config_file_path() -> Path:
Expand Down Expand Up @@ -159,7 +172,7 @@ def identify_code_sync_parameters(args) -> Dict:
parameters['local_dir'] = args.local_dir
parameters['remote_dir'] = args.remote_dir
parameters['target'] = args.target
parameters['port'] = args.local_dir
parameters['port'] = args.port
return parameters


Expand All @@ -172,6 +185,7 @@ def main():
parser.add_argument('--remote_dir', help='The remote directory you want to sync', required=False)
parser.add_argument('--target', help='Specify which remote machine to connect to', required=False)
parser.add_argument('--port', type=int, help='ssh port for connecting to remote', default=22)
parser.add_argument('--verbose', help='Print verbose output', default=False, action='store_true')
args = parser.parse_args()

if args.register is not None:
Expand All @@ -181,7 +195,7 @@ def main():
else:
params = identify_code_sync_parameters(args)
code_sync(local_dir=params['local_dir'], remote_dir=params['remote_dir'], target=params['target'],
port=params['port'])
port=params['port'], verbose=args.verbose)


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages

setup(name='code_sync',
version='0.1.0',
version='0.2.0',
description='',
url='https://github.com/uzh-dqbm-cmi/code-sync',
packages=find_packages(),
Expand Down

0 comments on commit 0b968e7

Please sign in to comment.