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

Embed typing_extensions package to support Python < 3.8 #10

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example/
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
# Bazel rules for creating Python virtual environments.

See `example/` for an example.

## Installation

Add the following to your `WORKSPACE`.

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

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

http_archive(
name = "rules_pyvenv",
strip_prefix = "rules_pyvenv-main",
url = "https://github.com/cedarai/rules_pyvenv/archive/main.tar.gz",
)

load("@rules_pyvenv//:repositories.bzl", "py_venv_repositories")

py_venv_repositories(
name = "py_venv",
)
```

Add the following to your `BUILD`.

```starlark
load("@py_venv//:venv.bzl", "py_venv")

py_venv(
name = "venv",
...
)

```

Run the following command to create a venv in directory _env_.

```
bazel run //:venv env
```

These rules require a recent version of Python 3.6+ and `rules_python`.
The environment is built using the `venv` library that ships with the Python standard library.
If using the system-provided Python on Debian/Ubuntu, you may need to run

```
apt install python3.8-venv
```

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

## Example

```
$ cd example
$ bazel run //:venv env
Expand Down
2 changes: 1 addition & 1 deletion build_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def install_data_file(env_path: pathlib.Path, file: EnvFile) -> None:

def install_site_file(site_packages_path: pathlib.Path, file: EnvFile) -> None:
site_path = site_packages_path / file.site_packages_path
if not site_path.exists():
if not site_path.exists() and file.path.exists():
site_path.parent.mkdir(parents=True, exist_ok=True)
site_path.symlink_to(file.path.resolve())

Expand Down
16 changes: 8 additions & 8 deletions example/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.

package(default_visibility = ["//visibility:public"])

load("@rules_pyvenv//:venv.bzl", "py_venv")
load("@py_venv//:venv.bzl", "py_venv")
load("@example_deps//:requirements.bzl", "requirement")
load("@rules_python//python/pip_install:requirements.bzl", "compile_pip_requirements")

package(default_visibility = ["//visibility:public"])

py_venv(
name = "venv",
extra_pip_commands = [
# "install ipython jupyter-console",
],
deps = [
"//libraries/liba",
"//libraries/liba",
requirement("black"),
requirement("numpy"),
],
extra_pip_commands = [
# "install ipython jupyter-console",
]
)

py_venv(
Expand All @@ -40,11 +40,11 @@ py_venv(

py_venv(
name = "venv_only_local_always_link",
always_link = True,
deps = [
"//libraries/liba",
"//libraries/libb",
],
always_link = True,
)

py_venv(
Expand Down
13 changes: 10 additions & 3 deletions example/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,21 @@ python_register_toolchains(
# For this example, we load rules_pyvenv locally. But you'd use http_archive.
local_repository(
name = "rules_pyvenv",
path = "../"
path = "../",
)

load("@rules_pyvenv//:repositories.bzl", "py_venv_repositories")

py_venv_repositories(
name = "py_venv",
)

# Fetch some python packages to install in the venv,
load("@rules_python//python:pip.bzl", "pip_parse")

pip_parse(
name = "example_deps",
requirements_lock = "//:requirements.txt",
name = "example_deps",
requirements_lock = "//:requirements.txt",
)

load("@example_deps//:requirements.bzl", "install_deps")
Expand Down
92 changes: 92 additions & 0 deletions repositories.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Copyright 2023 cedar.ai. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def _impl(rctx):
rctx.file("BUILD.bazel", """
package(default_visibility = ["//visibility:public"])

exports_files(["venv.bzl"])
""")

rctx.download_and_extract(
output = "importlib_metadata",
sha256 = "9e6fafdbf0601a5825c53a842f7bb6928c8e8eb6c46a228199ca02396cf6007a",
stripPrefix = "importlib_metadata-6.0.0",
url = "https://github.com/python/importlib_metadata/archive/refs/tags/v6.0.0.tar.gz",
)

rctx.file("importlib_metadata/BUILD.bazel", """
package(default_visibility = ["//visibility:public"])

py_library(
name = "importlib_metadata",
srcs = glob(["importlib_metadata/*.py"]),
imports = ["."],
deps = [
"@{name}//typing_extensions",
"@{name}//zipp",
],
)
""".format(
name = rctx.name,
))

rctx.download_and_extract(
output = "typing_extensions",
sha256 = "c8fd5561e1bd88b743ef2ee065a5e661b2fd7b56e9cbe9ae2aeb928f41438819",
stripPrefix = "typing_extensions-4.5.0/src",
url = "https://github.com/python/typing_extensions/archive/refs/tags/4.5.0.tar.gz",
)

rctx.file("typing_extensions/BUILD.bazel", """
package(default_visibility = ["//visibility:public"])

py_library(
name = "typing_extensions",
srcs = [
"typing_extensions.py",
],
imports = ["."],
)
""")

rctx.download_and_extract(
output = "zipp",
sha256 = "a2e6a09c22d6d36221e2d37f08d9566554f4de9e6e3ed8eb9ddcc0b0440162c0",
stripPrefix = "zipp-3.15.0",
url = "https://github.com/jaraco/zipp/archive/refs/tags/v3.15.0.tar.gz",
)

rctx.file("zipp/BUILD.bazel", """
package(default_visibility = ["//visibility:public"])

py_library(
name = "zipp",
srcs = glob(["zipp/*.py"]),
imports = ["."],
)
""")

rctx.template("venv.bzl", rctx.attr._template, {
"%%NAME%%": rctx.name,
})

py_venv_repositories = repository_rule(
implementation = _impl,
attrs = {
"_template": attr.label(
default = ":venv.bzl.tmpl",
),
},
)
8 changes: 0 additions & 8 deletions vendor/importlib_metadata/BUILD.bazel

This file was deleted.

Loading