OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Cleans up a swarm slave after the tests have run.""" | |
7 | |
8 import errno | |
9 import glob | |
10 import os | |
11 import tempfile | |
12 import time | |
13 import shutil | |
14 import subprocess | |
15 import sys | |
16 | |
17 | |
18 # This is copied from Chromium's project build/scripts/common/chromium_utils.py. | |
19 def RemoveDirectory(*path): | |
20 """Recursively removes a directory, even if it's marked read-only. | |
21 | |
22 Remove the directory located at *path, if it exists. | |
23 | |
24 shutil.rmtree() doesn't work on Windows if any of the files or directories | |
25 are read-only, which svn repositories and some .svn files are. We need to | |
26 be able to force the files to be writable (i.e., deletable) as we traverse | |
27 the tree. | |
28 | |
29 Even with all this, Windows still sometimes fails to delete a file, citing | |
30 a permission error (maybe something to do with antivirus scans or disk | |
31 indexing). The best suggestion any of the user forums had was to wait a | |
32 bit and try again, so we do that too. It's hand-waving, but sometimes it | |
33 works. :/ | |
34 """ | |
35 file_path = os.path.join(*path) | |
36 if not os.path.exists(file_path): | |
37 return | |
38 | |
39 if sys.platform == 'win32': | |
40 # Give up and use cmd.exe's rd command. | |
41 file_path = os.path.normcase(file_path) | |
42 for _ in xrange(3): | |
43 if not subprocess.call(['cmd.exe', '/c', 'rd', '/q', '/s', file_path]): | |
44 break | |
45 time.sleep(3) | |
46 return | |
47 | |
48 def RemoveWithRetry_non_win(rmfunc, path): | |
49 if os.path.islink(path): | |
50 return os.remove(path) | |
51 else: | |
52 return rmfunc(path) | |
53 | |
54 remove_with_retry = RemoveWithRetry_non_win | |
55 | |
56 def RmTreeOnError(function, path, excinfo): | |
57 """This works around a problem whereby python 2.x on Windows has no ability | |
58 to check for symbolic links. os.path.islink() always returns False but | |
59 shutil.rmtree() will fail if invoked on a symbolic link whose target was | |
60 deleted before the link. E.g., reproduce like this: | |
61 > mkdir test | |
62 > mkdir test\1 | |
63 > mklink /D test\current test\1 | |
64 > python -c "import shutl; shutil.rmtree('test')" | |
65 To avoid this issue, we pass this error-handling function to rmtree. If we | |
66 see the exact sort of failure, we ignore it. All other failures we | |
67 re-raise. | |
68 """ | |
69 exception_type = excinfo[0] | |
70 exception_value = excinfo[1] | |
71 # If shutil.rmtree encounters a symbolic link on Windows, os.listdir will | |
72 # fail with a WindowsError exception with an ENOENT errno (i.e., file not | |
73 # found). We'll ignore that error. Note that WindowsError is not defined | |
74 # for non-Windows platforms, so we use OSError (of which it is a subclass) | |
75 # to avoid lint complaints about an undefined global on non-Windows | |
76 # platforms. | |
77 if (function is os.listdir) and issubclass(exception_type, OSError): | |
78 if exception_value.errno == errno.ENOENT: | |
79 # File does not exist, and we're trying to delete, so we can ignore the | |
80 # failure. | |
81 print 'WARNING: Failed to list %s during rmtree. Ignoring.\n' % path | |
82 else: | |
83 raise | |
84 else: | |
85 raise | |
86 | |
87 for root, dirs, files in os.walk(file_path, topdown=False): | |
88 # For POSIX: making the directory writable guarantees removability. | |
89 # Windows will ignore the non-read-only bits in the chmod value. | |
90 os.chmod(root, 0770) | |
91 for name in files: | |
92 remove_with_retry(os.remove, os.path.join(root, name)) | |
93 for name in dirs: | |
94 remove_with_retry(lambda p: shutil.rmtree(p, onerror=RmTreeOnError), | |
95 os.path.join(root, name)) | |
96 | |
97 remove_with_retry(os.rmdir, file_path) | |
98 | |
99 | |
100 def delete(filename): | |
101 try: | |
102 if os.path.isdir(filename): | |
103 RemoveDirectory(filename) | |
104 else: | |
105 os.remove(filename) | |
106 except OSError: | |
107 pass | |
108 | |
109 | |
110 def main(): | |
111 for filename in glob.iglob('*.zip'): | |
112 delete(filename) | |
113 | |
114 # Clear the temp directory. | |
115 for filename in glob.iglob(os.path.join(tempfile.gettempdir(), '*')): | |
116 delete(filename) | |
117 | |
118 # Clears up stale run_isolated.py temporary directories. | |
119 for filename in glob.iglob('run_tha_test*'): | |
120 delete(filename) | |
121 print '' | |
122 return 0 | |
123 | |
124 | |
125 if __name__ == '__main__': | |
126 sys.exit(main()) | |
OLD | NEW |