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-2014 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 lib.io import delete 
31   
32   
33 -def deletion(obj=None, name=None, dir=False):
34 """Cleanly removing files and directories, handling WindowsErrors. 35 36 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. 37 38 39 @keyword obj: The base object containing the file or directory name variable. 40 @type obj: Python object 41 @keyword name: The name of the file or directory name variable. 42 @type name: str 43 @keyword dir: A flag which if True indicates that a directory should be removed. Otherwise a file should be deleted. 44 """ 45 46 # No variable present. 47 if not hasattr(obj, name): 48 return 49 50 # The variable. 51 var = getattr(obj, name) 52 53 # Attempt to remove the file or directory as well as the variable. 54 try: 55 if dir: 56 rmtree(var) 57 else: 58 delete(var, fail=False) 59 del var 60 61 # Already deleted. 62 except OSError: 63 pass 64 65 # Handle MS Windows strangeness. 66 except: 67 sleep(3) 68 try: 69 if dir: 70 rmtree(var) 71 else: 72 delete(var, fail=False) 73 74 # The files no longer exist? Oh well. 75 except: 76 pass 77 78 finally: 79 del var 80 pass
81