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 if not hasattr(builtins, 'WindowsError'):
56 builtins.WindowsError = None
57
58
59 try:
60 if dir:
61 rmtree(var)
62 else:
63 delete(var, fail=False)
64 del var
65
66
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
78 except OSError:
79 pass
80