Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitac89cd8

Browse files
os_ops::rmdirs (local, remote) was refactored
LocalOperations::rmdirs - parameter 'retries' was remaned with 'attempts' - if ignore_errors we raise an errorRemoteOperations::rmdirs - parameter 'verbose' was removed - method returns bool - we prevent to delete a file
1 parente1a5bb4 commitac89cd8

File tree

3 files changed

+93
-30
lines changed

3 files changed

+93
-30
lines changed

‎testgres/operations/local_ops.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ def makedirs(self, path, remove_existing=False):
174174
exceptFileExistsError:
175175
pass
176176

177-
defrmdirs(self,path,ignore_errors=True,retries=3,delay=1):
177+
# [2025-02-03] Old name of parameter attempts is "retries".
178+
defrmdirs(self,path,ignore_errors=True,attempts=3,delay=1):
178179
"""
179180
Removes a directory and its contents, retrying on failure.
180181
@@ -183,18 +184,38 @@ def rmdirs(self, path, ignore_errors=True, retries=3, delay=1):
183184
:param retries: Number of attempts to remove the directory.
184185
:param delay: Delay between attempts in seconds.
185186
"""
186-
forattemptinrange(retries):
187+
asserttype(path)==str# noqa: E721
188+
asserttype(ignore_errors)==bool# noqa: E721
189+
asserttype(attempts)==int# noqa: E721
190+
asserttype(delay)==intortype(delay)==float# noqa: E721
191+
assertattempts>0
192+
assertdelay>=0
193+
194+
attempt=0
195+
whileTrue:
196+
assertattempt<attempts
197+
attempt+=1
187198
try:
188-
rmtree(path,ignore_errors=ignore_errors)
189-
ifnotos.path.exists(path):
190-
returnTrue
199+
rmtree(path)
191200
exceptFileNotFoundError:
192-
returnTrue
201+
pass
193202
exceptExceptionase:
194-
logging.error(f"Error: Failed to remove directory{path} on attempt{attempt+1}:{e}")
195-
time.sleep(delay)
196-
logging.error(f"Error: Failed to remove directory{path} after{retries} attempts.")
197-
returnFalse
203+
ifattempt<attempt:
204+
errMsg="Failed to remove directory {0} on attempt {1} ({2}): {3}".format(
205+
path,attempt,type(e).__name__,e
206+
)
207+
logging.warning(errMsg)
208+
time.sleep(delay)
209+
continue
210+
211+
assertattempt==attempts
212+
ifnotignore_errors:
213+
raise
214+
215+
returnFalse
216+
217+
# OK!
218+
returnTrue
198219

199220
deflistdir(self,path):
200221
returnos.listdir(path)

‎testgres/operations/remote_ops.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
importsubprocess
55
importtempfile
66
importio
7+
importlogging
78

89
# we support both pg8000 and psycopg2
910
try:
@@ -222,20 +223,45 @@ def makedirs(self, path, remove_existing=False):
222223
raiseException("Couldn't create dir {} because of error {}".format(path,error))
223224
returnresult
224225

225-
defrmdirs(self,path,verbose=False,ignore_errors=True):
226+
defrmdirs(self,path,ignore_errors=True):
226227
"""
227228
Remove a directory in the remote server.
228229
Args:
229230
- path (str): The path to the directory to be removed.
230-
- verbose (bool): If True, return exit status, result, and error.
231231
- ignore_errors (bool): If True, do not raise error if directory does not exist.
232232
"""
233-
cmd="rm -rf {}".format(path)
234-
exit_status,result,error=self.exec_command(cmd,verbose=True)
235-
ifverbose:
236-
returnexit_status,result,error
237-
else:
238-
returnresult
233+
asserttype(path)==str# noqa: E721
234+
asserttype(ignore_errors)==bool# noqa: E721
235+
236+
# ENOENT = 2 - No such file or directory
237+
# ENOTDIR = 20 - Not a directory
238+
239+
cmd1= [
240+
"if","[","-d",path,"]",";",
241+
"then","rm","-rf",path,";",
242+
"elif","[","-e",path,"]",";",
243+
"then","{","echo","cannot remove '"+path+"': it is not a directory",">&2",";","exit","20",";","}",";",
244+
"else","{","echo","directory '"+path+"' does not exist",">&2",";","exit","2",";","}",";",
245+
"fi"
246+
]
247+
248+
cmd2= ["sh","-c",subprocess.list2cmdline(cmd1)]
249+
250+
try:
251+
self.exec_command(cmd2,encoding=Helpers.GetDefaultEncoding())
252+
exceptExecUtilExceptionase:
253+
ife.exit_code==2:# No such file or directory
254+
returnTrue
255+
256+
ifnotignore_errors:
257+
raise
258+
259+
errMsg="Failed to remove directory {0} ({1}): {2}".format(
260+
path,type(e).__name__,e
261+
)
262+
logging.warning(errMsg)
263+
returnFalse
264+
returnTrue
239265

240266
deflistdir(self,path):
241267
"""

‎tests/test_remote.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ def test_makedirs_and_rmdirs_success(self):
9999
assertnotos.path.exists(path)
100100
assertnotself.operations.path_exists(path)
101101

102-
deftest_makedirs_and_rmdirs_failure(self):
102+
deftest_makedirs_failure(self):
103103
"""
104-
Test makedirsand rmdirsfor directory creation and removal failure.
104+
Test makedirs for failure.
105105
"""
106106
# Try to create a directory in a read-only location
107107
path="/root/test_dir"
@@ -110,16 +110,32 @@ def test_makedirs_and_rmdirs_failure(self):
110110
withpytest.raises(Exception):
111111
self.operations.makedirs(path)
112112

113-
# Test rmdirs
114-
whileTrue:
115-
try:
116-
self.operations.rmdirs(path,verbose=True)
117-
exceptExecUtilExceptionase:
118-
asserte.message=="Utility exited with non-zero code (1). Error: `rm: cannot remove '/root/test_dir': Permission denied`"
119-
asserttype(e.error)==bytes# noqa: E721
120-
asserte.error.strip()==b"rm: cannot remove '/root/test_dir': Permission denied"
121-
break
122-
raiseException("We wait an exception!")
113+
deftest_rmdirs(self):
114+
path=self.operations.mkdtemp()
115+
assertos.path.exists(path)
116+
117+
assertself.operations.rmdirs(path,ignore_errors=False)isTrue
118+
assertnotos.path.exists(path)
119+
120+
deftest_rmdirs__try_to_delete_nonexist_path(self):
121+
path="/root/test_dir"
122+
123+
assertself.operations.rmdirs(path,ignore_errors=False)isTrue
124+
125+
deftest_rmdirs__try_to_delete_file(self):
126+
path=self.operations.mkstemp()
127+
assertos.path.exists(path)
128+
129+
withpytest.raises(ExecUtilException)asx:
130+
self.operations.rmdirs(path,ignore_errors=False)
131+
132+
assertos.path.exists(path)
133+
asserttype(x.value)==ExecUtilException# noqa: E721
134+
assertx.value.message=="Utility exited with non-zero code (20). Error: `cannot remove '"+path+"': it is not a directory`"
135+
asserttype(x.value.error)==str# noqa: E721
136+
assertx.value.error.strip()=="cannot remove '"+path+"': it is not a directory"
137+
asserttype(x.value.exit_code)==int# noqa: E721
138+
assertx.value.exit_code==20
123139

124140
deftest_listdir(self):
125141
"""

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp