rsync.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python
  2. #encoding:utf-8
  3. import sys
  4. import os
  5. import string
  6. import getpass
  7. __author__ = 'ZhenQin'
  8. NODE_FILE = "nodes"
  9. COMMAND = "scp "
  10. def rsync(files):
  11. PARAMS = ""
  12. nodes = getNodes(NODE_FILE)
  13. if len(nodes) > 0:
  14. for file in files:
  15. #dist_dir = os.path.dirname(file)
  16. dist_dir = os.path.abspath(file)
  17. file_name = os.path.basename(dist_dir)
  18. if dist_dir == "":
  19. dist_dir = os.getcwd()
  20. dist_dir = dist_dir[:dist_dir.index(file_name) - 1]
  21. print file_name, " ==> ", dist_dir
  22. if os.path.isdir(file):
  23. PARAMS = " -r "
  24. for node in nodes:
  25. cmd = COMMAND + PARAMS + file + " " + getpass.getuser()+"@"+node+":"+dist_dir
  26. print cmd
  27. result = os.popen(cmd).readlines()
  28. print result
  29. else:
  30. print "no nodes"
  31. def getNodes(filePath):
  32. nodes = list()
  33. for line in open(filePath, "r"):
  34. nodes.append(string.strip(line))
  35. return nodes
  36. if __name__ == "__main__" :
  37. if len(sys.argv) == 1:
  38. print "python rsync.py file1 [file2,...]"
  39. rsync(sys.argv[1:])