Skip to content

Commit 41777ee

Browse files
Merge pull request #12 from dillon-giacoppo/dillon/fetch-deps
2 parents 2b20e0e + ecefb3b commit 41777ee

File tree

515 files changed

+196
-152603
lines changed

Some content is hidden

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

515 files changed

+196
-152603
lines changed

.bazelignore

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

.gitattributes

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ boto3==1.9.253
1212

1313
In `WORKSPACE`
1414

15-
```
15+
```python
1616
rules_python_external_version = "{COMMIT_SHA}"
1717

1818
http_archive(
@@ -22,17 +22,20 @@ http_archive(
2222
url = "https://github.com/dillon-giacoppo/rules_python_external/archive/{version}.zip".format(version = rules_python_external_version),
2323
)
2424

25-
load("@rules_python_external//:defs.bzl", "pip_repository")
25+
# Install the rule dependencies
26+
load("@rules_python_external//:repositories.bzl", "rules_python_external_dependencies")
27+
rules_python_external_dependencies()
2628

27-
pip_repository(
29+
load("@rules_python_external//:defs.bzl", "pip_install")
30+
pip_install(
2831
name = "py_deps",
2932
requirements = "//:requirements.txt",
3033
)
3134
```
3235

3336
Example `BUILD` file.
3437

35-
```
38+
```python
3639
load("@py_deps//:requirements.bzl", "requirement")
3740

3841
py_binary(

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: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,39 @@
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")
49

510
rctx.file("BUILD", "")
611

12+
# Get the root directory of these rules
13+
rules_root = rctx.path(Label("//:BUILD")).dirname
14+
thirdparty_roots = [
15+
# Includes all the external dependencies from repositories.bzl
16+
rctx.path(Label("@" + repo + "//:BUILD.bazel")).dirname
17+
for repo in all_requirements
18+
]
19+
pypath = ":".join([str(p) for p in [rules_root] + thirdparty_roots])
20+
721
result = rctx.execute(
822
[
923
rctx.which(rctx.attr.python_interpreter),
10-
rctx.path(rctx.attr._script),
24+
rctx.path(rctx.attr._script).dirname,
1125
"--requirements",
1226
rctx.path(rctx.attr.requirements),
1327
"--repo",
1428
"@%s" % rctx.attr.name,
1529
],
16-
17-
environment = rctx.attr.wheel_env,
30+
environment={
31+
# Manually construct the PYTHONPATH since we cannot use the toolchain here
32+
"PYTHONPATH": pypath
33+
},
1834
)
1935
if result.return_code:
20-
fail("failed to create pip repository: %s (%s)" % (result.stdout, result.stderr))
36+
fail("rules_python_external failed: %s (%s)" % (result.stdout, result.stderr))
2137

2238
return
2339

@@ -28,10 +44,14 @@ pip_repository = repository_rule(
2844
"wheel_env": attr.string_dict(),
2945
"python_interpreter": attr.string(default="python3"),
3046
"_script": attr.label(
31-
executable=True,
32-
default=Label("//tools:wheel_wrapper.py"),
33-
cfg="host",
47+
executable=True, default=Label("//src:__main__.py"), cfg="host",
3448
),
3549
},
3650
implementation=_pip_repository_impl,
3751
)
52+
53+
54+
def pip_install(requirements, name=DEFAULT_REPOSITORY_NAME):
55+
pip_repository(
56+
name=name, requirements=requirements,
57+
)

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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
# Uses the default repository name "pip"
24+
requirements = "//:requirements.txt",
25+
)

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+
)

0 commit comments

Comments
 (0)