Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / mojo / services / upload_service.py
blobea528b3a39f7361fff98489773a6d997250c38a1
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.
6 import argparse
7 import imp
8 import os
9 import subprocess
10 import sys
11 import tempfile
12 import time
13 import zipfile
15 SERVICES = ["html_viewer", "network_service", "network_service_apptests"]
17 # A service does not need to expose interfaces. Those that do expose interfaces
18 # have their mojoms located in the directories listed below, in paths relative
19 # to the directory of this script.
20 MOJOMS_IN_DIR = {
21 "network_service": os.path.join("network", "public", "interfaces")
24 # The network service is downloaded out-of-band rather than dynamically by the
25 # shell and thus can be stored zipped in the cloud. Other services are intended
26 # to be downloaded dynamically by the shell, which doesn't currently understand
27 # zipped binaries.
28 SERVICES_WITH_ZIPPED_BINARIES = ["network_service", "network_service_apptests"]
30 if not sys.platform.startswith("linux"):
31 print "Only support linux for now"
32 sys.exit(1)
34 root_path = os.path.realpath(
35 os.path.join(
36 os.path.dirname(
37 os.path.realpath(__file__)),
38 os.pardir,
39 os.pardir))
40 version = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=root_path)
41 version = version.strip()
43 find_depot_tools_path = os.path.join(root_path, "tools", "find_depot_tools.py")
44 find_depot_tools = imp.load_source("find_depot_tools", find_depot_tools_path)
46 depot_tools_path = find_depot_tools.add_depot_tools_to_path()
47 gsutil_exe = os.path.join(depot_tools_path, "third_party", "gsutil", "gsutil")
50 def gsutil_cp(source, dest, dry_run):
51 if dry_run:
52 print "gsutil cp %s %s" % (source, dest)
53 else:
54 subprocess.check_call([gsutil_exe, "cp", source, dest])
57 def upload_mojoms(service, absolute_mojom_directory_path, dry_run):
58 dest = "gs://mojo/" + service + "/" + version + "/" + "mojoms.zip"
60 with tempfile.NamedTemporaryFile() as mojom_zip_file:
61 with zipfile.ZipFile(mojom_zip_file, 'w') as z:
62 for root, _, files in os.walk(absolute_mojom_directory_path):
63 for filename in files:
64 absolute_file_path = os.path.join(root, filename)
65 relative_file_path = os.path.relpath(absolute_file_path, root)
66 z.write(absolute_file_path, relative_file_path)
67 gsutil_cp(mojom_zip_file.name, dest, dry_run)
70 def upload_binary(service, binary_dir, platform, dry_run):
71 dest_dir = "gs://mojo/" + service + "/" + version + "/" + platform + "/"
72 should_zip = service in SERVICES_WITH_ZIPPED_BINARIES
73 binary_name = service + ".mojo"
74 absolute_binary_path = os.path.join(root_path, binary_dir, binary_name)
76 if not should_zip:
77 dest = dest_dir + binary_name
78 gsutil_cp(absolute_binary_path, dest, dry_run)
79 return
81 # Zip the binary before uploading it to the cloud.
82 dest = dest_dir + binary_name + ".zip"
83 with tempfile.NamedTemporaryFile() as binary_zip_file:
84 with zipfile.ZipFile(binary_zip_file, 'w') as z:
85 with open(absolute_binary_path) as service_binary:
86 zipinfo = zipfile.ZipInfo(binary_name)
87 zipinfo.external_attr = 0o777 << 16
88 zipinfo.compress_type = zipfile.ZIP_DEFLATED
89 zipinfo.date_time = time.gmtime(os.path.getmtime(absolute_binary_path))
90 z.writestr(zipinfo, service_binary.read())
91 gsutil_cp(binary_zip_file.name, dest, dry_run)
94 def main():
95 parser = argparse.ArgumentParser(
96 description="Upload service mojoms and binaries to Google storage")
97 parser.add_argument("-n", "--dry-run", action="store_true", help="Dry run")
98 parser.add_argument(
99 "--linux-x64-binary-dir",
100 help="Path to the dir containing the linux-x64 service binary relative "
101 "to the repo root, e.g. out/Release")
102 parser.add_argument(
103 "--android-arm-binary-dir",
104 help="Path to the dir containing the android-arm service binary relative "
105 "to the repo root, e.g. out/android_Release")
106 parser.add_argument("service",
107 help="The service to be uploaded (one of %s)" % SERVICES)
109 args = parser.parse_args()
111 if args.service not in SERVICES:
112 print args.service + " is not one of the recognized services:"
113 print SERVICES
114 return 1
116 if args.service in MOJOMS_IN_DIR:
117 script_dir = os.path.dirname(os.path.realpath(__file__))
118 absolute_mojom_directory_path = os.path.join(script_dir,
119 MOJOMS_IN_DIR[args.service])
120 upload_mojoms(args.service, absolute_mojom_directory_path, args.dry_run)
122 if args.linux_x64_binary_dir:
123 upload_binary(args.service, args.linux_x64_binary_dir,
124 "linux-x64", args.dry_run)
126 if args.android_arm_binary_dir:
127 upload_binary(args.service, args.android_arm_binary_dir,
128 "android-arm", args.dry_run)
130 if not args.dry_run:
131 print "Uploaded artifacts for version %s" % (version, )
132 else:
133 print "No artifacts uploaded (dry run)"
134 return 0
136 if __name__ == '__main__':
137 sys.exit(main())