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 This script takes an Android Tools tree, creates a tar.gz archive and |
| 8 uploads it to Google Cloud Storage at gs://mojo/android/tool. It also produces |
| 9 the VERSION stamp files with the sha1 code of the uploaded archive. |
| 10 |
| 11 This script operates in the INSTALL_DIR directory, so it automatically updates |
| 12 your current installation of the android tools binaries on success. On failure |
| 13 it invalidates your current installation; to fix it, run |
| 14 run download_android_tools.py. |
| 15 """ |
| 16 |
| 17 import hashlib |
| 18 import os |
| 19 import shutil |
| 20 import subprocess |
| 21 import sys |
| 22 import tarfile |
| 23 import optparse |
| 24 |
| 25 # Path constants. (All of these should be absolute paths.) |
| 26 THIS_DIR = os.path.abspath(os.path.dirname(__file__)) |
| 27 MOJO_DIR = os.path.abspath(os.path.join(THIS_DIR, '..', '..')) |
| 28 # Should be the same as in download.py. |
| 29 INSTALL_DIR = os.path.join(MOJO_DIR, 'third_party', 'android_tools') |
| 30 |
| 31 sys.path.insert(0, os.path.join(MOJO_DIR, 'tools')) |
| 32 import find_depot_tools |
| 33 |
| 34 DEPOT_PATH = find_depot_tools.add_depot_tools_to_path() |
| 35 GSUTIL_PATH = os.path.join(DEPOT_PATH, 'gsutil.py') |
| 36 |
| 37 def RunCommand(command, env=None): |
| 38 """Run command and return success (True) or failure.""" |
| 39 |
| 40 print 'Running %s' % (str(command)) |
| 41 if subprocess.call(command, shell=False, env=env) == 0: |
| 42 return True |
| 43 print 'Failed.' |
| 44 return False |
| 45 |
| 46 def VersionStampName(tools_name): |
| 47 if sys.platform.startswith('linux'): |
| 48 return 'VERSION_LINUX_' + tools_name.upper() |
| 49 elif sys.platform == 'darwin': |
| 50 return 'VERSION_MACOSX_' + tools_name.upper() |
| 51 else: |
| 52 raise Exception('Unsupported platform: ' + sys.platform) |
| 53 |
| 54 def CheckInstallDir(tools_name): |
| 55 """Check if the tools directory exists.""" |
| 56 |
| 57 tools_dir = os.path.join(INSTALL_DIR, tools_name) |
| 58 if not os.path.exists(tools_dir): |
| 59 print tools_dir + ' does not exists' |
| 60 sys.exit(1) |
| 61 # Remove the existing version stamp. |
| 62 version_stamp = VersionStampName(tools_name) |
| 63 stamp_file = os.path.join(INSTALL_DIR, version_stamp) |
| 64 if os.path.exists(stamp_file): |
| 65 os.remove(stamp_file) |
| 66 |
| 67 def Compress(tools_name): |
| 68 """Compresses the tools into tar.gz and generates sha1 code, renames the |
| 69 archive to sha1.tar.gz and returns the sha1 code.""" |
| 70 |
| 71 print "Compressing tools, this may take several minutes." |
| 72 os.chdir(INSTALL_DIR) |
| 73 archive_name = tools_name + '.tar.gz' |
| 74 with tarfile.open(os.path.join(archive_name), 'w|gz') as tools: |
| 75 tools.add(tools_name) |
| 76 |
| 77 sha1 = '' |
| 78 with open(os.path.join(INSTALL_DIR, archive_name)) as f: |
| 79 sha1 = hashlib.sha1(f.read()).hexdigest() |
| 80 os.rename(os.path.join(INSTALL_DIR, archive_name), |
| 81 os.path.join(INSTALL_DIR, '%s.tar.gz' % sha1)) |
| 82 return sha1 |
| 83 |
| 84 def Upload(tools_name, sha1): |
| 85 """Uploads INSTALL_DIR/sha1.tar.gz to Google Cloud Storage under |
| 86 gs://mojo/android/tool and writes sha1 to THIS_DIR/VERSION_*.""" |
| 87 |
| 88 file_name = '%s.tar.gz' % sha1 |
| 89 upload_cmd = ['python', GSUTIL_PATH, 'cp', |
| 90 '-n', # Do not upload if the file already exists. |
| 91 os.path.join(INSTALL_DIR, file_name), |
| 92 'gs://mojo/android/tool/%s' % file_name] |
| 93 |
| 94 print "Uploading ' + tools_name + ' tools to GCS." |
| 95 if not RunCommand(upload_cmd): |
| 96 print "Failed to upload android tool to GCS." |
| 97 sys.exit(1) |
| 98 os.remove(os.path.join(INSTALL_DIR, file_name)) |
| 99 # Write versions as the last step. |
| 100 version_stamp = VersionStampName(tools_name) |
| 101 stamp_file = os.path.join(THIS_DIR, version_stamp) |
| 102 with open(stamp_file, 'w+') as stamp: |
| 103 stamp.write('%s\n' % sha1) |
| 104 |
| 105 stamp_file = os.path.join(INSTALL_DIR, version_stamp) |
| 106 with open(stamp_file, 'w+') as stamp: |
| 107 stamp.write('%s\n' % sha1) |
| 108 |
| 109 def main(argv): |
| 110 option_parser = optparse.OptionParser() |
| 111 option_parser.add_option('-t', |
| 112 '--type', |
| 113 help='type of the tools: sdk or ndk', |
| 114 type='string') |
| 115 (options, args) = option_parser.parse_args(argv) |
| 116 |
| 117 if len(args) > 1: |
| 118 print 'Unknown argument: ', args[1:] |
| 119 option_parser.print_help() |
| 120 sys.exit(1) |
| 121 |
| 122 if not options.type in {'sdk', 'ndk'}: |
| 123 option_parser.print_help() |
| 124 sys.exit(1) |
| 125 |
| 126 CheckInstallDir(options.type) |
| 127 sha1 = Compress(options.type) |
| 128 Upload(options.type, sha1) |
| 129 print "Done." |
| 130 |
| 131 if __name__ == '__main__': |
| 132 sys.exit(main(sys.argv)) |
OLD | NEW |