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 os | |
8 import subprocess | |
9 import sys | |
10 import tempfile | |
11 import zipfile | |
12 | |
13 BINARY_FOR_PLATFORM = { | |
14 "linux-x64" : "dart_snapshotter", | |
15 "mac-x64": "dart_snapshotter", | |
16 } | |
17 | |
18 CURRENT_PATH = os.path.dirname(os.path.realpath(__file__)) | |
19 sys.path.insert(0, os.path.join(CURRENT_PATH, "pylib")) | |
20 import gs | |
21 | |
22 PREBUILT_FILE_PATH = os.path.join(CURRENT_PATH, "prebuilt", "dart_snapshotter") | |
23 | |
24 | |
25 def download(tools_directory, version_file): | |
26 stamp_path = os.path.join(PREBUILT_FILE_PATH, "VERSION") | |
27 | |
28 version_path = os.path.join(CURRENT_PATH, version_file) | |
29 with open(version_path) as version_file: | |
30 version = version_file.read().strip() | |
31 | |
32 try: | |
33 with open(stamp_path) as stamp_file: | |
34 current_version = stamp_file.read().strip() | |
35 if current_version == version: | |
36 return 0 # Already have the right version. | |
37 except IOError: | |
38 pass # If the stamp file does not exist we need to download new binaries. | |
39 | |
40 if sys.platform.startswith("linux"): | |
41 platforms = ["linux-x64"] | |
42 elif sys.platform.startswith("darwin"): | |
43 platforms = ["mac-x64"] | |
44 else: | |
45 print "No prebuilt dart_snapshotter available for %s" % sys.platform | |
46 return 0 | |
47 | |
48 for platform in platforms: | |
49 download_version_for_platform(version, platform, tools_directory) | |
50 | |
51 with open(stamp_path, 'w') as stamp_file: | |
52 stamp_file.write(version) | |
53 return 0 | |
54 | |
55 def download_version_for_platform(version, platform, tools_directory): | |
56 find_depot_tools_path = os.path.join(CURRENT_PATH, tools_directory) | |
57 sys.path.insert(0, find_depot_tools_path) | |
58 # pylint: disable=F0401 | |
59 import find_depot_tools | |
60 depot_tools_path = find_depot_tools.add_depot_tools_to_path() | |
61 | |
62 basename = platform + ".zip" | |
63 gs_path = "gs://mojo/dart_snapshotter/" + version + "/" + basename | |
64 | |
65 with tempfile.NamedTemporaryFile() as temp_zip_file: | |
66 gs.download_from_public_bucket(gs_path, temp_zip_file.name, | |
67 depot_tools_path) | |
68 binary_name = BINARY_FOR_PLATFORM[platform] | |
69 output_dir = os.path.join(PREBUILT_FILE_PATH, platform) | |
70 with zipfile.ZipFile(temp_zip_file.name) as z: | |
71 zi = z.getinfo(binary_name) | |
72 mode = zi.external_attr >> 16 | |
73 z.extract(zi, output_dir) | |
74 os.chmod(os.path.join(output_dir, binary_name), mode) | |
75 | |
76 | |
77 def main(): | |
78 parser = argparse.ArgumentParser( | |
79 description="Download dart_snapshotter binaries from google storage") | |
80 parser.add_argument("--tools-directory", | |
81 dest="tools_directory", | |
82 metavar="<tools-directory>", | |
83 type=str, | |
84 required=True, | |
85 help="Path to the directory containing " | |
86 "find_depot_tools.py, specified as a relative path " | |
87 "from the location of this file.") | |
88 parser.add_argument("--version-file", | |
89 dest="version_file", | |
90 metavar="<version-file>", | |
91 type=str, | |
92 default="../VERSION", | |
93 help="Path to the file containing the version of the " | |
94 "dart_snapshotter to be fetched, specified as a" | |
95 "relative path from the location of this file " | |
96 "(default: %(default)s).") | |
97 args = parser.parse_args() | |
98 return download(args.tools_directory, args.version_file) | |
99 | |
100 if __name__ == "__main__": | |
101 sys.exit(main()) | |
OLD | NEW |