OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
| 5 """Utility functions for sdk_update.py and sdk_update_main.py.""" |
| 6 |
5 import errno | 7 import errno |
6 import os | 8 import os |
7 import shutil | 9 import shutil |
8 import subprocess | 10 import subprocess |
9 import sys | 11 import sys |
10 import time | 12 import time |
11 | 13 |
12 """Utility functions for sdk_update.py and sdk_update_main.py.""" | |
13 | |
14 | 14 |
15 class Error(Exception): | 15 class Error(Exception): |
16 """Generic error/exception for sdk_update module""" | 16 """Generic error/exception for sdk_update module""" |
17 pass | 17 pass |
18 | 18 |
19 | 19 |
20 def RemoveDir(outdir): | 20 def RemoveDir(outdir): |
21 """Removes the given directory | 21 """Removes the given directory |
22 | 22 |
23 On Unix systems, this just runs shutil.rmtree, but on Windows, this doesn't | 23 On Unix systems, this just runs shutil.rmtree, but on Windows, this doesn't |
24 work when the directory contains junctions (as does our SDK installer). | 24 work when the directory contains junctions (as does our SDK installer). |
25 Therefore, on Windows, it runs rmdir /S /Q as a shell command. This always | 25 Therefore, on Windows, it runs rmdir /S /Q as a shell command. This always |
26 does the right thing on Windows. If the directory already didn't exist, | 26 does the right thing on Windows. If the directory already didn't exist, |
27 RemoveDir will return successfully without taking any action. | 27 RemoveDir will return successfully without taking any action. |
28 | 28 |
29 Args: | 29 Args: |
30 outdir: The directory to delete | 30 outdir: The directory to delete |
31 | 31 |
32 Raises: | 32 Raises: |
33 CalledProcessError - if the delete operation fails on Windows | 33 CalledProcessError - if the delete operation fails on Windows |
34 OSError - if the delete operation fails on Linux | 34 OSError - if the delete operation fails on Linux |
35 """ | 35 """ |
36 | 36 |
37 try: | 37 try: |
38 shutil.rmtree(outdir) | 38 shutil.rmtree(outdir) |
39 except: | 39 except OSError: |
40 if not os.path.exists(outdir): | 40 if not os.path.exists(outdir): |
41 return | 41 return |
42 # On Windows this could be an issue with junctions, so try again with rmdir | 42 # On Windows this could be an issue with junctions, so try again with rmdir |
43 if sys.platform == 'win32': | 43 if sys.platform == 'win32': |
44 subprocess.check_call(['rmdir', '/S', '/Q', outdir], shell=True) | 44 subprocess.check_call(['rmdir', '/S', '/Q', outdir], shell=True) |
45 | 45 |
46 | 46 |
47 def RenameDir(srcdir, destdir): | 47 def RenameDir(srcdir, destdir): |
48 """Renames srcdir to destdir. Removes destdir before doing the | 48 """Renames srcdir to destdir. Removes destdir before doing the |
49 rename if it already exists.""" | 49 rename if it already exists.""" |
50 | 50 |
51 max_tries = 5 | 51 max_tries = 5 |
| 52 num_tries = 0 |
52 for num_tries in xrange(max_tries): | 53 for num_tries in xrange(max_tries): |
53 try: | 54 try: |
54 RemoveDir(destdir) | 55 RemoveDir(destdir) |
55 os.rename(srcdir, destdir) | 56 os.rename(srcdir, destdir) |
56 return | 57 return |
57 except OSError as err: | 58 except OSError as err: |
58 if err.errno != errno.EACCES: | 59 if err.errno != errno.EACCES: |
59 raise err | 60 raise err |
60 # If we are here, we didn't exit due to raised exception, so we are | 61 # If we are here, we didn't exit due to raised exception, so we are |
61 # handling a Windows flaky access error. Sleep one second and try | 62 # handling a Windows flaky access error. Sleep one second and try |
62 # again. | 63 # again. |
63 time.sleep(num_tries + 1) | 64 time.sleep(num_tries + 1) |
| 65 |
64 # end of while loop -- could not RenameDir | 66 # end of while loop -- could not RenameDir |
65 raise Error('Could not RenameDir %s => %s after %d tries.\n' % | 67 raise Error('Could not RenameDir %s => %s after %d tries.\n' |
66 'Please check that no shells or applications ' | 68 'Please check that no shells or applications ' |
67 'are accessing files in %s.' | 69 'are accessing files in %s.' |
68 % (srcdir, destdir, num_tries, destdir)) | 70 % (srcdir, destdir, num_tries, destdir)) |
OLD | NEW |