Skip to content

Instantly share code, notes, and snippets.

@michael34435
Last active August 29, 2015 14:26
Show Gist options
  • Save michael34435/f1a7c7a3f363bc6ef655 to your computer and use it in GitHub Desktop.
Save michael34435/f1a7c7a3f363bc6ef655 to your computer and use it in GitHub Desktop.
a tool to upload to remote with tar and ssh
#!/usr/local/bin/python
# coding=utf-8
# Copyright (c) 2015 CapsLock, Studio All Rights Reserved.
import os
import sys
import uuid
def main():
argument = ['--help', '-t', '-i', '-h', '-o']
data = {}
flag = False
for arg in argument:
for argv in sys.argv:
if flag is True:
flag = False
if argv in argument:
data[argv] = data.get(argv)
else:
data[arg] = argv
continue
if arg == argv:
flag = True
data[argv] = data.get(argv)
temp_folder = None
input_file = None
host = None
output_path = None
try:
data['--help']
print '\nUse tar and ssh to upload file to the folder.\nUsage: %s -i input_file -h host [-o output_path] [-t temp_folder]\n\temail: michael34435[at]gmail.com' % __file__
sys.exit(0)
except Exception as e:
pass
try:
input_file = data.get('-i', (_ for _ in ()).throw(Exception('argument `-i’ is required.')) if data.get('-i') is None else None)
host = data.get('-h', (_ for _ in ()).throw(Exception('argument `-h’ is required.')) if data.get('-h') is None else None)
except Exception as e:
print e
sys.exit(0)
temp_folder = data.get('-t', '~')
output_path = data.get('-o', '~')
_file = str(uuid.uuid1())
os.system('tar -zc %s | ssh %s "cat > %s/%s.tar.gz && tar zxvf %s/%s.tar.gz -C %s; rm -rf %s/%s.tar.gz"' % tuple([input_file, host] + [temp_folder, _file] * 2 + [output_path] + [temp_folder, _file]))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment