|
7 | 7 | DvcException, |
8 | 8 | NotDvcRepoError, |
9 | 9 | OutputNotFoundError, |
10 | | - UrlNotDvcRepoError, |
11 | 10 | PathMissingError, |
12 | 11 | ) |
13 | | -from dvc.external_repo import external_repo |
| 12 | +from dvc.external_repo import external_repo, cached_clone |
14 | 13 | from dvc.path_info import PathInfo |
15 | 14 | from dvc.stage import Stage |
16 | 15 | from dvc.utils import resolve_output |
@@ -42,37 +41,47 @@ def get(url, path, out=None, rev=None): |
42 | 41 | # and won't work with reflink/hardlink. |
43 | 42 | dpath = os.path.dirname(os.path.abspath(out)) |
44 | 43 | tmp_dir = os.path.join(dpath, "." + str(shortuuid.uuid())) |
| 44 | + repo_dir = None |
45 | 45 | try: |
46 | | - with external_repo(cache_dir=tmp_dir, url=url, rev=rev) as repo: |
47 | | - # Try any links possible to avoid data duplication. |
48 | | - # |
49 | | - # Not using symlink, because we need to remove cache after we are |
50 | | - # done, and to make that work we would have to copy data over |
51 | | - # anyway before removing the cache, so we might just copy it |
52 | | - # right away. |
53 | | - # |
54 | | - # Also, we can't use theoretical "move" link type here, because |
55 | | - # the same cache file might be used a few times in a directory. |
56 | | - repo.cache.local.cache_types = ["reflink", "hardlink", "copy"] |
57 | | - |
58 | | - try: |
59 | | - output = repo.find_out_by_relpath(path) |
60 | | - except OutputNotFoundError: |
61 | | - output = None |
62 | | - |
63 | | - if output and output.use_cache: |
64 | | - _get_cached(repo, output, out) |
65 | | - else: |
| 46 | + try: |
| 47 | + with external_repo(cache_dir=tmp_dir, url=url, rev=rev) as repo: |
| 48 | + # Try any links possible to avoid data duplication. |
| 49 | + # |
| 50 | + # Not using symlink, because we need to remove cache after we |
| 51 | + # are done, and to make that work we would have to copy data |
| 52 | + # over anyway before removing the cache, so we might just copy |
| 53 | + # it right away. |
| 54 | + # |
| 55 | + # Also, we can't use theoretical "move" link type here, because |
| 56 | + # the same cache file might be used a few times in a directory. |
| 57 | + repo.cache.local.cache_types = ["reflink", "hardlink", "copy"] |
| 58 | + |
| 59 | + try: |
| 60 | + output = repo.find_out_by_relpath(path) |
| 61 | + except OutputNotFoundError: |
| 62 | + output = None |
| 63 | + |
| 64 | + if output and output.use_cache: |
| 65 | + _get_cached(repo, output, out) |
| 66 | + return |
| 67 | + |
66 | 68 | # Either an uncached out with absolute path or a user error |
67 | | - if os.path.isabs(path): |
68 | | - raise FileNotFoundError |
69 | 69 |
|
70 | | - fs_copy(os.path.join(repo.root_dir, path), out) |
| 70 | + repo_dir = repo.root_dir |
| 71 | + |
| 72 | + except NotDvcRepoError: |
| 73 | + # Not a DVC repository, continue below and copy from git |
| 74 | + pass |
| 75 | + |
| 76 | + if os.path.isabs(path): |
| 77 | + raise FileNotFoundError |
| 78 | + |
| 79 | + if not repo_dir: |
| 80 | + repo_dir = cached_clone(url, rev=rev) |
71 | 81 |
|
| 82 | + fs_copy(os.path.join(repo_dir, path), out) |
72 | 83 | except (OutputNotFoundError, FileNotFoundError): |
73 | 84 | raise PathMissingError(path, url) |
74 | | - except NotDvcRepoError: |
75 | | - raise UrlNotDvcRepoError(url) |
76 | 85 | finally: |
77 | 86 | remove(tmp_dir) |
78 | 87 |
|
|
0 commit comments