Package test_suite :: Module clean_up
[hide private]
[frames] | no frames]

Source Code for Module test_suite.clean_up

 1  ############################################################################### 
 2  #                                                                             # 
 3  # Copyright (C) 2013 Edward d'Auvergne                                        # 
 4  #                                                                             # 
 5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
 6  #                                                                             # 
 7  # This program is free software: you can redistribute it and/or modify        # 
 8  # it under the terms of the GNU General Public License as published by        # 
 9  # the Free Software Foundation, either version 3 of the License, or           # 
10  # (at your option) any later version.                                         # 
11  #                                                                             # 
12  # This program is distributed in the hope that it will be useful,             # 
13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
15  # GNU General Public License for more details.                                # 
16  #                                                                             # 
17  # You should have received a copy of the GNU General Public License           # 
18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
19  #                                                                             # 
20  ############################################################################### 
21   
22  # Module docstring. 
23  """Module of functions for cleaning up after the tests.""" 
24   
25  # Python module imports. 
26  from shutil import rmtree 
27  from time import sleep 
28   
29  # relax module imports. 
30  from compat import builtins 
31  from lib.io import delete 
32   
33   
34 -def deletion(obj=None, name=None, dir=False):
35 """Cleanly removing files and directories, handling WindowsErrors. 36 37 The problem of MS Windows not releasing the file handle on a close() call is handled. This method should be resilient to the strange MS Windows behaviour of not releasing the relax state files. It should complete even when this WindowsError occurs. A delay of 3 seconds has been added when the WindowsError occurs to give the OS some time before attempting to delete the directory or file again. If this fails the deletion operation is skipped. 38 39 40 @keyword obj: The base object containing the file or directory name variable. 41 @type obj: Python object 42 @keyword name: The name of the file or directory name variable. 43 @type name: str 44 @keyword dir: A flag which if True indicates that a directory should be removed. Otherwise a file should be deleted. 45 """ 46 47 # No variable present. 48 if not hasattr(obj, name): 49 return 50 51 # The variable. 52 var = getattr(obj, name) 53 54 # Non-windows systems. 55 if not hasattr(builtins, 'WindowsError'): 56 builtins.WindowsError = None 57 58 # Attempt to remove the file or directory as well as the variable. 59 try: 60 if dir: 61 rmtree(var) 62 else: 63 delete(var, fail=False) 64 del var 65 66 # Handle MS Windows strangeness. 67 except WindowsError: 68 sleep(3) 69 try: 70 if dir: 71 rmtree(var) 72 else: 73 delete(var, fail=False) 74 finally: 75 del var 76 77 # Already deleted. 78 except OSError: 79 pass
80