Skip to content

Commit 26ab3e4

Browse files
committed
MAINT: further simplify
1 parent ba2743e commit 26ab3e4

File tree

2 files changed

+42
-64
lines changed

2 files changed

+42
-64
lines changed

mesonpy/__init__.py

Lines changed: 41 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,44 @@ def _init_colors() -> Dict[str, str]:
128128
assert all(re.match(_EXTENSION_SUFFIX_REGEX, x) for x in _EXTENSION_SUFFIXES)
129129

130130

131+
# Maps wheel installation paths to Meson installation path placeholders.
132+
# See https://docs.python.org/3/library/sysconfig.html#installation-paths
133+
_SCHEME_MAP = {
134+
'scripts': ('{bindir}',),
135+
'purelib': ('{py_purelib}',),
136+
'platlib': ('{py_platlib}', '{moduledir_shared}'),
137+
'headers': ('{includedir}',),
138+
'data': ('{datadir}',),
139+
# our custom location
140+
'mesonpy-libs': ('{libdir}', '{libdir_shared}')
141+
}
142+
143+
144+
def _map_meson_destination(destination: str) -> Tuple[Optional[str], pathlib.Path]:
145+
"""Map a Meson installation path to a wheel installation location.
146+
147+
Return a (wheel path identifier, subpath inside the wheel path) tuple.
148+
149+
"""
150+
parts = pathlib.Path(destination).parts
151+
for folder, placeholders in _SCHEME_MAP.items():
152+
if parts[0] in placeholders:
153+
return folder, pathlib.Path(*parts[1:])
154+
warnings.warn(f'Could not map installation path to an equivalent wheel directory: {destination!r}')
155+
return None, pathlib.Path(destination)
156+
157+
158+
def _map_to_wheel(sources: Dict[str, Dict[str, Any]]) -> DefaultDict[str, List[Tuple[pathlib.Path, str]]]:
159+
"""Map files to the wheel, organized by wheel installation directrory."""
160+
wheel_files = collections.defaultdict(list)
161+
for group in sources.values():
162+
for src, target in group.items():
163+
directory, path = _map_meson_destination(target['destination'])
164+
if directory is not None:
165+
wheel_files[directory].append((path, src))
166+
return wheel_files
167+
168+
131169
def _showwarning(
132170
message: Union[Warning, str],
133171
category: Type[Warning],
@@ -180,40 +218,25 @@ class MesonBuilderError(Error):
180218
class _WheelBuilder():
181219
"""Helper class to build wheels from projects."""
182220

183-
# Maps wheel scheme names to Meson placeholder directories
184-
_SCHEME_MAP: ClassVar[Dict[str, Tuple[str, ...]]] = {
185-
'scripts': ('{bindir}',),
186-
'purelib': ('{py_purelib}',),
187-
'platlib': ('{py_platlib}', '{moduledir_shared}'),
188-
'headers': ('{includedir}',),
189-
'data': ('{datadir}',),
190-
# our custom location
191-
'mesonpy-libs': ('{libdir}', '{libdir_shared}')
192-
}
193-
194221
def __init__(
195222
self,
196223
project: Project,
197224
metadata: Optional[pyproject_metadata.StandardMetadata],
198225
source_dir: pathlib.Path,
199-
install_dir: pathlib.Path,
200226
build_dir: pathlib.Path,
201227
sources: Dict[str, Dict[str, Any]],
202-
copy_files: Dict[str, str],
203228
) -> None:
204229
self._project = project
205230
self._metadata = metadata
206231
self._source_dir = source_dir
207-
self._install_dir = install_dir
208232
self._build_dir = build_dir
209233
self._sources = sources
210-
self._copy_files = copy_files
211234

212235
self._libs_build_dir = self._build_dir / 'mesonpy-wheel-libs'
213236

214237
@cached_property
215238
def _wheel_files(self) -> DefaultDict[str, List[Tuple[pathlib.Path, str]]]:
216-
return self._map_to_wheel(self._sources, self._copy_files)
239+
return _map_to_wheel(self._sources)
217240

218241
@property
219242
def _has_internal_libs(self) -> bool:
@@ -396,38 +419,6 @@ def _is_native(self, file: Union[str, pathlib.Path]) -> bool:
396419
return True
397420
return False
398421

399-
def _map_from_scheme_map(self, destination: str) -> Optional[Tuple[str, pathlib.Path]]:
400-
"""Extracts scheme and relative destination from installation paths."""
401-
parts = pathlib.Path(destination).parts
402-
for scheme, placeholders in self._SCHEME_MAP.items():
403-
if parts[0] in placeholders:
404-
return scheme, pathlib.Path(*parts[1:])
405-
return None
406-
407-
def _map_to_wheel(
408-
self,
409-
sources: Dict[str, Dict[str, Any]],
410-
copy_files: Dict[str, str],
411-
) -> DefaultDict[str, List[Tuple[pathlib.Path, str]]]:
412-
"""Map files to the wheel, organized by scheme."""
413-
wheel_files = collections.defaultdict(list)
414-
for files in sources.values(): # entries in intro-install_plan.json
415-
for file, details in files.items(): # install path -> {destination, tag}
416-
# try mapping to wheel location
417-
meson_destination = details['destination']
418-
install_details = self._map_from_scheme_map(meson_destination)
419-
if install_details:
420-
scheme, destination = install_details
421-
wheel_files[scheme].append((destination, file))
422-
continue
423-
# not found
424-
warnings.warn(
425-
'File could not be mapped to an equivalent wheel directory: '
426-
'{} ({})'.format(copy_files[file], meson_destination)
427-
)
428-
429-
return wheel_files
430-
431422
def _install_path(
432423
self,
433424
wheel_file: mesonpy._wheelfile.WheelFile,
@@ -522,7 +513,7 @@ def build(self, directory: Path) -> pathlib.Path:
522513
self._install_path(whl, counter, origin, destination)
523514

524515
# install the other schemes
525-
for scheme in self._SCHEME_MAP:
516+
for scheme in _SCHEME_MAP:
526517
if scheme in (root_scheme, 'mesonpy-libs'):
527518
continue
528519
for destination, origin in self._wheel_files[scheme]:
@@ -580,7 +571,7 @@ def build_editable(self, directory: Path, verbose: bool = False) -> pathlib.Path
580571
)
581572

582573
# install non-code schemes
583-
for scheme in self._SCHEME_MAP:
574+
for scheme in _SCHEME_MAP:
584575
if scheme in ('purelib', 'platlib', 'mesonpy-libs'):
585576
continue
586577
for destination, origin in self._wheel_files[scheme]:
@@ -831,10 +822,8 @@ def _wheel_builder(self) -> _WheelBuilder:
831822
self,
832823
self._metadata,
833824
self._source_dir,
834-
self._install_dir,
835825
self._build_dir,
836826
self._install_plan,
837-
self._copy_files,
838827
)
839828

