def sync(self, source, destination): """Synchronise two locations.""" start_time = time.time() self.source = normalise_url(source) self.destination = normalise_url(destination) # Instantiate the transports. try: self.source_transport = self.transports[url_split( self.source).scheme]() except KeyError: log.error("Protocol not supported: %s." % url_split(self.source).scheme) return try: self.destination_transport = self.transports[url_split( self.destination).scheme]() except KeyError: log.error("Protocol not supported: %s." % url_split(self.destination).scheme) return # Give the transports a chance to connect to their servers. try: self.source_transport.connect(self.source, self.config) except: log.error("Connection to source failed, exiting...") self.exit(1) try: self.destination_transport.connect(self.destination, self.config) except: log.error("Connection to destination failed, exiting...") self.exit(1) # These are the most attributes we can expect from getattr calls in these two protocols. self.max_attributes = (self.source_transport.getattr_attributes & self.destination_transport.getattr_attributes) self.max_evaluation_attributes = ( self.source_transport.evaluation_attributes & self.destination_transport.evaluation_attributes) if not self.check_locations(): self.exit(1) # Begin the actual synchronisation. self.recurse() self.source_transport.disconnect() self.destination_transport.disconnect() total_time = time.time() - start_time locale.setlocale(locale.LC_NUMERIC, '') try: bps = locale.format("%d", int(self.bytes_total / total_time), True) except ZeroDivisionError: bps = "inf" log.info("Copied %s files (%s bytes) in %s sec (%s Bps)." % (locale.format("%d", self.file_counter, True), locale.format("%d", self.bytes_total, True), locale.format("%.2f", total_time, True), bps))
def sync(self, source, destination): """Synchronise two locations.""" start_time = time.time() self.source = normalise_url(source) self.destination = normalise_url(destination) # Instantiate the transports. try: self.source_transport = self.transports[url_split(self.source).scheme]() except KeyError: log.error("Protocol not supported: %s." % url_split(self.source).scheme) return try: self.destination_transport = self.transports[url_split(self.destination).scheme]() except KeyError: log.error("Protocol not supported: %s." % url_split(self.destination).scheme) return # Give the transports a chance to connect to their servers. try: self.source_transport.connect(self.source, self.config) except: log.error("Connection to source failed, exiting...") self.exit(1) try: self.destination_transport.connect(self.destination, self.config) except: log.error("Connection to destination failed, exiting...") self.exit(1) # These are the most attributes we can expect from getattr calls in these two protocols. self.max_attributes = (self.source_transport.getattr_attributes & self.destination_transport.getattr_attributes) self.max_evaluation_attributes = (self.source_transport.evaluation_attributes & self.destination_transport.evaluation_attributes) if not self.check_locations(): self.exit(1) # Begin the actual synchronisation. self.recurse() self.source_transport.disconnect() self.destination_transport.disconnect() total_time = time.time() - start_time locale.setlocale(locale.LC_NUMERIC, '') try: bps = locale.format("%d", int(self.bytes_total / total_time), True) except ZeroDivisionError: bps = "inf" log.info("Copied %s files (%s bytes) in %s sec (%s Bps)." % ( locale.format("%d", self.file_counter, True), locale.format("%d", self.bytes_total, True), locale.format("%.2f", total_time, True), bps))
def test_urls(self): """Test URL normalisation.""" urls = ( ("C:\\test\\file", "file://C:/test/file"), ("C:\\test\\directory\\", "file://C:/test/directory/"), ("file", "file://file"), ("/root/file", "file:///root/file"), ("/root/dir/", "file:///root/dir/"), ) for test, expected_output in urls: self.assertEqual(urlfunctions.normalise_url(test), expected_output)