OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2014 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 import argparse |
| 7 import glob |
| 8 import imp |
| 9 import itertools |
| 10 import os |
| 11 import subprocess |
| 12 import sys |
| 13 import tempfile |
| 14 import time |
| 15 import zipfile |
| 16 |
| 17 sys.path.append(os.path.join(os.path.dirname(__file__), |
| 18 os.pardir, 'third_party', 'pyelftools')) |
| 19 import elftools.elf.elffile as elffile |
| 20 |
| 21 sys.path.append(os.path.join(os.path.dirname(__file__), |
| 22 os.pardir, 'third_party', 'mojo_devtools')) |
| 23 import android_gdb.signatures as signatures |
| 24 |
| 25 SERVICES = ["network_service", "network_service_apptests"] |
| 26 |
| 27 # A service does not need to expose interfaces. Those that do expose interfaces |
| 28 # have their mojoms located in the directories listed below, in paths relative |
| 29 # to the directory of this script. |
| 30 MOJOMS_IN_DIR = { |
| 31 "network_service": os.path.join("network", "public", "interfaces") |
| 32 } |
| 33 |
| 34 # The network service is downloaded out-of-band rather than dynamically by the |
| 35 # shell and thus can be stored zipped in the cloud. Other services are intended |
| 36 # to be downloaded dynamically by the shell, which doesn't currently understand |
| 37 # zipped binaries. |
| 38 SERVICES_WITH_ZIPPED_BINARIES = ["network_service", "network_service_apptests"] |
| 39 |
| 40 if not sys.platform.startswith("linux"): |
| 41 print "Only support linux for now" |
| 42 sys.exit(1) |
| 43 |
| 44 root_path = os.path.realpath(os.path.dirname(__file__)) |
| 45 |
| 46 find_depot_tools_path = os.path.join(root_path, "find_depot_tools.py") |
| 47 find_depot_tools = imp.load_source("find_depot_tools", find_depot_tools_path) |
| 48 |
| 49 depot_tools_path = find_depot_tools.add_depot_tools_to_path() |
| 50 gsutil_exe = os.path.join(depot_tools_path, "third_party", "gsutil", "gsutil") |
| 51 |
| 52 def get_version_name(custom_build): |
| 53 if custom_build: |
| 54 branch = subprocess.check_output( |
| 55 ["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=root_path).strip() |
| 56 try: |
| 57 base = subprocess.check_output( |
| 58 ["git", "config", "--get", "branch." + branch + ".base"], |
| 59 cwd=root_path).strip() |
| 60 issue = subprocess.check_output( |
| 61 ["git", "config", "--get", "branch." + branch + ".rietveldissue"], |
| 62 cwd=root_path).strip() |
| 63 patchset = subprocess.check_output( |
| 64 ["git", "config", "--get", "branch." + branch + ".rietveldpatchset"], |
| 65 cwd=root_path).strip() |
| 66 except subprocess.CalledProcessError: |
| 67 return None |
| 68 |
| 69 if not base or not issue or not patchset: |
| 70 return None |
| 71 else: |
| 72 return "custom_build_base_%s_issue_%s_patchset_%s" % (base, issue, |
| 73 patchset) |
| 74 else: |
| 75 return subprocess.check_output(["git", "rev-parse", "HEAD"], |
| 76 cwd=root_path).strip() |
| 77 |
| 78 def gsutil_cp(source, dest, dry_run): |
| 79 if dry_run: |
| 80 print "gsutil cp %s %s" % (source, dest) |
| 81 else: |
| 82 subprocess.check_call([gsutil_exe, "cp", source, dest]) |
| 83 |
| 84 |
| 85 def upload_mojoms(version_name, service, absolute_mojom_directory_path, |
| 86 dry_run): |
| 87 dest = "gs://mojo/" + service + "/" + version_name + "/" + "mojoms.zip" |
| 88 |
| 89 with tempfile.NamedTemporaryFile() as mojom_zip_file: |
| 90 with zipfile.ZipFile(mojom_zip_file, 'w') as z: |
| 91 for root, _, files in os.walk(absolute_mojom_directory_path): |
| 92 for filename in files: |
| 93 absolute_file_path = os.path.join(root, filename) |
| 94 relative_file_path = os.path.relpath(absolute_file_path, root) |
| 95 z.write(absolute_file_path, relative_file_path) |
| 96 gsutil_cp(mojom_zip_file.name, dest, dry_run) |
| 97 |
| 98 |
| 99 def upload_symbols(binary_dir, dry_run): |
| 100 dest_dir = "gs://mojo/symbols/" |
| 101 symbols_dir = os.path.join(binary_dir, "symbols") |
| 102 for name in os.listdir(symbols_dir): |
| 103 path = os.path.join(symbols_dir, name) |
| 104 with open(path) as f: |
| 105 signature = signatures.get_signature(f, elffile) |
| 106 if signature is not None: |
| 107 dest = dest_dir + signature |
| 108 gsutil_cp(path, dest, dry_run) |
| 109 |
| 110 def upload_binary(version_name, service, binary_dir, platform, dry_run): |
| 111 dest_dir = "gs://mojo/" + service + "/" + version_name + "/" + platform + "/" |
| 112 should_zip = service in SERVICES_WITH_ZIPPED_BINARIES |
| 113 binary_name = service + ".mojo" |
| 114 absolute_binary_path = os.path.join(root_path, binary_dir, binary_name) |
| 115 |
| 116 if not should_zip: |
| 117 # Upload the binary. |
| 118 dest = dest_dir + binary_name |
| 119 gsutil_cp(absolute_binary_path, dest, dry_run) |
| 120 |
| 121 # Update the pointer to the service's location to point to the |
| 122 # newly-uploaded binary. |
| 123 service_location = dest.replace("gs://", "https://storage.googleapis.com/") |
| 124 location_file = ("gs://mojo/services/" + platform + "/" + service + |
| 125 "_location") |
| 126 with tempfile.NamedTemporaryFile() as tmp: |
| 127 tmp.write(service_location) |
| 128 tmp.flush() |
| 129 gsutil_cp(tmp.name, location_file, dry_run) |
| 130 return |
| 131 |
| 132 # Zip the binary before uploading it to the cloud. |
| 133 dest = dest_dir + binary_name + ".zip" |
| 134 with tempfile.NamedTemporaryFile() as binary_zip_file: |
| 135 with zipfile.ZipFile(binary_zip_file, 'w') as z: |
| 136 with open(absolute_binary_path) as service_binary: |
| 137 zipinfo = zipfile.ZipInfo(binary_name) |
| 138 zipinfo.external_attr = 0o777 << 16 |
| 139 zipinfo.compress_type = zipfile.ZIP_DEFLATED |
| 140 zipinfo.date_time = time.gmtime(os.path.getmtime(absolute_binary_path)) |
| 141 z.writestr(zipinfo, service_binary.read()) |
| 142 gsutil_cp(binary_zip_file.name, dest, dry_run) |
| 143 |
| 144 |
| 145 def main(): |
| 146 parser = argparse.ArgumentParser( |
| 147 description="Upload service mojoms and binaries to Google storage") |
| 148 parser.add_argument("-n", "--dry-run", action="store_true", help="Dry run") |
| 149 parser.add_argument( |
| 150 "--linux-x64-binary-dir", |
| 151 help="Path to the dir containing the linux-x64 service binary relative " |
| 152 "to the repo root, e.g. out/Release") |
| 153 parser.add_argument( |
| 154 "--android-arm-binary-dir", |
| 155 help="Path to the dir containing the android-arm service binary relative " |
| 156 "to the repo root, e.g. out/android_Release") |
| 157 parser.add_argument("service", |
| 158 help="The service to be uploaded (one of %s)" % SERVICES) |
| 159 parser.add_argument( |
| 160 "--custom-build", action="store_true", |
| 161 help="Indicates that this is a build with change that is not committed. " |
| 162 "The change must be uploaded to Rietveld. The script needs to be " |
| 163 "run from the branch associated with the change.") |
| 164 parser.add_argument( |
| 165 "--upload-symbols", action="store_true", |
| 166 help="Indicates that this should also upload all symbols.") |
| 167 args = parser.parse_args() |
| 168 |
| 169 if args.service not in SERVICES: |
| 170 print args.service + " is not one of the recognized services:" |
| 171 print SERVICES |
| 172 return 1 |
| 173 |
| 174 version_name = get_version_name(args.custom_build) |
| 175 if args.custom_build and not version_name: |
| 176 print ("When uploading a custom build, the corresponding change to source " |
| 177 "code must be uploaded to Rietveld. Besides, this script needs to " |
| 178 "be run from the branch associated with the change.") |
| 179 return 1 |
| 180 |
| 181 if args.service in MOJOMS_IN_DIR: |
| 182 script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 183 absolute_mojom_directory_path = os.path.join(script_dir, |
| 184 MOJOMS_IN_DIR[args.service]) |
| 185 upload_mojoms(version_name, args.service, absolute_mojom_directory_path, |
| 186 args.dry_run) |
| 187 |
| 188 if args.linux_x64_binary_dir: |
| 189 upload_binary(version_name, args.service, args.linux_x64_binary_dir, |
| 190 "linux-x64", args.dry_run) |
| 191 if args.upload_symbols: |
| 192 upload_symbols(args.linux_x64_binary_dir, args.dry_run) |
| 193 |
| 194 if args.android_arm_binary_dir: |
| 195 upload_binary(version_name, args.service, args.android_arm_binary_dir, |
| 196 "android-arm", args.dry_run) |
| 197 if args.upload_symbols: |
| 198 upload_symbols(args.android_arm_binary_dir, args.dry_run) |
| 199 |
| 200 if not args.dry_run: |
| 201 print "Uploaded artifacts for version %s" % (version_name, ) |
| 202 else: |
| 203 print "No artifacts uploaded (dry run)" |
| 204 return 0 |
| 205 |
| 206 if __name__ == '__main__': |
| 207 sys.exit(main()) |
OLD | NEW |