Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aee9704

Browse filesBrowse files
aaliddellalexeagle
andauthored
Add support for relative requirements in pip_install (bazel-contrib#433)
* Run pip within the directory containing the requirements.txt file This allows for relative requirements to be resolved in the way that standalone pip would without patching or parsing the requirements file manually * Print test errors of sub-workspaces in bazel_integration_test Prior to this, errors would be written to logs in the temp directory that would promptly be deleted on parent test teardown. This means at least the test logs will be printed on failure * Add example of relative requirement as test * Explicitly cast Path to str to support Python 3.5 * Fix multiple declarations of pip_args * Apply buildifier fix Co-authored-by: Alex Eagle <eagle@post.harvard.edu>
1 parent b622c4c commit aee9704
Copy full SHA for aee9704

File tree

10 files changed

+58
-3
lines changed
Filter options

10 files changed

+58
-3
lines changed

‎.bazelrc

Copy file name to clipboardExpand all lines: .bazelrc
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# This lets us glob() up all the files inside the examples to make them inputs to tests
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, run tools/bazel_integration_test/update_deleted_packages.sh
6-
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
7-
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
6+
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
7+
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
88

99
test --test_output=errors

‎examples/BUILD

Copy file name to clipboardExpand all lines: examples/BUILD
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ bazel_integration_test(
3636
name = "py_import_example",
3737
timeout = "long",
3838
)
39+
40+
bazel_integration_test(
41+
name = "relative_requirements_example",
42+
timeout = "long",
43+
)

‎examples/relative_requirements/BUILD

Copy file name to clipboard
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load("@pip//:requirements.bzl", "requirement")
2+
load("@rules_python//python:defs.bzl", "py_test")
3+
4+
py_test(
5+
name = "main",
6+
srcs = ["main.py"],
7+
deps = [
8+
requirement("relative_package_name"),
9+
],
10+
)
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# relative_requirements example
2+
3+
This example shows how to use pip to fetch relative dependencies from a requirements.txt file,
4+
then use them in BUILD files as dependencies of Bazel targets.
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
workspace(name = "example_repo")
2+
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4+
5+
http_archive(
6+
name = "rules_python",
7+
sha256 = "b6d46438523a3ec0f3cead544190ee13223a52f6a6765a29eae7b7cc24cc83a0",
8+
url = "https://github.com/bazelbuild/rules_python/releases/download/0.1.0/rules_python-0.1.0.tar.gz",
9+
)
10+
11+
load("@rules_python//python:pip.bzl", "pip_install")
12+
13+
pip_install(
14+
requirements = "//:requirements.txt",
15+
)
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import relative_package_name
2+
3+
if __name__ == "__main__":
4+
# Run a function from the relative package
5+
print(relative_package_name.test())
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def test():
2+
return True
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from setuptools import setup
2+
3+
setup(
4+
name='relative_package_name',
5+
version='1.0.0',
6+
packages=['relative_package_name'],
7+
)
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./relative_package

‎python/pip_install/extract_wheels/__init__.py

Copy file name to clipboardExpand all lines: python/pip_install/extract_wheels/__init__.py
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import argparse
99
import glob
1010
import os
11+
import pathlib
1112
import subprocess
1213
import sys
1314
import json
@@ -63,17 +64,22 @@ def main() -> None:
6364
deserialized_args = dict(vars(args))
6465
arguments.deserialize_structured_args(deserialized_args)
6566

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

7378
env = os.environ.copy()
7479
env.update(deserialized_args["environment"])
80+
7581
# Assumes any errors are logged by pip so do nothing. This command will fail if pip fails
76-
subprocess.run(pip_args, check=True, env=env)
82+
subprocess.run(pip_args, check=True, env=env, cwd=str(pathlib.Path(args.requirements).parent.resolve()))
7783

7884
extras = requirements.parse_extras(args.requirements)
7985

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.