Skip to content

Commit

Permalink
Merge pull request #1 from uzh-dqbm-cmi/initialization
Browse files Browse the repository at this point in the history
Initialization
  • Loading branch information
lokijuhy authored Mar 29, 2021
2 parents 0db7257 + bcd8bdd commit 7795072
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 6 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ name: Python package

on:
push:
branches: [ main ]
branches: [ master ]
pull_request:
branches: [ main ]
branches: [ master ]

jobs:
build:
Expand All @@ -34,6 +34,6 @@ jobs:
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
# - name: Test with pytest
# run: |
# pytest
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,34 @@
# code-sync
# code_sync
Python utility for syncing code to a remote machine in the background

## Installation
`pip install code_sync`

## Usage

After installing this package, the `code_sync` tool will be available from the command line.

The `code_sync` script allows you to auto-sync any changes to code in a local directory to a remote machine.
Under the hood, it is running an `rsync` command whenever `watchdog` notices changes to the code.

### Example usage
Assuming you have defined `my_remote_machine` in your ssh config:

`code_sync --local_dir mylocaldir/ --remote_dir myremotedir/ --target my_remote_machine --port 2222`

### Notes
**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 happen until a file in the `local_dir` is touched. This is normal!

**Stopping**
* You can safely quit `code_sync` with control-c.

**About `code_sync` + `git`**
* The destination directory should not be treated as an active git repo.
The destination dir must exist already, but need not already be empty.
If the destination directory is a git repo already, it will be overwritten with the "git state" of the local git directory.
* **Do not run git commands from the destination terminal** on the destination directory.
The destination dir will have its contents synced to exactly match the local dir, including when you checkout a different branch on local.
* The sync command adheres to any filters set by `.gitignore` files within the specified directories.
It also excludes `.git` and `.ipynb` files.
Empty file added code_sync/__init__.py
Empty file.
41 changes: 41 additions & 0 deletions code_sync/code_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#! python

import argparse
import os
import subprocess

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

epilog_str = '''
Example for connecting to LeoMed:
code_sync --local_dir mylocaldir/ --remote_dir myremotedir/ --target medinfmk --port 2222\n
'''


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

# subprocess.call()
cmd = cmd_str.format(local_dir=local_dir, remote_dir=remote_dir, target=target, port=port)
subprocess.call(cmd, shell=True)


def main():
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, epilog=epilog_str)
parser.add_argument('--local_dir', help='the local code directory you want to sync', required=True)
parser.add_argument('--remote_dir', help='the remote directory you want to sync', required=True)
parser.add_argument('--target', help='specify which remote machine to connect to', required=True)
parser.add_argument('--port', type=int, help='ssh port for connecting to remote', default=22)

args = parser.parse_args()

code_sync(local_dir=args.local_dir, remote_dir=args.remote_dir, target=args.target, port=args.port)


if __name__ == '__main__':
main()
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 120
exclude = .git,__pycache__,tests,docs,build
19 changes: 19 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from setuptools import setup, find_packages

setup(name='code_sync',
version='0.0.1',
description='',
url='https://github.com/uzh-dqbm-cmi/code-sync',
packages=find_packages(),
python_requires='>3.6.0',
install_requires=[
'watchdog',
'pyyaml',
'argh',
],
entry_points={
'console_scripts': [
'code_sync = code_sync.code_sync:main',
]
},
zip_safe=False)

0 comments on commit 7795072

Please sign in to comment.