OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Utilities for all our deps-management stuff.""" | 6 """Utilities for all our deps-management stuff.""" |
7 | 7 |
8 import hashlib | 8 import hashlib |
9 import os | 9 import os |
10 import shutil | 10 import shutil |
11 import sys | 11 import sys |
12 import subprocess | 12 import subprocess |
13 import tarfile | 13 import tarfile |
14 import time | 14 import time |
15 import zipfile | 15 import zipfile |
16 | 16 |
17 | 17 |
18 def RunSubprocessWithRetry(cmd): | 18 def RunSubprocessWithRetry(cmd): |
19 """Invokes the subprocess and backs off exponentially on fail.""" | 19 """Invokes the subprocess and backs off exponentially on fail.""" |
20 for i in range(5): | 20 for i in range(5): |
21 try: | 21 try: |
22 subprocess.check_call(cmd) | 22 subprocess.check_call(cmd) |
23 return | 23 return |
24 except subprocess.CalledProcessError as exception: | 24 except subprocess.CalledProcessError as exception: |
25 backoff = pow(2, i) | 25 backoff = pow(2, i) |
26 print 'Got %s, retrying in %d seconds...' % (exception, backoff) | 26 print 'Got %s, retrying in %d seconds...' % (exception, backoff) |
27 time.sleep(backoff) | 27 time.sleep(backoff) |
28 | 28 |
29 print 'Giving up.' | 29 print 'Giving up.' |
30 raise exception | 30 raise exception |
31 | 31 |
32 | 32 |
33 def DownloadFilesFromGoogleStorage(path): | 33 def DownloadFilesFromGoogleStorage(path): |
34 print 'Downloading files in %s...' % path | 34 print 'Downloading files in %s...' % path |
35 | 35 |
36 extension = 'bat' if 'win32' in sys.platform else 'py' | 36 extension = 'bat' if 'win32' in sys.platform else 'py' |
37 cmd = ['download_from_google_storage.%s' % extension, | 37 cmd = ['download_from_google_storage.%s' % extension, |
38 '--bucket=chromium-webrtc-resources', | 38 '--bucket=chromium-webrtc-resources', |
(...skipping 10 matching lines...) Expand all Loading... |
49 sha1 = hashlib.sha1() | 49 sha1 = hashlib.sha1() |
50 file_to_hash = open(path, 'rb') | 50 file_to_hash = open(path, 'rb') |
51 try: | 51 try: |
52 sha1.update(file_to_hash.read()) | 52 sha1.update(file_to_hash.read()) |
53 finally: | 53 finally: |
54 file_to_hash.close() | 54 file_to_hash.close() |
55 | 55 |
56 return sha1.hexdigest() | 56 return sha1.hexdigest() |
57 | 57 |
58 | 58 |
| 59 # Code partially copied from |
| 60 # https://cs.chromium.org#chromium/build/scripts/common/chromium_utils.py |
| 61 def RemoveDirectory(*path): |
| 62 """Recursively removes a directory, even if it's marked read-only. |
| 63 |
| 64 Remove the directory located at *path, if it exists. |
| 65 |
| 66 shutil.rmtree() doesn't work on Windows if any of the files or directories |
| 67 are read-only, which svn repositories and some .svn files are. We need to |
| 68 be able to force the files to be writable (i.e., deletable) as we traverse |
| 69 the tree. |
| 70 |
| 71 Even with all this, Windows still sometimes fails to delete a file, citing |
| 72 a permission error (maybe something to do with antivirus scans or disk |
| 73 indexing). The best suggestion any of the user forums had was to wait a |
| 74 bit and try again, so we do that too. It's hand-waving, but sometimes it |
| 75 works. :/ |
| 76 """ |
| 77 file_path = os.path.join(*path) |
| 78 if not os.path.exists(file_path): |
| 79 return |
| 80 |
| 81 if sys.platform == 'win32': |
| 82 # Give up and use cmd.exe's rd command. |
| 83 file_path = os.path.normcase(file_path) |
| 84 for _ in xrange(3): |
| 85 print 'RemoveDirectory running %s' % (' '.join( |
| 86 ['cmd.exe', '/c', 'rd', '/q', '/s', file_path])) |
| 87 if not subprocess.call(['cmd.exe', '/c', 'rd', '/q', '/s', file_path]): |
| 88 break |
| 89 print ' Failed' |
| 90 time.sleep(3) |
| 91 return |
| 92 else: |
| 93 shutil.rmtree(file_path, ignore_errors=True) |
| 94 |
| 95 |
59 def DeleteDirNextToGclient(directory): | 96 def DeleteDirNextToGclient(directory): |
60 # Sanity check to avoid nuking the wrong dirs. | 97 # Sanity check to avoid nuking the wrong dirs. |
61 if not os.path.exists('.gclient'): | 98 if not os.path.exists('.gclient'): |
62 raise Exception('Invoked from wrong dir; invoke from dir with .gclient') | 99 raise Exception('Invoked from wrong dir; invoke from dir with .gclient') |
63 print 'Deleting %s in %s...' % (directory, os.getcwd()) | 100 print 'Deleting %s in %s...' % (directory, os.getcwd()) |
64 shutil.rmtree(directory, ignore_errors=True) | 101 RemoveDirectory(directory) |
65 | 102 |
66 | 103 |
67 def UnpackToWorkingDir(archive_path): | 104 def UnpackToWorkingDir(archive_path): |
68 extension = os.path.splitext(archive_path)[1] | 105 extension = os.path.splitext(archive_path)[1] |
69 if extension == '.zip': | 106 if extension == '.zip': |
70 _Unzip(archive_path) | 107 _Unzip(archive_path) |
71 else: | 108 else: |
72 _Untar(archive_path) | 109 _Untar(archive_path) |
73 | 110 |
74 | 111 |
(...skipping 17 matching lines...) Expand all Loading... |
92 | 129 |
93 def GetPlatform(): | 130 def GetPlatform(): |
94 if sys.platform.startswith('win'): | 131 if sys.platform.startswith('win'): |
95 return 'win' | 132 return 'win' |
96 if sys.platform.startswith('linux'): | 133 if sys.platform.startswith('linux'): |
97 return 'linux' | 134 return 'linux' |
98 if sys.platform.startswith('darwin'): | 135 if sys.platform.startswith('darwin'): |
99 return 'mac' | 136 return 'mac' |
100 raise Exception("Can't run on platform %s." % sys.platform) | 137 raise Exception("Can't run on platform %s." % sys.platform) |
101 | 138 |
OLD | NEW |