Beispiel #1
0
    def rename_dont_move(self, path, dest):
        """
        Potentially rename ``path`` to ``dest``, but don't move it into the
        ``dest`` folder (if it is a folder).  This relates to :ref:`AtomicWrites`.

        This method has a reasonable but not bullet proof default
        implementation.  It will just do ``move()`` if the file doesn't
        ``exists()`` already.
        """
        warnings.warn(
            "File system {} client doesn't support atomic mv.".format(
                self.__class__.__name__))
        if self.exists(dest):
            raise FileAlreadyExists()
        self.move(path, dest)
Beispiel #2
0
    def mkdir(self, path, parents=True, raise_if_exists=False):
        if raise_if_exists and self.isdir(path):
            raise FileAlreadyExists()

        # storage_account, container_name, prefix = self._path_to_account_container_and_blob(
        #     path
        # )
        # assert self.account == storage_account
        #
        # if self._is_container(prefix):
        #     # isdir raises if the bucket doesn't exist; nothing to do here.
        #     return
        #
        if not parents and not self.isdir(os.path.dirname(path)):
            raise MissingParentDirectory()
Beispiel #3
0
    def mkdir(self, path, parents=True, raise_if_exists=False):
        if self.exists(path):
            if raise_if_exists:
                raise FileAlreadyExists()
            elif not self.isdir(path):
                raise NotADirectory()
            else:
                return

        if parents:
            try:
                os.makedirs(path)
            except OSError as err:
                # somebody already created the path
                if err.errno != errno.EEXIST:
                    raise
        else:
            if not os.path.exists(os.path.dirname(path)):
                raise MissingParentDirectory()
            os.mkdir(path)
Beispiel #4
0
 def move(self, old_path, new_path, raise_if_exists=False):
     """
     Move file atomically. If source and destination are located
     on different filesystems, atomicity is approximated
     but cannot be guaranteed.
     """
     if raise_if_exists and os.path.exists(new_path):
         raise FileAlreadyExists("Destination exists: %s" % new_path)
     d = os.path.dirname(new_path)
     if d and not os.path.exists(d):
         self.mkdir(d)
     try:
         os.rename(old_path, new_path)
     except OSError as err:
         if err.errno == errno.EXDEV:
             new_path_tmp = "%s-%09d" % (new_path, random.randint(0, 999999999))
             shutil.copy(old_path, new_path_tmp)
             os.rename(new_path_tmp, new_path)
             os.remove(old_path)
         elif err.errno == errno.EEXIST:
             shutil.move(old_path, new_path)
         else:
             raise err