OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """ |
| 7 Downloads trimmed-down Android Tools from Google Cloud Storage and extracts |
| 8 them to INSTALL_DIR, updating INSTALL_DIR/VERSION_* stamp files with current |
| 9 version. Does nothing if INSTALL_DIR/VERSION_* are already up to date. |
| 10 """ |
| 11 |
| 12 import multiprocessing |
| 13 import os |
| 14 import shutil |
| 15 import subprocess |
| 16 import sys |
| 17 import tarfile |
| 18 |
| 19 # Path constants. (All of these should be absolute paths.) |
| 20 THIS_DIR = os.path.abspath(os.path.dirname(__file__)) |
| 21 MOJO_DIR = os.path.abspath(os.path.join(THIS_DIR, '..', '..')) |
| 22 # Should be the same as in upload.py. |
| 23 INSTALL_DIR = os.path.join(MOJO_DIR, 'third_party', 'android_tools') |
| 24 |
| 25 sys.path.insert(0, os.path.join(MOJO_DIR, 'tools')) |
| 26 import find_depot_tools |
| 27 |
| 28 DEPOT_PATH = find_depot_tools.add_depot_tools_to_path() |
| 29 GSUTIL_PATH = os.path.join(DEPOT_PATH, 'gsutil.py') |
| 30 |
| 31 def RunCommand(command): |
| 32 """Run command and return success (True) or failure.""" |
| 33 |
| 34 print 'Running %s' % (str(command)) |
| 35 if subprocess.call(command, shell=False) == 0: |
| 36 return True |
| 37 print 'Failed.' |
| 38 return False |
| 39 |
| 40 def GetInstalledVersion(version_stamp): |
| 41 version_file = os.path.join(INSTALL_DIR, version_stamp) |
| 42 if not os.path.exists(version_file): |
| 43 return None |
| 44 with open(version_file) as f: |
| 45 return f.read().strip() |
| 46 |
| 47 def VersionStampName(tools_name): |
| 48 if sys.platform.startswith('linux'): |
| 49 return 'VERSION_LINUX_' + tools_name.upper() |
| 50 elif sys.platform == 'darwin': |
| 51 return 'VERSION_MACOSX_' + tools_name.upper() |
| 52 else: |
| 53 raise Exception('Unsupported platform: ' + sys.platform) |
| 54 |
| 55 def UpdateTools(tools_name): |
| 56 """Downloads zipped tools from Google Cloud Storage and extracts them, |
| 57 stamping current version.""" |
| 58 |
| 59 # Read latest version. |
| 60 version_stamp = VersionStampName(tools_name) |
| 61 version = '' |
| 62 with open(os.path.join(THIS_DIR, version_stamp)) as f: |
| 63 version = f.read().strip() |
| 64 # Return if installed binaries are up to date. |
| 65 if version == GetInstalledVersion(version_stamp): |
| 66 return |
| 67 |
| 68 # Remove the old install directory checked out from git. |
| 69 if os.path.exists(os.path.join(INSTALL_DIR, '.git')): |
| 70 shutil.rmtree(INSTALL_DIR) |
| 71 # Make sure that the install directory exists. |
| 72 if not os.path.exists(INSTALL_DIR): |
| 73 os.mkdir(INSTALL_DIR) |
| 74 # Remove current installation. |
| 75 tools_root = os.path.join(INSTALL_DIR, tools_name) |
| 76 if os.path.exists(tools_root): |
| 77 shutil.rmtree(tools_root) |
| 78 |
| 79 # Download tools from GCS. |
| 80 archive_path = os.path.join(INSTALL_DIR, tools_name + '.tar.gz') |
| 81 download_cmd = ['python', GSUTIL_PATH, 'cp', |
| 82 'gs://mojo/android/tool/%s.tar.gz' % version, |
| 83 archive_path] |
| 84 if not RunCommand(download_cmd): |
| 85 print ('WARNING: Failed to download Android tools.') |
| 86 return |
| 87 |
| 88 def Extract(): |
| 89 print "Extracting Android tools (" + tools_name + ")..." |
| 90 with tarfile.open(archive_path) as arch: |
| 91 arch.extractall(INSTALL_DIR) |
| 92 os.remove(archive_path) |
| 93 # Write version as the last step. |
| 94 with open(os.path.join(INSTALL_DIR, version_stamp), 'w+') as f: |
| 95 f.write('%s\n' % version) |
| 96 print "Finished extracting Android tools (" + tools_name + ")" |
| 97 multiprocessing.Process(target=Extract).start() |
| 98 |
| 99 def main(): |
| 100 UpdateTools('ndk') |
| 101 UpdateTools('sdk') |
| 102 |
| 103 if __name__ == '__main__': |
| 104 sys.exit(main()) |
OLD | NEW |