1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Module of functions for cleaning up after the tests."""
24
25
26 from shutil import rmtree
27 from time import sleep
28
29
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
48 if not hasattr(obj, name):
49 return
50
51
52 var = getattr(obj, name)
53
54
55 try:
56 if dir:
57 rmtree(var)
58 else:
59 delete(var, fail=False)
60 del var
61
62
63 except OSError:
64 pass
65
66
67 except:
68 sleep(3)
69 try:
70 if dir:
71 rmtree(var)
72 else:
73 delete(var, fail=False)
74
75
76 except:
77 pass
78
79 finally:
80 del var
81 pass
82