Skip to content

Commit 5c89da6

Browse files
committed
remove delete_removed_files ?
1 parent 1c33abe commit 5c89da6

File tree

3 files changed

+18
-55
lines changed

3 files changed

+18
-55
lines changed

src/monty/tempfile.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(
5151
copy_from_current_on_enter: bool = False,
5252
copy_to_current_on_exit: bool = False,
5353
gzip_on_exit: bool = False,
54-
delete_removed_files: bool = False,
54+
delete_removed_files: bool | None = None,
5555
) -> None:
5656
"""
5757
Initializes scratch directory given a **root** path. There is no need
@@ -83,10 +83,15 @@ def __init__(
8383
gzip_on_exit (bool): Whether to gzip the files generated in the
8484
ScratchDir before copying them back.
8585
Defaults to False.
86-
delete_removed_files (bool): Whether to delete files in the cwd
87-
that are not present in the tmp dir. WARNING: this could
88-
wipe your CWD. Defaults to False..
86+
delete_removed_files (bool): Deprecated, don't use.
8987
"""
88+
if delete_removed_files is not None:
89+
warnings.warn(
90+
"delete_removed_files is deprecated and would have no effect",
91+
DeprecationWarning,
92+
stacklevel=2,
93+
)
94+
9095
self.cwd: str = os.getcwd()
9196
self.rootpath: str | None = (
9297
None if rootpath is None else os.path.abspath(rootpath)
@@ -105,7 +110,6 @@ def __init__(
105110
self.enter_copy: bool = copy_from_current_on_enter
106111
self.exit_copy: bool = copy_to_current_on_exit
107112
self.gzip_on_exit: bool = gzip_on_exit
108-
self.delete_removed_files: bool = delete_removed_files
109113

110114
def __enter__(self) -> str:
111115
tempdir: str = self.cwd
@@ -122,9 +126,6 @@ def __enter__(self) -> str:
122126
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
123127
if not self.pass_through:
124128
if self.exit_copy:
125-
files = set(os.listdir(self.tempdir))
126-
orig_files = set(os.listdir(self.cwd))
127-
128129
# gzip files
129130
if self.gzip_on_exit:
130131
gzip_dir(self.tempdir)
@@ -175,12 +176,6 @@ def get_modif_times(
175176
# copy files over
176177
shutil.copytree(self.tempdir, self.cwd, dirs_exist_ok=True)
177178

178-
# Delete any files that are now gone
179-
if self.delete_removed_files:
180-
for f in orig_files - files:
181-
fpath = os.path.join(self.cwd, f)
182-
remove(fpath)
183-
184179
os.chdir(self.cwd)
185180
remove(self.tempdir)
186181
if self.create_symbolic_link and os.path.islink(ScratchDir.SCR_LINK):

tests/test_tempfile.py

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,10 @@ def test_with_copy(self):
5454
assert not os.path.exists(d)
5555
files = os.listdir(".")
5656
assert "scratch_text" in files
57-
58-
# We check that the pre-scratch file no longer exists (because it is
59-
# deleted in the scratch)
60-
assert "pre_scratch_text" not in files
6157
os.remove("scratch_text")
6258

59+
os.remove("pre_scratch_text")
60+
6361
def test_with_copy_gzip(self):
6462
# We write a pre-scratch file.
6563
with open("pre_scratch_text", "w", encoding="utf-8") as f:
@@ -84,37 +82,7 @@ def test_with_copy_gzip(self):
8482
if f.endswith(".gz") and f not in init_gz_files:
8583
os.remove(f)
8684
os.remove("pre_scratch_text")
87-
88-
def test_with_copy_nodelete(self):
89-
# We write a pre-scratch file.
90-
with open("pre_scratch_text", "w", encoding="utf-8") as f:
91-
f.write("write")
92-
93-
with ScratchDir(
94-
self.scratch_root,
95-
copy_from_current_on_enter=True,
96-
copy_to_current_on_exit=True,
97-
delete_removed_files=False,
98-
) as d:
99-
with open("scratch_text", "w", encoding="utf-8") as f:
100-
f.write("write")
101-
files = os.listdir(d)
102-
assert "scratch_text" in files
103-
assert "empty_file.txt" in files
104-
assert "pre_scratch_text" in files
105-
106-
# We remove the pre-scratch file.
107-
os.remove("pre_scratch_text")
108-
109-
# Make sure the tempdir is deleted.
110-
assert not os.path.exists(d)
111-
files = os.listdir(".")
112-
assert "scratch_text" in files
113-
114-
# We check that the pre-scratch file DOES still exists
115-
assert "pre_scratch_text" in files
11685
os.remove("scratch_text")
117-
os.remove("pre_scratch_text")
11886

11987
def test_no_copy(self):
12088
with ScratchDir(

uv.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)