OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | |
3 # | |
4 # Use of this source code is governed by a BSD-style license | |
5 # that can be found in the LICENSE file in the root of the source | |
6 # tree. An additional intellectual property rights grant can be found | |
7 # in the file PATENTS. All contributing project authors may | |
8 # be found in the AUTHORS file in the root of the source tree. | |
9 | |
10 """Utilities for all our deps-management stuff.""" | |
11 | |
12 import hashlib | |
13 import os | |
14 import shutil | |
15 import sys | |
16 import subprocess | |
17 import tarfile | |
18 import time | |
19 import zipfile | |
20 | |
21 | |
22 def RunSubprocessWithRetry(cmd): | |
23 """Invokes the subprocess and backs off exponentially on fail.""" | |
24 for i in range(5): | |
25 try: | |
26 subprocess.check_call(cmd) | |
27 return | |
28 except subprocess.CalledProcessError as exception: | |
29 backoff = pow(2, i) | |
30 print 'Got %s, retrying in %d seconds...' % (exception, backoff) | |
31 time.sleep(backoff) | |
32 | |
33 print 'Giving up.' | |
34 raise exception | |
35 | |
36 | |
37 def DownloadFilesFromGoogleStorage(path): | |
38 print 'Downloading files in %s...' % path | |
39 | |
40 extension = 'bat' if 'win32' in sys.platform else 'py' | |
41 cmd = ['download_from_google_storage.%s' % extension, | |
42 '--bucket=chromium-webrtc-resources', | |
43 '--auto_platform', | |
44 '--recursive', | |
45 '--directory', path] | |
46 subprocess.check_call(cmd) | |
47 | |
48 | |
49 def ComputeSHA1(path): | |
50 if not os.path.exists(path): | |
51 return 0 | |
52 | |
53 sha1 = hashlib.sha1() | |
54 file_to_hash = open(path, 'rb') | |
55 try: | |
56 sha1.update(file_to_hash.read()) | |
57 finally: | |
58 file_to_hash.close() | |
59 | |
60 return sha1.hexdigest() | |
61 | |
62 | |
63 # Code partially copied from | |
64 # https://cs.chromium.org#chromium/build/scripts/common/chromium_utils.py | |
65 def RemoveDirectory(*path): | |
66 """Recursively removes a directory, even if it's marked read-only. | |
67 | |
68 Remove the directory located at *path, if it exists. | |
69 | |
70 shutil.rmtree() doesn't work on Windows if any of the files or directories | |
71 are read-only, which svn repositories and some .svn files are. We need to | |
72 be able to force the files to be writable (i.e., deletable) as we traverse | |
73 the tree. | |
74 | |
75 Even with all this, Windows still sometimes fails to delete a file, citing | |
76 a permission error (maybe something to do with antivirus scans or disk | |
77 indexing). The best suggestion any of the user forums had was to wait a | |
78 bit and try again, so we do that too. It's hand-waving, but sometimes it | |
79 works. :/ | |
80 """ | |
81 file_path = os.path.join(*path) | |
82 if not os.path.exists(file_path): | |
83 return | |
84 | |
85 if sys.platform == 'win32': | |
86 # Give up and use cmd.exe's rd command. | |
87 file_path = os.path.normcase(file_path) | |
88 for _ in xrange(3): | |
89 print 'RemoveDirectory running %s' % (' '.join( | |
90 ['cmd.exe', '/c', 'rd', '/q', '/s', file_path])) | |
91 if not subprocess.call(['cmd.exe', '/c', 'rd', '/q', '/s', file_path]): | |
92 break | |
93 print ' Failed' | |
94 time.sleep(3) | |
95 return | |
96 else: | |
97 shutil.rmtree(file_path, ignore_errors=True) | |
98 | |
99 | |
100 def DeleteDirNextToGclient(directory): | |
kjellander_webrtc
2017/05/08 13:28:43
We don't use this, right? Then delete it.
mbonadei
2017/05/08 15:31:20
Thanks, I forgot this around. :)
| |
101 # Sanity check to avoid nuking the wrong dirs. | |
102 if not os.path.exists('.gclient'): | |
103 raise Exception('Invoked from wrong dir; invoke from dir with .gclient') | |
104 print 'Deleting %s in %s...' % (directory, os.getcwd()) | |
105 RemoveDirectory(directory) | |
106 | |
107 | |
108 def DeleteDir(parent_dir, dir_to_delete): | |
kjellander_webrtc
2017/05/08 13:28:43
It seems we don't even need this function.
I think
mbonadei
2017/05/08 15:31:21
Done.
| |
109 print 'Deleting {} in {}'.format(dir_to_delete, parent_dir) | |
110 RemoveDirectory(os.path.join(parent_dir, dir_to_delete)) | |
111 | |
112 | |
113 def UnpackArchiveTo(archive_path, output_dir): | |
114 extension = os.path.splitext(archive_path)[1] | |
115 if extension == '.zip': | |
116 _UnzipArchiveTo(archive_path, output_dir) | |
117 else: | |
118 _UntarArchiveTo(archive_path, output_dir) | |
119 | |
120 | |
121 def _UnzipArchiveTo(archive_path, output_dir): | |
122 print 'Unzipping {} in {}.'.format(archive_path, output_dir) | |
123 zip_file = zipfile.ZipFile(archive_path) | |
124 try: | |
125 zip_file.extractall(output_dir) | |
126 finally: | |
127 zip_file.close() | |
128 | |
129 | |
130 def _UntarArchiveTo(archive_path, output_dir): | |
131 print 'Untarring {} in {}.'.format(archive_path, output_dir) | |
132 tar_file = tarfile.open(archive_path, 'r:gz') | |
133 try: | |
134 tar_file.extractall(output_dir) | |
135 finally: | |
136 tar_file.close() | |
137 | |
138 | |
139 def GetPlatform(): | |
140 if sys.platform.startswith('win'): | |
141 return 'win' | |
142 if sys.platform.startswith('linux'): | |
143 return 'linux' | |
144 if sys.platform.startswith('darwin'): | |
145 return 'mac' | |
146 raise Exception("Can't run on platform %s." % sys.platform) | |
OLD | NEW |