840829
def build_commands(self, install_dir: Optional[pathlib.Path] = None) -> Sequence[Sequence[str]]:
@@ -902,17 +891,6 @@ def _install_plan(self) -> Dict[str, Dict[str, Dict[str, str]]]:
902891

903892
return install_plan
904893

905-
@property
906-
def _copy_files(self) -> Dict[str, str]:
907-
"""Files that Meson will copy on install and the target location."""
908-
copy_files = {}
909-
for origin, destination in self._info('intro-installed').items():
910-
destination_path = pathlib.Path(destination).absolute()
911-
copy_files[origin] = os.fspath(
912-
self._install_dir / destination_path.relative_to(destination_path.anchor)
913-
)
914-
return copy_files
915-
916894
@property
917895
def _meson_name(self) -> str:
918896
"""Name in meson.build."""

tests/test_tags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def wheel_builder_test_factory(monkeypatch, content):
5656
files = defaultdict(list)
5757
files.update({key: [(pathlib.Path(x), os.path.join('build', x)) for x in value] for key, value in content.items()})
5858
monkeypatch.setattr(mesonpy._WheelBuilder, '_wheel_files', files)
59-
return mesonpy._WheelBuilder(None, None, pathlib.Path(), pathlib.Path(), pathlib.Path(), pathlib.Path(), pathlib.Path())
59+
return mesonpy._WheelBuilder(None, None, pathlib.Path(), pathlib.Path(), pathlib.Path())
6060

6161

6262
def test_tag_empty_wheel(monkeypatch):

0 commit comments

Comments
 (0)