Skip to content

Commit 98e1260

Browse files
committed
Fix building of wheels for packages with src/ layout or namespace packages.
1 parent d5003d2 commit 98e1260

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

repo_helper/build.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import configparser
2828
import os
2929
import pathlib
30+
import posixpath
3031
import re
3132
import shutil
3233
import tarfile
@@ -38,7 +39,7 @@
3839
from io import StringIO
3940
from subprocess import PIPE, Popen
4041
from textwrap import dedent, indent
41-
from typing import Optional
42+
from typing import Iterator, Optional
4243
from zipfile import ZipFile
4344

4445
# 3rd party
@@ -107,7 +108,7 @@ def __init__(
107108
if self.build_dir.is_dir():
108109
shutil.rmtree(self.build_dir)
109110
self.build_dir.maybe_make(parents=True)
110-
(self.build_dir / self.import_name).maybe_make()
111+
(self.build_dir / self.pkg_dir).maybe_make(parents=True)
111112

112113
out_dir = out_dir or self.repo_dir / "dist"
113114
self.out_dir = PathPlus(out_dir)
@@ -135,27 +136,39 @@ def info_dir(self) -> PathPlus:
135136
info_dir.maybe_make()
136137
return info_dir
137138

138-
def copy_source(self) -> None:
139+
@property
140+
def pkg_dir(self) -> Optional[str]:
139141
"""
140-
Copy source files into the build directory.
142+
The path of the package directory.
143+
144+
Returns :py:obj:`None` if the project only has modules.
141145
"""
142146

143-
source_files = []
147+
if not self.config["py_modules"]:
148+
if self.config["stubs_package"]:
149+
return posixpath.join(self.config["source_dir"], f"{self.config['import_name'].replace('.', '/')}-stubs")
150+
else:
151+
return posixpath.join(self.config["source_dir"], self.config["import_name"].replace(".", "/"))
152+
153+
return None
144154

155+
def iter_source_files(self) -> Iterator[PathPlus]:
145156
if self.config["py_modules"]:
146-
source_files.extend(self.config["py_modules"])
157+
yield from self.config["py_modules"]
147158
else:
148-
if self.config["stubs_package"]:
149-
pkgdir = self.repo_dir / self.config["source_dir"] / f"{self.config['import_name'].replace('.', '/')}-stubs"
150-
else:
151-
pkgdir = self.repo_dir / self.config["source_dir"] / self.config["import_name"].replace(".", "/")
159+
pkgdir = self.repo_dir / self.pkg_dir
152160

153161
for py_pattern in {"**/*.py", "**/*.pyi", "**/*.pyx", "**/py.typed"}:
154162
for py_file in pkgdir.rglob(py_pattern):
155163
if "__pycache__" not in py_file.parts:
156-
source_files.append(py_file)
164+
yield py_file
165+
166+
def copy_source(self) -> None:
167+
"""
168+
Copy source files into the build directory.
169+
"""
157170

158-
for py_file in source_files:
171+
for py_file in self.iter_source_files():
159172
target = self.build_dir / py_file.relative_to(self.repo_dir)
160173
target.parent.maybe_make(parents=True)
161174
target.write_clean(py_file.read_text())
@@ -487,7 +500,7 @@ def get_record_entry(path: pathlib.Path, build_dir: pathlib.Path, blocksize: int
487500
wheel_filename = self.out_dir / f"{self.archive_name}-{self.tag}.whl"
488501
with ZipFile(wheel_filename, mode='w') as wheel_archive:
489502
with (self.dist_info / "RECORD").open('w') as fp:
490-
for file in (self.build_dir / self.import_name).rglob("*"):
503+
for file in (self.build_dir / self.pkg_dir).rglob("*"):
491504
if file.is_file():
492505
fp.write(get_record_entry(file, self.build_dir))
493506
fp.write("\n")
@@ -535,7 +548,7 @@ def create_conda_archive(self, wheel_contents_dir: PathLike, build_number: int =
535548
with tarfile.open(conda_filename, mode="w:bz2") as conda_archive:
536549
with (self.info_dir / "files").open('w') as fp:
537550

538-
for file in (PathPlus(wheel_contents_dir) / self.import_name).rglob("*"):
551+
for file in (PathPlus(wheel_contents_dir) / self.pkg_dir).rglob("*"):
539552
if file.is_file():
540553
filename = (site_packages / file.relative_to(wheel_contents_dir)).as_posix()
541554
fp.write(f"{filename}\n")
@@ -596,7 +609,7 @@ def build_wheel(self) -> str:
596609
self.write_entry_points()
597610
self.write_metadata(self.dist_info / "METADATA")
598611
self.write_wheel()
599-
(self.dist_info / "top_level.txt").write_clean(self.import_name)
612+
(self.dist_info / "top_level.txt").write_clean(posixpath.split(self.pkg_dir)[0])
600613
self.report_written(self.dist_info / "top_level.txt")
601614

602615
return self.create_wheel_archive()

repo_helper/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
2323
# MA 02110-1301, USA.
2424
#
25-
# calc_easter from https://code.activestate.com/recipes/576517-calculate-easter-western-given-a-year/
26-
# Copyright © 2008 Martin Diers
27-
# Licensed under the MIT License
25+
# calc_easter from https://code.activestate.com/recipes/576517-calculate-easter-western-given-a-year/
26+
# Copyright © 2008 Martin Diers
27+
# Licensed under the MIT License
2828
#
2929

3030
# stdlib

0 commit comments

Comments
 (0)