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