Skip to content

Commit 3e1a6a5

Browse files
Add pip_data_exclude to pip_repository (#43)
1 parent a83aa9b commit 3e1a6a5

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

defs.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ def _pip_repository_impl(rctx):
4040
"\"" + " ".join(rctx.attr.extra_pip_args) + "\"",
4141
]
4242

43+
if rctx.attr.pip_data_exclude:
44+
args += [
45+
"--pip_data_exclude",
46+
struct(exclude = rctx.attr.pip_data_exclude).to_json(),
47+
]
48+
4349
result = rctx.execute(
4450
args,
4551
environment = {
@@ -71,6 +77,9 @@ python_interpreter.
7177
"extra_pip_args": attr.string_list(
7278
doc = "Extra arguments to pass on to pip. Must not contain spaces.",
7379
),
80+
"pip_data_exclude": attr.string_list(
81+
doc = "Additional data exclusion parameters to add to the pip packages BUILD file.",
82+
),
7483
},
7584
implementation = _pip_repository_impl,
7685
)

extract_wheels/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os
1111
import subprocess
1212
import sys
13+
import json
1314

1415
from extract_wheels.lib import bazel, requirements
1516

@@ -65,6 +66,11 @@ def main() -> None:
6566
)
6667
parser.add_argument('--extra_pip_args', action='store',
6768
help=('Extra arguments to pass down to pip.'))
69+
parser.add_argument(
70+
"--pip_data_exclude",
71+
action='store',
72+
help='Additional data exclusion parameters to add to the pip packages BUILD file.'
73+
)
6874
args = parser.parse_args()
6975

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

7682
extras = requirements.parse_extras(args.requirements)
7783

84+
if args.pip_data_exclude:
85+
pip_data_exclude = json.loads(args.pip_data_exclude)["exclude"]
86+
else:
87+
pip_data_exclude = []
88+
7889
targets = [
79-
'"%s%s"' % (args.repo, bazel.extract_wheel(whl, extras))
90+
'"%s%s"' % (args.repo, bazel.extract_wheel(whl, extras, pip_data_exclude))
8091
for whl in glob.glob("*.whl")
8192
]
8293

extract_wheels/lib/bazel.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"""Utility functions to manipulate Bazel files"""
22
import os
33
import textwrap
4+
import json
45
from typing import Iterable, List, Dict, Set
56

67
from extract_wheels.lib import namespace_pkgs, wheel, purelib
78

89

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

24+
data_exclude = ["**/*.py", "**/* *", "BUILD", "WORKSPACE"] + pip_data_exclude
25+
2326
return textwrap.dedent(
2427
"""\
2528
package(default_visibility = ["//visibility:public"])
@@ -29,14 +32,14 @@ def generate_build_file_contents(name: str, dependencies: List[str]) -> str:
2932
py_library(
3033
name = "{name}",
3134
srcs = glob(["**/*.py"], allow_empty = True),
32-
data = glob(["**/*"], exclude=["**/*.py", "**/* *", "BUILD", "WORKSPACE"]),
35+
data = glob(["**/*"], exclude={data_exclude}),
3336
# This makes this directory a top-level in the python import
3437
# search path for anything that depends on this.
3538
imports = ["."],
3639
deps = [{dependencies}],
3740
)
3841
""".format(
39-
name=name, dependencies=",".join(dependencies)
42+
name=name, dependencies=",".join(dependencies), data_exclude=json.dumps(data_exclude)
4043
)
4144
)
4245

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

118121

119-
def extract_wheel(wheel_file: str, extras: Dict[str, Set[str]]) -> str:
122+
def extract_wheel(wheel_file: str, extras: Dict[str, Set[str]], pip_data_exclude: List[str]) -> str:
120123
"""Extracts wheel into given directory and creates a py_library target.
121124
122125
Args:
@@ -145,7 +148,7 @@ def extract_wheel(wheel_file: str, extras: Dict[str, Set[str]]) -> str:
145148

146149
with open(os.path.join(directory, "BUILD"), "w") as build_file:
147150
contents = generate_build_file_contents(
148-
sanitise_name(whl.name), sanitised_dependencies,
151+
sanitise_name(whl.name), sanitised_dependencies, pip_data_exclude,
149152
)
150153
build_file.write(contents)
151154

0 commit comments

Comments
 (0)