def download(args, parser): """Download locally non-existant files from a remote server. """ options, args = parser.parse_args(args) session = FTPSession( options=options, host=parser.config.get('destination', 'host'), username=parser.config.get('destination', 'username'), password=parser.config.get('destination', 'password')) session.connect() def process(dir, remote_dir, filename): destfile = os.path.normpath(os.path.join('.', dir, filename)) if os.path.exists(destfile): pass elif options.dry_run: print "WOULD download '%s/%s' to '%s'" % ( remote_dir, filename, destfile) else: print "downloading '%s/%s' to '%s'" % ( remote_dir, filename, destfile) session.download_to(filename, destfile) print "Traversing FTP directory..." session.traverse_ftp_dir(process, '.') print "All done, logging off." session.disconnect() return 0
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