Repository navigation
Cut ug startup cost: fast --version entry point and lazy heavy imports #1018
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
david-siqi-liu
merged 7 commits into
databricks:main
from
yaoharry:harry-yao/port-startup-imports
Oct 13, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a68bc97
Cut ug startup cost with a fast version entry point and lazy imports
faec297
Clarify CLI health probes after startup-import review
d2fbf05
Merge origin/main into harry-yao/port-startup-imports
0f4019a
Merge remote-tracking branch 'origin/main' into codex/pr1018
268bc4f
Merge remote-tracking branch 'ugh/main' into pr-1018-update
david-siqi-liu 1b46a92
Merge remote-tracking branch 'ugh/main' into pr-1018-update
david-siqi-liu 8739e7a
Skip the Windows console-script launcher in the fast version lookup
david-siqi-liu 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
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
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| """Console-script entry point for ``ug`` / ``ucode``. | ||
|
|
||
| Launchers such as isaac run ``ug --version`` on every start. Importing ``ucode.cli`` (Typer, Rich, | ||
| questionary, every subcommand module) costs far more than printing the version, so a bare version | ||
| request is answered here without importing it. Every other invocation goes to ``ucode.cli.main``. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import sys | ||
|
|
||
| _DISTRIBUTION = "unity_gateway" | ||
| _VERSION_ARGV = (["--version"], ["-V"]) | ||
|
|
||
|
|
||
| def _read_installed_version() -> str | None: | ||
| """Return the installed ``unity-gateway`` version from its dist-info ``METADATA``. | ||
|
|
||
| Mirrors the lookup ``importlib.metadata.version`` does for a plain directory ``sys.path`` | ||
| (first matching ``*.dist-info`` wins) without importing ``importlib.metadata``, which alone | ||
| costs several times a bare interpreter start. Returns None for anything it does not handle | ||
| (zip/egg entries, missing or unreadable metadata) so the caller uses the standard lookup. | ||
| """ | ||
| for entry in sys.path: | ||
| try: | ||
| names = os.listdir(entry or ".") | ||
| except (FileNotFoundError, PermissionError): | ||
| continue | ||
| except NotADirectoryError: | ||
| # Windows puts the console-script launcher (e.g. Scripts\ug.exe) on sys.path; it holds | ||
| # no distribution metadata. Other files may be zip archives, so they fall back. | ||
| if entry.lower().endswith(".exe"): | ||
| continue | ||
| return None | ||
| except OSError: | ||
| return None | ||
| for name in names: | ||
| lowered = name.lower() | ||
| if lowered.endswith((".egg-info", ".egg")) and lowered.startswith(_DISTRIBUTION): | ||
| return None | ||
| if not lowered.endswith(".dist-info"): | ||
| continue | ||
| if lowered.rpartition(".")[0].partition("-")[0].replace(".", "_") != _DISTRIBUTION: | ||
| continue | ||
| try: | ||
| with open(os.path.join(entry, name, "METADATA"), encoding="utf-8") as metadata: | ||
| for line in metadata: | ||
| if not line.strip(): | ||
| break | ||
| key, _, value = line.partition(":") | ||
| if key.strip().lower() == "version": | ||
| return value.strip() or None | ||
| except OSError: | ||
| return None | ||
| return None | ||
| return None | ||
|
|
||
|
|
||
| def main() -> None: | ||
| if sys.argv[1:] in _VERSION_ARGV: | ||
| version = _read_installed_version() | ||
| if version is None: | ||
| from ucode.telemetry import ug_version | ||
|
|
||
| version = ug_version() | ||
| print(version) | ||
| return | ||
|
|
||
| from ucode.cli import main as cli_main | ||
|
|
||
| cli_main() | ||
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.