-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 61bb6a0
Showing
19 changed files
with
490 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bazel-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
load("@rules_pkg//:pkg.bzl", "pkg_tar") | ||
version = "0.0.1" | ||
|
||
exports_files([ | ||
"LICENSE" | ||
]) | ||
|
||
filegroup( | ||
name = "distribution", | ||
srcs = [ | ||
"LICENSE", | ||
"//rules_python_poetry:distribution" | ||
] | ||
) | ||
|
||
pkg_tar( | ||
name = "rules_python_poetry-%s" % version, | ||
srcs = [ | ||
":distribution", | ||
], | ||
extension = "tar.gz", | ||
# It is all source code, so make it read-only. | ||
mode = "0444", | ||
# Make it owned by root so it does not have the uid of the CI robot. | ||
owner = "0.0", | ||
package_dir = ".", | ||
strip_prefix = "./rules_python_poetry", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Chris Sidebottom | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# Python Poetry Rules for Bazel | ||
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`. | ||
|
||
## Getting started | ||
To illustrate how to use this package, there's an [example project](./example) included which shows all the relevant wiring. | ||
|
||
## Poetry Setup | ||
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): | ||
|
||
``` | ||
poetry config virtualenvs.in-project true --local | ||
``` | ||
|
||
Which results in the [`poetry.toml` file found in our example project](./examples/poetry.toml). You can then use the normal Poetry commands. | ||
|
||
## Bazel Setup | ||
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: | ||
|
||
```py | ||
workspace( | ||
name = "basic_project", | ||
managed_directories = { | ||
"@poetry_environment": [".venv"] | ||
} | ||
) | ||
``` | ||
|
||
Afterwards, we use the `http_archive` package to download the rules: | ||
|
||
```py | ||
http_archive( | ||
name = "rules_python_poetry", | ||
url = "https://github.com/DaMouse404/rules_python_poetry/releases/download/0.0.1/rules_python_poetry-0.0.1.tar.gz", | ||
sha256 = "3ac54f1e9b070d2ed727c58f30798f4cea1123242279e7d6e1a48e1f06ca16d6", | ||
) | ||
``` | ||
|
||
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: | ||
|
||
```py | ||
load("@rules_python_poetry//:environment.bzl", "poetry_environment") | ||
poetry_environment( | ||
name="poetry_environment", | ||
project="//:pyproject.toml", | ||
lock="//:poetry.lock", | ||
config="//:poetry.toml" | ||
) | ||
``` | ||
|
||
Then we can register the Poetry Python intrepreter as a toolchain for `PY3` in Bazel: | ||
|
||
```py | ||
register_toolchains("@poetry_environment//:poetry_toolchain") | ||
``` | ||
|
||
And finally we use `poetry export` to export requirements to `requirements.txt` format: | ||
|
||
```py | ||
load("@poetry_environment//:runtime.bzl", "interpreter_path") | ||
load("@poetry_environment//:export.bzl", "poetry_export") | ||
poetry_export( | ||
name="poetry_requirements" | ||
) | ||
``` | ||
|
||
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): | ||
|
||
```py | ||
http_archive( | ||
name = "rules_python", | ||
url = "https://github.com/bazelbuild/rules_python/releases/download/0.0.2/rules_python-0.0.2.tar.gz", | ||
strip_prefix = "rules_python-0.0.2", | ||
sha256 = "b5668cde8bb6e3515057ef465a35ad712214962f0b3a314e551204266c7be90c", | ||
) | ||
load("@rules_python//python:repositories.bzl", "py_repositories") | ||
py_repositories() | ||
load("@rules_python//python:pip.bzl", "pip_repositories", "pip_import") | ||
pip_repositories() | ||
|
||
pip_import( | ||
name = "basic_project_pip", | ||
requirements = "@poetry_requirements//:requirements.txt", | ||
python_interpreter = interpreter_path | ||
) | ||
load("@basic_project_pip//:requirements.bzl", "pip_install") | ||
pip_install() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
http_archive( | ||
name = "rules_pkg", | ||
urls = [ | ||
"https://github.com/bazelbuild/rules_pkg/releases/download/0.2.5/rules_pkg-0.2.5.tar.gz", | ||
"https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.2.5/rules_pkg-0.2.5.tar.gz", | ||
], | ||
sha256 = "352c090cc3d3f9a6b4e676cf42a6047c16824959b438895a76c2989c6d7c246a", | ||
) | ||
load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies") | ||
rules_pkg_dependencies() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
bazel-* | ||
.venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
load("@rules_python//python:defs.bzl", "py_binary") | ||
load("@basic_project_pip//:requirements.bzl", "requirement") | ||
|
||
py_binary( | ||
name = 'main', | ||
main = 'main.py', | ||
srcs = ['main.py'], | ||
deps = [ | ||
requirement('six') | ||
], | ||
python_version = 'PY3' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Python Poetry Hello World | ||
Run the following for one of the most elaborately setup `hello world`s: | ||
|
||
``` | ||
bazel run //:main | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
workspace( | ||
name = "basic_project", | ||
managed_directories = { | ||
"@poetry_environment": [".venv"] | ||
} | ||
) | ||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
http_archive( | ||
name = "rules_python_poetry", | ||
url = "https://github.com/DaMouse404/rules_python_poetry/releases/download/0.0.1/rules_python_poetry-0.0.1.tar.gz", | ||
sha256 = "3ac54f1e9b070d2ed727c58f30798f4cea1123242279e7d6e1a48e1f06ca16d6", | ||
) | ||
load("@rules_python_poetry//:environment.bzl", "poetry_environment") | ||
poetry_environment( | ||
name="poetry_environment", | ||
project="//:pyproject.toml", | ||
lock="//:poetry.lock", | ||
config="//:poetry.toml" | ||
) | ||
register_toolchains("@poetry_environment//:poetry_toolchain") | ||
|
||
load("@poetry_environment//:runtime.bzl", "interpreter_path") | ||
load("@poetry_environment//:export.bzl", "poetry_export") | ||
poetry_export( | ||
name="poetry_requirements" | ||
) | ||
|
||
http_archive( | ||
name = "rules_python", | ||
url = "https://github.com/bazelbuild/rules_python/releases/download/0.0.2/rules_python-0.0.2.tar.gz", | ||
strip_prefix = "rules_python-0.0.2", | ||
sha256 = "b5668cde8bb6e3515057ef465a35ad712214962f0b3a314e551204266c7be90c", | ||
) | ||
load("@rules_python//python:repositories.bzl", "py_repositories") | ||
py_repositories() | ||
load("@rules_python//python:pip.bzl", "pip_repositories", "pip_import") | ||
pip_repositories() | ||
|
||
pip_import( | ||
name = "basic_project_pip", | ||
requirements = "@poetry_requirements//:requirements.txt", | ||
python_interpreter = interpreter_path | ||
) | ||
load("@basic_project_pip//:requirements.bzl", "pip_install") | ||
pip_install() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import pandas | ||
print("hello world") |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[virtualenvs] | ||
in-project = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[tool.poetry] | ||
name = "basic_project" | ||
version = "0.1.0" | ||
description = "" | ||
authors = ["Chris Sidebottom <[email protected]>"] | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.7" | ||
pandas = "^1.0.5" | ||
|
||
[tool.poetry.dev-dependencies] | ||
|
||
[build-system] | ||
requires = ["poetry>=0.12"] | ||
build-backend = "poetry.masonry.api" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
exports_files([ | ||
"environment.bzl" | ||
]) | ||
|
||
|
||
filegroup( | ||
name = "distribution", | ||
srcs = [ | ||
"WORKSPACE", | ||
"BUILD", | ||
"environment.bzl", | ||
"BUILD.template", | ||
"export.bzl.template", | ||
"runtime.bzl.template", | ||
] | ||
) |
Oops, something went wrong.