# Copyright 2017 The Bazel Authors. 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. """Import pip requirements into Bazel.""" def _pip_import_impl(repository_ctx): """Core implementation of pip_import.""" # Add an empty top-level BUILD file. # This is because Bazel requires BUILD files along all paths accessed # via //this/sort/of:path and we wouldn't be able to load our generated # requirements.bzl without it. repository_ctx.file("BUILD", "") # To see the output, pass: quiet=False result = repository_ctx.execute([ "python", repository_ctx.path(repository_ctx.attr._script), "--name", repository_ctx.attr.name, "--input", repository_ctx.path(repository_ctx.attr.requirements), "--output", repository_ctx.path("requirements.bzl"), "--directory", repository_ctx.path(""), ]) if result.return_code: fail("pip_import failed: %s (%s)" % (result.stdout, result.stderr)) pip_import = repository_rule( attrs = { "requirements": attr.label( allow_files = True, mandatory = True, single_file = True, ), "_script": attr.label( executable = True, default = Label("//tools:piptool.par"), cfg = "host", ), }, implementation = _pip_import_impl, ) """A rule for importing requirements.txt dependencies into Bazel. This rule imports a requirements.txt file and generates a new requirements.bzl file. This is used via the WORKSPACE pattern:
pip_import(
    name = "foo",
    requirements = ":requirements.txt",
)
load("@foo//:requirements.bzl", "pip_install")
pip_install()
You can then reference imported dependencies from your BUILD file with:
load("@foo//:requirements.bzl", "requirement")
py_library(
    name = "bar",
    ...
    deps = [
       "//my/other:dep",
       requirement("futures"),
       requirement("mock"),
    ],
)
Or alternatively:
load("@foo//:requirements.bzl", "all_requirements")
py_binary(
    name = "baz",
    ...
    deps = [
       ":foo",
    ] + all_requirements,
)
Args: requirements: The label of a requirements.txt file. """ def pip_repositories(): """Pull in dependencies needed for pulling in pip dependencies. A placeholder method that will eventually pull in any dependencies needed to install pip dependencies. """ pass