Skip to content

Commit 6f4313f

Browse files
authored
Refactor collect_package_data to make logic clearer (#120)
This is a preliminary refactoring for fixing python/typeshed#11254. The idea is that the new class `PackageData` encapsulates all data concerning packages and their contents. This allows us later to find the top-level non-namespace packages, instead of just the top-level packages as we are doing now.
1 parent c7e93a9 commit 6f4313f

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

stub_uploader/build_wheel.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -280,34 +280,40 @@ def collect_package_data(base_path: Path) -> PackageData:
280280
"""
281281
package_data: dict[str, list[str]] = {}
282282
for entry in base_path.iterdir():
283-
if entry.name == META:
284-
# Metadata file entry is added at the end.
283+
if is_ignored_distribution_file(entry):
285284
continue
286-
if entry.is_file():
287-
if entry.suffix != ".pyi":
288-
if entry.suffix not in (".md", ".rst"):
289-
if (
290-
subprocess.run(
291-
["git", "check-ignore", entry.name], cwd=str(base_path)
292-
).returncode
293-
!= 0
294-
):
295-
raise ValueError(
296-
f"Only stub files are allowed, not {entry.name!r}"
297-
)
298-
continue
299-
pkg_name = entry.stem + SUFFIX
285+
elif entry.is_file() and entry.suffix == ".pyi":
286+
pkg_name = entry.stem
300287
# Module -> package transformation is done while copying.
301-
package_data[pkg_name] = ["__init__.pyi"]
288+
stub_files = ["__init__.pyi"]
289+
elif entry.is_dir():
290+
pkg_name = entry.name
291+
stub_files = find_stub_files(str(entry))
302292
else:
303-
if entry.name == TESTS_NAMESPACE:
304-
continue
305-
pkg_name = entry.name + SUFFIX
306-
package_data[pkg_name] = find_stub_files(str(entry))
307-
package_data[pkg_name].append(META)
293+
raise ValueError(f"Only stub files are allowed, not {entry.name!r}")
294+
package_data[pkg_name + SUFFIX] = [*stub_files, META]
308295
return PackageData(base_path, package_data)
309296

310297

298+
def is_ignored_distribution_file(entry: Path) -> bool:
299+
if entry.is_file():
300+
if entry.name == META:
301+
return True
302+
if entry.suffix in (".md", ".rst"):
303+
return True
304+
if (
305+
subprocess.run(
306+
["git", "check-ignore", entry.name], cwd=str(entry.parent)
307+
).returncode
308+
== 0
309+
):
310+
return True
311+
elif entry.is_dir():
312+
if entry.name == TESTS_NAMESPACE:
313+
return True
314+
return False
315+
316+
311317
def add_partial_markers(pkg_data: PackageData) -> None:
312318
for package in pkg_data.top_level_non_namespace_packages:
313319
pkg_data.add_file(package, "py.typed", "partial\n")

0 commit comments

Comments
 (0)