Skip to content

Commit 61bb6a0

Browse files
MousiusChris Sidebottom
authored andcommitted
Initial Commit
0 parents  commit 61bb6a0

19 files changed

+490
-0
lines changed

.gitignore

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

BUILD

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
load("@rules_pkg//:pkg.bzl", "pkg_tar")
2+
version = "0.0.1"
3+
4+
exports_files([
5+
"LICENSE"
6+
])
7+
8+
filegroup(
9+
name = "distribution",
10+
srcs = [
11+
"LICENSE",
12+
"//rules_python_poetry:distribution"
13+
]
14+
)
15+
16+
pkg_tar(
17+
name = "rules_python_poetry-%s" % version,
18+
srcs = [
19+
":distribution",
20+
],
21+
extension = "tar.gz",
22+
# It is all source code, so make it read-only.
23+
mode = "0444",
24+
# Make it owned by root so it does not have the uid of the CI robot.
25+
owner = "0.0",
26+
package_dir = ".",
27+
strip_prefix = "./rules_python_poetry",
28+
)

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Chris Sidebottom
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Python Poetry Rules for Bazel
2+
These rules are designed to allow you to easily use the [Poetry Package Manager](https://python-poetry.org/) with [Bazel](https://bazel.build/). It does this whilst still allowing you to use Poetry as usual with `poetry add` and `poetry run`.
3+
4+
## Getting started
5+
To illustrate how to use this package, there's an [example project](./example) included which shows all the relevant wiring.
6+
7+
## Poetry Setup
8+
In order to smoothen out the interactions between Bazel and Poetry, we use the common Python location of `.venv` for the Virtual Environment. This makes it easier for both tools to find it, this is configured using [the `virtualenvs.in-project` configuration with Poetry](https://python-poetry.org/docs/configuration/#virtualenvsin-project-boolean):
9+
10+
```
11+
poetry config virtualenvs.in-project true --local
12+
```
13+
14+
Which results in the [`poetry.toml` file found in our example project](./examples/poetry.toml). You can then use the normal Poetry commands.
15+
16+
## Bazel Setup
17+
To enable Bazel to manage the Poetry Virtual Environment, we use the [`managed_directories` property in our example WORKSPACE](./examples/WORKSPACE); this lets Bazel recreate the environment within our workspace, and symlinks it into the Bazel environment:
18+
19+
```py
20+
workspace(
21+
name = "basic_project",
22+
managed_directories = {
23+
"@poetry_environment": [".venv"]
24+
}
25+
)
26+
```
27+
28+
Afterwards, we use the `http_archive` package to download the rules:
29+
30+
```py
31+
http_archive(
32+
name = "rules_python_poetry",
33+
url = "https://github.com/DaMouse404/rules_python_poetry/releases/download/0.0.1/rules_python_poetry-0.0.1.tar.gz",
34+
sha256 = "3ac54f1e9b070d2ed727c58f30798f4cea1123242279e7d6e1a48e1f06ca16d6",
35+
)
36+
```
37+
38+
Then we run the `poetry_enviromment` rule from `environment.bzl`, which generates the Virtual Environment if necessary, alongside symlinking the various Poetry configuration files into the Bazel environment:
39+
40+
```py
41+
load("@rules_python_poetry//:environment.bzl", "poetry_environment")
42+
poetry_environment(
43+
name="poetry_environment",
44+
project="//:pyproject.toml",
45+
lock="//:poetry.lock",
46+
config="//:poetry.toml"
47+
)
48+
```
49+
50+
Then we can register the Poetry Python intrepreter as a toolchain for `PY3` in Bazel:
51+
52+
```py
53+
register_toolchains("@poetry_environment//:poetry_toolchain")
54+
```
55+
56+
And finally we use `poetry export` to export requirements to `requirements.txt` format:
57+
58+
```py
59+
load("@poetry_environment//:runtime.bzl", "interpreter_path")
60+
load("@poetry_environment//:export.bzl", "poetry_export")
61+
poetry_export(
62+
name="poetry_requirements"
63+
)
64+
```
65+
66+
Which can then be used to interact with [the standard `rules_python`](https://github.com/bazelbuild/rules_python) (take note of the `python_interpreter` passed to `pip_import` here to use the virtual env one):
67+
68+
```py
69+
http_archive(
70+
name = "rules_python",
71+
url = "https://github.com/bazelbuild/rules_python/releases/download/0.0.2/rules_python-0.0.2.tar.gz",
72+
strip_prefix = "rules_python-0.0.2",
73+
sha256 = "b5668cde8bb6e3515057ef465a35ad712214962f0b3a314e551204266c7be90c",
74+
)
75+
load("@rules_python//python:repositories.bzl", "py_repositories")
76+
py_repositories()
77+
load("@rules_python//python:pip.bzl", "pip_repositories", "pip_import")
78+
pip_repositories()
79+
80+
pip_import(
81+
name = "basic_project_pip",
82+
requirements = "@poetry_requirements//:requirements.txt",
83+
python_interpreter = interpreter_path
84+
)
85+
load("@basic_project_pip//:requirements.bzl", "pip_install")
86+
pip_install()
87+
```

WORKSPACE

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
http_archive(
3+
name = "rules_pkg",
4+
urls = [
5+
"https://github.com/bazelbuild/rules_pkg/releases/download/0.2.5/rules_pkg-0.2.5.tar.gz",
6+
"https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.2.5/rules_pkg-0.2.5.tar.gz",
7+
],
8+
sha256 = "352c090cc3d3f9a6b4e676cf42a6047c16824959b438895a76c2989c6d7c246a",
9+
)
10+
load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
11+
rules_pkg_dependencies()

example/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bazel-*
2+
.venv

example/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
load("@rules_python//python:defs.bzl", "py_binary")
2+
load("@basic_project_pip//:requirements.bzl", "requirement")
3+
4+
py_binary(
5+
name = 'main',
6+
main = 'main.py',
7+
srcs = ['main.py'],
8+
deps = [
9+
requirement('six')
10+
],
11+
python_version = 'PY3'
12+
)

example/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Python Poetry Hello World
2+
Run the following for one of the most elaborately setup `hello world`s:
3+
4+
```
5+
bazel run //:main
6+
```

example/WORKSPACE

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
workspace(
2+
name = "basic_project",
3+
managed_directories = {
4+
"@poetry_environment": [".venv"]
5+
}
6+
)
7+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
8+
9+
http_archive(
10+
name = "rules_python_poetry",
11+
url = "https://github.com/DaMouse404/rules_python_poetry/releases/download/0.0.1/rules_python_poetry-0.0.1.tar.gz",
12+
sha256 = "3ac54f1e9b070d2ed727c58f30798f4cea1123242279e7d6e1a48e1f06ca16d6",
13+
)
14+
load("@rules_python_poetry//:environment.bzl", "poetry_environment")
15+
poetry_environment(
16+
name="poetry_environment",
17+
project="//:pyproject.toml",
18+
lock="//:poetry.lock",
19+
config="//:poetry.toml"
20+
)
21+
register_toolchains("@poetry_environment//:poetry_toolchain")
22+
23+
load("@poetry_environment//:runtime.bzl", "interpreter_path")
24+
load("@poetry_environment//:export.bzl", "poetry_export")
25+
poetry_export(
26+
name="poetry_requirements"
27+
)
28+
29+
http_archive(
30+
name = "rules_python",
31+
url = "https://github.com/bazelbuild/rules_python/releases/download/0.0.2/rules_python-0.0.2.tar.gz",
32+
strip_prefix = "rules_python-0.0.2",
33+
sha256 = "b5668cde8bb6e3515057ef465a35ad712214962f0b3a314e551204266c7be90c",
34+
)
35+
load("@rules_python//python:repositories.bzl", "py_repositories")
36+
py_repositories()
37+
load("@rules_python//python:pip.bzl", "pip_repositories", "pip_import")
38+
pip_repositories()
39+
40+
pip_import(
41+
name = "basic_project_pip",
42+
requirements = "@poetry_requirements//:requirements.txt",
43+
python_interpreter = interpreter_path
44+
)
45+
load("@basic_project_pip//:requirements.bzl", "pip_install")
46+
pip_install()

example/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import pandas
2+
print("hello world")

0 commit comments

Comments
 (0)