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.""" | 5 """Utility functions for sdk_update.py and sdk_update_main.py.""" |
6 | 6 |
7 import errno | 7 import errno |
8 import os | 8 import os |
9 import shutil | 9 import shutil |
10 import subprocess | 10 import subprocess |
11 import sys | 11 import sys |
12 import time | 12 import time |
13 | 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 MakeDirs(directory): | |
21 try: | |
22 os.makedirs(directory) | |
23 except OSError as e: | |
24 if e.errno != errno.EEXIST: | |
25 raise Error('Unable to make directory "%s".\n %s' % (directory, e)) | |
26 | |
Sam Clegg
2012/10/22 22:55:30
I'm still not completely sold on this pattern.
I
binji
2012/10/23 00:14:09
Fair enough, we use os.path.exists elsewhere -- ma
| |
27 | |
20 def RemoveDir(outdir): | 28 def RemoveDir(outdir): |
21 """Removes the given directory | 29 """Removes the given directory |
22 | 30 |
23 On Unix systems, this just runs shutil.rmtree, but on Windows, this doesn't | 31 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). | 32 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 | 33 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, | 34 does the right thing on Windows. If the directory already didn't exist, |
27 RemoveDir will return successfully without taking any action. | 35 RemoveDir will return successfully without taking any action. |
28 | 36 |
29 Args: | 37 Args: |
30 outdir: The directory to delete | 38 outdir: The directory to delete |
31 | 39 |
32 Raises: | 40 Raises: |
33 CalledProcessError - if the delete operation fails on Windows | 41 Error - If this operation fails for any reason. |
34 OSError - if the delete operation fails on Linux | |
35 """ | 42 """ |
36 | 43 |
37 try: | 44 try: |
38 shutil.rmtree(outdir) | 45 try: |
39 except OSError: | 46 shutil.rmtree(outdir) |
40 if not os.path.exists(outdir): | 47 except OSError: |
41 return | 48 if not os.path.exists(outdir): |
42 # On Windows this could be an issue with junctions, so try again with rmdir | 49 return |
43 if sys.platform == 'win32': | 50 # On Windows this could be an issue with junctions, so try again with |
44 subprocess.check_call(['rmdir', '/S', '/Q', outdir], shell=True) | 51 # rmdir |
52 if sys.platform == 'win32': | |
53 subprocess.check_call(['rmdir', '/S', '/Q', outdir], shell=True) | |
54 except Exception as e: | |
55 raise Error('Unable to remove directory "%s"\n %s' % (outdir, e)) | |
45 | 56 |
46 | 57 |
47 def RenameDir(srcdir, destdir): | 58 def RenameDir(srcdir, destdir): |
48 """Renames srcdir to destdir. Removes destdir before doing the | 59 """Renames srcdir to destdir. Removes destdir before doing the |
49 rename if it already exists.""" | 60 rename if it already exists.""" |
50 | 61 |
51 max_tries = 5 | 62 max_tries = 5 |
52 num_tries = 0 | 63 num_tries = 0 |
53 for num_tries in xrange(max_tries): | 64 for num_tries in xrange(max_tries): |
54 try: | 65 try: |
55 RemoveDir(destdir) | 66 RemoveDir(destdir) |
56 shutil.move(srcdir, destdir) | 67 shutil.move(srcdir, destdir) |
57 return | 68 return |
58 except OSError as err: | 69 except OSError as err: |
59 if err.errno != errno.EACCES: | 70 if err.errno != errno.EACCES: |
60 raise err | 71 raise err |
61 # If we are here, we didn't exit due to raised exception, so we are | 72 # If we are here, we didn't exit due to raised exception, so we are |
62 # handling a Windows flaky access error. Sleep one second and try | 73 # handling a Windows flaky access error. Sleep one second and try |
63 # again. | 74 # again. |
64 time.sleep(num_tries + 1) | 75 time.sleep(num_tries + 1) |
65 | 76 |
66 # end of while loop -- could not RenameDir | 77 # end of while loop -- could not RenameDir |
67 raise Error('Could not RenameDir %s => %s after %d tries.\n' | 78 raise Error('Could not RenameDir %s => %s after %d tries.\n' |
68 'Please check that no shells or applications ' | 79 'Please check that no shells or applications ' |
69 'are accessing files in %s.' | 80 'are accessing files in %s.' |
70 % (srcdir, destdir, num_tries, destdir)) | 81 % (srcdir, destdir, num_tries, destdir)) |
OLD | NEW |