Skip to content

Commit 0a577da

Browse files
committed
Enhance sdist section handling in build configuration
This commit improves the logic for managing the sdist section in the build configuration by checking for the existence of the section and the 'only-include' setting before appending them. This ensures that the configuration is not duplicated and maintains clarity in the generated output, particularly for subfolder builds.
1 parent aecd567 commit 0a577da

1 file changed

Lines changed: 33 additions & 12 deletions

File tree

src/python_package_folder/subfolder_build.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -742,18 +742,39 @@ def _modify_pyproject_string(
742742
# Use only-include for source distributions to ensure only the subfolder is included
743743
# This prevents including files from the project root
744744
if package_dirs:
745-
result.append("")
746-
result.append("[tool.hatch.build.targets.sdist]")
747-
# Include only the subfolder directory and necessary files
748-
only_include_paths = [package_dirs[0]]
749-
# Also include pyproject.toml and README if they exist
750-
only_include_paths.append("pyproject.toml")
751-
only_include_paths.append("README.md")
752-
only_include_paths.append("README.rst")
753-
only_include_paths.append("README.txt")
754-
only_include_paths.append("README")
755-
only_include_str = ", ".join(f'"{p}"' for p in only_include_paths)
756-
result.append(f"only-include = [{only_include_str}]")
745+
# Check if sdist section already exists
746+
sdist_section_exists = any(
747+
line.strip().startswith("[tool.hatch.build.targets.sdist]")
748+
for line in result
749+
)
750+
# Check if only-include is already set in the sdist section
751+
only_include_set = False
752+
if sdist_section_exists:
753+
in_sdist_section = False
754+
for line in result:
755+
if line.strip().startswith("[tool.hatch.build.targets.sdist]"):
756+
in_sdist_section = True
757+
elif line.strip().startswith("[") and in_sdist_section:
758+
in_sdist_section = False
759+
elif in_sdist_section and re.match(r"^\s*only-include\s*=", line):
760+
only_include_set = True
761+
break
762+
763+
if not sdist_section_exists or not only_include_set:
764+
if not sdist_section_exists:
765+
result.append("")
766+
result.append("[tool.hatch.build.targets.sdist]")
767+
# Include only the subfolder directory and necessary files
768+
only_include_paths = [package_dirs[0]]
769+
# Also include pyproject.toml and README if they exist
770+
only_include_paths.append("pyproject.toml")
771+
only_include_paths.append("README.md")
772+
only_include_paths.append("README.rst")
773+
only_include_paths.append("README.txt")
774+
only_include_paths.append("README")
775+
only_include_str = ", ".join(f'"{p}"' for p in only_include_paths)
776+
if not only_include_set:
777+
result.append(f"only-include = [{only_include_str}]")
757778

758779
# Add dependency group if specified
759780
if dependency_group:

0 commit comments

Comments
 (0)