8000 #38: Add enable_implicit_namespace_pkgs (#55) · axivion/rules_python@275995e · GitHub
[go: up one dir, main page]

Skip to content

Commit 275995e

Browse files
bazel-contrib#38: Add enable_implicit_namespace_pkgs (bazel-contrib#55)
1 parent cdc0305 commit 275995e

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

defs.bzl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ def _pip_repository_impl(rctx):
4646
struct(exclude = rctx.attr.pip_data_exclude).to_json(),
4747
]
4848

49+
if rctx.attr.enable_implicit_namespace_pkgs:
50+
args += [
51+
"--enable_implicit_namespace_pkgs"
52+
]
53+
4954
result = rctx.execute(
5055
args,
5156
environment = {
@@ -80,6 +85,16 @@ python_interpreter.
8085
"pip_data_exclude": attr.string_list(
8186
doc = "Additional data exclusion parameters to add to the pip packages BUILD file.",
8287
),
88+
"enable_implicit_namespace_pkgs": attr.bool(
89+
default = False,
90+
doc = """
91+
If true, disables conversion of native namespace packages into pkg-util style namespace packages. When set all py_binary
92+
and py_test targets must specify either `legacy_create_init=False` or the global Bazel option
93+
`--incompatible_default_to_explicit_init_py` to prevent `__init__.py` being automatically generated in every directory.
94+
95+
This option is required to support some packages which cannot handle the conversion to pkg-util style.
96+
""",
97+
),
8398
},
8499
implementation = _pip_repository_impl,
85100
)

extract_wheels/__init__.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,18 @@ def main() -> None:
6565
help="The external repo name to install dependencies. In the format '@{REPO_NAME}'",
6666
)
6767
parser.add_argument(
68-
"--extra_pip_args",
69-
action="store",
70-
help=("Extra arguments to pass down to pip."),
68+
"--extra_pip_args", action="store", help="Extra arguments to pass down to pip.",
7169
)
7270
parser.add_argument(
7371
"--pip_data_exclude",
7472
action="store",
7573
help="Additional data exclusion parameters to add to the pip packages BUILD file.",
7674
)
75+
parser.add_argument(
76+
"--enable_implicit_namespace_pkgs",
77+
action="store_true",
78 8000 +
help="Disables conversion of implicit namespace packages into pkg-util style packages.",
79+
)
7780
args = parser.parse_args()
7881

7982
pip_args = [sys.executable, "-m", "pip", "wheel", "-r", args.requirements]
@@ -91,7 +94,13 @@ def main() -> None:
9194
pip_data_exclude = []
9295

9396
targets = [
94-
'"%s%s"' % (args.repo, bazel.extract_wheel(whl, extras, pip_data_exclude))
97+
'"%s%s"'
98+
% (
99+
args.repo,
100+
bazel.extract_wheel(
101+
whl, extras, pip_data_exclude, args.enable_implicit_namespace_pkgs
102+
),
103+
)
95104
for whl in glob.glob("*.whl")
96105
]
97106

extract_wheels/lib/bazel.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,18 @@ def setup_namespace_pkg_compatibility(wheel_dir: str) -> None:
120120

121121

122122
def extract_wheel(
123-
wheel_file: str, extras: Dict[str, Set[str]], pip_data_exclude: List[str]
123+
wheel_file: str,
124+
extras: Dict[str, Set[str]],
125+
pip_data_exclude: List[str],
126+
enable_implicit_namespace_pkgs: bool,
124127
) -> str:
125128
"""Extracts wheel into given directory and creates a py_library target.
126129
127130
Args:
128131
wheel_file: the filepath of the .whl
129132
extras: a list of extras to add as dependencies for the installed wheel
133+
pip_data_exclude: list of file patterns to exclude from the generated data section of the py_library
134+
enable_implicit_namespace_pkgs: if true, disables conversion of implicit namespace packages and will unzip as-is
130135
131136
Returns:
132137
The Bazel label for the extracted wheel, in the form '//path/to/wheel'.
@@ -140,7 +145,9 @@ def extract_wheel(
140145

141146
# Note: Order of operations matters here
142147
purelib.spread_purelib_into_root(directory)
143-
setup_namespace_pkg_compatibility(directory)
148+
149+
if not enable_implicit_namespace_pkgs:
150+
setup_namespace_pkg_compatibility(directory)
144151

145152
extras_requested = extras[whl.name] if whl.name in extras else set()
146153

0 commit comments

Comments
 (0)
0