Skip to content

Commit

Permalink
Add pip_data_exclude to pip_repository (bazelbuild#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
gergelyfabian authored Jul 9, 2020
1 parent a83aa9b commit 3e1a6a5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
9 changes: 9 additions & 0 deletions defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def _pip_repository_impl(rctx):
"\"" + " ".join(rctx.attr.extra_pip_args) + "\"",
]

if rctx.attr.pip_data_exclude:
args += [
"--pip_data_exclude",
struct(exclude = rctx.attr.pip_data_exclude).to_json(),
]

result = rctx.execute(
args,
environment = {
Expand Down Expand Up @@ -71,6 +77,9 @@ python_interpreter.
"extra_pip_args": attr.string_list(
doc = "Extra arguments to pass on to pip. Must not contain spaces.",
),
"pip_data_exclude": attr.string_list(
doc = "Additional data exclusion parameters to add to the pip packages BUILD file.",
),
},
implementation = _pip_repository_impl,
)
Expand Down
13 changes: 12 additions & 1 deletion extract_wheels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
import subprocess
import sys
import json

from extract_wheels.lib import bazel, requirements

Expand Down Expand Up @@ -65,6 +66,11 @@ def main() -> None:
)
parser.add_argument('--extra_pip_args', action='store',
help=('Extra arguments to pass down to pip.'))
parser.add_argument(
"--pip_data_exclude",
action='store',
help='Additional data exclusion parameters to add to the pip packages BUILD file.'
)
args = parser.parse_args()

pip_args = [sys.executable, "-m", "pip", "wheel", "-r", args.requirements]
Expand All @@ -75,8 +81,13 @@ def main() -> None:

extras = requirements.parse_extras(args.requirements)

if args.pip_data_exclude:
pip_data_exclude = json.loads(args.pip_data_exclude)["exclude"]
else:
pip_data_exclude = []

targets = [
'"%s%s"' % (args.repo, bazel.extract_wheel(whl, extras))
'"%s%s"' % (args.repo, bazel.extract_wheel(whl, extras, pip_data_exclude))
for whl in glob.glob("*.whl")
]

Expand Down
13 changes: 8 additions & 5 deletions extract_wheels/lib/bazel.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Utility functions to manipulate Bazel files"""
import os
import textwrap
import json
from typing import Iterable, List, Dict, Set

from extract_wheels.lib import namespace_pkgs, wheel, purelib


def generate_build_file_contents(name: str, dependencies: List[str]) -> str:
def generate_build_file_contents(name: str, dependencies: List[str], pip_data_exclude: List[str]) -> str:
"""Generate a BUILD file for an unzipped Wheel
Args:
Expand All @@ -20,6 +21,8 @@ def generate_build_file_contents(name: str, dependencies: List[str]) -> str:
there may be no Python sources whatsoever (e.g. packages written in Cython: like `pymssql`).
"""

data_exclude = ["**/*.py", "**/* *", "BUILD", "WORKSPACE"] + pip_data_exclude

return textwrap.dedent(
"""\
package(default_visibility = ["//visibility:public"])
Expand All @@ -29,14 +32,14 @@ def generate_build_file_contents(name: str, dependencies: List[str]) -> str:
py_library(
name = "{name}",
srcs = glob(["**/*.py"], allow_empty = True),
data = glob(["**/*"], exclude=["**/*.py", "**/* *", "BUILD", "WORKSPACE"]),
data = glob(["**/*"], exclude={data_exclude}),
# This makes this directory a top-level in the python import
# search path for anything that depends on this.
imports = ["."],
deps = [{dependencies}],
)
""".format(
name=name, dependencies=",".join(dependencies)
name=name, dependencies=",".join(dependencies), data_exclude=json.dumps(data_exclude)
)
)

Expand Down Expand Up @@ -116,7 +119,7 @@ def setup_namespace_pkg_compatibility(wheel_dir: str) -> None:
namespace_pkgs.add_pkgutil_style_namespace_pkg_init(ns_pkg_dir)


def extract_wheel(wheel_file: str, extras: Dict[str, Set[str]]) -> str:
def extract_wheel(wheel_file: str, extras: Dict[str, Set[str]], pip_data_exclude: List[str]) -> str:
"""Extracts wheel into given directory and creates a py_library target.
Args:
Expand Down Expand Up @@ -145,7 +148,7 @@ def extract_wheel(wheel_file: str, extras: Dict[str, Set[str]]) -> str:

with open(os.path.join(directory, "BUILD"), "w") as build_file:
contents = generate_build_file_contents(
sanitise_name(whl.name), sanitised_dependencies,
sanitise_name(whl.name), sanitised_dependencies, pip_data_exclude,
)
build_file.write(contents)

Expand Down

0 comments on commit 3e1a6a5

Please sign in to comment.