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