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

use tempfile.TemporaryDirectory #337

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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/poetry/core/masonry/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from base64 import urlsafe_b64encode
from io import StringIO
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING
from typing import TextIO

Expand All @@ -28,7 +29,6 @@
from poetry.core.masonry.utils.helpers import distribution_name
from poetry.core.masonry.utils.helpers import normalize_file_permissions
from poetry.core.masonry.utils.package_include import PackageInclude
from poetry.core.utils.helpers import temporary_directory


if TYPE_CHECKING:
Expand Down Expand Up @@ -126,7 +126,7 @@ def build(
self._copy_file_scripts(zip_file)

if self._metadata_directory is None:
with temporary_directory() as temp_dir:
with TemporaryDirectory(ignore_cleanup_errors=True) as temp_dir:
metadata_directory = self.prepare_metadata(Path(temp_dir))
self._copy_dist_info(zip_file, metadata_directory)
else:
Expand Down
62 changes: 0 additions & 62 deletions src/poetry/core/utils/helpers.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
from __future__ import annotations

import os
import shutil
import stat
import sys
import tempfile
import time
import unicodedata

from contextlib import contextmanager
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any

from packaging.utils import canonicalize_name


if TYPE_CHECKING:
from collections.abc import Iterator


def combine_unicode(string: str) -> str:
return unicodedata.normalize("NFC", string)

Expand All @@ -28,20 +15,6 @@ def module_name(name: str) -> str:
return canonicalize_name(name).replace("-", "_")


@contextmanager
def temporary_directory(*args: Any, **kwargs: Any) -> Iterator[str]:
if sys.version_info >= (3, 10):
# mypy reports an error if ignore_cleanup_errors is
# specified literally in the call
kwargs["ignore_cleanup_errors"] = True
with tempfile.TemporaryDirectory(*args, **kwargs) as name:
yield name
else:
name = tempfile.mkdtemp(*args, **kwargs)
yield name
robust_rmtree(name)


def parse_requires(requires: str) -> list[str]:
lines = requires.split("\n")

Expand Down Expand Up @@ -79,41 +52,6 @@ def parse_requires(requires: str) -> list[str]:
return requires_dist


def _on_rm_error(func: Any, path: str | Path, exc_info: Any) -> None:
if not os.path.exists(path):
return

os.chmod(path, stat.S_IWRITE)
func(path)


def robust_rmtree(path: str | Path, max_timeout: float = 1) -> None:
"""
Robustly tries to delete paths.
Retries several times if an OSError occurs.
If the final attempt fails, the Exception is propagated
to the caller.
"""
path = Path(path) # make sure this is a Path object, not str
timeout = 0.001
while timeout < max_timeout:
try:
# both os.unlink and shutil.rmtree can throw exceptions on Windows
# if the files are in use when called
if path.is_symlink():
path.unlink()
else:
shutil.rmtree(path)
return # Only hits this on success
except OSError:
# Increase the timeout and try again
time.sleep(timeout)
timeout *= 2

# Final attempt, pass any Exceptions up to caller.
shutil.rmtree(path, onerror=_on_rm_error)


def readme_content_type(path: str | Path) -> str:
suffix = Path(path).suffix
if suffix == ".rst":
Expand Down
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ def masonry_project(

@pytest.fixture
def temporary_directory() -> Iterator[Path]:
with tempfile.TemporaryDirectory(prefix="poetry-core") as tmp:
with tempfile.TemporaryDirectory(
prefix="poetry-core", ignore_cleanup_errors=True
) as tmp:
yield Path(tmp)


Expand Down
52 changes: 34 additions & 18 deletions tests/masonry/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

from contextlib import contextmanager
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING
from typing import Iterator

import pytest

from poetry.core import __version__
from poetry.core.masonry import api
from poetry.core.utils.helpers import temporary_directory
from tests.testutils import validate_sdist_contents
from tests.testutils import validate_wheel_contents

Expand Down Expand Up @@ -48,7 +48,9 @@ def test_get_requires_for_build_sdist() -> None:

@pytest.mark.filterwarnings("ignore:.* script .* extra:DeprecationWarning")
def test_build_wheel() -> None:
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "complete")):
with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir, cwd(
os.path.join(fixtures, "complete")
):
filename = api.build_wheel(tmp_dir)
validate_wheel_contents(
name="my_package",
Expand All @@ -59,7 +61,9 @@ def test_build_wheel() -> None:


def test_build_wheel_with_include() -> None:
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "with-include")):
with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir, cwd(
os.path.join(fixtures, "with-include")
):
filename = api.build_wheel(tmp_dir)
validate_wheel_contents(
name="with_include",
Expand All @@ -70,14 +74,14 @@ def test_build_wheel_with_include() -> None:


def test_build_wheel_with_bad_path_dev_dep_succeeds() -> None:
with temporary_directory() as tmp_dir, cwd(
with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir, cwd(
os.path.join(fixtures, "with_bad_path_dev_dep")
):
api.build_wheel(tmp_dir)


def test_build_wheel_with_bad_path_dep_succeeds(caplog: LogCaptureFixture) -> None:
with temporary_directory() as tmp_dir, cwd(
with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir, cwd(
os.path.join(fixtures, "with_bad_path_dep")
):
api.build_wheel(tmp_dir)
Expand All @@ -88,15 +92,19 @@ def test_build_wheel_with_bad_path_dep_succeeds(caplog: LogCaptureFixture) -> No


def test_build_wheel_extended() -> None:
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "extended")):
with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir, cwd(
os.path.join(fixtures, "extended")
):
filename = api.build_wheel(tmp_dir)
whl = Path(tmp_dir) / filename
assert whl.exists()
validate_wheel_contents(name="extended", version="0.1", path=whl.as_posix())


def test_build_sdist() -> None:
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "complete")):
with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir, cwd(
os.path.join(fixtures, "complete")
):
filename = api.build_sdist(tmp_dir)
validate_sdist_contents(
name="my-package",
Expand All @@ -107,7 +115,9 @@ def test_build_sdist() -> None:


def test_build_sdist_with_include() -> None:
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "with-include")):
with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir, cwd(
os.path.join(fixtures, "with-include")
):
filename = api.build_sdist(tmp_dir)
validate_sdist_contents(
name="with-include",
Expand All @@ -118,14 +128,14 @@ def test_build_sdist_with_include() -> None:


def test_build_sdist_with_bad_path_dev_dep_succeeds() -> None:
with temporary_directory() as tmp_dir, cwd(
with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir, cwd(
os.path.join(fixtures, "with_bad_path_dev_dep")
):
api.build_sdist(tmp_dir)


def test_build_sdist_with_bad_path_dep_succeeds(caplog: LogCaptureFixture) -> None:
with temporary_directory() as tmp_dir, cwd(
with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir, cwd(
os.path.join(fixtures, "with_bad_path_dep")
):
api.build_sdist(tmp_dir)
Expand Down Expand Up @@ -187,7 +197,9 @@ def test_prepare_metadata_for_build_wheel() -> None:
==========

"""
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "complete")):
with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir, cwd(
os.path.join(fixtures, "complete")
):
dirname = api.prepare_metadata_for_build_wheel(tmp_dir)

assert dirname == "my_package-1.2.3.dist-info"
Expand All @@ -209,7 +221,7 @@ def test_prepare_metadata_for_build_wheel() -> None:


def test_prepare_metadata_for_build_wheel_with_bad_path_dev_dep_succeeds() -> None:
with temporary_directory() as tmp_dir, cwd(
with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir, cwd(
os.path.join(fixtures, "with_bad_path_dev_dep")
):
api.prepare_metadata_for_build_wheel(tmp_dir)
Expand All @@ -218,7 +230,7 @@ def test_prepare_metadata_for_build_wheel_with_bad_path_dev_dep_succeeds() -> No
def test_prepare_metadata_for_build_wheel_with_bad_path_dep_succeeds(
caplog: LogCaptureFixture,
) -> None:
with temporary_directory() as tmp_dir, cwd(
with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir, cwd(
os.path.join(fixtures, "with_bad_path_dep")
):
api.prepare_metadata_for_build_wheel(tmp_dir)
Expand All @@ -232,7 +244,7 @@ def test_prepare_metadata_for_build_wheel_with_bad_path_dep_succeeds(
def test_build_editable_wheel() -> None:
pkg_dir = Path(fixtures) / "complete"

with temporary_directory() as tmp_dir, cwd(pkg_dir):
with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir, cwd(pkg_dir):
filename = api.build_editable(tmp_dir)
wheel_pth = Path(tmp_dir) / filename

Expand All @@ -253,10 +265,12 @@ def test_build_editable_wheel() -> None:
def test_build_wheel_with_metadata_directory() -> None:
pkg_dir = Path(fixtures) / "complete"

with temporary_directory() as metadata_tmp_dir, cwd(pkg_dir):
with TemporaryDirectory(ignore_cleanup_errors=True) as metadata_tmp_dir, cwd(
pkg_dir
):
metadata_directory = api.prepare_metadata_for_build_wheel(metadata_tmp_dir)

with temporary_directory() as wheel_tmp_dir:
with TemporaryDirectory(ignore_cleanup_errors=True) as wheel_tmp_dir:
dist_info_path = Path(metadata_tmp_dir) / metadata_directory
(dist_info_path / "CUSTOM").touch()
filename = api.build_wheel(
Expand All @@ -281,10 +295,12 @@ def test_build_wheel_with_metadata_directory() -> None:
def test_build_editable_wheel_with_metadata_directory() -> None:
pkg_dir = Path(fixtures) / "complete"

with temporary_directory() as metadata_tmp_dir, cwd(pkg_dir):
with TemporaryDirectory(ignore_cleanup_errors=True) as metadata_tmp_dir, cwd(
pkg_dir
):
metadata_directory = api.prepare_metadata_for_build_editable(metadata_tmp_dir)

with temporary_directory() as wheel_tmp_dir:
with TemporaryDirectory(ignore_cleanup_errors=True) as wheel_tmp_dir:
dist_info_path = Path(metadata_tmp_dir) / metadata_directory
(dist_info_path / "CUSTOM").touch()
filename = api.build_editable(
Expand Down
4 changes: 3 additions & 1 deletion tests/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def temporary_project_directory(
"""
assert (path / "pyproject.toml").exists()

with tempfile.TemporaryDirectory(prefix="poetry-core-pep517") as tmp:
with tempfile.TemporaryDirectory(
prefix="poetry-core-pep517", ignore_cleanup_errors=True
) as tmp:
dst = Path(tmp) / path.name
shutil.copytree(str(path), dst)
toml = dst / "pyproject.toml"
Expand Down
Loading
Loading