Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor collect_package_data to make logic clearer #120

Merged
merged 7 commits into from
Jan 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions stub_uploader/build_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,34 +228,40 @@ def collect_package_data(base_path: Path) -> PackageData:
"""
package_data: dict[str, list[str]] = {}
for entry in base_path.iterdir():
if entry.name == META:
# Metadata file entry is added at the end.
if is_ignored_distribution_file(entry):
continue
if entry.is_file():
if entry.suffix != ".pyi":
if entry.suffix not in (".md", ".rst"):
if (
subprocess.run(
["git", "check-ignore", entry.name], cwd=str(base_path)
).returncode
!= 0
):
raise ValueError(
f"Only stub files are allowed, not {entry.name!r}"
)
continue
pkg_name = entry.stem + SUFFIX
elif entry.is_file() and entry.suffix == ".pyi":
pkg_name = entry.stem
# Module -> package transformation is done while copying.
package_data[pkg_name] = ["__init__.pyi"]
stub_files = ["__init__.pyi"]
elif entry.is_dir():
pkg_name = entry.name
stub_files = find_stub_files(str(entry))
else:
if entry.name == TESTS_NAMESPACE:
continue
pkg_name = entry.name + SUFFIX
package_data[pkg_name] = find_stub_files(str(entry))
package_data[pkg_name].append(META)
raise ValueError(f"Only stub files are allowed, not {entry.name!r}")
package_data[pkg_name + SUFFIX] = [*stub_files, META]
Comment on lines +233 to +242
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you won't wanna do that now, but since you're touching this area of code I'll mention it:
With #112 (setuptools v69) , the stub_files logic could become unnecessary.

According to https://setuptools.pypa.io/en/stable/userguide/miscellaneous.html#controlling-files-in-the-distribution this is an opt-out feature, so you shouldn't need to add anything new either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's punt this to a separate PR to keep this as a simple refactoring.

return PackageData(base_path, package_data)


def is_ignored_distribution_file(entry: Path) -> bool:
if entry.is_file():
if entry.name == META:
return True
if entry.suffix in (".md", ".rst"):
return True
if (
subprocess.run(
["git", "check-ignore", entry.name], cwd=str(entry.parent)
).returncode
== 0
):
return True
elif entry.is_dir():
if entry.name == TESTS_NAMESPACE:
return True
return False


def add_partial_markers(pkg_data: PackageData) -> None:
for package in pkg_data.top_level_packages:
pkg_data.add_file(package, "py.typed", "partial\n")
Expand Down