[go: up one dir, main page]

Skip to content
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

feat(pypi): support env markers in requirements files #2059

Merged
merged 43 commits into from
Aug 15, 2024

Conversation

aignas
Copy link
Collaborator
@aignas aignas commented Jul 12, 2024

Before this change the all_requirements and related constants will have
packages that need to be installed only on specific platforms and will mean
that users relying on those constants (e.g. gazelle) will need to do extra
work to exclude platform-specific packages. The package managers that that
support outputting such files now include uv and pdm. This might be also
useful in cases where we attempt to handle non-requirements lock files.

Note, that the best way to handle this would be to move all of the requirements
parsing code to Python, but that may cause regressions as it is a much bigger
change. This is only changing the code so that we are doing extra processing
for the requirement lines that have env markers. The lines that have no markers
will not see any change in the code execution paths and the python interpreter
will not be downloaded.

We also use the *_ctx.watch API where available to correctly re-evaluate the
markers if the packaging Python sources for this change.

Extra changes that are included in this PR:

  • Extend the repo_utils to have a method for arch getting from the ctx.
  • Change the local_runtime_repo to perform the validation not relying on the
    implementation detail of the get_platforms_os_name.
  • Add $(UV) make variable for the uv:current_toolchain so that we can
    generate the requirements for sphinx using uv.
  • Swap the requirement generation using genrule and uv for sphinx and co
    so that we can test the requirement marker code. Note, the requirement
    markers are not working well with the requirement_cycles.

Fixes #1105.
Fixes #1868.
Work towards #260, #1975.
Related #1663.

@aignas
Copy link
Collaborator Author
aignas commented Jul 16, 2024

Since #2000 is introducing useful helpers, I am gonna wait until that is merged and this PR is getting a little big, so I'm gonna need to split it into smaller portions.

aignas added a commit to aignas/rules_python that referenced this pull request Jul 16, 2024
aignas added a commit to aignas/rules_python that referenced this pull request Jul 16, 2024
…around

This is extra preparation needed for bazelbuild#2059.

Summary:
- Create `pypi_repo_utils` for more ergonomic handling of Python in repo context.
- Split the resolution of requirements files to platforms into a separate function
  to make the testing easier. This also allows more validation that was realized
  that there is a need for in the WIP feature PR.
- Make the code more robust about the assumption of the target platform label.

Work towards bazelbuild#260, bazelbuild#1105, bazelbuild#1868.
@aignas aignas force-pushed the feat/support-requirement-markers branch from d93f1ae to 69a296a Compare July 16, 2024 07:33
@aignas
Copy link
Collaborator Author
aignas commented Jul 16, 2024

currently stacked on #2069

aignas added a commit to aignas/rules_python that referenced this pull request Jul 16, 2024
…around

This is extra preparation needed for bazelbuild#2059.

Summary:
- Create `pypi_repo_utils` for more ergonomic handling of Python in repo context.
- Split the resolution of requirements files to platforms into a separate function
  to make the testing easier. This also allows more validation that was realized
  that there is a need for in the WIP feature PR.
- Make the code more robust about the assumption of the target platform label.

Work towards bazelbuild#260, bazelbuild#1105, bazelbuild#1868.
github-merge-queue bot pushed a commit that referenced this pull request Jul 17, 2024
…around (#2069)

This is extra preparation needed for #2059.

Summary:
- Create `pypi_repo_utils` for more ergonomic handling of Python in repo
context.
- Split the resolution of requirements files to platforms into a
separate function
to make the testing easier. This also allows more validation that was
realized
  that there is a need for in the WIP feature PR.
- Make the code more robust about the assumption of the target platform
label.

Work towards #260, #1105, #1868.
github-merge-queue bot pushed a commit that referenced this pull request Jul 17, 2024
…2068)

This is just a small PR to reduce the scope of #2059.

This just moves some code from one python file to a separate one.

Work towards #260, #1105, #1868.
@aignas aignas force-pushed the feat/support-requirement-markers branch from 69a296a to 2106402 Compare July 17, 2024 07:22
Move more things to repo_utils and stop using watch_tree, because it
also starts watching __pycache__ files, that is causing excesive
restarts. What is more, we previously where using uname to get the OS,
but it does not make sense to do that, we can use rctx.os.name instead
and common utilities.

What is more, be more explicit about what OSes are supported in the
local_runtime_repo.
@aignas aignas marked this pull request as ready for review July 17, 2024 13:55
… added to the bzlmod lockfile to re-evaluate the files if the sources change
Copy link
Collaborator Author
@aignas aignas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self review.

MODULE.bazel Show resolved Hide resolved
docs/sphinx/BUILD.bazel Outdated Show resolved Hide resolved
python/private/pypi/evaluate_markers.bzl Outdated Show resolved Hide resolved
python/private/pypi/extension.bzl Outdated Show resolved Hide resolved
python/private/pypi/pip_repository.bzl Outdated Show resolved Hide resolved
Comment on lines +46 to +61
with args.input_path.open() as f:
reqs = json.load(f)

response = {}
for requirement_line, target_platforms in reqs.items():
entry, prefix, hashes = requirement_line.partition("--hash")
hashes = prefix + hashes

req = Requirement(entry)
for p in target_platforms:
(platform,) = Platform.from_string(p)
if not req.marker or req.marker.evaluate(platform.env_markers("")):
response.setdefault(requirement_line, []).append(p)

with args.output_path.open("w") as f:
json.dump(response, f)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently there are no unit tests, but if we agree on the approach I could add a few.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine. Whether we run a python program, uv, or Starlark, how we evaluate the markers is an internal detail, right?

python/private/repo_utils.bzl Show resolved Hide resolved
tests/pypi/evaluate_markers/BUILD.bazel Show resolved Hide resolved
@aignas aignas marked this pull request as ready for review August 6, 2024 11:37
@aignas aignas requested a review from rickeylev August 7, 2024 11:53
Copy link
Collaborator
@groodt groodt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rickeylev
Copy link
Contributor

I pushed a couple doc fixes, otherwise LGTM

@rickeylev rickeylev added this pull request to the merge queue Aug 15, 2024
Merged via the queue into bazelbuild:main with commit 519574c Aug 15, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants