diff --git a/python/pip.bzl b/python/pip.bzl index 237b7d2baa..302ba2c951 100644 --- a/python/pip.bzl +++ b/python/pip.bzl @@ -13,7 +13,7 @@ # limitations under the License. """Import pip requirements into Bazel.""" -def _pip_import_impl(repository_ctx): +def _shared_pip_import_impl(python_interpreter, repository_ctx): """Core implementation of pip_import.""" # Add an empty top-level BUILD file. @@ -24,7 +24,8 @@ def _pip_import_impl(repository_ctx): # To see the output, pass: quiet=False result = repository_ctx.execute([ - "python", repository_ctx.path(repository_ctx.attr._script), + python_interpreter, repository_ctx.path(repository_ctx.attr._script), + "--python_interpreter", python_interpreter, "--name", repository_ctx.attr.name, "--input", repository_ctx.path(repository_ctx.attr.requirements), "--output", repository_ctx.path("requirements.bzl"), @@ -34,22 +35,43 @@ def _pip_import_impl(repository_ctx): if result.return_code: fail("pip_import failed: %s (%s)" % (result.stdout, result.stderr)) +_shared_attrs = { + "requirements": attr.label( + allow_files = True, + mandatory = True, + single_file = True, + ), + "_script": attr.label( + executable = True, + default = Label("//tools:piptool.par"), + cfg = "host", + ), +} + +def _pip_import_impl(repository_ctx): + _shared_pip_import_impl("python", repository_ctx) + 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", - ), - }, + attrs = _shared_attrs, implementation = _pip_import_impl, ) +def _pip2_import_impl(repository_ctx): + _shared_pip_import_impl("python2", repository_ctx) + +pip2_import = repository_rule( + attrs = _shared_attrs, + implementation = _pip2_import_impl, +) + +def _pip3_import_impl(repository_ctx): + _shared_pip_import_impl("python3", repository_ctx) + +pip3_import = repository_rule( + attrs = _shared_attrs, + implementation = _pip3_import_impl, +) + """A rule for importing requirements.txt dependencies into Bazel. This rule imports a requirements.txt file and generates a new diff --git a/python/whl.bzl b/python/whl.bzl index 496755671f..f9e6f654a3 100644 --- a/python/whl.bzl +++ b/python/whl.bzl @@ -17,7 +17,7 @@ def _whl_impl(repository_ctx): """Core implementation of whl_library.""" args = [ - "python", + repository_ctx.attr.python_interpreter, repository_ctx.path(repository_ctx.attr._script), "--whl", repository_ctx.path(repository_ctx.attr.whl), "--requirements", repository_ctx.attr.requirements, @@ -35,6 +35,7 @@ def _whl_impl(repository_ctx): whl_library = repository_rule( attrs = { + "python_interpreter": attr.string(), "whl": attr.label( allow_files = True, mandatory = True, diff --git a/rules_python/piptool.py b/rules_python/piptool.py index f5d504aa87..47fc3cdcf7 100644 --- a/rules_python/piptool.py +++ b/rules_python/piptool.py @@ -84,6 +84,9 @@ def pip_main(argv): parser = argparse.ArgumentParser( description='Import Python dependencies into Bazel.') +parser.add_argument('--python_interpreter', action='store', + help=('The python python interpreter to use ')) + parser.add_argument('--name', action='store', help=('The namespace of the import.')) @@ -175,10 +178,12 @@ def whl_library(wheel): if "{repo_name}" not in native.existing_rules(): whl_library( name = "{repo_name}", + python_interpreter = "{python_interpreter}", whl = "@{name}//:{path}", requirements = "@{name}//:requirements.bzl", extras = [{extras}] )""".format(name=args.name, repo_name=wheel.repository_name(), + python_interpreter=args.python_interpreter, path=wheel.basename(), extras=','.join([ '"%s"' % extra diff --git a/tools/piptool.par b/tools/piptool.par index 11ec453cd7..85794aa05d 100755 Binary files a/tools/piptool.par and b/tools/piptool.par differ diff --git a/tools/whltool.par b/tools/whltool.par index 7cb59c0fbe..f420e6606e 100755 Binary files a/tools/whltool.par and b/tools/whltool.par differ