Ignore:
Timestamp:
Dec 22, 2015, 8:53:39 PM (4 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Apply a variant of Roger Demetrescu's patch (see ticket #86)

Thanks, Roger! :-)

The only difference between this changeset and Roger's patch is that
the new code in the changeset has slightly different formatting to
limit the line length to 79 characters.

Contrary to my original intention I haven't added unit tests for the
virtual directory support yet. I'd expect the tests to become rather
convoluted with the current mocking infrastructure in
`test.mock_ftplib`. I'm planning to add tests when more code is
migrated to use the `mock` library (see ticket #98),
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ftputil/host.py

    r1610 r1616  
    616616        self._robust_ftp_command(command, path)
    617617
     618    # TODO: The virtual directory support doesn't have unit tests yet
     619    # because the mocking most likely would be quite complicated. The
     620    # tests should be added when mainly the `mock` library is used
     621    # instead of the mock code in `test.mock_ftplib`.
     622    #
    618623    # Ignore unused argument `mode`
    619624    # pylint: disable=unused-argument
     
    621626        """
    622627        Make the directory `path`, but also make not yet existing
    623         intermediate directories, like `os.makedirs`. The value
    624         of `mode` is only accepted for compatibility with
    625         `os.makedirs` but otherwise ignored.
     628        intermediate directories, like `os.makedirs`. The value of
     629        `mode` is only accepted for compatibility with `os.makedirs`
     630        but otherwise ignored.
    626631        """
    627632        path = ftputil.tool.as_unicode(path)
    628633        path = self.path.abspath(path)
    629634        directories = path.split(self.sep)
    630         # Try to build the directory chain from the "uppermost" to
    631         # the "lowermost" directory.
    632         for index in range(1, len(directories)):
    633             # Re-insert the separator which got lost by using `path.split`.
    634             next_directory = self.sep + self.path.join(*directories[:index+1])
    635             try:
    636                 self.mkdir(next_directory)
    637             except ftputil.error.PermanentError:
    638                 # Find out the cause of the error. Re-raise the
    639                 # exception only if the directory didn't exist already,
    640                 # else something went _really_ wrong, e. g. there's a
    641                 # regular file with the name of the directory.
    642                 if not self.path.isdir(next_directory):
    643                     raise
     635        old_dir = self.getcwd()
     636        try:
     637            # Try to build the directory chain from the "uppermost" to
     638            # the "lowermost" directory.
     639            for index in range(1, len(directories)):
     640                # Re-insert the separator which got lost by using
     641                # `path.split`.
     642                next_directory = (self.sep +
     643                                  self.path.join(*directories[:index+1]))
     644                try:
     645                    self.chdir(next_directory)
     646                except ftputil.error.PermanentError:
     647                    try:
     648                        self.mkdir(next_directory)
     649                    except ftputil.error.PermanentError:
     650                        # Find out the cause of the error. Re-raise
     651                        # the exception only if the directory didn't
     652                        # exist already, else something went _really_
     653                        # wrong, e. g. there's a regular file with the
     654                        # name of the directory.
     655                        if not self.path.isdir(next_directory):
     656                            raise
     657        finally:
     658            self.chdir(old_dir)
    644659
    645660    def rmdir(self, path):
Note: See TracChangeset for help on using the changeset viewer.