OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import argparse | 6 import argparse |
7 import imp | 7 import imp |
8 import os | 8 import os |
9 import subprocess | 9 import subprocess |
10 import sys | 10 import sys |
11 import tempfile | 11 import tempfile |
12 import time | 12 import time |
13 import zipfile | 13 import zipfile |
14 | 14 |
15 # A service's name is defined as the name of its subdirectory in the directory | |
16 # containing this file. | |
ppi
2015/02/03 16:08:09
nit: note that it also defines the gs path that th
blundell
2015/02/03 16:19:40
I actually would prefer not to do this unless you
| |
17 SERVICES = [ "network", "html_viewer" ] | |
18 | |
19 SERVICE_BINARY_NAMES = { | |
20 "network" : "network_service.mojo", | |
21 "html_viewer" : "html_viewer.mojo" | |
22 } | |
23 | |
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" ] | |
29 | |
15 if not sys.platform.startswith("linux"): | 30 if not sys.platform.startswith("linux"): |
16 print "Only support linux for now" | 31 print "Only support linux for now" |
17 sys.exit(1) | 32 sys.exit(1) |
18 | 33 |
19 root_path = os.path.realpath( | 34 root_path = os.path.realpath( |
20 os.path.join( | 35 os.path.join( |
21 os.path.dirname( | 36 os.path.dirname( |
22 os.path.realpath(__file__)), | 37 os.path.realpath(__file__)), |
23 os.pardir, | 38 os.pardir, |
24 os.pardir, | |
25 os.pardir)) | 39 os.pardir)) |
26 version = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=root_path) | 40 version = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=root_path) |
27 version = version.strip() | 41 version = version.strip() |
28 | 42 |
29 find_depot_tools_path = os.path.join(root_path, "tools", "find_depot_tools.py") | 43 find_depot_tools_path = os.path.join(root_path, "tools", "find_depot_tools.py") |
30 find_depot_tools = imp.load_source("find_depot_tools", find_depot_tools_path) | 44 find_depot_tools = imp.load_source("find_depot_tools", find_depot_tools_path) |
31 | 45 |
32 depot_tools_path = find_depot_tools.add_depot_tools_to_path() | 46 depot_tools_path = find_depot_tools.add_depot_tools_to_path() |
33 gsutil_exe = os.path.join(depot_tools_path, "third_party", "gsutil", "gsutil") | 47 gsutil_exe = os.path.join(depot_tools_path, "third_party", "gsutil", "gsutil") |
34 | 48 |
35 | 49 |
36 def gsutil_cp(source, dest, dry_run): | 50 def gsutil_cp(source, dest, dry_run): |
37 if dry_run: | 51 if dry_run: |
38 print "gsutil cp %s %s" % (source, dest) | 52 print "gsutil cp %s %s" % (source, dest) |
39 else: | 53 else: |
40 subprocess.check_call([gsutil_exe, "cp", source, dest]) | 54 subprocess.check_call([gsutil_exe, "cp", source, dest]) |
41 | 55 |
42 | 56 |
43 def upload_mojoms(dry_run): | 57 def upload_mojoms(service, dry_run): |
58 script_dir = os.path.dirname(os.path.realpath(__file__)) | |
59 service_dir = os.path.join(script_dir, service) | |
44 absolute_mojom_directory_path = os.path.join( | 60 absolute_mojom_directory_path = os.path.join( |
45 os.path.dirname(os.path.realpath(__file__)), | 61 service_dir, |
46 "public", | 62 "public", |
47 "interfaces") | 63 "interfaces") |
48 dest = "gs://mojo/network/" + version + "/" + "mojoms.zip" | 64 |
65 if not os.path.exists(absolute_mojom_directory_path): | |
66 # This service has no interfaces. | |
67 return | |
68 | |
69 dest = "gs://mojo/" + service + "/" + version + "/" + "mojoms.zip" | |
49 with tempfile.NamedTemporaryFile() as mojom_zip_file: | 70 with tempfile.NamedTemporaryFile() as mojom_zip_file: |
50 with zipfile.ZipFile(mojom_zip_file, 'w') as z: | 71 with zipfile.ZipFile(mojom_zip_file, 'w') as z: |
51 for root, _, files in os.walk(absolute_mojom_directory_path): | 72 for root, _, files in os.walk(absolute_mojom_directory_path): |
52 for filename in files: | 73 for filename in files: |
53 absolute_file_path = os.path.join(root, filename) | 74 absolute_file_path = os.path.join(root, filename) |
54 relative_file_path = os.path.relpath(absolute_file_path, root) | 75 relative_file_path = os.path.relpath(absolute_file_path, root) |
55 z.write(absolute_file_path, relative_file_path) | 76 z.write(absolute_file_path, relative_file_path) |
56 gsutil_cp(mojom_zip_file.name, dest, dry_run) | 77 gsutil_cp(mojom_zip_file.name, dest, dry_run) |
57 | 78 |
58 | 79 |
59 def upload_binary(binary_path, platform, dry_run): | 80 def upload_binary(service, binary_dir, platform, dry_run): |
60 absolute_binary_path = os.path.join(root_path, binary_path) | 81 binary_name = SERVICE_BINARY_NAMES[service] |
61 binary_dest = "gs://mojo/network/" + version + "/" + platform + ".zip" | 82 absolute_binary_path = os.path.join(root_path, binary_dir, binary_name) |
83 binary_dest_prefix = "gs://mojo/" + service + "/" + version + "/" + platform | |
84 | |
85 if service not in SERVICES_WITH_ZIPPED_BINARIES: | |
86 binary_dest = binary_dest_prefix + "/" + binary_name | |
87 gsutil_cp(absolute_binary_path, binary_dest, dry_run) | |
88 return | |
89 | |
90 # Zip the binary before uploading it to the cloud. | |
91 binary_dest = binary_dest_prefix + ".zip" | |
62 with tempfile.NamedTemporaryFile() as binary_zip_file: | 92 with tempfile.NamedTemporaryFile() as binary_zip_file: |
63 with zipfile.ZipFile(binary_zip_file, 'w') as z: | 93 with zipfile.ZipFile(binary_zip_file, 'w') as z: |
64 with open(absolute_binary_path) as service_binary: | 94 with open(absolute_binary_path) as service_binary: |
65 zipinfo = zipfile.ZipInfo("network_service.mojo") | 95 zipinfo = zipfile.ZipInfo(binary_name) |
66 zipinfo.external_attr = 0o777 << 16 | 96 zipinfo.external_attr = 0o777 << 16 |
67 zipinfo.compress_type = zipfile.ZIP_DEFLATED | 97 zipinfo.compress_type = zipfile.ZIP_DEFLATED |
68 zipinfo.date_time = time.gmtime(os.path.getmtime(absolute_binary_path)) | 98 zipinfo.date_time = time.gmtime(os.path.getmtime(absolute_binary_path)) |
69 z.writestr(zipinfo, service_binary.read()) | 99 z.writestr(zipinfo, service_binary.read()) |
70 gsutil_cp(binary_zip_file.name, binary_dest, dry_run) | 100 gsutil_cp(binary_zip_file.name, binary_dest, dry_run) |
71 | 101 |
72 | 102 |
73 def main(): | 103 def main(): |
74 parser = argparse.ArgumentParser( | 104 parser = argparse.ArgumentParser( |
75 description="Upload network service mojoms and binaries to Google " + | 105 description="Upload service mojoms and binaries to Google storage") |
76 "storage") | |
77 parser.add_argument("-n", "--dry-run", action="store_true", help="Dry run") | 106 parser.add_argument("-n", "--dry-run", action="store_true", help="Dry run") |
78 parser.add_argument( | 107 parser.add_argument( |
79 "--linux-x64-binary-path", | 108 "--linux-x64-binary-dir", |
80 help="Path to the linux-x64 network service binary relative to the " + | 109 help="Path to the dir containing the linux-x64 service binary relative " |
81 "repo root, e.g. out/Release/network_service.mojo") | 110 "to the repo root, e.g. out/Release") |
82 parser.add_argument( | 111 parser.add_argument( |
83 "--android-arm-binary-path", | 112 "--android-arm-binary-dir", |
84 help="Path to the android-arm network service binary relative to the " + | 113 help="Path to the dir containing the android-arm service binary relative " |
85 "repo root, e.g. out/android_Release/network_service.mojo") | 114 "to the repo root, e.g. out/android_Release") |
115 parser.add_argument("service", | |
ppi
2015/02/03 16:08:09
The mandatory argument should go first I think.
ppi
2015/02/03 16:18:20
As discussed live, turn's out it shouldn't, please
| |
116 help="The service to be uploaded (one of %s)" % SERVICES) | |
86 | 117 |
87 args = parser.parse_args() | 118 args = parser.parse_args() |
88 upload_mojoms(args.dry_run) | 119 |
89 if args.linux_x64_binary_path: | 120 if args.service not in SERVICES: |
90 upload_binary(args.linux_x64_binary_path, "linux-x64", args.dry_run) | 121 print args.service + " is not one of the recognized services:" |
91 if args.android_arm_binary_path: | 122 print SERVICES |
92 upload_binary(args.android_arm_binary_path, "android-arm", args.dry_run) | 123 sys.exit(1) |
jamesr
2015/02/03 18:29:57
return 1, don't sys.exit().
blundell
2015/02/04 10:04:18
Done.
| |
124 | |
125 upload_mojoms(args.service, args.dry_run) | |
126 if args.linux_x64_binary_dir: | |
127 upload_binary(args.service, args.linux_x64_binary_dir, | |
128 "linux-x64", args.dry_run) | |
129 if args.android_arm_binary_dir: | |
130 upload_binary(args.service, args.android_arm_binary_dir, | |
131 "android-arm", args.dry_run) | |
93 | 132 |
94 if not args.dry_run: | 133 if not args.dry_run: |
95 print "Uploaded artifacts for version %s" % (version, ) | 134 print "Uploaded artifacts for version %s" % (version, ) |
96 else: | 135 else: |
97 print "No artifacts uploaded (dry run)" | 136 print "No artifacts uploaded (dry run)" |
98 return 0 | 137 return 0 |
99 | 138 |
100 if __name__ == '__main__': | 139 if __name__ == '__main__': |
101 sys.exit(main()) | 140 sys.exit(main()) |
OLD | NEW |