|
27 | 27 | import configparser |
28 | 28 | import os |
29 | 29 | import pathlib |
| 30 | +import posixpath |
30 | 31 | import re |
31 | 32 | import shutil |
32 | 33 | import tarfile |
|
38 | 39 | from io import StringIO |
39 | 40 | from subprocess import PIPE, Popen |
40 | 41 | from textwrap import dedent, indent |
41 | | -from typing import Optional |
| 42 | +from typing import Iterator, Optional |
42 | 43 | from zipfile import ZipFile |
43 | 44 |
|
44 | 45 | # 3rd party |
@@ -107,7 +108,7 @@ def __init__( |
107 | 108 | if self.build_dir.is_dir(): |
108 | 109 | shutil.rmtree(self.build_dir) |
109 | 110 | 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) |
111 | 112 |
|
112 | 113 | out_dir = out_dir or self.repo_dir / "dist" |
113 | 114 | self.out_dir = PathPlus(out_dir) |
@@ -135,27 +136,39 @@ def info_dir(self) -> PathPlus: |
135 | 136 | info_dir.maybe_make() |
136 | 137 | return info_dir |
137 | 138 |
|
138 | | - def copy_source(self) -> None: |
| 139 | + @property |
| 140 | + def pkg_dir(self) -> Optional[str]: |
139 | 141 | """ |
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. |
141 | 145 | """ |
142 | 146 |
|
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 |
144 | 154 |
|
| 155 | + def iter_source_files(self) -> Iterator[PathPlus]: |
145 | 156 | if self.config["py_modules"]: |
146 | | - source_files.extend(self.config["py_modules"]) |
| 157 | + yield from self.config["py_modules"] |
147 | 158 | 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 |
152 | 160 |
|
153 | 161 | for py_pattern in {"**/*.py", "**/*.pyi", "**/*.pyx", "**/py.typed"}: |
154 | 162 | for py_file in pkgdir.rglob(py_pattern): |
155 | 163 | 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 | + """ |
157 | 170 |
|
158 | | - for py_file in source_files: |
| 171 | + for py_file in self.iter_source_files(): |
159 | 172 | target = self.build_dir / py_file.relative_to(self.repo_dir) |
160 | 173 | target.parent.maybe_make(parents=True) |
161 | 174 | target.write_clean(py_file.read_text()) |
@@ -487,7 +500,7 @@ def get_record_entry(path: pathlib.Path, build_dir: pathlib.Path, blocksize: int |
487 | 500 | wheel_filename = self.out_dir / f"{self.archive_name}-{self.tag}.whl" |
488 | 501 | with ZipFile(wheel_filename, mode='w') as wheel_archive: |
489 | 502 | 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("*"): |
491 | 504 | if file.is_file(): |
492 | 505 | fp.write(get_record_entry(file, self.build_dir)) |
493 | 506 | fp.write("\n") |
@@ -535,7 +548,7 @@ def create_conda_archive(self, wheel_contents_dir: PathLike, build_number: int = |
535 | 548 | with tarfile.open(conda_filename, mode="w:bz2") as conda_archive: |
536 | 549 | with (self.info_dir / "files").open('w') as fp: |
537 | 550 |
|
538 | | - for file in (PathPlus(wheel_contents_dir) / self.import_name).rglob("*"): |
| 551 | + for file in (PathPlus(wheel_contents_dir) / self.pkg_dir).rglob("*"): |
539 | 552 | if file.is_file(): |
540 | 553 | filename = (site_packages / file.relative_to(wheel_contents_dir)).as_posix() |
541 | 554 | fp.write(f"{filename}\n") |
@@ -596,7 +609,7 @@ def build_wheel(self) -> str: |
596 | 609 | self.write_entry_points() |
597 | 610 | self.write_metadata(self.dist_info / "METADATA") |
598 | 611 | 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]) |
600 | 613 | self.report_written(self.dist_info / "top_level.txt") |
601 | 614 |
|
602 | 615 | return self.create_wheel_archive() |
|
0 commit comments