-
Notifications
You must be signed in to change notification settings - Fork 14
feat(cli): add lockfile support to pywrangler sync command #108
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
+402
−94
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e71384d
feat(cli): add lockfile support to pywrangler sync command
ryanking13 6371d1e
Merge remote-tracking branch 'origin/main' into gyeongjae/lockfile-re…
ryanking13 08e54ed
Merge remote-tracking branch 'origin/main' into gyeongjae/lockfile-re…
ryanking13 623b348
chore: move get_lockfile_path function to utils.py
ryanking13 c5d3b3f
chore: replace sp.run with run_command
ryanking13 aae2413
chore: Use temp_requirements_file fixture
ryanking13 4f78767
chore: always use lockfile
ryanking13 1a2649d
chore: remove unnecessary mocks in test_cli
ryanking13 8e59556
Merge remote-tracking branch 'origin/main' into gyeongjae/lockfile-re…
ryanking13 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import logging | ||
| import tomllib | ||
| from pathlib import Path | ||
|
|
||
| from .utils import ( | ||
| get_lockfile_path, | ||
| get_project_root, | ||
| get_pyodide_index, | ||
| get_uv_pyodide_interp_name, | ||
| read_pyproject_toml, | ||
| run_command, | ||
| temp_requirements_file, | ||
| ) | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| MANAGED_SDK_PACKAGE = "workers-runtime-sdk" | ||
|
|
||
|
|
||
| class InstallPlan: | ||
| def __init__(self, lockfile: Path) -> None: | ||
| self.lockfile = lockfile | ||
| self.requirements: list[tuple[str, str]] = [] | ||
|
|
||
| with open(lockfile, "rb") as f: | ||
| data = tomllib.load(f) | ||
|
|
||
| for pkg in data.get("packages", []): | ||
| name = pkg.get("name") | ||
| version = pkg.get("version") | ||
| if not name or not version: | ||
| logger.warning("Skipping malformed lockfile entry: %s", pkg) | ||
| continue | ||
| self.requirements.append((name, version)) | ||
|
|
||
| def to_requirement_strings(self) -> list[str]: | ||
| return [f"{name}=={version}" for name, version in self.requirements] | ||
|
|
||
|
|
||
| def parse_requirements() -> list[str]: | ||
| pyproject_data = read_pyproject_toml() | ||
|
|
||
| # Extract dependencies from [project.dependencies] | ||
| return pyproject_data.get("project", {}).get("dependencies", []) | ||
|
|
||
|
|
||
| def _compile_lockfile( | ||
| requirements: list[str], | ||
| lockfile_path: Path, | ||
| *, | ||
| upgrade: bool = False, | ||
| ) -> None: | ||
| """Run ``uv pip compile`` targeting Pyodide. | ||
|
|
||
| Writes the compiled output to *lockfile_path*. When *lockfile_path* already | ||
| exists, ``uv pip compile`` uses it as a constraint source so pinned versions | ||
| are preserved across re-runs (no silent upgrades). | ||
| """ | ||
| with temp_requirements_file(requirements) as req_in_path: | ||
| cmd = [ | ||
| "uv", | ||
| "pip", | ||
| "compile", | ||
| req_in_path, | ||
| "--python", | ||
| get_uv_pyodide_interp_name(), | ||
| "--extra-index-url", | ||
| get_pyodide_index(), | ||
| "--index-strategy", | ||
| "unsafe-best-match", | ||
| "--no-build", | ||
| "--no-header", | ||
| "-o", | ||
| str(lockfile_path), | ||
| ] | ||
| if upgrade: | ||
| cmd.append("--upgrade") | ||
|
|
||
| run_command(cmd, cwd=get_project_root(), capture_output=True) | ||
|
|
||
|
|
||
| def resolve_requirements(*, upgrade: bool = False) -> InstallPlan: | ||
| """Build an InstallPlan by compiling dependencies for the Pyodide target. | ||
|
|
||
| Runs ``uv pip compile`` with the Pyodide interpreter and ``--no-build`` | ||
| to resolve versions that have Pyodide wheels. The compiled output is | ||
| written to ``pylock.toml``; on subsequent runs the existing file | ||
| constrains versions so they don't drift. | ||
| """ | ||
| lockfile = get_lockfile_path() | ||
|
|
||
| deps = parse_requirements() | ||
| deps.append(MANAGED_SDK_PACKAGE) | ||
|
|
||
| _compile_lockfile(deps, lockfile, upgrade=upgrade) | ||
| plan = InstallPlan(lockfile) | ||
|
|
||
| logger.info("Resolved %d requirements from %s.", len(plan.requirements), lockfile) | ||
| for name, version in plan.requirements: | ||
| logger.debug(" - %s==%s", name, version) | ||
| return plan |
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.