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

type(feat): Allow parsing of wheel file paths in requirements.in #2345

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import sys
from pathlib import Path
from typing import Optional, Tuple
import multiprocessing

import click
import piptools.writer as piptools_writer
Expand Down Expand Up @@ -97,6 +98,7 @@ def main(
extra_args: Tuple[str, ...],
) -> None:
bazel_runfiles = runfiles.Create()
runfiles_dir = Path(bazel_runfiles._python_runfiles_root) / "_main"

requirements_file = _select_golden_requirements_file(
requirements_txt=requirements_txt,
Expand Down Expand Up @@ -182,10 +184,16 @@ def main(
resolved_requirements_file, requirements_file_tree
)
)
cli(argv)
# Run in a process as pip tools is calling sys.exit()
piptools_process = multiprocessing.Process(target=cli, args=[argv])
piptools_process.start()
piptools_process.join()
requirements_file_relative_path = Path(requirements_file_relative)
content = requirements_file_relative_path.read_text()
content = content.replace(absolute_path_prefix, "")
# For windows or linux.
content = content.replace(str(runfiles_dir) + "/", "")
content = content.replace(str(runfiles_dir) + "\\", "")
requirements_file_relative_path.write_text(content)
else:
# cli will exit(0) on success
Expand Down
4 changes: 3 additions & 1 deletion python/private/pypi/pip_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def pip_compile(
src = None,
extra_args = [],
extra_deps = [],
extra_data = [],
generate_hashes = True,
py_binary = _py_binary,
py_test = _py_test,
Expand Down Expand Up @@ -66,6 +67,7 @@ def pip_compile(
[PEP621](https://peps.python.org/pep-0621/).
extra_args: passed to pip-compile.
extra_deps: extra dependencies passed to pip-compile.
extra_data: extra data passed to pip-compile.
generate_hashes: whether to put hashes in the requirements_txt file.
py_binary: the py_binary rule to be used.
py_test: the py_test rule to be used.
Expand Down Expand Up @@ -99,7 +101,7 @@ def pip_compile(
visibility = visibility,
)

data = [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None]
data = [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None] + extra_data

# Use the Label constructor so this is expanded in the context of the file
# where it appears, which is to say, in @rules_python
Expand Down
8 changes: 6 additions & 2 deletions python/private/pypi/requirements.bzl.tmpl.workspace
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _get_annotation(requirement):
name = requirement.split(" ")[0].split("=")[0].split("[")[0]
return _annotations.get(name)

def install_deps(**whl_library_kwargs):
def install_deps(whl_files = {}, **whl_library_kwargs):
"""Repository rule macro. Install dependencies from `pip_parse`.

Args:
Expand All @@ -61,12 +61,16 @@ def install_deps(**whl_library_kwargs):
for name, requirement in _packages:
group_name = requirement_group_mapping.get(name.replace("%%NAME%%_", ""))
group_deps = all_requirement_groups.get(group_name, [])

if "@ file://" in requirement:
whl_file = whl_files[name]
else:
whl_file = None
whl_library(
name = name,
requirement = requirement,
group_name = group_name,
group_deps = group_deps,
annotation = _get_annotation(requirement),
whl_file = whl_file,
**whl_config
)