commit | 522222ae63f30a9ecb0c6a0ed0a628b4eaca4f84 | [log] [tgz] |
---|---|---|
author | Jon Brandvein <brandjon@google.com> | Fri Jul 26 23:35:39 2019 -0400 |
committer | GitHub <noreply@github.com> | Fri Jul 26 23:35:39 2019 -0400 |
tree | 7c73513487643102bad76a50851ac75b7a07f22c | |
parent | d1596a309ac9216b1a17fc984eb1376e64aa5fcf [diff] |
Add exports for core Python logic that's bundled with Bazel (#202) * Introduce defs.bzl as the official home of the core Python rules The "core" Python rules are the rules that traditionally have been bundled with Bazel. This includes native rules like `py_binary`, and Starlark-defined rules under `@bazel_tools` like `py_runtime_pair`. These should all live in or around `@rules_python//python:defs.bzl`. Currently we re-export the native rules here, with a magic tag to allow them to survive the flag flip for `--incompatible_load_python_rules_from_bzl`. When native rules are ported to Starlark their definitions will live here. * Add re-exports for Starlark-defined symbols This adds export definitions for built-in symbols like `PyInfo` and `@bazel_tools`-defined symbols like py_runtime_pair. * Vendor in runfiles library This vendors in the @bazel_tools//tools/python/runfiles target as //python/runfiles. See comment in the BUILD file for why we couldn't re-export the bundled implementation. * Fix README to prefer defs.bzl over python.bzl
Status: This is ALPHA software.
@io_bazel_rules_python
to just @rules_python
, in accordance with convention. Please update your WORKSPACE file and labels that reference this repo accordingly.This repository provides Python rules for Bazel. Currently, support for rules that are available from Bazel core are simple aliases to that bundled functionality. On top of that, this repository provides support for installing dependencies typically managed via pip
.
Add the following to your WORKSPACE
file to add the external repositories:
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") git_repository( name = "rules_python", remote = "https://github.com/bazelbuild/rules_python.git", # NOT VALID! Replace this with a Git commit SHA. commit = "{HEAD}", ) # Only needed for PIP support: load("@rules_python//python:pip.bzl", "pip_repositories") pip_repositories()
Then in your BUILD
files load the python rules with:
load( "@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test", ) py_binary( name = "main", ... )
pip
dependenciesThese rules are designed to have developers continue using requirements.txt
to express their dependencies in a Python idiomatic manner. These dependencies are imported into the Bazel dependency graph via a two-phased process in WORKSPACE
:
load("@rules_python//python:pip.bzl", "pip_import") # This rule translates the specified requirements.txt into # @my_deps//:requirements.bzl, which itself exposes a pip_install method. pip_import( name = "my_deps", requirements = "//path/to:requirements.txt", ) # Load the pip_install symbol for my_deps, and create the dependencies' # repositories. load("@my_deps//:requirements.bzl", "pip_install") pip_install()
pip
dependenciesOnce a set of dependencies has been imported via pip_import
and pip_install
we can start consuming them in our py_{binary,library,test}
rules. In support of this, the generated requirements.bzl
also contains a requirement
method, which can be used directly in deps=[]
to reference an imported py_library
.
load("@my_deps//:requirements.bzl", "requirement") py_library( name = "mylib", srcs = ["mylib.py"], deps = [ ":myotherlib", # This takes the name as specified in requirements.txt requirement("importeddep"), ] )
whl_library
namingIt is notable that whl_library
rules imported via pip_import
are canonically named, following the pattern: pypi__{distribution}_{version}
. Characters in these components that are illegal in Bazel label names (e.g. -
, .
) are replaced with _
.
This canonical naming helps avoid redundant work to import the same library multiple times. It is expected that this naming will remain stable, so folks should be able to reliably depend directly on e.g. @pypi__futures_3_1_1//:pkg
for dependencies, however, it is recommended that folks stick with the requirement
pattern in case the need arises for us to make changes to this format in the future.
“Extras” will have a target of the extra name (in place of pkg
above).
docs/
All of the content (except BUILD
) under docs/
is generated. To update the documentation simply run this in the root of the repository:
./update_docs.sh
tools/
All of the content (except BUILD
) under tools/
is generated. To update the documentation simply run this in the root of the repository:
./update_tools.sh