Skip to content

Commit bb5a7c4

Browse files
committed
Replace manual unzip with installer.
1 parent e8e927b commit bb5a7c4

File tree

13 files changed

+111
-334
lines changed

13 files changed

+111
-334
lines changed

examples/pip_install/pip_install_test.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ def test_data(self):
3737
self.assertListEqual(
3838
env.split(" "),
3939
[
40-
"external/pip/pypi__s3cmd/s3cmd-2.1.0.data/data/share/doc/packages/s3cmd/INSTALL.md",
41-
"external/pip/pypi__s3cmd/s3cmd-2.1.0.data/data/share/doc/packages/s3cmd/LICENSE",
42-
"external/pip/pypi__s3cmd/s3cmd-2.1.0.data/data/share/doc/packages/s3cmd/NEWS",
43-
"external/pip/pypi__s3cmd/s3cmd-2.1.0.data/data/share/doc/packages/s3cmd/README.md",
44-
"external/pip/pypi__s3cmd/s3cmd-2.1.0.data/data/share/man/man1/s3cmd.1",
45-
"external/pip/pypi__s3cmd/s3cmd-2.1.0.data/scripts/s3cmd",
40+
"external/pip/pypi__s3cmd/data/share/doc/packages/s3cmd/INSTALL.md",
41+
"external/pip/pypi__s3cmd/data/share/doc/packages/s3cmd/LICENSE",
42+
"external/pip/pypi__s3cmd/data/share/doc/packages/s3cmd/NEWS",
43+
"external/pip/pypi__s3cmd/data/share/doc/packages/s3cmd/README.md",
44+
"external/pip/pypi__s3cmd/data/share/man/man1/s3cmd.1",
4645
],
4746
)
4847

@@ -52,12 +51,13 @@ def test_dist_info(self):
5251
self.assertListEqual(
5352
env.split(" "),
5453
[
55-
"external/pip/pypi__boto3/boto3-1.14.51.dist-info/DESCRIPTION.rst",
56-
"external/pip/pypi__boto3/boto3-1.14.51.dist-info/METADATA",
57-
"external/pip/pypi__boto3/boto3-1.14.51.dist-info/RECORD",
58-
"external/pip/pypi__boto3/boto3-1.14.51.dist-info/WHEEL",
59-
"external/pip/pypi__boto3/boto3-1.14.51.dist-info/metadata.json",
60-
"external/pip/pypi__boto3/boto3-1.14.51.dist-info/top_level.txt",
54+
"external/pip/pypi__boto3/site-packages/boto3-1.14.51.dist-info/DESCRIPTION.rst",
55+
'external/pip/pypi__boto3/site-packages/boto3-1.14.51.dist-info/INSTALLER',
56+
"external/pip/pypi__boto3/site-packages/boto3-1.14.51.dist-info/METADATA",
57+
"external/pip/pypi__boto3/site-packages/boto3-1.14.51.dist-info/RECORD",
58+
"external/pip/pypi__boto3/site-packages/boto3-1.14.51.dist-info/WHEEL",
59+
"external/pip/pypi__boto3/site-packages/boto3-1.14.51.dist-info/metadata.json",
60+
"external/pip/pypi__boto3/site-packages/boto3-1.14.51.dist-info/top_level.txt",
6161
],
6262
)
6363

examples/pip_parse/pip_parse_test.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ def test_data(self):
3535
self.assertListEqual(
3636
env.split(" "),
3737
[
38-
"external/pypi_s3cmd/s3cmd-2.1.0.data/data/share/doc/packages/s3cmd/INSTALL.md",
39-
"external/pypi_s3cmd/s3cmd-2.1.0.data/data/share/doc/packages/s3cmd/LICENSE",
40-
"external/pypi_s3cmd/s3cmd-2.1.0.data/data/share/doc/packages/s3cmd/NEWS",
41-
"external/pypi_s3cmd/s3cmd-2.1.0.data/data/share/doc/packages/s3cmd/README.md",
42-
"external/pypi_s3cmd/s3cmd-2.1.0.data/data/share/man/man1/s3cmd.1",
43-
"external/pypi_s3cmd/s3cmd-2.1.0.data/scripts/s3cmd",
38+
"external/pypi_s3cmd/data/share/doc/packages/s3cmd/INSTALL.md",
39+
"external/pypi_s3cmd/data/share/doc/packages/s3cmd/LICENSE",
40+
"external/pypi_s3cmd/data/share/doc/packages/s3cmd/NEWS",
41+
"external/pypi_s3cmd/data/share/doc/packages/s3cmd/README.md",
42+
"external/pypi_s3cmd/data/share/man/man1/s3cmd.1",
4443
],
4544
)
4645

@@ -50,11 +49,12 @@ def test_dist_info(self):
5049
self.assertListEqual(
5150
env.split(" "),
5251
[
53-
"external/pypi_requests/requests-2.25.1.dist-info/LICENSE",
54-
"external/pypi_requests/requests-2.25.1.dist-info/METADATA",
55-
"external/pypi_requests/requests-2.25.1.dist-info/RECORD",
56-
"external/pypi_requests/requests-2.25.1.dist-info/WHEEL",
57-
"external/pypi_requests/requests-2.25.1.dist-info/top_level.txt",
52+
'external/pypi_requests/site-packages/requests-2.25.1.dist-info/INSTALLER',
53+
"external/pypi_requests/site-packages/requests-2.25.1.dist-info/LICENSE",
54+
"external/pypi_requests/site-packages/requests-2.25.1.dist-info/METADATA",
55+
"external/pypi_requests/site-packages/requests-2.25.1.dist-info/RECORD",
56+
"external/pypi_requests/site-packages/requests-2.25.1.dist-info/WHEEL",
57+
"external/pypi_requests/site-packages/requests-2.25.1.dist-info/top_level.txt",
5858
],
5959
)
6060

examples/pip_repository_annotations/WORKSPACE

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ write_file(
4242
copy_executables = {"@pip_repository_annotations_example//:data/copy_executable.py": "copied_content/executable.py"},
4343
copy_files = {"@pip_repository_annotations_example//:data/copy_file.txt": "copied_content/file.txt"},
4444
data = [":generated_file"],
45-
data_exclude_glob = ["*.dist-info/WHEEL"],
45+
data_exclude_glob = ["site-packages/*.dist-info/WHEEL"],
4646
),
4747
}
4848

examples/pip_repository_annotations/pip_repository_annotations_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test_data_exclude_glob(self):
6969

7070
r = runfiles.Create()
7171
dist_info_dir = (
72-
"pip_repository_annotations_example/external/{}/wheel-{}.dist-info".format(
72+
"pip_repository_annotations_example/external/{}/site-packages/wheel-{}.dist-info".format(
7373
self.wheel_pkg_dir(),
7474
current_wheel_version,
7575
)

python/pip_install/extract_wheels/lib/BUILD

+1-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ py_library(
99
"arguments.py",
1010
"bazel.py",
1111
"namespace_pkgs.py",
12-
"purelib.py",
1312
"requirements.py",
1413
"wheel.py",
1514
],
@@ -18,7 +17,7 @@ py_library(
1817
"//python/pip_install/parse_requirements_to_bzl:__subpackages__",
1918
],
2019
deps = [
21-
requirement("pkginfo"),
20+
requirement("installer"),
2221
requirement("setuptools"),
2322
],
2423
)
@@ -135,17 +134,6 @@ py_test(
135134
],
136135
)
137136

138-
py_test(
139-
name = "purelib_test",
140-
size = "small",
141-
srcs = [
142-
"purelib_test.py",
143-
],
144-
deps = [
145-
":lib",
146-
],
147-
)
148-
149137
filegroup(
150138
name = "distribution",
151139
srcs = glob(

python/pip_install/extract_wheels/lib/bazel.py

+28-37
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from python.pip_install.extract_wheels.lib import (
1010
annotation,
1111
namespace_pkgs,
12-
purelib,
1312
wheel,
1413
)
1514

@@ -21,41 +20,41 @@
2120

2221

2322
def generate_entry_point_contents(
24-
entry_point: str, shebang: str = "#!/usr/bin/env python3"
23+
module: str, attribute: str, shebang: str = "#!/usr/bin/env python3"
2524
) -> str:
2625
"""Generate the contents of an entry point script.
2726
2827
Args:
29-
entry_point (str): The name of the entry point as show in the
30-
`console_scripts` section of `entry_point.txt`.
28+
module (str): The name of the module to use.
29+
attribute (str): The name of the attribute to call.
3130
shebang (str, optional): The shebang to use for the entry point python
3231
file.
3332
3433
Returns:
3534
str: A string of python code.
3635
"""
37-
module, method = entry_point.split(":", 1)
3836
return textwrap.dedent(
3937
"""\
4038
{shebang}
4139
import sys
42-
from {module} import {method}
40+
from {module} import {attribute}
4341
if __name__ == "__main__":
44-
sys.exit({method}())
42+
sys.exit({attribute}())
4543
""".format(
46-
shebang=shebang, module=module, method=method
44+
shebang=shebang, module=module, attribute=attribute
4745
)
4846
)
4947

5048

51-
def generate_entry_point_rule(script: str, pkg: str) -> str:
49+
def generate_entry_point_rule(name: str, script: str, pkg: str) -> str:
5250
"""Generate a Bazel `py_binary` rule for an entry point script.
5351
5452
Note that the script is used to determine the name of the target. The name of
5553
entry point targets should be uniuqe to avoid conflicts with existing sources or
5654
directories within a wheel.
5755
5856
Args:
57+
name (str): The name of the generated py_binary.
5958
script (str): The path to the entry point's python file.
6059
pkg (str): The package owning the entry point. This is expected to
6160
match up with the `py_library` defined for each repository.
@@ -64,7 +63,6 @@ def generate_entry_point_rule(script: str, pkg: str) -> str:
6463
Returns:
6564
str: A `py_binary` instantiation.
6665
"""
67-
name = os.path.splitext(script)[0]
6866
return textwrap.dedent(
6967
"""\
7068
py_binary(
@@ -138,27 +136,18 @@ def generate_build_file_contents(
138136
there may be no Python sources whatsoever (e.g. packages written in Cython: like `pymssql`).
139137
"""
140138

141-
dist_info_ignores = [
142-
# RECORD is known to contain sha256 checksums of files which might include the checksums
143-
# of generated files produced when wheels are installed. The file is ignored to avoid
144-
# Bazel caching issues.
145-
"**/*.dist-info/RECORD",
146-
]
147-
148139
data_exclude = list(
149140
set(
150141
[
151-
"*.whl",
152-
"**/__pycache__/**",
153142
"**/* *",
154143
"**/*.py",
155144
"**/*.pyc",
156-
"BUILD.bazel",
157-
"WORKSPACE",
158-
f"{WHEEL_ENTRY_POINT_PREFIX}*.py",
145+
# RECORD is known to contain sha256 checksums of files which might include the checksums
146+
# of generated files produced when wheels are installed. The file is ignored to avoid
147+
# Bazel caching issues.
148+
"**/*.dist-info/RECORD",
159149
]
160150
+ data_exclude
161-
+ dist_info_ignores
162151
)
163152
)
164153

@@ -173,12 +162,12 @@ def generate_build_file_contents(
173162
174163
filegroup(
175164
name = "{dist_info_label}",
176-
srcs = glob(["*.dist-info/**"], allow_empty = True),
165+
srcs = glob(["site-packages/*.dist-info/**"], allow_empty = True),
177166
)
178167
179168
filegroup(
180169
name = "{data_label}",
181-
srcs = glob(["*.data/**"], allow_empty = True),
170+
srcs = glob(["data/**"], allow_empty = True),
182171
)
183172
184173
filegroup(
@@ -189,11 +178,11 @@ def generate_build_file_contents(
189178
190179
py_library(
191180
name = "{name}",
192-
srcs = glob(["**/*.py"], exclude={srcs_exclude}, allow_empty = True),
193-
data = {data} + glob(["**/*"], exclude={data_exclude}),
181+
srcs = glob(["site-packages/**/*.py"], exclude={srcs_exclude}, allow_empty = True),
182+
data = {data} + glob(["site-packages/**/*"], exclude={data_exclude}),
194183
# This makes this directory a top-level in the python import
195184
# search path for anything that depends on this.
196-
imports = ["."],
185+
imports = ["site-packages"],
197186
deps = [{dependencies}],
198187
tags = [{tags}],
199188
)
@@ -378,9 +367,6 @@ def extract_wheel(
378367
shutil.copy(whl.path, directory)
379368
whl.unzip(directory)
380369

381-
# Note: Order of operations matters here
382-
purelib.spread_purelib_into_root(directory)
383-
384370
if not enable_implicit_namespace_pkgs:
385371
setup_namespace_pkg_compatibility(directory)
386372

@@ -408,14 +394,19 @@ def extract_wheel(
408394

409395
directory_path = Path(directory)
410396
entry_points = []
411-
for name, entry_point in sorted(whl.entry_points().items()):
412-
entry_point_script = f"{WHEEL_ENTRY_POINT_PREFIX}_{name}.py"
413-
(directory_path / entry_point_script).write_text(
414-
generate_entry_point_contents(entry_point)
397+
for name, (module, attribute) in sorted(whl.entry_points().items()):
398+
# There is an extreme edge-case with entry_points that end with `.py`
399+
# See: https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java#L174
400+
entry_point_without_py = name[:-3] if name.endswith(".py") else name
401+
entry_point_target_name = f"{WHEEL_ENTRY_POINT_PREFIX}_{entry_point_without_py}"
402+
entry_point_script_name = f"{entry_point_target_name}.py"
403+
(directory_path / entry_point_script_name).write_text(
404+
generate_entry_point_contents(module, attribute)
415405
)
416406
entry_points.append(
417407
generate_entry_point_rule(
418-
entry_point_script,
408+
entry_point_target_name,
409+
entry_point_script_name,
419410
library_name,
420411
)
421412
)
@@ -449,7 +440,7 @@ def extract_wheel(
449440
data_exclude=data_exclude,
450441
data=data,
451442
srcs_exclude=srcs_exclude,
452-
tags=["pypi_name=" + whl.name, "pypi_version=" + whl.metadata.version],
443+
tags=["pypi_name=" + whl.name, "pypi_version=" + whl.version],
453444
additional_content=additional_content,
454445
)
455446
build_file.write(contents)

python/pip_install/extract_wheels/lib/purelib.py

-67
This file was deleted.

0 commit comments

Comments
 (0)