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

Add support for relative requirements in pip_install #433

Merged
merged 10 commits into from
Oct 26, 2021
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This lets us glob() up all the files inside the examples to make them inputs to tests
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
# To update these lines, run tools/bazel_integration_test/update_deleted_packages.sh
build --deleted_packages=examples/legacy_pip_import/boto,examples/legacy_pip_import/extras,examples/legacy_pip_import/helloworld,examples/pip_install,examples/pip_parse,examples/py_import
query --deleted_packages=examples/legacy_pip_import/boto,examples/legacy_pip_import/extras,examples/legacy_pip_import/helloworld,examples/pip_install,examples/pip_parse,examples/py_import
build --deleted_packages=examples/legacy_pip_import/boto,examples/legacy_pip_import/extras,examples/legacy_pip_import/helloworld,examples/pip_install,examples/pip_parse,examples/py_import,examples/relative_requirements
query --deleted_packages=examples/legacy_pip_import/boto,examples/legacy_pip_import/extras,examples/legacy_pip_import/helloworld,examples/pip_install,examples/pip_parse,examples/py_import,examples/relative_requirements

test --test_output=errors
5 changes: 5 additions & 0 deletions examples/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ bazel_integration_test(
name = "py_import_example",
timeout = "long",
)

bazel_integration_test(
name = "relative_requirements_example",
timeout = "long",
)
10 changes: 10 additions & 0 deletions examples/relative_requirements/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("@pip//:requirements.bzl", "requirement")
load("@rules_python//python:defs.bzl", "py_test")

py_test(
name = "main",
srcs = ["main.py"],
deps = [
requirement("relative_package_name"),
],
)
4 changes: 4 additions & 0 deletions examples/relative_requirements/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# relative_requirements example

This example shows how to use pip to fetch relative dependencies from a requirements.txt file,
then use them in BUILD files as dependencies of Bazel targets.
15 changes: 15 additions & 0 deletions examples/relative_requirements/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
workspace(name = "example_repo")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "rules_python",
sha256 = "b6d46438523a3ec0f3cead544190ee13223a52f6a6765a29eae7b7cc24cc83a0",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.1.0/rules_python-0.1.0.tar.gz",
)

load("@rules_python//python:pip.bzl", "pip_install")

pip_install(
requirements = "//:requirements.txt",
)
5 changes: 5 additions & 0 deletions examples/relative_requirements/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import relative_package_name

if __name__ == "__main__":
# Run a function from the relative package
print(relative_package_name.test())
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test():
return True
7 changes: 7 additions & 0 deletions examples/relative_requirements/relative_package/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from setuptools import setup

setup(
name='relative_package_name',
version='1.0.0',
packages=['relative_package_name'],
)
1 change: 1 addition & 0 deletions examples/relative_requirements/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./relative_package
thundergolfer marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 7 additions & 1 deletion python/pip_install/extract_wheels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import argparse
import glob
import os
import pathlib
import subprocess
import sys
import json
Expand Down Expand Up @@ -63,17 +64,22 @@ def main() -> None:
deserialized_args = dict(vars(args))
arguments.deserialize_structured_args(deserialized_args)

# Pip is run with the working directory changed to the folder containing the requirements.txt file, to allow for
# relative requirements to be correctly resolved. The --wheel-dir is therefore required to be repointed back to the
# current calling working directory (the repo root in .../external/name), where the wheel files should be written to
pip_args = (
[sys.executable, "-m", "pip"] +
(["--isolated"] if args.isolated else []) +
["wheel", "-r", args.requirements] +
["--wheel-dir", os.getcwd()] +
deserialized_args["extra_pip_args"]
)

env = os.environ.copy()
env.update(deserialized_args["environment"])

# Assumes any errors are logged by pip so do nothing. This command will fail if pip fails
subprocess.run(pip_args, check=True, env=env)
subprocess.run(pip_args, check=True, env=env, cwd=str(pathlib.Path(args.requirements).parent.resolve()))

extras = requirements.parse_extras(args.requirements)

Expand Down