Skip to content

Instantly share code, notes, and snippets.

@samuelantonioli
Last active August 31, 2017 15:11
Show Gist options
  • Save samuelantonioli/0ed497f038c59517ce8201ed3cea477f to your computer and use it in GitHub Desktop.
Save samuelantonioli/0ed497f038c59517ce8201ed3cea477f to your computer and use it in GitHub Desktop.
scp_changed - upload modified files of your git repository to your server
#!/usr/bin/env python
'''
# scp_changed
Upload changed files in a git repository to a server that has a copy of the project
## installation
save it as scp_changed
chmod +x scp_changed
echo alias scp_changed=\'$(pwd)/scp_changed\' >> .bash_profile
## usage
scp_changed ?
'''
from os import system, getcwd
from os.path import exists
from sys import argv
import subprocess
__DEFAULT_SERVER__ = 'myserver'
__KNOWN_CWD_CONFIGURATIONS__ = {
'/Users/demo/projects/deusex': ('devbox', 'projects/deusex'),
}
def upload_changed(server, server_project_path):
'''
upload changed files of git repository using scp
'''
if not exists('.git'):
print('not a git repository')
return
sp = server_project_path
cwd = getcwd()
files = subprocess.check_output('git ls-files -m --full-name', shell=True)
for file in files.split('\n'):
if not file: continue
c = 'scp {cwd}/{path} {server}:{server_path}/{path}'.format(
cwd=cwd,
path=file,
server_path=sp[:-1] if sp[-1] == '/' else sp,
server=server,
)
print(c)
system(c)
def usage():
print('''
scp_changed - upload modified files of your git repository to your server.
usage:
scp_changed server_name:projects/deusex
scp_changed <path> (use default server)
scp_changed <path> <server>
scp_changed (determine configuration based on current working directory)
''')
def main():
args = argv[1:]
args_len = len(args)
if args_len == 0:
cwd = getcwd()
config = __KNOWN_CWD_CONFIGURATIONS__.get(cwd)
if config:
print('found config for current working directory')
upload_changed(*config)
else:
usage()
elif args_len == 1:
arg = args[0]
if ':' in arg:
# server:path pair
try:
server, path = arg.split(':')
if server and path:
upload_changed(server, path)
return
except Exception:
pass
usage()
elif '?' == arg:
usage()
elif arg:
upload_changed(__DEFAULT_SERVER__, arg)
else:
usage()
elif args_len == 2:
upload_changed(args[1], args[0])
else:
usage()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment