Evaluate PEP 508 environment markers for package dependencies (#50)

* Evaluate PEP 508 environment markers for package dependencies

Previously any wheel dependencies that had an environment marker
(such as 'python_version>3.3') were simply ignored, leading to
missing packages in the Python environment constructed by bazel.

Fixes https://github.com/bazelbuild/rules_python/issues/49

* Regenerate the piptool.par

Required after making changes to whl.py

* Pin the version of setuptools in piptool & extract whltool

Some common operators in version markers (e.g., <=) are only supported
in setuptools>=17.1. Rather than risk failing because the environment
has an old setuptools version it's better to include it. Pinning to
an exact version (currently the latest) to make things as predictable
as possible.

In addition, whl.py used during workspace setup also now depends on
setuptools. We package this in a separate whltool.par to make this
predictable as well.
9 files changed
tree: db847fa0d3a263a4bcba67d922c3bfbf14a4dbda
  1. .ci/
  2. docs/
  3. examples/
  4. python/
  5. rules_python/
  6. tools/
  7. .gitignore
  8. .travis.yml
  9. AUTHORS
  10. BUILD
  11. CONTRIBUTING.md
  12. CONTRIBUTORS
  13. LICENSE
  14. README.md
  15. update_docs.sh
  16. update_tools.sh
  17. WORKSPACE
README.md

Bazel Python Rules

Build Status

Rules

Overview

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.

Setup

Add the following to your WORKSPACE file to add the external repositories:

git_repository(
    name = "io_bazel_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("@io_bazel_rules_python//python:pip.bzl", "pip_repositories")

pip_repositories()

Then in your BUILD files load the python rules with:

load(
  "@io_bazel_rules_python//python:python.bzl",
  "py_binary", "py_library", "py_test",
)

py_binary(
  name = "main",
  ...
)

Importing pip dependencies

These 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("@io_bazel_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()

Consuming pip dependencies

Once 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"),
    ]
)

Canonical whl_library naming

It 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).

Updating 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

Updating 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