Skip to content

Commit 8caa860

Browse files
author
Jonathon Belotti
committed
finished (untested) sketch implementation
1 parent b1495ed commit 8caa860

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/extract_wheels.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
from . import namespace_pkgs
9+
from . import purelib
910
from .wheel import Wheel
1011

1112
BUILD_TEMPLATE = """\
@@ -78,6 +79,7 @@ def extract_wheel(whl, directory, extras):
7879

7980
whl.unzip(directory)
8081

82+
purelib.spread_purelib_into_root(directory)
8183
_setup_namespace_pkg_compatibility(directory)
8284

8385
with open(os.path.join(directory, "BUILD"), "w") as f:

src/purelib.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1+
import pathlib
2+
import shutil
3+
4+
from . import wheel
5+
16

27
def spread_purelib_into_root(extracted_whl_directory: str) -> None:
3-
pass
4-
# Root-Is-Purelib: false
8+
dist_info = wheel.get_dist_info(extracted_whl_directory)
9+
wheel_metadata_file_path = pathlib.Path(dist_info, "WHEEL")
10+
wheel_metadata_dict = wheel.parse_WHEEL_file(str(wheel_metadata_file_path))
11+
12+
if "Root-Is-Purelib" not in wheel_metadata_dict:
13+
raise ValueError(f"Invalid WHEEL file '{wheel_metadata_file_path}'. Expected key 'Root-Is-Purelib'.")
14+
root_is_purelib = wheel_metadata_dict["Root-Is-Purelib"]
15+
16+
if root_is_purelib.lower() == "true":
17+
# The Python package code is in the root of the Wheel, so no need to 'spread' anything.
18+
return
19+
20+
dot_data_dir = wheel.get_dot_data_directory(extracted_whl_directory)
21+
shutil.move(dot_data_dir, extracted_whl_directory)

src/wheel.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import pkg_resources
55
import glob
66

7+
from typing import Dict
8+
79

810
class Wheel(object):
911
def __init__(self, path):
@@ -50,3 +52,29 @@ def get_dist_info(extracted_whl_directory):
5052
else:
5153
dist_info = dist_info_dirs[0]
5254
return dist_info
55+
56+
57+
def get_dot_data_directory(extracted_whl_directory):
58+
# See: https://www.python.org/dev/peps/pep-0491/#the-data-directory
59+
dot_data_dirs = glob.glob(os.path.join(extracted_whl_directory, "*.data"))
60+
if not dot_data_dirs:
61+
raise ValueError(f"No *.data directory found. {extracted_whl_directory} is not a valid Wheel.")
62+
elif len(dot_data_dirs) > 1:
63+
raise ValueError(f"Found more than 1 *.data directory. {extracted_whl_directory} is not a valid Wheel.")
64+
else:
65+
dot_data_dir = dot_data_dirs[0]
66+
return dot_data_dir
67+
68+
69+
def parse_WHEEL_file(whl_file_path: str) -> Dict[str, str]:
70+
contents = {}
71+
with open(whl_file_path, "r") as f:
72+
for line in f:
73+
cleaned = line.strip()
74+
try:
75+
key, value = cleaned.split(":", maxsplit=1)
76+
contents[key] = value.strip()
77+
except ValueError:
78+
raise RuntimeError(f"Encounted invalid line in WHEEL file: '{cleaned}'")
79+
return contents
80+

0 commit comments

Comments
 (0)