Skip to content

Commit 12bfbdf

Browse files
wip: fetch dependencies
1 parent 2b20e0e commit 12bfbdf

File tree

512 files changed

+185
-152598
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

512 files changed

+185
-152598
lines changed

WORKSPACE

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
11
workspace(name = "rules_python_external")
2+
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4+
5+
http_archive(
6+
name = "rules_python",
7+
url = "https://github.com/bazelbuild/rules_python/releases/download/0.0.1/rules_python-0.0.1.tar.gz",
8+
sha256 = "aa96a691d3a8177f3215b14b0edc9641787abaaa30363a080165d06ab65e1161",
9+
)
10+
11+
load("@rules_python//python:repositories.bzl", "py_repositories")
12+
py_repositories()
13+
14+
load("//:repositories.bzl", "rules_python_external_dependencies")
15+
rules_python_external_dependencies()

defs.bzl

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
load("//:repositories.bzl", "all_requirements")
2+
3+
DEFAULT_REPOSITORY_NAME = "pip"
4+
5+
16
def _pip_repository_impl(rctx):
27
if not rctx.which(rctx.attr.python_interpreter):
38
fail("python interpreter not found")
@@ -7,17 +12,26 @@ def _pip_repository_impl(rctx):
712
result = rctx.execute(
813
[
914
rctx.which(rctx.attr.python_interpreter),
10-
rctx.path(rctx.attr._script),
15+
rctx.path(rctx.attr._script).dirname,
1116
"--requirements",
1217
rctx.path(rctx.attr.requirements),
1318
"--repo",
1419
"@%s" % rctx.attr.name,
1520
],
16-
17-
environment = rctx.attr.wheel_env,
21+
environment={
22+
# Manually construct the PYTHONPATH since we cannot use the toolchain here
23+
"PYTHONPATH": ":".join(
24+
# Includes the root of this repo and all the external dependencies from repositories.bzl
25+
[str(rctx.path(rctx.attr._script).dirname.dirname)]
26+
+ [
27+
str(rctx.path(Label("@" + repo + "//:BUILD.bazel")).dirname)
28+
for repo in all_requirements
29+
]
30+
)
31+
},
1832
)
1933
if result.return_code:
20-
fail("failed to create pip repository: %s (%s)" % (result.stdout, result.stderr))
34+
fail("rules_python_external failed: %s (%s)" % (result.stdout, result.stderr))
2135

2236
return
2337

@@ -28,10 +42,14 @@ pip_repository = repository_rule(
2842
"wheel_env": attr.string_dict(),
2943
"python_interpreter": attr.string(default="python3"),
3044
"_script": attr.label(
31-
executable=True,
32-
default=Label("//tools:wheel_wrapper.py"),
33-
cfg="host",
45+
executable=True, default=Label("//src:__main__.py"), cfg="host",
3446
),
3547
},
3648
implementation=_pip_repository_impl,
3749
)
50+
51+
52+
def pip_install(requirements, name=DEFAULT_REPOSITORY_NAME):
53+
pip_repository(
54+
name=name, requirements=requirements,
55+
)

example/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("@pip//:requirements.bzl", "requirement")
2+
3+
py_binary(
4+
name = "main",
5+
srcs = ["main.py"],
6+
deps = [
7+
requirement("boto3")
8+
],
9+
)

example/WORKSPACE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
url = "https://github.com/bazelbuild/rules_python/releases/download/0.0.1/rules_python-0.0.1.tar.gz",
8+
sha256 = "aa96a691d3a8177f3215b14b0edc9641787abaaa30363a080165d06ab65e1161",
9+
)
10+
load("@rules_python//python:repositories.bzl", "py_repositories")
11+
py_repositories()
12+
13+
local_repository(
14+
name = "rules_python_external",
15+
path = "../",
16+
)
17+
18+
load("@rules_python_external//:repositories.bzl", "rules_python_external_dependencies")
19+
rules_python_external_dependencies()
20+
21+
load("@rules_python_external//:defs.bzl", "pip_install")
22+
pip_install(
23+
requirements = "//:requirements.txt",
24+
)

example/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import boto3
2+
3+
print("Hello World")

example/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
boto3

