mbonadei | 9e5841a | 2017-05-09 00:13:47 -0700 | [diff] [blame^] | 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 | print 'Deleting `{}`.'.format(file_path) |
| 83 | if not os.path.exists(file_path): |
| 84 | print '`{}` does not exist.'.format(file_path) |
| 85 | return |
| 86 | |
| 87 | if sys.platform == 'win32': |
| 88 | # Give up and use cmd.exe's rd command. |
| 89 | file_path = os.path.normcase(file_path) |
| 90 | for _ in xrange(3): |
| 91 | print 'RemoveDirectory running %s' % (' '.join( |
| 92 | ['cmd.exe', '/c', 'rd', '/q', '/s', file_path])) |
| 93 | if not subprocess.call(['cmd.exe', '/c', 'rd', '/q', '/s', file_path]): |
| 94 | break |
| 95 | print ' Failed' |
| 96 | time.sleep(3) |
| 97 | return |
| 98 | else: |
| 99 | shutil.rmtree(file_path, ignore_errors=True) |
| 100 | |
| 101 | |
| 102 | def UnpackArchiveTo(archive_path, output_dir): |
| 103 | extension = os.path.splitext(archive_path)[1] |
| 104 | if extension == '.zip': |
| 105 | _UnzipArchiveTo(archive_path, output_dir) |
| 106 | else: |
| 107 | _UntarArchiveTo(archive_path, output_dir) |
| 108 | |
| 109 | |
| 110 | def _UnzipArchiveTo(archive_path, output_dir): |
| 111 | print 'Unzipping {} in {}.'.format(archive_path, output_dir) |
| 112 | zip_file = zipfile.ZipFile(archive_path) |
| 113 | try: |
| 114 | zip_file.extractall(output_dir) |
| 115 | finally: |
| 116 | zip_file.close() |
| 117 | |
| 118 | |
| 119 | def _UntarArchiveTo(archive_path, output_dir): |
| 120 | print 'Untarring {} in {}.'.format(archive_path, output_dir) |
| 121 | tar_file = tarfile.open(archive_path, 'r:gz') |
| 122 | try: |
| 123 | tar_file.extractall(output_dir) |
| 124 | finally: |
| 125 | tar_file.close() |
| 126 | |
| 127 | |
| 128 | def GetPlatform(): |
| 129 | if sys.platform.startswith('win'): |
| 130 | return 'win' |
| 131 | if sys.platform.startswith('linux'): |
| 132 | return 'linux' |
| 133 | if sys.platform.startswith('darwin'): |
| 134 | return 'mac' |
| 135 | raise Exception("Can't run on platform %s." % sys.platform) |