Skip to content

Commit

Permalink
use requests.utils.atomic_open directly
Browse files Browse the repository at this point in the history
poetry is already using requests, and the docstring for requests.utils
indicates that "this module provides utility functions ... that are also
useful for external consumption".

ie there's no reason copy the code, just use it.
  • Loading branch information
dimbleby committed Jul 29, 2023
1 parent bfe45a4 commit 6a87dc2
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/poetry/installation/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from cleo.io.null_io import NullIO
from poetry.core.packages.utils.link import Link
from requests.utils import atomic_open

from poetry.installation.chef import Chef
from poetry.installation.chef import ChefBuildError
Expand All @@ -29,7 +30,6 @@
from poetry.utils.authenticator import Authenticator
from poetry.utils.cache import ArtifactCache
from poetry.utils.env import EnvCommandError
from poetry.utils.helpers import atomic_open
from poetry.utils.helpers import get_file_hash
from poetry.utils.helpers import pluralize
from poetry.utils.helpers import remove_directory
Expand Down
21 changes: 2 additions & 19 deletions src/poetry/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
from typing import TYPE_CHECKING
from typing import Any

from requests.utils import atomic_open

from poetry.utils.constants import REQUESTS_TIMEOUT


if TYPE_CHECKING:
from collections.abc import Callable
from collections.abc import Iterator
from io import BufferedWriter

from poetry.core.packages.package import Package
from requests import Session
Expand All @@ -38,24 +39,6 @@ def directory(path: Path) -> Iterator[Path]:
os.chdir(cwd)


@contextmanager
def atomic_open(filename: str | os.PathLike[str]) -> Iterator[BufferedWriter]:
"""
write a file to the disk in an atomic fashion
Taken from requests.utils
(https://github.com/psf/requests/blob/7104ad4b135daab0ed19d8e41bd469874702342b/requests/utils.py#L296)
"""
tmp_descriptor, tmp_name = tempfile.mkstemp(dir=os.path.dirname(filename))
try:
with os.fdopen(tmp_descriptor, "wb") as tmp_handler:
yield tmp_handler
os.replace(tmp_name, filename)
except BaseException:
os.remove(tmp_name)
raise


def _on_rm_error(func: Callable[[str], None], path: str, exc_info: Exception) -> None:
if not os.path.exists(path):
return
Expand Down

0 comments on commit 6a87dc2

Please sign in to comment.