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): |
| 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 UnpackToWorkingDir(archive_path): |
| 109 extension = os.path.splitext(archive_path)[1] |
| 110 if extension == '.zip': |
| 111 _Unzip(archive_path) |
| 112 else: |
| 113 _Untar(archive_path) |
| 114 |
| 115 |
| 116 def _Unzip(path): |
| 117 print 'Unzipping %s in %s...' % (path, os.getcwd()) |
| 118 zip_file = zipfile.ZipFile(path) |
| 119 try: |
| 120 zip_file.extractall() |
| 121 finally: |
| 122 zip_file.close() |
| 123 |
| 124 |
| 125 def _Untar(path): |
| 126 print 'Untarring %s in %s...' % (path, os.getcwd()) |
| 127 tar_file = tarfile.open(path, 'r:gz') |
| 128 try: |
| 129 tar_file.extractall() |
| 130 finally: |
| 131 tar_file.close() |
| 132 |
| 133 |
| 134 def GetPlatform(): |
| 135 if sys.platform.startswith('win'): |
| 136 return 'win' |
| 137 if sys.platform.startswith('linux'): |
| 138 return 'linux' |
| 139 if sys.platform.startswith('darwin'): |
| 140 return 'mac' |
| 141 raise Exception("Can't run on platform %s." % sys.platform) |
| 142 |
OLD | NEW |