Skip to content

Commit

Permalink
Some experimental changes to allow importing Python 3 PyPi packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Burkart committed Feb 15, 2018
1 parent 73a154a commit 90a70d5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
37 changes: 37 additions & 0 deletions python/pip.bzl
Expand Up @@ -34,6 +34,27 @@ def _pip_import_impl(repository_ctx):
if result.return_code:
fail("pip_import failed: %s (%s)" % (result.stdout, result.stderr))

def _pip3_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([
"python3", 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(
Expand All @@ -50,6 +71,22 @@ pip_import = repository_rule(
implementation = _pip_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 <code>requirements.txt</code> dependencies into Bazel.
This rule imports a <code>requirements.txt</code> file and generates a new
Expand Down
7 changes: 5 additions & 2 deletions rules_python/piptool.py
Expand Up @@ -178,7 +178,7 @@ def whl_library(wheel):
whl = "@{name}//:{path}",
requirements = "@{name}//:requirements.bzl",
extras = [{extras}]
)""".format(name=args.name, repo_name=wheel.repository_name(),
)""".format(name=args.name, repo_name=_make_wheel_name(args.name, wheel),
path=wheel.basename(),
extras=','.join([
'"%s"' % extra
Expand All @@ -187,7 +187,7 @@ def whl_library(wheel):

whl_targets = ','.join([
','.join([
'"%s": "@%s//:pkg"' % (whl.distribution().lower(), whl.repository_name())
'"%s": "@%s//:pkg"' % (whl.distribution().lower(), _make_wheel_name(args.name, whl))
] + [
# For every extra that is possible from this requirements.txt
'"%s[%s]": "@%s//:%s"' % (whl.distribution().lower(), extra.lower(),
Expand Down Expand Up @@ -223,5 +223,8 @@ def requirement(name):
whl_libraries='\n'.join(map(whl_library, whls)) if whls else "pass",
mappings=whl_targets))

def _make_wheel_name(namespace, wheel):
return "{}_{}".format(namespace, wheel.repository_name())

if __name__ == '__main__':
main()
Binary file modified tools/piptool.par
Binary file not shown.

0 comments on commit 90a70d5

Please sign in to comment.