Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Kyle Thomas <kyle@kylethomas.io>
- ##
- ## This file will only upload EXISTING files. Any files you add to the server
- ## you will need to manually copy the first time. If this isn't your desired
- ## behavior, just remove the "if ftp.size(...) is not None:" logic.
- ##
- ## ftp.size() is supposed to return None if the file does not exist. This does
- ## not seem to be standardized though. Some servers throw an error if the file
- ## you are testing does not exist.
- ##
- ## If you are trying to move binary files you'll need to modify the following
- ## lines since binary and non-binary files are handled differently over FTP.
- ## - Open the file as binary ("r" => "rb")
- ## `open(local_root_dir + modified_file, "rb")`
- ## - Use the command `ftp.storbinary` instead of `ftp.storlines`
- ##
- ## *_root_dir variables require trailing slash
- import subprocess
- import ftplib
- # Set Parameters
- local_root_dir = "/projects/my_project/"
- remote_root_dir = "/my_project/kyle/"
- ftp_hostname = "ftp.example.com"
- ftp_username = "kyle"
- ftp_password = "password"
- try:
- # Get a list of modified files from git
- modified_files = subprocess.check_output(["git", "diff", "--name-only"],
- stderr=subprocess.STDOUT,
- cwd=local_root_dir)
- except subprocess.CalledProcessError as e:
- print "Exception: " + str(e)
- print ("Exception: Are you sure '%s' is a git repository?" % local_root_dir)
- exit()
- # There is an extra newline at the end
- modified_file_list = modified_files.split("\n")
- modified_file_list.remove("")
- if modified_file_list is None:
- print "Nothing to sync"
- exit()
- # Open FTP connection
- ftp = ftplib.FTP(ftp_hostname)
- ftp.login(ftp_username, ftp_password)
- # Copy any files modified in git to remote directory
- for modified_file in modified_file_list:
- # Fixes: "550 SIZE not allowed in ASCII mode"
- ftp.voidcmd('TYPE I')
- # Check file exists and upload
- if ftp.size(remote_root_dir + modified_file) is not None:
- with open(local_root_dir + modified_file, "r") as local_file:
- ftp.storlines('STOR ' + remote_root_dir + modified_file, local_file)
- print modified_file + " uploaded..."
- else :
- print "WARNING: " + modified_file + " skipped..."
- # Close FTP connection
- ftp.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement