def copy(self, src_path, dst_path, overwrite=False): # type: (Text, Text, bool) -> None """Copy file contents from ``src_path`` to ``dst_path``. Arguments: src_path (str): Path of source file. dst_path (str): Path to destination file. overwrite (bool): If `True`, overwrite the destination file if it exists (defaults to `False`). Raises: fs.errors.DestinationExists: If ``dst_path`` exists, and ``overwrite`` is `False`. fs.errors.ResourceNotFound: If a parent directory of ``dst_path`` does not exist. """ _src_path = self.validatepath(src_path) _dst_path = self.validatepath(dst_path) with self._lock: if not overwrite and self.exists(dst_path): raise errors.DestinationExists(dst_path) dir_path, file_name = split(_dst_path) _dir_res = self._getresource(dir_path) if not _dir_res or not _dir_res.is_collection: raise errors.ResourceNotFound(dst_path) _src_res = self._getresource(src_path) if not _src_res: raise errors.ResourceNotFound(src_path) if _src_res.is_collection: raise errors.FileExpected(src_path) _src_res.copy_move_single(_dst_path, False)
def copy(self, src_path, dst_path, overwrite=False): src_path = self.fix_path(src_path) dst_path = self.fix_path(dst_path) try: src_meta = self.getinfo(src_path) if src_meta.is_dir: raise errors.FileExpected(src_path) except ApiError as e: raise errors.ResourceNotFound dst_meta = None try: dst_meta = self.getinfo(dst_path) except Exception as e: pass if dst_meta is not None: if overwrite == True: self.remove(dst_path) else: raise errors.DestinationExists(dst_path) parent_path = self.get_parent(dst_path) if not self.exists(parent_path): raise errors.ResourceNotFound(dst_path) self.dropbox.files_copy_v2(src_path, dst_path)
def copy(self, src_path, dst_path, overwrite=False): if not overwrite and self.exists(dst_path): raise errors.DestinationExists(dst_path) _src_path = self.validatepath(src_path) _dst_path = self.validatepath(dst_path) if self.strict: if not self.isdir(dirname(_dst_path)): raise errors.ResourceNotFound(dst_path) _src_key = self._path_to_key(_src_path) _dst_key = self._path_to_key(_dst_path) try: with osserrors(src_path): self.client.copy_object( Bucket=self._bucket_name, Key=_dst_key, CopySource={ "Bucket": self._bucket_name, "Key": _src_key }, ) except errors.ResourceNotFound: if self.exists(src_path): raise errors.FileExpected(src_path) raise
def copy(self, src_path, dst_path, overwrite=False, preserve_time=False): _log.info( f'copy({src_path}, {dst_path}, {overwrite}, {preserve_time})') src_path = self.validatepath(src_path) dst_path = self.validatepath(dst_path) with self._lock: if not overwrite and self.exists(dst_path): raise errors.DestinationExists(dst_path) driveItemResponse = self.session.get_path(src_path) if driveItemResponse.status_code == 404: raise ResourceNotFound(src_path) driveItemResponse.raise_for_status() driveItem = driveItemResponse.json() if 'folder' in driveItem: raise errors.FileExpected(src_path) newParentDir = dirname(dst_path) newFilename = basename(dst_path) parentDirResponse = self.session.get_path(newParentDir) if parentDirResponse.status_code == 404: raise ResourceNotFound(src_path) parentDirResponse.raise_for_status() parentDirItem = parentDirResponse.json() # This just asynchronously starts the copy response = self.session.post_item( driveItem['id'], '/[email protected]=replace', json={ 'parentReference': { 'driveId': parentDirItem['parentReference']['driveId'], 'id': parentDirItem['id'] }, 'name': newFilename, }) response.raise_for_status() assert response.status_code == 202, 'Response code should be 202 (Accepted)' monitorUri = response.headers['Location'] while True: # monitor uris don't require authentication # (https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/long-running-actions) jobStatusResponse = requests.get(monitorUri) jobStatusResponse.raise_for_status() jobStatus = jobStatusResponse.json() if 'operation' in jobStatus and jobStatus[ 'operation'] != 'itemCopy': _log.warning( f'Unexpected operation: {jobStatus["operation"]}') if jobStatus['status'] not in [ 'inProgress', 'completed', 'notStarted' ]: _log.warning(f'Unexpected status: {jobStatus}') if jobStatus['status'] == 'completed': break
def move(self, src_path, dst_path, overwrite=False): _src_path = self.validatepath(src_path) _dst_path = self.validatepath(dst_path) if not self.getinfo(_src_path).is_file: raise errors.FileExpected(src_path) if not overwrite and self.exists(_dst_path): raise errors.DestinationExists(dst_path) with self._lock: try: self.client.move(_src_path.encode('utf-8'), _dst_path.encode('utf-8'), overwrite=overwrite) except we.RemoteResourceNotFound as exc: raise errors.ResourceNotFound(src_path, exc=exc) except we.RemoteParentNotFound as exc: raise errors.ResourceNotFound(dst_path, exc=exc)
def copy(self, src_path, dst_path, overwrite=False): if not overwrite and self.exists(dst_path): raise errors.DestinationExists(dst_path) _src_path = self.validatepath(src_path) _dst_path = self.validatepath(dst_path) if not self.isdir(dirname(_dst_path)): raise errors.ResourceNotFound(dst_path) _src_key = self._path_to_key(_src_path) _dst_key = self._path_to_key(_dst_path) with s3errors(src_path): self.client.copy_object(Bucket=self._bucket_name, Key=_dst_key, CopySource={ 'Bucket': self._bucket_name, 'Key': _src_key })
def move(self, src_path, dst_path, overwrite=False): # type: (Text, Text, bool) -> None """Move a file from ``src_path`` to ``dst_path``. Arguments: src_path (str): A path on the filesystem to move. dst_path (str): A path on the filesystem where the source file will be written to. overwrite (bool): If `True`, destination path will be overwritten if it exists. Raises: fs.errors.FileExpected: If ``src_path`` maps to a directory instead of a file. fs.errors.DestinationExists: If ``dst_path`` exists, and ``overwrite`` is `False`. fs.errors.ResourceNotFound: If a parent directory of ``dst_path`` does not exist. """ _src_path = self.validatepath(src_path) _dst_path = self.validatepath(dst_path) with self._lock: if not overwrite and self.exists(dst_path): raise errors.DestinationExists(dst_path) dir_path, file_name = split(_dst_path) _dir_res = self._getresource(dir_path) if not _dir_res or not _dir_res.is_collection: raise errors.ResourceNotFound(dst_path) _src_res = self._getresource(src_path) if not _src_res: raise errors.ResourceNotFound(src_path) if _src_res.is_collection: raise errors.FileExpected(src_path) if not overwrite and _src_res.support_recursive_move(_dst_path): _src_res.move_recursive(_dst_path) else: # CHECKME: this doesn't actually seem to delete _src_res in DAV Provider _src_res.copy_move_single(_dst_path, True) try: _src_res.delete() except: pass
def copy(self, src_path, dst_path, overwrite=False): if not overwrite and self.exists(dst_path): raise errors.DestinationExists(dst_path) _src_path = self.validatepath(src_path) _dst_path = self.validatepath(dst_path) if self.strict: if not self.isdir(dirname(_dst_path)): raise errors.ResourceNotFound(dst_path) _src_key = self._path_to_key(_src_path) _dst_key = self._path_to_key(_dst_path) try: with s3errors(src_path): self.s3_copy(self._bucket_name, _dst_key, self._bucket_name, _src_key) except errors.ResourceNotFound: if self.exists(src_path): raise errors.FileExpected(src_path) raise
def copy(self, src_path: str, dst_path: str, overwrite: bool = False) -> None: if not overwrite and self.exists(dst_path): raise errors.DestinationExists(dst_path) _src_path = self.validatepath(src_path) _dst_path = self.validatepath(dst_path) if self.strict: if not self.isdir(dirname(_dst_path)): raise errors.ResourceNotFound(dst_path) _src_key = self._path_to_key(_src_path) _dst_key = self._path_to_key(_dst_path) blob = self.bucket.get_blob(_src_key) if not blob: if self.exists(src_path): raise errors.FileExpected(src_path) raise errors.ResourceNotFound(_src_key) self.bucket.copy_blob(blob, self.bucket, new_name=_dst_key)
def move(self, src_path, dst_path, overwrite=False): _src_path = self.fix_path(src_path) _dst_path = self.fix_path(dst_path) if not self.getinfo(_src_path).is_file: raise errors.FileExpected(src_path) if not overwrite and self.exists(_dst_path): raise errors.DestinationExists(dst_path) if "/" in dst_path and not self.exists(self.get_parent(_dst_path)): raise errors.ResourceNotFound(src_path) with self._lock: try: if overwrite: try: # remove file anyways self.dropbox.files_delete_v2(_dst_path) except Exception as e: pass self.dropbox.files_move_v2(_src_path, _dst_path) except ApiError as e: raise errors.ResourceNotFound(src_path, exc=e)