Skip to content

Commit 923c0c6

Browse files
committed
Download dependencies instead of embedding them
1 parent 115dcaf commit 923c0c6

File tree

24 files changed

+123
-3165
lines changed

24 files changed

+123
-3165
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,59 @@
11
# Bazel rules for creating Python virtual environments.
2+
23
See `example/` for an example.
34

45
## Installation
6+
57
Add the following to your `WORKSPACE`.
68

79
(Note: see the releases page for release-specific `WORKSPACE` config)
810

9-
```
11+
```starlark
1012
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
1113

1214
http_archive(
1315
name = "rules_pyvenv",
1416
strip_prefix = "rules_pyvenv-main",
1517
url = "https://github.com/cedarai/rules_pyvenv/archive/main.tar.gz",
1618
)
19+
20+
load("@rules_pyvenv//:repositories.bzl", "py_venv_repositories")
21+
22+
py_venv_repositories(
23+
name = "py_venv",
24+
)
25+
```
26+
27+
Add the following to your `BUILD`.
28+
29+
```starlark
30+
load("@py_venv//:venv.bzl", "py_venv")
31+
32+
py_venv(
33+
name = "venv",
34+
...
35+
)
36+
37+
```
38+
39+
Run the following command to create a venv in directory _env_.
40+
41+
```
42+
bazel run //:venv env
1743
```
1844

1945
These rules require a recent version of Python 3.6+ and `rules_python`.
2046
The environment is built using the `venv` library that ships with the Python standard library.
2147
If using the system-provided Python on Debian/Ubuntu, you may need to run
48+
2249
```
2350
apt install python3.8-venv
2451
```
2552

2653
On Windows, [you need to enable symlink support](https://bazel.build/configure/windows#symlink).
2754

2855
## Example
56+
2957
```
3058
$ cd example
3159
$ bazel run //:venv env

example/BUILD.bazel

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
package(default_visibility = ["//visibility:public"])
16-
17-
load("@rules_pyvenv//:venv.bzl", "py_venv")
15+
load("@py_venv//:venv.bzl", "py_venv")
1816
load("@example_deps//:requirements.bzl", "requirement")
1917
load("@rules_python//python/pip_install:requirements.bzl", "compile_pip_requirements")
2018

19+
package(default_visibility = ["//visibility:public"])
20+
2121
py_venv(
2222
name = "venv",
23+
extra_pip_commands = [
24+
# "install ipython jupyter-console",
25+
],
2326
deps = [
24-
"//libraries/liba",
27+
"//libraries/liba",
2528
requirement("black"),
2629
requirement("numpy"),
2730
],
28-
extra_pip_commands = [
29-
# "install ipython jupyter-console",
30-
]
3131
)
3232

3333
py_venv(
@@ -40,11 +40,11 @@ py_venv(
4040

4141
py_venv(
4242
name = "venv_only_local_always_link",
43+
always_link = True,
4344
deps = [
4445
"//libraries/liba",
4546
"//libraries/libb",
4647
],
47-
always_link = True,
4848
)
4949

5050
py_venv(

example/WORKSPACE

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,21 @@ python_register_toolchains(
3636
# For this example, we load rules_pyvenv locally. But you'd use http_archive.
3737
local_repository(
3838
name = "rules_pyvenv",
39-
path = "../"
39+
path = "../",
40+
)
41+
42+
load("@rules_pyvenv//:repositories.bzl", "py_venv_repositories")
43+
44+
py_venv_repositories(
45+
name = "py_venv",
4046
)
4147

4248
# Fetch some python packages to install in the venv,
4349
load("@rules_python//python:pip.bzl", "pip_parse")
50+
4451
pip_parse(
45-
name = "example_deps",
46-
requirements_lock = "//:requirements.txt",
52+
name = "example_deps",
53+
requirements_lock = "//:requirements.txt",
4754
)
4855

4956
load("@example_deps//:requirements.bzl", "install_deps")

repositories.bzl

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright 2023 cedar.ai. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
def _impl(rctx):
16+
rctx.file("BUILD.bazel", """
17+
package(default_visibility = ["//visibility:public"])
18+
19+
exports_files(["venv.bzl"])
20+
""")
21+
22+
rctx.download_and_extract(
23+
output = "importlib_metadata",
24+
sha256 = "9e6fafdbf0601a5825c53a842f7bb6928c8e8eb6c46a228199ca02396cf6007a",
25+
stripPrefix = "importlib_metadata-6.0.0",
26+
url = "https://github.com/python/importlib_metadata/archive/refs/tags/v6.0.0.tar.gz",
27+
)
28+
29+
rctx.file("importlib_metadata/BUILD.bazel", """
30+
package(default_visibility = ["//visibility:public"])
31+
32+
py_library(
33+
name = "importlib_metadata",
34+
srcs = glob(["importlib_metadata/*.py"]),
35+
imports = ["."],
36+
deps = [
37+
"@{name}//zipp",
38+
],
39+
)
40+
""".format(
41+
name = rctx.name,
42+
))
43+
44+
rctx.download_and_extract(
45+
output = "zipp",
46+
sha256 = "a2e6a09c22d6d36221e2d37f08d9566554f4de9e6e3ed8eb9ddcc0b0440162c0",
47+
stripPrefix = "zipp-3.15.0",
48+
url = "https://github.com/jaraco/zipp/archive/refs/tags/v3.15.0.tar.gz",
49+
)
50+
51+
rctx.file("zipp/BUILD.bazel", """
52+
package(default_visibility = ["//visibility:public"])
53+
54+
py_library(
55+
name = "zipp",
56+
srcs = glob(["zipp/*.py"]),
57+
imports = ["."],
58+
)
59+
""")
60+
61+
rctx.template("venv.bzl", rctx.attr._template, {
62+
"%%NAME%%": rctx.name,
63+
})
64+
65+
py_venv_repositories = repository_rule(
66+
implementation = _impl,
67+
local = True,
68+
attrs = {
69+
"_template": attr.label(
70+
default = ":venv.bzl.tmpl",
71+
),
72+
},
73+
)

vendor/importlib_metadata/BUILD.bazel

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)