Exemplo n.º 1
0
    def recurse(self):
        """Recursively synchronise everything."""
        source_dir_list = self.source_transport.listdir(self.source)
        dest = FileObject(self.destination_transport, self.destination)
        # If the source is a file, rather than a directory, just copy it. We know for sure that
        # it exists from the checks we did before, so the "False" return value can't be because
        # of that.
        if not source_dir_list:
            # If the destination ends in a slash or is an actual directory:
            if self.destination.endswith("/") or dest.isdir:
                if not dest.isdir:
                    self.destination_transport.mkdir(dest.url)
                # Splice the source filename onto the destination URL.
                dest_url = url_split(dest.url)
                dest_url.file = url_split(self.source,
                                          uses_hostname=self.source_transport.uses_hostname,
                                          split_filename=True).file
                dest_url = url_join(dest_url)
            else:
                dest_url = self.destination
            self.compare_and_copy(
                FileObject(self.source_transport, self.source, {"isdir": False}),
                FileObject(self.destination_transport, dest_url, {"isdir": False}),
                )
            return

        # If source is a directory...
        directory_stack = [FileObject(self.source_transport, self.source, {"isdir": True})]

        # Depth-first tree traversal.
        while directory_stack:
            # TODO: Rethink the assumption that a file cannot have the same name as a directory.
            item = directory_stack.pop()
            log.debug("URL %s is %sa directory." % \
                          (item.url, not item.isdir and "not " or ""))
            if item.isdir:
                # Don't skip the first directory.
                if not self.config.recursive and item.url != self.source:
                    log.info("Skipping directory %s..." % item)
                    continue
                # Obtain a directory list.
                new_dir_list = []
                for new_file in reversed(self.source_transport.listdir(item.url)):
                    if self.include_file(new_file):
                        new_dir_list.append(new_file)
                    else:
                        log.debug("Skipping %s..." % (new_file))
                dest = url_splice(self.source, item.url, self.destination)
                dest = FileObject(self.destination_transport, dest)
                log.debug("Comparing directories %s and %s..." % (item.url, dest.url))
                self.compare_directories(item, new_dir_list, dest.url)
                directory_stack.extend(new_dir_list)
            else:
                dest_url = url_splice(self.source, item.url, self.destination)
                log.debug("Destination URL is %s." % dest_url)
                dest = FileObject(self.destination_transport, dest_url)
                self.compare_and_copy(item, dest)
Exemplo n.º 2
0
 def test_url_join(self):
     """Test url_join."""
     tests = (
         ("http://*****:*****@myhost:80/some/path/file;things?myhost=hi#lala", True, True),
         ("http://*****:*****@myhost:80/some/path/;things?myhost=hi#lala", True, True),
         ("http://user@myhost/file;things?myhost=hi#lala", True, True),
         ("http://myhost/;things?myhost=hi#lala", True, True),
         ("http://*****:*****@myhost:80/?myhost=hi#lala", True, True),
         ("myhost/", True, True),
         ("user:pass@myhost:80/", True, True),
         ("user:pass@myhost/some#lala", True, True),
         ("http://myhost:80/;things?myhost=hi#lala", True, True),
         ("http://myhost/#lala", True, True),
         ("file://path", False, True),
         ("file://path/file", False, True),
         ("file:///path", False, True),
         ("file:///path/file", False, True),
         ("file:///path/file?something=else", False, True),
     )
     for test in tests:
         self.assertEqual(urlfunctions.url_join(urlfunctions.url_split(*test)), test[0])
Exemplo n.º 3
0
 def test_url_join(self):
     """Test url_join."""
     tests = (
         ("http://*****:*****@myhost:80/some/path/file;things?myhost=hi#lala",
          True, True),
         ("http://*****:*****@myhost:80/some/path/;things?myhost=hi#lala",
          True, True),
         ("http://user@myhost/file;things?myhost=hi#lala", True, True),
         ("http://myhost/;things?myhost=hi#lala", True, True),
         ("http://*****:*****@myhost:80/?myhost=hi#lala", True, True),
         ("myhost/", True, True),
         ("user:pass@myhost:80/", True, True),
         ("user:pass@myhost/some#lala", True, True),
         ("http://myhost:80/;things?myhost=hi#lala", True, True),
         ("http://myhost/#lala", True, True),
         ("file://path", False, True),
         ("file://path/file", False, True),
         ("file:///path", False, True),
         ("file:///path/file", False, True),
         ("file:///path/file?something=else", False, True),
     )
     for test in tests:
         self.assertEqual(
             urlfunctions.url_join(urlfunctions.url_split(*test)), test[0])
Exemplo n.º 4
0
    def recurse(self):
        """Recursively synchronise everything."""
        source_dir_list = self.source_transport.listdir(self.source)
        dest = FileObject(self.destination_transport, self.destination)
        # If the source is a file, rather than a directory, just copy it. We know for sure that
        # it exists from the checks we did before, so the "False" return value can't be because
        # of that.
        if not source_dir_list:
            # If the destination ends in a slash or is an actual directory:
            if self.destination.endswith("/") or dest.isdir:
                if not dest.isdir:
                    self.destination_transport.mkdir(dest.url)
                # Splice the source filename onto the destination URL.
                dest_url = url_split(dest.url)
                dest_url.file = url_split(
                    self.source,
                    uses_hostname=self.source_transport.uses_hostname,
                    split_filename=True).file
                dest_url = url_join(dest_url)
            else:
                dest_url = self.destination
            self.compare_and_copy(
                FileObject(self.source_transport, self.source,
                           {"isdir": False}),
                FileObject(self.destination_transport, dest_url,
                           {"isdir": False}),
            )
            return

        # If source is a directory...
        directory_stack = [
            FileObject(self.source_transport, self.source, {"isdir": True})
        ]

        # Depth-first tree traversal.
        while directory_stack:
            # TODO: Rethink the assumption that a file cannot have the same name as a directory.
            item = directory_stack.pop()
            log.debug("URL %s is %sa directory." % \
                          (item.url, not item.isdir and "not " or ""))
            if item.isdir:
                # Don't skip the first directory.
                if not self.config.recursive and item.url != self.source:
                    log.info("Skipping directory %s..." % item)
                    continue
                # Obtain a directory list.
                new_dir_list = []
                for new_file in reversed(
                        self.source_transport.listdir(item.url)):
                    if self.include_file(new_file):
                        new_dir_list.append(new_file)
                    else:
                        log.debug("Skipping %s..." % (new_file))
                dest = url_splice(self.source, item.url, self.destination)
                dest = FileObject(self.destination_transport, dest)
                log.debug("Comparing directories %s and %s..." %
                          (item.url, dest.url))
                self.compare_directories(item, new_dir_list, dest.url)
                directory_stack.extend(new_dir_list)
            else:
                dest_url = url_splice(self.source, item.url, self.destination)
                log.debug("Destination URL is %s." % dest_url)
                dest = FileObject(self.destination_transport, dest_url)
                self.compare_and_copy(item, dest)