Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/python_inspector/resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,13 @@ async def fetch_and_extract_sdist(

def get_sdist_file_path_from_filename(sdist):
if sdist.endswith(".tar.gz"):
sdist_file = sdist.rstrip(".tar.gz")
sdist_file = sdist.removesuffix(".tar.gz")
with tarfile.open(os.path.join(settings.CACHE_THIRDPARTY_DIR, sdist)) as file:
file.extractall(
os.path.join(settings.CACHE_THIRDPARTY_DIR, "extracted_sdists", sdist_file)
)
elif sdist.endswith(".zip"):
sdist_file = sdist.rstrip(".zip")
sdist_file = sdist.removesuffix(".zip")
with ZipFile(os.path.join(settings.CACHE_THIRDPARTY_DIR, sdist)) as zip:
zip.extractall(
os.path.join(settings.CACHE_THIRDPARTY_DIR, "extracted_sdists", sdist_file)
Expand Down
30 changes: 30 additions & 0 deletions tests/test_sdist_directory_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) nexB Inc. and others. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
# See https://github.com/aboutcode-org/python-inspector for support or download.

import io
import tarfile
import zipfile
from pathlib import Path

import pytest

from python_inspector import resolution


@pytest.mark.parametrize("stem,extension", [("demo-1.0a", ".tar.gz"), ("demo-1.0+zip", ".zip")])
def test_sdist_extracted_directory_name(tmp_path, monkeypatch, stem, extension):
monkeypatch.setattr(resolution.settings, "CACHE_THIRDPARTY_DIR", str(tmp_path))
name = stem + extension
member = stem + "/setup.py"
if extension == ".tar.gz":
with tarfile.open(tmp_path / name, "w:gz") as archive:
info = tarfile.TarInfo(member)
info.size = 4
archive.addfile(info, io.BytesIO(b"pass"))
else:
with zipfile.ZipFile(tmp_path / name, "w") as archive:
archive.writestr(member, "pass")
result = Path(resolution.get_sdist_file_path_from_filename(name))
assert result.name == stem
assert (result / "setup.py").read_text() == "pass"
Loading