Created
October 13, 2016 04:28
-
-
Save yugui/b7e80987dc2077754d91750b26eca3de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load("@com_github_gengo_rules_pypi//pypi:def.bzl", "pypi_universal_repository") | |
BUILD_FILE = """ | |
py_library( | |
name = "library", | |
srcs = glob(["lib/python*/site-packages/**/*.py"]), | |
imports = [ | |
"lib/python2.7/site-packages", | |
"lib/python3.5/site-packages", | |
], | |
data = glob( | |
include = ["lib/python*/**/*"], | |
exclude = [ | |
"lib/python*/**/*.py", | |
"lib/python*/site-packages/pip/**/*", | |
"lib/python*/site-packages/setuptools/**/*", | |
"lib/python/site-packages/*/wheel/**/*", | |
] + {excludes}, | |
), | |
visibility = ["//visibility:public"], | |
) | |
""" | |
def _py_requirements_impl(ctx): | |
pip = ctx.path(ctx.attr.pip) | |
virtualenv = ctx.path(ctx.attr.virtualenv) | |
virtualenv_lib = virtualenv.dirname.dirname.get_child("lib") | |
specs = [] | |
for i in range(0, len(ctx.attr.inputs)): | |
input = ctx.attr.inputs[i] | |
input_type = ctx.attr.input_types[i] | |
if input_type: | |
specs += [[input_type, ctx.path(input)]] | |
else: | |
specs += [[ctx.path(input).dirname]] | |
for spec in specs: | |
cmds = [ | |
["env", "PYTHONPATH=%s" % virtualenv_lib, | |
virtualenv, | |
"-p", ctx.attr.python, "."], | |
[".", "bin/activate"], | |
["python", pip, "install", "--no-compile", "--isolated", "-v"] + spec, | |
["pip", "freeze", ">constraints.txt"], | |
["deactivate"], | |
] | |
cmds = [" ".join(argv) for argv in cmds] | |
result = ctx.execute(["sh", "-c", " && ".join(cmds)]) | |
if result.return_code: | |
fail("Failed to install packages: %s\n%s" % (result.stdout, result.stderr)) | |
build = BUILD_FILE.format( | |
excludes = repr(ctx.attr.excludes), | |
) | |
ctx.file("BUILD", build, False) | |
_py_requirements = repository_rule( | |
_py_requirements_impl, | |
attrs = { | |
"python": attr.string(default = "python3"), | |
"inputs": attr.label_list( | |
mandatory = True, | |
allow_files = True, | |
), | |
"input_types": attr.string_list( | |
mandatory = True, | |
), | |
"excludes": attr.string_list(default=[]), | |
"pip": attr.label( | |
default = Label("@python_pip_tools//:pip.py"), | |
executable = True, | |
cfg = HOST_CFG, | |
), | |
"virtualenv": attr.label( | |
default = Label("@org_python_pypi_virtualenv//:bin/virtualenv"), | |
executable = True, | |
cfg = HOST_CFG, | |
), | |
}, | |
) | |
def py_requirements(name, requirements, **kwargs): | |
inputs = [] | |
input_types = [] | |
for r in requirements: | |
if r.startswith("-r "): | |
inputs += [r[len("-r "):]] | |
input_types += ["-r"] | |
continue | |
inputs += [r] | |
input_types += [""] | |
_py_requirements( | |
name = name, | |
inputs = inputs, | |
input_types = input_types, | |
**kwargs | |
) | |
def py_requirements_base_repositories(): | |
pypi_universal_repository( | |
name = "org_python_pypi_virtualenv", | |
pkg = "virtualenv", | |
version = "15.0.3", | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment