Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. ## Kyle Thomas <kyle@kylethomas.io>
  2. ##
  3. ## This file will only upload EXISTING files. Any files you add to the server
  4. ## you will need to manually copy the first time. If this isn't your desired
  5. ## behavior, just remove the "if ftp.size(...) is not None:" logic.
  6. ##
  7. ## ftp.size() is supposed to return None if the file does not exist. This does
  8. ## not seem to be standardized though. Some servers throw an error if the file
  9. ## you are testing does not exist.
  10. ##
  11. ## If you are trying to move binary files you'll need to modify the following
  12. ## lines since binary and non-binary files are handled differently over FTP.
  13. ## - Open the file as binary ("r" => "rb")
  14. ## `open(local_root_dir + modified_file, "rb")`
  15. ## - Use the command `ftp.storbinary` instead of `ftp.storlines`
  16. ##
  17. ## *_root_dir variables require trailing slash
  18.  
  19. import subprocess
  20. import ftplib
  21.  
  22. # Set Parameters
  23. local_root_dir = "/projects/my_project/"
  24. remote_root_dir = "/my_project/kyle/"
  25. ftp_hostname = "ftp.example.com"
  26. ftp_username = "kyle"
  27. ftp_password = "password"
  28.  
  29. try:
  30. # Get a list of modified files from git
  31. modified_files = subprocess.check_output(["git", "diff", "--name-only"],
  32. stderr=subprocess.STDOUT,
  33. cwd=local_root_dir)
  34. except subprocess.CalledProcessError as e:
  35. print "Exception: " + str(e)
  36. print ("Exception: Are you sure '%s' is a git repository?" % local_root_dir)
  37. exit()
  38.  
  39. # There is an extra newline at the end
  40. modified_file_list = modified_files.split("\n")
  41. modified_file_list.remove("")
  42.  
  43. if modified_file_list is None:
  44. print "Nothing to sync"
  45. exit()
  46.  
  47. # Open FTP connection
  48. ftp = ftplib.FTP(ftp_hostname)
  49. ftp.login(ftp_username, ftp_password)
  50.  
  51. # Copy any files modified in git to remote directory
  52. for modified_file in modified_file_list:
  53.  
  54. # Fixes: "550 SIZE not allowed in ASCII mode"
  55. ftp.voidcmd('TYPE I')
  56.  
  57. # Check file exists and upload
  58. if ftp.size(remote_root_dir + modified_file) is not None:
  59.  
  60. with open(local_root_dir + modified_file, "r") as local_file:
  61. ftp.storlines('STOR ' + remote_root_dir + modified_file, local_file)
  62.  
  63. print modified_file + " uploaded..."
  64.  
  65. else :
  66. print "WARNING: " + modified_file + " skipped..."
  67.  
  68. # Close FTP connection
  69. ftp.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement