Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9b8ab1e

Browse filesBrowse files
groodtf0rmiga
andauthored
Init: pypa/installer (bazel-contrib#700)
* . * . * . * . * Fixes from review. Co-authored-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com>
1 parent 4f4f792 commit 9b8ab1e
Copy full SHA for 9b8ab1e

File tree

Expand file treeCollapse file tree

4 files changed

+41
-53
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+41
-53
lines changed

‎python/pip_install/extract_wheels/lib/BUILD

Copy file name to clipboardExpand all lines: python/pip_install/extract_wheels/lib/BUILD
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ py_library(
1818
"//python/pip_install/parse_requirements_to_bzl:__subpackages__",
1919
],
2020
deps = [
21-
requirement("pkginfo"),
21+
requirement("installer"),
2222
requirement("setuptools"),
2323
],
2424
)

‎python/pip_install/extract_wheels/lib/bazel.py

Copy file name to clipboardExpand all lines: python/pip_install/extract_wheels/lib/bazel.py
+9-10Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,28 @@
2121

2222

2323
def generate_entry_point_contents(
24-
entry_point: str, shebang: str = "#!/usr/bin/env python3"
24+
module: str, attribute: str, shebang: str = "#!/usr/bin/env python3"
2525
) -> str:
2626
"""Generate the contents of an entry point script.
2727
2828
Args:
29-
entry_point (str): The name of the entry point as show in the
30-
`console_scripts` section of `entry_point.txt`.
29+
module (str): The name of the module to use.
30+
attribute (str): The name of the attribute to call.
3131
shebang (str, optional): The shebang to use for the entry point python
3232
file.
3333
3434
Returns:
3535
str: A string of python code.
3636
"""
37-
module, method = entry_point.split(":", 1)
3837
return textwrap.dedent(
3938
"""\
4039
{shebang}
4140
import sys
42-
from {module} import {method}
41+
from {module} import {attribute}
4342
if __name__ == "__main__":
44-
sys.exit({method}())
43+
sys.exit({attribute}())
4544
""".format(
46-
shebang=shebang, module=module, method=method
45+
shebang=shebang, module=module, attribute=attribute
4746
)
4847
)
4948

@@ -408,14 +407,14 @@ def extract_wheel(
408407

409408
directory_path = Path(directory)
410409
entry_points = []
411-
for name, entry_point in sorted(whl.entry_points().items()):
410+
for name, (module, attribute) in sorted(whl.entry_points().items()):
412411
# There is an extreme edge-case with entry_points that end with `.py`
413412
# See: https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java#L174
414413
entry_point_without_py = name[:-3] if name.endswith(".py") else name
415414
entry_point_target_name = f"{WHEEL_ENTRY_POINT_PREFIX}_{entry_point_without_py}"
416415
entry_point_script_name = f"{entry_point_target_name}.py"
417416
(directory_path / entry_point_script_name).write_text(
418-
generate_entry_point_contents(entry_point)
417+
generate_entry_point_contents(module, attribute)
419418
)
420419
entry_points.append(
421420
generate_entry_point_rule(
@@ -454,7 +453,7 @@ def extract_wheel(
454453
data_exclude=data_exclude,
455454
data=data,
456455
srcs_exclude=srcs_exclude,
457-
tags=["pypi_name=" + whl.name, "pypi_version=" + whl.metadata.version],
456+
tags=["pypi_name=" + whl.name, "pypi_version=" + whl.version],
458457
additional_content=additional_content,
459458
)
460459
build_file.write(contents)

‎python/pip_install/extract_wheels/lib/wheel.py

Copy file name to clipboardExpand all lines: python/pip_install/extract_wheels/lib/wheel.py
+26-37Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""Utility class to inspect an extracted wheel directory"""
2-
import configparser
2+
import email
33
import glob
44
import os
55
import stat
66
import zipfile
77
from typing import Dict, Optional, Set
88

9+
import installer
910
import pkg_resources
10-
import pkginfo
1111

1212

1313
def current_umask() -> int:
@@ -37,57 +37,46 @@ def path(self) -> str:
3737

3838
@property
3939
def name(self) -> str:
40-
return str(self.metadata.name)
40+
# TODO Also available as installer.sources.WheelSource.distribution
41+
return str(self.metadata['Name'])
4142

4243
@property
43-
def metadata(self) -> pkginfo.Wheel:
44-
return pkginfo.get_metadata(self.path)
44+
def metadata(self) -> email.message.Message:
45+
with installer.sources.WheelFile.open(self.path) as wheel_source:
46+
metadata_contents = wheel_source.read_dist_info("METADATA")
47+
metadata = installer.utils.parse_metadata_file(metadata_contents)
48+
return metadata
4549

46-
def entry_points(self) -> Dict[str, str]:
50+
@property
51+
def version(self) -> str:
52+
# TODO Also available as installer.sources.WheelSource.version
53+
return str(self.metadata["Version"])
54+
55+
def entry_points(self) -> Dict[str, tuple[str, str]]:
4756
"""Returns the entrypoints defined in the current wheel
4857
4958
See https://packaging.python.org/specifications/entry-points/ for more info
5059
5160
Returns:
52-
Dict[str, str]: A mappying of the entry point's name to it's method
61+
Dict[str, Tuple[str, str]]: A mapping of the entry point's name to it's module and attribute
5362
"""
54-
with zipfile.ZipFile(self.path, "r") as whl:
55-
# Calculate the location of the entry_points.txt file
56-
metadata = self.metadata
57-
name = "{}-{}".format(metadata.name.replace("-", "_"), metadata.version)
58-
59-
# Note that the zipfile module always uses the forward slash as
60-
# directory separator, even on Windows, so don't use os.path.join
61-
# here. Reference for Python 3.10:
62-
# https://github.com/python/cpython/blob/3.10/Lib/zipfile.py#L355.
63-
# TODO: use zipfile.Path once 3.8 is our minimum supported version
64-
entry_points_path = "{}.dist-info/entry_points.txt".format(name)
65-
66-
# If this file does not exist in the wheel, there are no entry points
67-
if entry_points_path not in whl.namelist():
63+
with installer.sources.WheelFile.open(self.path) as wheel_source:
64+
if "entry_points.txt" not in wheel_source.dist_info_filenames:
6865
return dict()
6966

70-
# Parse the avaialble entry points
71-
config = configparser.ConfigParser()
72-
try:
73-
config.read_string(whl.read(entry_points_path).decode("utf-8"))
74-
if "console_scripts" in config.sections():
75-
return dict(config["console_scripts"])
76-
77-
# TODO: It's unclear what to do in a situation with duplicate sections or options.
78-
# For now, we treat the config file as though it contains no scripts. For more
79-
# details on the config parser, see:
80-
# https://docs.python.org/3.7/library/configparser.html#configparser.ConfigParser
81-
# https://docs.python.org/3.7/library/configparser.html#configparser.Error
82-
except configparser.Error:
83-
pass
67+
entry_points_mapping = dict()
68+
entry_points_contents = wheel_source.read_dist_info("entry_points.txt")
69+
entry_points = installer.utils.parse_entrypoints(entry_points_contents)
70+
for script, module, attribute, script_section in entry_points:
71+
if script_section == "console":
72+
entry_points_mapping[script] = (module, attribute)
8473

85-
return dict()
74+
return entry_points_mapping
8675

8776
def dependencies(self, extras_requested: Optional[Set[str]] = None) -> Set[str]:
8877
dependency_set = set()
8978

90-
for wheel_req in self.metadata.requires_dist:
79+
for wheel_req in self.metadata.get_all('Requires-Dist', []):
9180
req = pkg_resources.Requirement(wheel_req) # type: ignore
9281

9382
if req.marker is None or any(

‎python/pip_install/repositories.bzl

Copy file name to clipboardExpand all lines: python/pip_install/repositories.bzl
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ _RULE_DEPS = [
1717
"https://files.pythonhosted.org/packages/44/98/5b86278fbbf250d239ae0ecb724f8572af1c91f4a11edf4d36a206189440/colorama-0.4.4-py2.py3-none-any.whl",
1818
"9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2",
1919
),
20+
(
21+
"pypi__installer",
22+
"https://files.pythonhosted.org/packages/1b/21/3e6ebd12d8dccc55bcb7338db462c75ac86dbd0ac7439ac114616b21667b/installer-0.5.1-py3-none-any.whl",
23+
"1d6c8d916ed82771945b9c813699e6f57424ded970c9d8bf16bbc23e1e826ed3",
24+
),
2025
(
2126
"pypi__pip",
2227
"https://files.pythonhosted.org/packages/4d/16/0a14ca596f30316efd412a60bdfac02a7259bf8673d4d917dc60b9a21812/pip-22.0.4-py3-none-any.whl",
@@ -27,11 +32,6 @@ _RULE_DEPS = [
2732
"https://files.pythonhosted.org/packages/6d/16/75d65bdccd48bb59a08e2bf167b01d8532f65604270d0a292f0f16b7b022/pip_tools-5.5.0-py2.py3-none-any.whl",
2833
"10841c1e56c234d610d0466447685b9ea4ee4a2c274f858c0ef3c33d9bd0d985",
2934
),
30-
(
31-
"pypi__pkginfo",
32-
"https://files.pythonhosted.org/packages/cd/00/49f59cdd2c6a52e6665fda4de671dac5614366dc827e050c55428241b929/pkginfo-1.8.2-py2.py3-none-any.whl",
33-
"c24c487c6a7f72c66e816ab1796b96ac6c3d14d49338293d2141664330b55ffc",
34-
),
3535
(
3636
"pypi__setuptools",
3737
"https://files.pythonhosted.org/packages/7c/5b/3d92b9f0f7ca1645cba48c080b54fe7d8b1033a4e5720091d1631c4266db/setuptools-60.10.0-py3-none-any.whl",

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.