Skip to content

Commit d813daa

Browse files
committed
remove delete_removed_files
remove delete_removed_files ? remove `delete_removed_files` directly
1 parent 0e60eb8 commit d813daa

File tree

2 files changed

+3
-48
lines changed

2 files changed

+3
-48
lines changed

src/monty/tempfile.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import shutil
99
import tempfile
1010
import warnings
11+
from pathlib import Path
1112
from typing import TYPE_CHECKING
1213

1314
from monty.shutil import gzip_dir, remove
@@ -51,7 +52,6 @@ def __init__(
5152
copy_from_current_on_enter: bool = False,
5253
copy_to_current_on_exit: bool = False,
5354
gzip_on_exit: bool = False,
54-
delete_removed_files: bool = True,
5555
) -> None:
5656
"""
5757
Initializes scratch directory given a **root** path. There is no need
@@ -83,9 +83,6 @@ 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 removed from the tmp dir.
88-
Defaults to True.
8986
"""
9087
self.cwd: str = os.getcwd()
9188
self.rootpath: str | None = (
@@ -105,7 +102,6 @@ def __init__(
105102
self.enter_copy: bool = copy_from_current_on_enter
106103
self.exit_copy: bool = copy_to_current_on_exit
107104
self.gzip_on_exit: bool = gzip_on_exit
108-
self.delete_removed_files: bool = delete_removed_files
109105

110106
def __enter__(self) -> str:
111107
tempdir: str = self.cwd
@@ -122,9 +118,6 @@ def __enter__(self) -> str:
122118
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
123119
if not self.pass_through:
124120
if self.exit_copy:
125-
files = set(os.listdir(self.tempdir))
126-
orig_files = set(os.listdir(self.cwd))
127-
128121
# gzip files
129122
if self.gzip_on_exit:
130123
gzip_dir(self.tempdir)
@@ -175,12 +168,6 @@ def get_modif_times(
175168
# copy files over
176169
shutil.copytree(self.tempdir, self.cwd, dirs_exist_ok=True)
177170

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-
184171
os.chdir(self.cwd)
185172
remove(self.tempdir)
186173
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(

0 commit comments

Comments
 (0)