|
24 | 24 | REPO_TYPES_URL_PREFIXES,
|
25 | 25 | )
|
26 | 26 | from .file_download import hf_hub_url, http_get
|
27 |
| -from .hf_api import HfApi, LastCommitInfo, RepoFile |
| 27 | +from .hf_api import HfApi, LastCommitInfo, RepoFile, RepoFolder |
28 | 28 | from .utils import (
|
29 | 29 | EntryNotFoundError,
|
30 | 30 | HFValidationError,
|
@@ -375,29 +375,14 @@ def _ls_tree(
|
375 | 375 | revision=resolved_path.revision,
|
376 | 376 | repo_type=resolved_path.repo_type,
|
377 | 377 | )
|
| 378 | + |
378 | 379 | for path_info in tree:
|
379 |
| - if isinstance(path_info, RepoFile): |
380 |
| - cache_path_info = { |
381 |
| - "name": root_path + "/" + path_info.path, |
382 |
| - "size": path_info.size, |
383 |
| - "type": "file", |
384 |
| - "blob_id": path_info.blob_id, |
385 |
| - "lfs": path_info.lfs, |
386 |
| - "last_commit": path_info.last_commit, |
387 |
| - "security": path_info.security, |
388 |
| - } |
389 |
| - else: |
390 |
| - cache_path_info = { |
391 |
| - "name": root_path + "/" + path_info.path, |
392 |
| - "size": 0, |
393 |
| - "type": "directory", |
394 |
| - "tree_id": path_info.tree_id, |
395 |
| - "last_commit": path_info.last_commit, |
396 |
| - } |
| 380 | + cache_path_info = self._make_cache_path_info(root_path, path_info, should_copy=False) |
397 | 381 | parent_path = self._parent(cache_path_info["name"])
|
398 | 382 | self.dircache.setdefault(parent_path, []).append(cache_path_info)
|
399 |
| - out.append(cache_path_info) |
400 |
| - return copy.deepcopy(out) # copy to not let users modify the dircache |
| 383 | + out_cache_path_info = self._make_cache_path_info(root_path, path_info) |
| 384 | + out.append(out_cache_path_info) |
| 385 | + return out |
401 | 386 |
|
402 | 387 | def glob(self, path, **kwargs):
|
403 | 388 | # Set expand_info=False by default to get a x10 speed boost
|
@@ -540,28 +525,34 @@ def info(self, path: str, refresh: bool = False, revision: Optional[str] = None,
|
540 | 525 | path_in_repo="",
|
541 | 526 | _raw_revision=resolved_path._raw_revision,
|
542 | 527 | ).unresolve()
|
543 |
| - if isinstance(path_info, RepoFile): |
544 |
| - out = { |
545 |
| - "name": root_path + "/" + path_info.path, |
546 |
| - "size": path_info.size, |
547 |
| - "type": "file", |
548 |
| - "blob_id": path_info.blob_id, |
549 |
| - "lfs": path_info.lfs, |
550 |
| - "last_commit": path_info.last_commit, |
551 |
| - "security": path_info.security, |
552 |
| - } |
553 |
| - else: |
554 |
| - out = { |
555 |
| - "name": root_path + "/" + path_info.path, |
556 |
| - "size": 0, |
557 |
| - "type": "directory", |
558 |
| - "tree_id": path_info.tree_id, |
559 |
| - "last_commit": path_info.last_commit, |
560 |
| - } |
| 528 | + out = self._make_path_info(root_path, path_info) |
561 | 529 | if not expand_info:
|
562 | 530 | out = {k: out[k] for k in ["name", "size", "type"]}
|
563 | 531 | assert out is not None
|
564 |
| - return copy.deepcopy(out) # copy to not let users modify the dircache |
| 532 | + return out |
| 533 | + |
| 534 | + def _make_cache_path_info( |
| 535 | + self, root_path: str, path_info: RepoFile | RepoFolder, shallow_copy: bool = True |
| 536 | + ) -> Dict[str, Any]: |
| 537 | + return ( |
| 538 | + { |
| 539 | + "name": root_path + "/" + path_info.path, |
| 540 | + "size": path_info.size, |
| 541 | + "type": "file", |
| 542 | + "blob_id": path_info.blob_id, |
| 543 | + "lfs": copy.copy(path_info.lfs) if shallow_copy else path_info.lfs, |
| 544 | + "last_commit": copy.copy(path_info.last_commit) if shallow_copy else path_info.last_commit, |
| 545 | + "security": copy.copy(path_info.security) if shallow_copy else path_info.security, |
| 546 | + } |
| 547 | + if isinstance(path_info, RepoFile) |
| 548 | + else { |
| 549 | + "name": root_path + "/" + path_info.path, |
| 550 | + "size": 0, |
| 551 | + "type": "directory", |
| 552 | + "tree_id": path_info.tree_id, |
| 553 | + "last_commit": copy.copy(path_info.last_commit) if shallow_copy else path_info.last_commit, |
| 554 | + } |
| 555 | + ) |
565 | 556 |
|
566 | 557 | def exists(self, path, **kwargs):
|
567 | 558 | """Is there a file at the given path"""
|
|
0 commit comments