repositories.bzl

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
3+
4+
_RULE_DEPS = [
5+
(
6+
"pypi__pip",
7+
"https://files.pythonhosted.org/packages/00/b6/9cfa56b4081ad13874b0c6f96af8ce16cfbc1cb06bedf8e9164ce5551ec1/pip-19.3.1-py2.py3-none-any.whl",
8+
"6917c65fc3769ecdc61405d3dfd97afdedd75808d200b2838d7d961cebc0c2c7",
9+
),
10+
(
11+
"pypi__pkginfo",
12+
"https://files.pythonhosted.org/packages/e6/d5/451b913307b478c49eb29084916639dc53a88489b993530fed0a66bab8b9/pkginfo-1.5.0.1-py2.py3-none-any.whl",
13+
"a6d9e40ca61ad3ebd0b72fbadd4fba16e4c0e4df0428c041e01e06eb6ee71f32",
14+
),
15+
(
16+
"pypi__setuptools",
17+
"https://files.pythonhosted.org/packages/54/28/c45d8b54c1339f9644b87663945e54a8503cfef59cf0f65b3ff5dd17cf64/setuptools-42.0.2-py2.py3-none-any.whl",
18+
"c8abd0f3574bc23afd2f6fd2c415ba7d9e097c8a99b845473b0d957ba1e2dac6",
19+
),
20+
(
21+
"pypi__wheel",
22+
"https://files.pythonhosted.org/packages/00/83/b4a77d044e78ad1a45610eb88f745be2fd2c6d658f9798a15e384b7d57c9/wheel-0.33.6-py2.py3-none-any.whl",
23+
"f4da1763d3becf2e2cd92a14a7c920f0f00eca30fdde9ea992c836685b9faf28",
24+
),
25+
]
26+
27+
_GENERIC_WHEEL = """\
28+
package(default_visibility = ["//visibility:public"])
29+
30+
load("@rules_python//python:defs.bzl", "py_library")
31+
32+
py_library(
33+
name = "lib",
34+
srcs = glob(["**/*.py"]),
35+
data = glob(["**/*"], exclude=["**/*.py", "**/* *", "BUILD", "WORKSPACE"]),
36+
# This makes this directory a top-level in the python import
37+
# search path for anything that depends on this.
38+
imports = ["."],
39+
)
40+
"""
41+
42+
# Collate all the repository names so they can be easily consumed
43+
all_requirements = [name for (name, _, _) in _RULE_DEPS]
44+
45+
46+
def rules_python_external_dependencies():
47+
for (name, url, sha256) in _RULE_DEPS:
48+
maybe(
49+
http_archive,
50+
name,
51+
url=url,
52+
sha256=sha256,
53+
type="zip",
54+
build_file_content=_GENERIC_WHEEL,
55+
)

src/BUILD

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1+
load("//:repositories.bzl", "all_requirements")
2+
13
py_library(
24
name = "src",
3-
srcs = glob(["*.py"]),
45
srcs_version = "PY3",
5-
deps = [],
6+
srcs = glob(["**/*.py"]),
7+
data = glob(["**/*"], exclude=["**/*.py", "**/* *", "BUILD", "WORKSPACE"]),
8+
deps = ["@"+ repo + "//:lib" for repo in all_requirements],
69
)
710

811
py_test(
912
name = "test",
10-
srcs = glob(["tests/*.py"]),
13+
srcs = glob(["tests/**/*.py"]),
1114
main = "tests/__main__.py",
1215
deps = [
1316
":src",
14-
"//third_party/python:pkginfo",
1517
],
1618
size="small",
17-
imports = [
18-
"../third_party/python/",
19-
]
2019
)

src/__init__.py

Whitespace-only changes.
Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,8 @@
33
reproducible wheels
44
"""
55
import os
6-
import sys
76

8-
9-
def configure_vendor():
10-
"""
11-
The dependencies we vendor need to be added to be added to the PYTHONPATH. We add the root of our vendor tree
12-
`third_party` which enables the modules to find their own dependencies as expected.
13-
"""
14-
_this_file = __file__
15-
if (_this_file is None) or not os.path.isfile(_this_file):
16-
sys.exit("wheel_wrapper.py failed. Cannot determine __file__")
17-
18-
_tool_dir = os.path.dirname(_this_file)
19-
_root_dir = os.path.abspath(os.path.join(_tool_dir, ".."))
20-
21-
# This prepends the vendor directory to the sys.path so that we preference their use over any system modules.
22-
sys.path[0:0] = [
23-
os.path.join(_root_dir, "third_party/python"),
24-
os.path.join(_root_dir, "."),
25-
]
26-
27-
# Pip creates python subprocesses, so we need to add the new path to the PYTHONPATH
28-
os.environ["PYTHONPATH"] = os.pathsep.join(sys.path)
7+
import src.extract_wheels as whl
298

309

3110
def configure_reproducible_wheels():
@@ -53,15 +32,7 @@ def configure_reproducible_wheels():
5332
os.environ["PYTHONHASHSEED"] = "0"
5433

5534

56-
def main():
57-
configure_vendor()
35+
if __name__ == "__main__":
5836
configure_reproducible_wheels()
5937

60-
# This must be imported after vendoring has been configured. As it tries to resolve its own dependencies
61-
import src.extract_wheels as whl
62-
6338
whl.main()
64-
65-
66-
if __name__ == "__main__":
67-
main()

0 commit comments

Comments
 (0)