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