-
Notifications
You must be signed in to change notification settings - Fork 15
Reduce API call limit error #93
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
7ed0b48
Add _download_via_git
kAIto47802 0d45e78
Add GitPython
kAIto47802 2ef240e
Handle case where remote origin already exists
kAIto47802 0bb1d3c
Update docstring
kAIto47802 f079208
Update base_url
kAIto47802 a8ce467
Update docstring
kAIto47802 ff55f6b
Merged main to sync latest changes
kAIto47802 395e163
Apply formatter
kAIto47802 46a17c8
Update import order
kAIto47802 7fd366d
Update optunahub/hub.py
kAIto47802 afe981e
Update docstring
kAIto47802 e9276f9
Add note for regular expression
kAIto47802 0847e9e
Update docstring
kAIto47802 edb5a3b
Add a unit test
kAIto47802 bb9d917
Update unit tests
kAIto47802 7ea75ba
Add future annotations
kAIto47802 1a22a20
Merge branch 'main' into reduce-api-call-limit
nabenabe0928 fae1781
Update tests/test_hub.py
kAIto47802 3e34116
Update docstring
kAIto47802 855e4c9
Update line breaks of the docstring
kAIto47802 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,14 @@ | |
| import importlib.util | ||
| import logging | ||
| import os | ||
| import re | ||
| import shutil | ||
| import sys | ||
| import types | ||
| from urllib.parse import urlparse | ||
|
|
||
| from ga4mp import GtagMP # type: ignore | ||
| from git import Repo | ||
| from github import Auth | ||
| from github import Github | ||
| from github.ContentFile import ContentFile | ||
|
|
@@ -69,7 +71,7 @@ | |
| repo_owner: str = "optuna", | ||
| repo_name: str = "optunahub-registry", | ||
| ref: str = "main", | ||
| base_url: str = "https://api.github.com", | ||
| base_url: str | None = None, | ||
| force_reload: bool = False, | ||
| auth: Auth.Auth | None = None, | ||
| ) -> types.ModuleType: | ||
|
|
@@ -88,54 +90,50 @@ | |
| ref: | ||
| The Git reference (branch, tag, or commit SHA) for the package. | ||
| base_url: | ||
| The base URL for the GitHub API. | ||
| If ``auth`` is :obj:`None` and the ``git`` command is available, this should be the base URI for the remote repository. | ||
| In this case, specifying ``[email protected]`` allows access to private/internal repositories via SSH. | ||
| Otherwise, this should be the base URL for the GitHub API. | ||
| force_reload: | ||
| If :obj:`True`, the package will be downloaded from the repository. | ||
| If :obj:`False`, the package cached in the local directory will be | ||
| loaded if available. | ||
| auth: | ||
| `The authentication object <https://pygithub.readthedocs.io/en/latest/examples/Authentication.html>`__ for the GitHub API. | ||
| It is required to access private/internal repositories. | ||
| It also allows access to access private/internal repositories via the GitHub API. | ||
|
|
||
| Returns: | ||
| The module object of the package. | ||
| """ | ||
| registry_root = "package" | ||
| dir_path = f"{registry_root}/{package}" | ||
| hostname = urlparse(base_url).hostname | ||
| hostname = _extract_hostname(base_url) if base_url else "github.com" | ||
|
nabenabe0928 marked this conversation as resolved.
|
||
| if hostname is None: | ||
| raise ValueError(f"Invalid base URL: {base_url}") | ||
| raise ValueError(f"Invalid base URI: {base_url}") | ||
|
nabenabe0928 marked this conversation as resolved.
|
||
| cache_dir_prefix = os.path.join(_conf.cache_home(), hostname, repo_owner, repo_name, ref) | ||
| package_cache_dir = os.path.join(cache_dir_prefix, dir_path) | ||
| use_cache = not force_reload and os.path.exists(package_cache_dir) | ||
|
|
||
| if not use_cache: | ||
| # Download package from GitHub. | ||
| g = Github(auth=auth, base_url=base_url) | ||
| repo = g.get_repo(f"{repo_owner}/{repo_name}") | ||
|
|
||
| package_contents = repo.get_contents(dir_path, ref) | ||
|
|
||
| if isinstance(package_contents, ContentFile): | ||
| package_contents = [package_contents] | ||
|
|
||
| shutil.rmtree(package_cache_dir, ignore_errors=True) | ||
| os.makedirs(cache_dir_prefix, exist_ok=True) | ||
| for m in package_contents: | ||
| file_path = os.path.join(cache_dir_prefix, m.path) | ||
| os.makedirs(os.path.dirname(file_path), exist_ok=True) | ||
| if m.type == "dir": | ||
| dir_contents = repo.get_contents(m.path, ref) | ||
| if isinstance(dir_contents, ContentFile): | ||
| dir_contents = [dir_contents] | ||
| package_contents.extend(dir_contents) | ||
| else: | ||
| with open(file_path, "wb") as f: | ||
| try: | ||
| decoded_content = m.decoded_content | ||
| except AssertionError: | ||
| continue | ||
| f.write(decoded_content) | ||
| if auth is None and shutil.which("git") is not None: | ||
| _download_via_git( | ||
| repo_owner=repo_owner, | ||
| repo_name=repo_name, | ||
| dir_path=dir_path, | ||
| ref=ref, | ||
| base_url=base_url or "https://github.com", | ||
| cache_dir_prefix=cache_dir_prefix, | ||
| ) | ||
| else: | ||
| _download_via_github_api( | ||
|
kAIto47802 marked this conversation as resolved.
|
||
| auth=auth, | ||
| base_url=base_url or "https://api.github.com", | ||
| repo_owner=repo_owner, | ||
| repo_name=repo_name, | ||
| dir_path=dir_path, | ||
| ref=ref, | ||
| package_cache_dir=package_cache_dir, | ||
| cache_dir_prefix=cache_dir_prefix, | ||
| ) | ||
|
|
||
| local_registry_root = os.path.join(cache_dir_prefix, registry_root) | ||
| module = load_local_module( | ||
|
|
@@ -147,14 +145,82 @@ | |
| is_official_registry = ( | ||
| repo_owner == "optuna" | ||
| and repo_name == "optunahub-registry" | ||
| and base_url == "https://api.github.com" | ||
| and base_url == "https://github.com" | ||
| ) | ||
| if not _conf.is_no_analytics() and not use_cache and is_official_registry: | ||
| _report_stats(package, ref) | ||
|
|
||
| return module | ||
|
|
||
|
|
||
| def _extract_hostname(url: str) -> str | None: | ||
|
kAIto47802 marked this conversation as resolved.
|
||
| if "://" in url: | ||
| return urlparse(url).hostname | ||
| else: | ||
| # NOTE(kAIto47802) Extract hostname: skip optional user@, capture up to `:`, ignore the rest. | ||
| match = re.match(r"(?:.+@)?([^:]+)(?::.*)?", url) | ||
|
kAIto47802 marked this conversation as resolved.
|
||
| return match and match.group(1) | ||
|
|
||
|
|
||
| def _download_via_git( | ||
| repo_owner: str, | ||
| repo_name: str, | ||
| dir_path: str, | ||
| ref: str, | ||
| base_url: str, | ||
| cache_dir_prefix: str, | ||
| ) -> None: | ||
| repo_url_separator = "/" if "://" in base_url else ":" | ||
| repo_url = f"{base_url.rstrip('/')}{repo_url_separator}{repo_owner}/{repo_name}" | ||
| repo = Repo.init(cache_dir_prefix) | ||
| origin = ( | ||
| repo.remotes.origin if "origin" in repo.remotes else repo.create_remote("origin", repo_url) | ||
| ) | ||
| if repo.remotes.origin.url != repo_url: | ||
| repo.remotes.origin.set_url(repo_url) | ||
| repo.git.sparse_checkout("init", "--cone") | ||
| repo.git.sparse_checkout("set", dir_path) | ||
| origin.fetch(refspec=ref, depth=1) | ||
| repo.git.checkout("FETCH_HEAD") | ||
|
|
||
|
|
||
| def _download_via_github_api( | ||
| auth: Auth.Auth | None, | ||
| base_url: str, | ||
| repo_owner: str, | ||
| repo_name: str, | ||
| dir_path: str, | ||
| ref: str, | ||
| package_cache_dir: str, | ||
| cache_dir_prefix: str, | ||
| ) -> None: | ||
| g = Github(auth=auth, base_url=base_url) | ||
| repo = g.get_repo(f"{repo_owner}/{repo_name}") | ||
|
|
||
| package_contents = repo.get_contents(dir_path, ref) | ||
|
|
||
| if isinstance(package_contents, ContentFile): | ||
| package_contents = [package_contents] | ||
|
|
||
| shutil.rmtree(package_cache_dir, ignore_errors=True) | ||
| os.makedirs(cache_dir_prefix, exist_ok=True) | ||
| for m in package_contents: | ||
| file_path = os.path.join(cache_dir_prefix, m.path) | ||
| os.makedirs(os.path.dirname(file_path), exist_ok=True) | ||
| if m.type == "dir": | ||
| dir_contents = repo.get_contents(m.path, ref) | ||
| if isinstance(dir_contents, ContentFile): | ||
| dir_contents = [dir_contents] | ||
| package_contents.extend(dir_contents) | ||
| else: | ||
| with open(file_path, "wb") as f: | ||
| try: | ||
| decoded_content = m.decoded_content | ||
| except AssertionError: | ||
| continue | ||
| f.write(decoded_content) | ||
|
|
||
|
|
||
| def load_local_module( | ||
| package: str, | ||
| *, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your suggestion! However, the examples you suggested are actually the opposite, and the URL is incorrect---
https://api.github.com,https://github.enterprise.com/api/v3, andhttps://gitlab.com/api/v4are the examples for the GitHub API. Also, the endpoints for GitHub Enterprise, GitLab, and other services are not limited to this, since they support custom domains.12So let me update the docstring with modifications.
Footnotes
https://docs.gitlab.com/user/ssh/ ↩
https://docs.gitlab.com/api/rest/ ↩