def upload(args, parser): """Upload a file tree to a remote server. """ parser.add_option('-C', '--compare', action='store_true', default=False, dest='compare', help="Compare remote files that need to be changed " "to local files (implies -D.)") options, args = parser.parse_args(args) if options.compare: options.dry_run = True index = create_file_index(options) if not index: return 0 session = FTPSession( options=options, host=parser.config.get('destination', 'host'), username=parser.config.get('destination', 'username'), password=parser.config.get('destination', 'password'), index=index) session.connect() if not options._continue: print "Downloading file index from server..." index.download(session) print "Reading file index..." index.load() def process(filename): session.upload_if_newer(filename) print "Traversing current directory..." traverse_dir('.', process, options) print "Uploading file index to server..." index.upload(session) print "Successfully uploaded file index, backing it up..." do_it("mv %s %s.bak" % (index.filename, index.filename)) print "All done, logging off." session.disconnect() return 0
def upload_to(self, local_filename, remote_filename): if self.options.dry_run: print "WOULD UPLOAD %s -> %s" % (local_filename, remote_filename) if self.options.compare and remote_filename != '/filehash.txt': temp_filename = "/tmp/file" try: self.download_to(remote_filename, temp_filename) except Exception as e: print str(e) do_it("echo -n '' >%s" % temp_filename) do_it("diff -u '%s' '%s' || echo" % (temp_filename, local_filename)) return False print "UPLOAD %s -> %s" % (local_filename, remote_filename) dir = dirname(remote_filename) self.mkdir(dir) tries = 0 local_file = open(local_filename) done = False while not done: try: stor = "STOR %s" % remote_filename.encode('utf-8') self.client.storbinary(stor, local_file) done = True except Exception, e: print "FAILURE: FTP STOR failed: " + str(e) if tries < 10: self.reconnect() tries += 1 print "RETRY UPLOAD %s -> %s" % (local_filename, remote_filename) else: msg = "Can't connect to server, tried %s times" % tries raise Exception(msg)