Repository navigation
Recursive get and copy use the IDs from their listing, without a lookup per file #468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
417ae10
4651fa3
a4e60a4
b51b634
52f7cd8
b662738
9e85e42
bb491cc
ef53c29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -228,6 +228,77 @@ def _select_item_id(path, ids): | |
| raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path) | ||
|
|
||
|
|
||
| class _ListedTransfer: | ||
| """Run one fsspec get() or copy() by the IDs that its own listings return. | ||
|
|
||
| Callbacks still get the filesystem, so their calls look paths up again. | ||
| """ | ||
|
|
||
| def __init__(self, fs, destination=None): | ||
| self._fs = fs | ||
| self._destination = destination | ||
| self._ids: dict[str, tuple[str, bool]] | None = {} | ||
| self._unreachable: set[str] = set() | ||
|
|
||
| def __getattr__(self, name): | ||
| return getattr(self._fs, name) | ||
|
|
||
| # fsspec expands paths with these, and they must call find() on this object. | ||
| def expand_path(self, *args, **kwargs): | ||
| return AbstractFileSystem.expand_path(self, *args, **kwargs) | ||
|
|
||
| def glob(self, *args, **kwargs): | ||
| return AbstractFileSystem.glob(self, *args, **kwargs) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ID map retains entries the glob rejected. I tested |
||
|
|
||
| def find( | ||
| self, path, maxdepth=None, withdirs=False, detail=False, **kwargs | ||
| ): | ||
| return self._fs._find(path, maxdepth, withdirs, detail, self) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This entry point skips the boolean-depth validation in public |
||
|
|
||
| def listed(self, parent, path, item): | ||
| """Keep the lowest ID at a path, as a lookup of the path would.""" | ||
| if self._ids is None: | ||
| return | ||
| if item.ambiguous: | ||
| # A listing of several folders places such an item under only one. | ||
| self._ids = None | ||
| return | ||
| if parent in self._unreachable or "/" in item.name: | ||
| # A lookup splits paths at slashes and cannot reach it. | ||
| if item.is_folder: | ||
| self._unreachable.add(path) | ||
| return | ||
| known = self._ids.get(path) | ||
| if known is None or item.id < known[0]: | ||
| self._ids[path] = (item.id, item.is_folder) | ||
|
|
||
| def _known(self, path): | ||
| return self._ids.get(path) if self._ids else None | ||
|
|
||
| def isdir(self, path): | ||
| # fsspec places a copy by the destination's current type. | ||
| known = None if path == self._destination else self._known(path) | ||
| return self._fs.isdir(path) if known is None else known[1] | ||
|
|
||
| def get_file(self, rpath, lpath, callback=None, block_size=None, **kwargs): | ||
| listed = self._known(rpath) | ||
| return self._fs._get_file(rpath, lpath, listed, callback, block_size) | ||
|
|
||
| def cp_file(self, path1, path2, **kwargs): | ||
| return self._fs._cp_file(path1, path2, self._known(path1)) | ||
|
|
||
|
|
||
| # A subclass that changes one of these keeps fsspec's own transfer. | ||
| _TRANSFER_HOOKS = ( | ||
| "expand_path", | ||
| "glob", | ||
| "find", | ||
| "isdir", | ||
| "get_file", | ||
| "cp_file", | ||
| ) | ||
|
|
||
|
|
||
| class GDriveAuthError(AuthError): | ||
| pass | ||
|
|
||
|
|
@@ -883,9 +954,11 @@ def _get_item_id(self, path, create=False): | |
| match, _ = self._lookup_path(base) | ||
| return _select_item_id(path, match.ids) | ||
|
|
||
| def _lookup_item(self, path): | ||
| def _lookup_item(self, path, listed=None): | ||
| """Return the ID at a path and whether it is a folder.""" | ||
| _, base = self._split_mounted_path(path) | ||
| if listed is not None: | ||
| return listed | ||
| match, _ = self._lookup_path(base) | ||
| item_id = _select_item_id(path, match.ids) | ||
| if base or self.root in _ROOT_ALIASES: | ||
|
|
@@ -1155,6 +1228,9 @@ def find( | |
| "find() takes maxdepth as its second argument. " | ||
| f"Pass detail by keyword: find(path, detail={maxdepth})." | ||
| ) | ||
| return self._find(path, maxdepth, withdirs, detail) | ||
|
|
||
| def _find(self, path, maxdepth, withdirs, detail, transfer=None): | ||
| root, base = self._split_mounted_path(path) | ||
| if maxdepth is not None and maxdepth < 1: | ||
| raise ValueError("maxdepth must be at least 1") | ||
|
|
@@ -1198,11 +1274,13 @@ def find( | |
| check_empty_root = False | ||
| preferred = _preferred_ids(children, withdirs) if detail else {} | ||
| for item in _drain(children): | ||
| name = posixpath.join(parent, item.name) | ||
| if transfer is not None: | ||
| transfer.listed(parent, name, item) | ||
| if item.is_folder and not withdirs: | ||
| continue | ||
| if preferred and item.id != preferred.get(item.name, item.id): | ||
| continue | ||
| name = posixpath.join(parent, item.name) | ||
| contents.append(item.entry(name, detail)) | ||
|
|
||
| if check_empty_root: | ||
|
|
@@ -1217,6 +1295,25 @@ def find( | |
| contents.sort() | ||
| return contents | ||
|
|
||
| def _transfer(self, destination=None): | ||
| """Return the object that fsspec's get() or copy() should run on.""" | ||
| cls = type(self) | ||
| for name in _TRANSFER_HOOKS: | ||
| if getattr(cls, name) is not getattr(GDriveFileSystem, name): | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking these six class methods still misses behavior that fsspec previously honored. I tested a subclass whose |
||
| return self | ||
| return _ListedTransfer(self, destination) | ||
|
|
||
| @wraps(AbstractFileSystem.get) | ||
| def get(self, *args, **kwargs): | ||
| # Each listed file is downloaded by its listed ID, without a lookup. | ||
| return AbstractFileSystem.get(self._transfer(), *args, **kwargs) | ||
|
|
||
| @wraps(AbstractFileSystem.copy) | ||
| def copy(self, path1, path2, *args, **kwargs): | ||
| # Each listed file is copied by its listed ID, without a lookup. | ||
| transfer = self._transfer(destination=path2) | ||
| return AbstractFileSystem.copy(transfer, path1, path2, *args, **kwargs) | ||
|
|
||
| def upload_fobj(self, stream, rpath, callback=None, **kwargs): | ||
| parent, name = self._split_file_path(rpath) | ||
| parent_id = self._get_item_id(parent, create=True) | ||
|
|
@@ -1249,16 +1346,37 @@ def send(new_id): | |
|
|
||
| def cp_file(self, lpath, rpath, **kwargs): | ||
| """Ask Drive to copy the file directly into the destination folder.""" | ||
| self._cp_file(lpath, rpath) | ||
|
|
||
| def _cp_file(self, lpath, rpath, listed=None): | ||
| self._split_mounted_path(rpath) | ||
| source_id, is_folder = self._lookup_item(lpath) | ||
| source_id, is_folder = self._lookup_item(lpath, listed) | ||
| if is_folder: | ||
| self.makedirs(rpath, exist_ok=True) | ||
| return | ||
| parent, name = self._split_file_path(rpath) | ||
| parent_id = self._get_item_id(parent, create=True) | ||
| self._gdrive_copy_file(source_id, parent_id, name) | ||
| try: | ||
| self._gdrive_copy_file(source_id, parent_id, name) | ||
| except ApiRequestError as exc: | ||
| # A missing destination folder is also a 404 on the copy. | ||
| if _is_not_found(exc) and not self._gdrive_exists(source_id): | ||
| raise FileNotFoundError( | ||
| errno.ENOENT, os.strerror(errno.ENOENT), lpath | ||
| ) from exc | ||
| raise | ||
| self._dirs.file_added(parent_id, name) | ||
|
|
||
| def _gdrive_exists(self, item_id): | ||
| item = self.client.CreateFile({"id": item_id}) | ||
| try: | ||
| _gdrive_retry_repeatable(item.FetchMetadata)(fields="id") | ||
| except ApiRequestError as exc: | ||
| if _is_not_found(exc): | ||
| return False | ||
| raise | ||
| return True | ||
|
|
||
| def _gdrive_copy_file(self, source_id, parent_id, name): | ||
| source = self.client.CreateFile({"id": source_id}) | ||
| self._gdrive_create( | ||
|
|
@@ -1348,8 +1466,11 @@ def mv(self, path1, path2, recursive=False, maxdepth=None, **kwargs): | |
| ) | ||
|
|
||
| def get_file(self, rpath, lpath, callback=None, block_size=None, **kwargs): | ||
| return self._get_file(rpath, lpath, None, callback, block_size) | ||
|
|
||
| def _get_file(self, rpath, lpath, listed, callback, block_size): | ||
| with _translate_not_found(rpath): | ||
| item_id, is_folder = self._lookup_item(rpath) | ||
| item_id, is_folder = self._lookup_item(rpath, listed) | ||
| if is_folder: | ||
| os.makedirs(lpath, exist_ok=True) | ||
| return None | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.