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 os | |
13 import shutil | |
14 import sys | |
15 import subprocess | |
16 import tarfile | |
17 import time | |
18 import zipfile | |
19 | |
20 | |
21 def RunSubprocessWithRetry(cmd): | |
22 """Invokes the subprocess and backs off exponentially on fail.""" | |
23 for i in range(5): | |
24 try: | |
25 subprocess.check_call(cmd) | |
26 return | |
27 except subprocess.CalledProcessError as exception: | |
28 backoff = pow(2, i) | |
29 print 'Got %s, retrying in %d seconds...' % (exception, backoff) | |
30 time.sleep(backoff) | |
31 | |
32 print 'Giving up.' | |
33 raise exception | |
34 | |
35 | |
36 def DownloadFilesFromGoogleStorage(path, auto_platform=True): | |
37 print 'Downloading files in %s...' % path | |
38 | |
39 extension = 'bat' if 'win32' in sys.platform else 'py' | |
40 cmd = ['download_from_google_storage.%s' % extension, | |
41 '--bucket=chromium-webrtc-resources', | |
42 '--directory', path] | |
43 if auto_platform: | |
44 cmd += ['--auto_platform', '--recursive'] | |
45 subprocess.check_call(cmd) | |
46 | |
47 | |
48 # Code partially copied from | |
49 # https://cs.chromium.org#chromium/build/scripts/common/chromium_utils.py | |
50 def RemoveDirectory(*path): | |
51 """Recursively removes a directory, even if it's marked read-only. | |
52 | |
53 Remove the directory located at *path, if it exists. | |
54 | |
55 shutil.rmtree() doesn't work on Windows if any of the files or directories | |
56 are read-only, which svn repositories and some .svn files are. We need to | |
57 be able to force the files to be writable (i.e., deletable) as we traverse | |
58 the tree. | |
59 | |
60 Even with all this, Windows still sometimes fails to delete a file, citing | |
61 a permission error (maybe something to do with antivirus scans or disk | |
62 indexing). The best suggestion any of the user forums had was to wait a | |
63 bit and try again, so we do that too. It's hand-waving, but sometimes it | |
64 works. :/ | |
65 """ | |
66 file_path = os.path.join(*path) | |
67 print 'Deleting `{}`.'.format(file_path) | |
68 if not os.path.exists(file_path): | |
69 print '`{}` does not exist.'.format(file_path) | |
70 return | |
71 | |
72 if sys.platform == 'win32': | |
73 # Give up and use cmd.exe's rd command. | |
74 file_path = os.path.normcase(file_path) | |
75 for _ in xrange(3): | |
76 print 'RemoveDirectory running %s' % (' '.join( | |
77 ['cmd.exe', '/c', 'rd', '/q', '/s', file_path])) | |
78 if not subprocess.call(['cmd.exe', '/c', 'rd', '/q', '/s', file_path]): | |
79 break | |
80 print ' Failed' | |
81 time.sleep(3) | |
82 return | |
83 else: | |
84 shutil.rmtree(file_path, ignore_errors=True) | |
85 | |
86 | |
87 def UnpackArchiveTo(archive_path, output_dir): | |
88 extension = os.path.splitext(archive_path)[1] | |
89 if extension == '.zip': | |
90 _UnzipArchiveTo(archive_path, output_dir) | |
91 else: | |
92 _UntarArchiveTo(archive_path, output_dir) | |
93 | |
94 | |
95 def _UnzipArchiveTo(archive_path, output_dir): | |
96 print 'Unzipping {} in {}.'.format(archive_path, output_dir) | |
97 zip_file = zipfile.ZipFile(archive_path) | |
98 try: | |
99 zip_file.extractall(output_dir) | |
100 finally: | |
101 zip_file.close() | |
102 | |
103 | |
104 def _UntarArchiveTo(archive_path, output_dir): | |
105 print 'Untarring {} in {}.'.format(archive_path, output_dir) | |
106 tar_file = tarfile.open(archive_path, 'r:gz') | |
107 try: | |
108 tar_file.extractall(output_dir) | |
109 finally: | |
110 tar_file.close() | |
111 | |
112 | |
113 def GetPlatform(): | |
114 if sys.platform.startswith('win'): | |
115 return 'win' | |
116 if sys.platform.startswith('linux'): | |
117 return 'linux' | |
118 if sys.platform.startswith('darwin'): | |
119 return 'mac' | |
120 raise Exception("Can't run on platform %s." % sys.platform) | |
OLD | NEW |