|
4 | 4 | importstat |
5 | 5 | importsubprocess |
6 | 6 | importtempfile |
| 7 | +importtime |
7 | 8 |
|
8 | 9 | importpsutil |
9 | 10 |
|
|
19 | 20 |
|
20 | 21 | CMD_TIMEOUT_SEC=60 |
21 | 22 | error_markers= [b'error',b'Permission denied',b'fatal'] |
| 23 | +err_out_markers= [b'Failure'] |
22 | 24 |
|
23 | 25 |
|
24 | | -defhas_errors(output): |
| 26 | +defhas_errors(output=None,error=None): |
25 | 27 | ifoutput: |
26 | 28 | ifisinstance(output,str): |
27 | 29 | output=output.encode(get_default_encoding()) |
28 | | -returnany(markerinoutputformarkerinerror_markers) |
| 30 | +returnany(markerinoutputformarkerinerr_out_markers) |
| 31 | +iferror: |
| 32 | +ifisinstance(error,str): |
| 33 | +error=error.encode(get_default_encoding()) |
| 34 | +returnany(markerinerrorformarkerinerror_markers) |
29 | 35 | returnFalse |
30 | 36 |
|
31 | 37 |
|
@@ -107,8 +113,8 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False, |
107 | 113 | process,output,error=self._run_command(cmd,shell,input,stdin,stdout,stderr,get_process,timeout,encoding) |
108 | 114 | ifget_process: |
109 | 115 | returnprocess |
110 | | -ifprocess.returncode!=0or(has_errors(error)andnotexpect_error): |
111 | | -self._raise_exec_exception('Utility exited with non-zero code. Error `{}`',cmd,process.returncode,error) |
| 116 | +if(process.returncode!=0orhas_errors(output=output,error=error))andnotexpect_error: |
| 117 | +self._raise_exec_exception('Utility exited with non-zero code. Error `{}`',cmd,process.returncode,errororoutput) |
112 | 118 |
|
113 | 119 | ifverbose: |
114 | 120 | returnprocess.returncode,output,error |
@@ -142,8 +148,27 @@ def makedirs(self, path, remove_existing=False): |
142 | 148 | exceptFileExistsError: |
143 | 149 | pass |
144 | 150 |
|
145 | | -defrmdirs(self,path,ignore_errors=True): |
146 | | -returnrmtree(path,ignore_errors=ignore_errors) |
| 151 | +defrmdirs(self,path,ignore_errors=True,retries=3,delay=1): |
| 152 | +""" |
| 153 | + Removes a directory and its contents, retrying on failure. |
| 154 | +
|
| 155 | + :param path: Path to the directory. |
| 156 | + :param ignore_errors: If True, ignore errors. |
| 157 | + :param retries: Number of attempts to remove the directory. |
| 158 | + :param delay: Delay between attempts in seconds. |
| 159 | + """ |
| 160 | +forattemptinrange(retries): |
| 161 | +try: |
| 162 | +rmtree(path,ignore_errors=ignore_errors) |
| 163 | +ifnotos.path.exists(path): |
| 164 | +returnTrue |
| 165 | +exceptFileNotFoundError: |
| 166 | +returnTrue |
| 167 | +exceptExceptionase: |
| 168 | +print(f"Error: Failed to remove directory{path} on attempt{attempt+1}:{e}") |
| 169 | +time.sleep(delay) |
| 170 | +print(f"Error: Failed to remove directory{path} after{retries} attempts.") |
| 171 | +returnFalse |
147 | 172 |
|
148 | 173 | deflistdir(self,path): |
149 | 174 | returnos.listdir(path) |
|