From 7f9e41ce2e9da4c712eba469a0f4407afbe3583f Mon Sep 17 00:00:00 2001 From: royjr Date: Sun, 17 Dec 2023 00:58:19 -0500 Subject: [PATCH] dependency: remove atomicwrites package (#30769) * Update file_helpers.py * remove dependency * Revert "remove dependency" This reverts commit e2bd5f4edab7d14b75f65c296631855d886e38d1. * remove atomicwrites * allow overwrite * fix indents * add type hints * Update poetry.lock * optional * wrap --- common/file_helpers.py | 41 ++++++++++++++++++++++++----------------- poetry.lock | 23 +---------------------- pyproject.toml | 2 -- 3 files changed, 25 insertions(+), 41 deletions(-) diff --git a/common/file_helpers.py b/common/file_helpers.py index a29eafdd9f..7524860f96 100644 --- a/common/file_helpers.py +++ b/common/file_helpers.py @@ -1,7 +1,8 @@ import os import shutil import tempfile -from atomicwrites import AtomicWriter +import contextlib +from typing import Optional def rm_not_exists_ok(path): @@ -71,19 +72,25 @@ def _get_fileobject_func(writer, temp_dir): return writer.get_fileobject(dir=temp_dir) return _get_fileobject -def atomic_write_on_fs_tmp(path, **kwargs): - """Creates an atomic writer using a temporary file in a temporary directory - on the same filesystem as path. - """ - # TODO(mgraczyk): This use of AtomicWriter relies on implementation details to set the temp - # directory. - writer = AtomicWriter(path, **kwargs) - return writer._open(_get_fileobject_func(writer, get_tmpdir_on_same_filesystem(path))) - - -def atomic_write_in_dir(path, **kwargs): - """Creates an atomic writer using a temporary file in the same directory - as the destination file. - """ - writer = AtomicWriter(path, **kwargs) - return writer._open(_get_fileobject_func(writer, os.path.dirname(path))) +@contextlib.contextmanager +def atomic_write_on_fs_tmp(path: str, mode: str = 'w', buffering: int = -1, encoding: Optional[str] = None, newline: Optional[str] = None): + """Write to a file atomically using a temporary file in a temporary directory on the same filesystem as path.""" + temp_dir = get_tmpdir_on_same_filesystem(path) + with tempfile.NamedTemporaryFile(mode=mode, buffering=buffering, encoding=encoding, newline=newline, dir=temp_dir, delete=False) as tmp_file: + yield tmp_file + tmp_file_name = tmp_file.name + os.replace(tmp_file_name, path) + +@contextlib.contextmanager +def atomic_write_in_dir(path: str, mode: str = 'w', buffering: int = -1, encoding: Optional[str] = None, newline: Optional[str] = None, + overwrite: bool = False): + """Write to a file atomically using a temporary file in the same directory as the destination file.""" + dir_name = os.path.dirname(path) + + if not overwrite and os.path.exists(path): + raise FileExistsError(f"File '{path}' already exists. To overwrite it, set 'overwrite' to True.") + + with tempfile.NamedTemporaryFile(mode=mode, buffering=buffering, encoding=encoding, newline=newline, dir=dir_name, delete=False) as tmp_file: + yield tmp_file + tmp_file_name = tmp_file.name + os.replace(tmp_file_name, path) diff --git a/poetry.lock b/poetry.lock index c1f9bd890f..3ad023cdd7 100644 --- a/poetry.lock +++ b/poetry.lock @@ -180,16 +180,6 @@ files = [ {file = "alabaster-0.7.13.tar.gz", hash = "sha256:a27a4a084d5e690e16e01e03ad2b2e552c61a65469419b907243193de1a84ae2"}, ] -[[package]] -name = "atomicwrites" -version = "1.4.1" -description = "Atomic file writes." -optional = false -python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -files = [ - {file = "atomicwrites-1.4.1.tar.gz", hash = "sha256:81b2c9071a49367a7f770170e5eec8cb66567cfbbc8c73d20ce5ca4a8d71cf11"}, -] - [[package]] name = "attrs" version = "23.1.0" @@ -4665,17 +4655,6 @@ notebook = ["ipywidgets (>=6)"] slack = ["slack-sdk"] telegram = ["requests"] -[[package]] -name = "types-atomicwrites" -version = "1.4.5.1" -description = "Typing stubs for atomicwrites" -optional = false -python-versions = "*" -files = [ - {file = "types-atomicwrites-1.4.5.1.tar.gz", hash = "sha256:9e9f0923ebf93524b28bcece5a23ac8c3820f39b060df29f671936d2e4bc04bc"}, - {file = "types_atomicwrites-1.4.5.1-py3-none-any.whl", hash = "sha256:2f1febbdc78b55453b189fa5b136dce34bab7d1d82319163d470e404aab55c83"}, -] - [[package]] name = "types-pycurl" version = "7.45.2.5" @@ -4923,4 +4902,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = "~3.11" -content-hash = "68be4dd5356e38c1e5a004a6e40b4fef963aacd1cad902b5fba70c51c442d981" +content-hash = "eeb5efb6161eaec95fb863ddc9ec08afb540ed3cd5d521646a6a759ddf33ab2e" diff --git a/pyproject.toml b/pyproject.toml index 336c440f6b..98091e0088 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -108,7 +108,6 @@ websocket_client = "*" polyline = "*" # these should be removed -atomicwrites = "*" markdown-it-py = "*" timezonefinder = "*" pycurl = "*" @@ -154,7 +153,6 @@ sphinx-rtd-theme = "*" sphinx-sitemap = "*" tabulate = "*" tenacity = "*" -types-atomicwrites = "*" types-pycurl = "*" types-requests = "*" types-tabulate = "*"