From 492b18d9cc38d4400b0ee2ed43bc4822674ded4b Mon Sep 17 00:00:00 2001 From: Jesse Kinkead Date: Thu, 5 Apr 2018 17:18:53 -0700 Subject: [PATCH 1/4] Add a pip3_import target for Python 3 compatibility. --- python/pip.bzl | 58 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/python/pip.bzl b/python/pip.bzl index 9d27794e4d..3883f96c7a 100644 --- a/python/pip.bzl +++ b/python/pip.bzl @@ -13,8 +13,8 @@ # limitations under the License. """Import pip requirements into Bazel.""" -def _pip_import_impl(repository_ctx): - """Core implementation of pip_import.""" +def _pip_import_impl(repository_ctx, binary_name): + """Core implementation of pip_import.""" # Add an empty top-level BUILD file. # This is because Bazel requires BUILD files along all paths accessed @@ -22,23 +22,26 @@ def _pip_import_impl(repository_ctx): # 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(""), - ]) + # To see the output, pass: quiet=False + result = repository_ctx.execute([ + binary_name, 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)) +def _pip2_import_impl(repository_ctx): + """System python implementation. Assumes this links to python 2.""" + _pip_import_impl(repository_ctx, "python") + +def _pip3_import_impl(repository_ctx): + """Python 3 implementation.""" + _pip_import_impl(repository_ctx, "python3") + pip_import = repository_rule( attrs = { "requirements": attr.label( @@ -51,15 +54,32 @@ pip_import = repository_rule( cfg = "host", ), }, - implementation = _pip_import_impl, + implementation = _pip2_import_impl, +) + +pip3_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 = _pip3_import_impl, ) -"""A rule for importing requirements.txt dependencies into Bazel. +"""Rules for importing requirements.txt dependencies into Bazel. -This rule imports a requirements.txt file and generates a new +These rules import a requirements.txt file and generate a new requirements.bzl file. This is used via the WORKSPACE pattern: -
pip_import(
+
# Use pip3_import to force Python 3.
+pip_import(
     name = "foo",
     requirements = ":requirements.txt",
 )

From 01a0ec5dd23831e413c7820729f450c971c441c8 Mon Sep 17 00:00:00 2001
From: Jesse Kinkead 
Date: Fri, 6 Apr 2018 14:23:03 -0700
Subject: [PATCH 2/4] Update docs for pip3_import.

---
 README.md      |  4 ++++
 python/pip.bzl | 61 +++++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 55 insertions(+), 10 deletions(-)

diff --git a/README.md b/README.md
index 9c44f40d95..f58213b0ee 100644
--- a/README.md
+++ b/README.md
@@ -7,6 +7,7 @@ Status: This is **ALPHA** software.
 ## Rules
 
 * [pip_import](docs/python/pip.md#pip_import)
+* [pip3_import](docs/python/pip.md#pip3_import)
 * [py_library](docs/python/python.md#py_library)
 * [py_binary](docs/python/python.md#py_binary)
 * [py_test](docs/python/python.md#py_test)
@@ -75,6 +76,9 @@ load("@my_deps//:requirements.bzl", "pip_install")
 pip_install()
 ```
 
+The `pip_import` rule uses the system `python` command, which is usually
+Python 2. `pip3_import` uses the system `python3` command.
+
 ## Consuming `pip` dependencies
 
 Once a set of dependencies has been imported via `pip_import` and `pip_install`
diff --git a/python/pip.bzl b/python/pip.bzl
index 3883f96c7a..c8221a8efa 100644
--- a/python/pip.bzl
+++ b/python/pip.bzl
@@ -29,13 +29,13 @@ def _pip_import_impl(repository_ctx, binary_name):
     "--input", repository_ctx.path(repository_ctx.attr.requirements),
     "--output", repository_ctx.path("requirements.bzl"),
     "--directory", repository_ctx.path(""),
-  ])
+  ], quiet=False)
 
     if result.return_code:
         fail("pip_import failed: %s (%s)" % (result.stdout, result.stderr))
 
-def _pip2_import_impl(repository_ctx):
-  """System python implementation. Assumes this links to python 2."""
+def _pip_system_import_impl(repository_ctx):
+  """System python implementation."""
   _pip_import_impl(repository_ctx, "python")
 
 def _pip3_import_impl(repository_ctx):
@@ -54,9 +54,51 @@ pip_import = repository_rule(
             cfg = "host",
         ),
     },
-    implementation = _pip2_import_impl,
+    implementation = _pip_system_import_impl,
 )
 
+"""A rule for importing requirements.txt dependencies into Bazel.
+
+This rule imports a requirements.txt file using the system
+python, 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. +""" + pip3_import = repository_rule( attrs = { "requirements": attr.label( @@ -73,13 +115,12 @@ pip3_import = repository_rule( implementation = _pip3_import_impl, ) -"""Rules for importing requirements.txt dependencies into Bazel. +"""A rule for importing requirements.txt dependencies into Bazel. -These rules import a requirements.txt file and generate a new -requirements.bzl file. This is used via the WORKSPACE -pattern: -
# Use pip3_import to force Python 3.
-pip_import(
+This rule imports a requirements.txt file using the system
+python3, and generates a new requirements.bzl file.
+This is used via the WORKSPACE pattern:
+
pip3_import(
     name = "foo",
     requirements = ":requirements.txt",
 )

From 117b368ff5f20acc0341d4e3e991cde696fcaf08 Mon Sep 17 00:00:00 2001
From: Jesse Kinkead 
Date: Fri, 6 Apr 2018 14:33:41 -0700
Subject: [PATCH 3/4] Add output of ./update_docs.sh.

---
 docs/index.html      | 11 ++++++
 docs/index.md        | 11 ++++++
 docs/python/pip.html | 70 +++++++++++++++++++++++++++++++++++++--
 docs/python/pip.md   | 79 ++++++++++++++++++++++++++++++++++++++++++--
 docs/python/whl.html | 10 +++++-
 docs/python/whl.md   | 10 +++++-
 6 files changed, 183 insertions(+), 8 deletions(-)

diff --git a/docs/index.html b/docs/index.html
index f3487d21bd..5a35c5508d 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -97,6 +97,17 @@ 

Repository Rules

+ + + + pip3_import + + + +

A rule for importing <code>requirements.txt</code> dependencies into Bazel.

+ + + diff --git a/docs/index.md b/docs/index.md index 79f931a44c..ac62a15413 100644 --- a/docs/index.md +++ b/docs/index.md @@ -40,6 +40,17 @@ + + + + pip3_import + + + +

A rule for importing <code>requirements.txt</code> dependencies into Bazel.

+ + + diff --git a/docs/python/pip.html b/docs/python/pip.html index 8045e3f551..3bc0ee1774 100644 --- a/docs/python/pip.html +++ b/docs/python/pip.html @@ -61,6 +61,7 @@

Import pip requirements into Bazel.