(
github, force_reload, trust_repo, calling_fn, verbose=True, skip_validation=False
)
| 207 | |
| 208 | |
| 209 | def _get_cache_or_reload( |
| 210 | github, force_reload, trust_repo, calling_fn, verbose=True, skip_validation=False |
| 211 | ): |
| 212 | # Setup hub_dir to save downloaded files |
| 213 | hub_dir = get_dir() |
| 214 | if not os.path.exists(hub_dir): |
| 215 | os.makedirs(hub_dir) |
| 216 | # Parse github repo information |
| 217 | repo_owner, repo_name, ref = _parse_repo_info(github) |
| 218 | # Github allows branch name with slash '/', |
| 219 | # this causes confusion with path on both Linux and Windows. |
| 220 | # Backslash is not allowed in Github branch name so no need to |
| 221 | # to worry about it. |
| 222 | normalized_br = ref.replace("/", "_") |
| 223 | # Github renames folder repo-v1.x.x to repo-1.x.x |
| 224 | # We don't know the repo name before downloading the zip file |
| 225 | # and inspect name from it. |
| 226 | # To check if cached repo exists, we need to normalize folder names. |
| 227 | owner_name_branch = "_".join([repo_owner, repo_name, normalized_br]) |
| 228 | repo_dir = os.path.join(hub_dir, owner_name_branch) |
| 229 | # Check that the repo is in the trusted list |
| 230 | _check_repo_is_trusted( |
| 231 | repo_owner, |
| 232 | repo_name, |
| 233 | owner_name_branch, |
| 234 | trust_repo=trust_repo, |
| 235 | calling_fn=calling_fn, |
| 236 | ) |
| 237 | |
| 238 | use_cache = (not force_reload) and os.path.exists(repo_dir) |
| 239 | |
| 240 | if use_cache: |
| 241 | if verbose: |
| 242 | sys.stderr.write("Using cache found in {}\n".format(repo_dir)) |
| 243 | else: |
| 244 | # Validate the tag/branch is from the original repo instead of a forked repo |
| 245 | if not skip_validation: |
| 246 | _validate_not_a_forked_repo(repo_owner, repo_name, ref) |
| 247 | |
| 248 | cached_file = os.path.join(hub_dir, normalized_br + ".zip") |
| 249 | _remove_if_exists(cached_file) |
| 250 | |
| 251 | try: |
| 252 | url = _git_archive_link(repo_owner, repo_name, ref) |
| 253 | sys.stderr.write('Downloading: "{}" to {}\n'.format(url, cached_file)) |
| 254 | download_url_to_file(url, cached_file, progress=False) |
| 255 | except HTTPError as err: |
| 256 | if err.code == 300: |
| 257 | # Getting a 300 Multiple Choices error likely means that the ref is both a tag and a branch |
| 258 | # in the repo. This can be disambiguated by explicitely using refs/heads/ or refs/tags |
| 259 | # See https://git-scm.com/book/en/v2/Git-Internals-Git-References |
| 260 | # Here, we do the same as git: we throw a warning, and assume the user wanted the branch |
| 261 | warnings.warn( |
| 262 | f"The ref {ref} is ambiguous. Perhaps it is both a tag and a branch in the repo? " |
| 263 | "OneFlowhub will now assume that it's a branch. " |
| 264 | "You can disambiguate tags and branches by explicitly passing refs/heads/branch_name or " |
| 265 | "refs/tags/tag_name as the ref. That might require using skip_validation=True." |
| 266 | ) |
no test coverage detected