Skip to content

Commit

Permalink
Merge pull request #1 from wheel-next/sdist-dir
Browse files Browse the repository at this point in the history
Generate sdists with top level directory
  • Loading branch information
ethanhs-nv authored Nov 20, 2024
2 parents af1460a + 58d5b49 commit 679a570
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ dependencies = [
]

[tool.hatch.envs.default.scripts]
test = "pytest -n auto --dist=loadgroup {args:tests}"
test = "pytest -n auto --ignore=demo --dist=loadgroup {args:tests}"
fmt = "pre-commit run --all-files"

[[tool.hatch.envs.all.matrix]]
Expand Down
30 changes: 21 additions & 9 deletions src/wheel_stub/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def normalize_tarinfo(tarinfo, mtime):
tarinfo.uname = ""
tarinfo.gname = ""
tarinfo.mtime = mtime
if tarinfo.mode & 0o100:
if tarinfo.type == tarfile.DIRTYPE:
tarinfo.mode = 0o775
elif tarinfo.mode & 0o100:
tarinfo.mode = 0o755
else:
tarinfo.mode = 0o644
Expand Down Expand Up @@ -101,11 +103,9 @@ def build(self):
) as metadata_file:
metadata_bytes = metadata_file.read()
else:
with tarfile.open(self.source_wheel, mode="r:gz") as t:
with t.extractfile(
f"{self.distribution}-{self.version}/PKG-INFO"
) as tf:
metadata_bytes = tf.read()
raise RuntimeError(
f"Unsupported package format for file {self.source_wheel}"
)

# Verify METADATA is valid
parsed_metadata = parse_metadata(metadata_bytes.decode("utf-8"))
Expand All @@ -127,8 +127,14 @@ def build(self):
del parsed_metadata["Platform"]
del parsed_metadata["Supported-Platform"]
pkg_info_bytes = str(parsed_metadata).encode("utf-8")
# Read size, then seek to start for the tar file to read the actual data
pkg_info_tarinfo = tarfile.TarInfo("PKG-INFO")

sdist_dir = f"{self.distribution}-{self.version}"
sdist_dir_tarinfo = tarfile.TarInfo(sdist_dir)
sdist_dir_tarinfo.type = tarfile.DIRTYPE
normalize_tarinfo(sdist_dir_tarinfo, mtime)
tar.addfile(sdist_dir_tarinfo)

pkg_info_tarinfo = tarfile.TarInfo(os.path.join(sdist_dir, "PKG-INFO"))
pkg_info_tarinfo.size = len(pkg_info_bytes)
pkg_info_tarinfo = normalize_tarinfo(pkg_info_tarinfo, mtime)
pkg_info_fp = BytesIO(pkg_info_bytes)
Expand All @@ -141,6 +147,10 @@ def build(self):
pyproj_tarinfo = tar.gettarinfo(
name=pyproject_toml_path, arcname="pyproject.toml"
)
# Do not do a deep copy otherwise the file pointer will raise an error
pyproj_tarinfo = pyproj_tarinfo.replace(
name=os.path.join(sdist_dir, "pyproject.toml"), deep=False
)
pyproj_tarinfo = normalize_tarinfo(pyproj_tarinfo, mtime)
with open(pyproject_toml_path, "rb") as f:
toml_dict = tomllib.load(f)
Expand All @@ -166,7 +176,9 @@ def build(self):
content = f.read()
# toml settings can exist in their own table
content += b"\n[tool.wheel_stub]\nstub_only = true\n"
pyproj_tarinfo = tarfile.TarInfo("pyproject.toml")
pyproj_tarinfo = tarfile.TarInfo(
os.path.join(sdist_dir, "pyproject.toml")
)
pyproj_tarinfo.size = len(content)
pyproj_tarinfo = normalize_tarinfo(pyproj_tarinfo, mtime)
output = io.BytesIO(content)
Expand Down
10 changes: 5 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
"""


@pytest.fixture(scope="session")
def docker_compose_command() -> str:
return "docker-compose"


@pytest.fixture(scope="session")
def build_backend_wheel():
root_dir = BASE_DIR.parent
Expand All @@ -66,14 +71,9 @@ def install_in_pypi(build_backend_wheel):
pypi_simple = BASE_DIR / "pypi_simple" / "root"
pkg_dir = pypi_simple / "wheel-stub"
pkg_dir.mkdir(exist_ok=True)
os.chown(pkg_dir, 1000, 999)
shutil.copy(build_backend_wheel, pkg_dir)
os.chown(pkg_dir / build_backend_wheel.name, 1000, 999)
os.chmod(pkg_dir / build_backend_wheel.name, 644)
with open(pkg_dir / "index.html", "w") as f:
f.write(INDEX.format(build_backend_wheel.name))
os.chown(pkg_dir / "index.html", 1000, 999)
os.chmod(pkg_dir / "index.html", 644)
yield True
shutil.rmtree(pkg_dir, ignore_errors=True)

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 679a570

Please sign in to comment.