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 os | 7 import os |
8 import subprocess | 8 import subprocess |
9 import sys | 9 import sys |
10 import tempfile | 10 import tempfile |
11 import zipfile | 11 import zipfile |
12 | 12 |
13 BINARY_FOR_PLATFORM = { | 13 BINARY_FOR_PLATFORM = { |
14 "linux-x64" : "mojo_shell", | 14 "linux-x64" : "mojo_shell", |
15 "android-arm" : "MojoShell.apk" | 15 "android-arm" : "MojoShell.apk" |
16 } | 16 } |
17 | 17 |
18 if not sys.platform.startswith("linux"): | 18 if not sys.platform.startswith("linux"): |
19 print "Not supported for your platform" | 19 print "Not supported for your platform" |
20 sys.exit(0) | 20 sys.exit(0) |
21 | 21 |
22 CURRENT_PATH = os.path.dirname(os.path.realpath(__file__)) | 22 CURRENT_PATH = os.path.dirname(os.path.realpath(__file__)) |
| 23 sys.path.insert(0, os.path.join(CURRENT_PATH, "pylib")) |
| 24 import gs |
| 25 |
23 PREBUILT_FILE_PATH = os.path.join(CURRENT_PATH, "prebuilt", "shell") | 26 PREBUILT_FILE_PATH = os.path.join(CURRENT_PATH, "prebuilt", "shell") |
24 | 27 |
25 | 28 |
26 def download(tools_directory): | 29 def download(tools_directory): |
27 stamp_path = os.path.join(PREBUILT_FILE_PATH, "VERSION") | 30 stamp_path = os.path.join(PREBUILT_FILE_PATH, "VERSION") |
28 | 31 |
29 version_path = os.path.join(CURRENT_PATH, "../VERSION") | 32 version_path = os.path.join(CURRENT_PATH, "../VERSION") |
30 with open(version_path) as version_file: | 33 with open(version_path) as version_file: |
31 version = version_file.read().strip() | 34 version = version_file.read().strip() |
32 | 35 |
33 try: | 36 try: |
34 with open(stamp_path) as stamp_file: | 37 with open(stamp_path) as stamp_file: |
35 current_version = stamp_file.read().strip() | 38 current_version = stamp_file.read().strip() |
36 if current_version == version: | 39 if current_version == version: |
37 return 0 # Already have the right version. | 40 return 0 # Already have the right version. |
38 except IOError: | 41 except IOError: |
39 pass # If the stamp file does not exist we need to download new binaries. | 42 pass # If the stamp file does not exist we need to download new binaries. |
40 | 43 |
41 for platform in ["linux-x64", "android-arm"]: | 44 for platform in ["linux-x64", "android-arm"]: |
42 download_version_for_platform(version, platform, tools_directory) | 45 download_version_for_platform(version, platform, tools_directory) |
43 | 46 |
44 with open(stamp_path, 'w') as stamp_file: | 47 with open(stamp_path, 'w') as stamp_file: |
45 stamp_file.write(version) | 48 stamp_file.write(version) |
46 return 0 | 49 return 0 |
47 | 50 |
48 def download_version_for_platform(version, platform, tools_directory): | 51 def download_version_for_platform(version, platform, tools_directory): |
49 find_depot_tools_path = os.path.join(CURRENT_PATH, tools_directory) | 52 find_depot_tools_path = os.path.join(CURRENT_PATH, tools_directory) |
50 sys.path.insert(0, find_depot_tools_path) | |
51 # pylint: disable=F0401 | |
52 import find_depot_tools | |
53 | 53 |
54 basename = platform + ".zip" | 54 basename = platform + ".zip" |
55 gs_path = "gs://mojo/shell/" + version + "/" + basename | 55 gs_path = "gs://mojo/shell/" + version + "/" + basename |
56 | 56 |
57 depot_tools_path = find_depot_tools.add_depot_tools_to_path() | |
58 gsutil_exe = os.path.join(depot_tools_path, "third_party", "gsutil", "gsutil") | |
59 | |
60 with tempfile.NamedTemporaryFile() as temp_zip_file: | 57 with tempfile.NamedTemporaryFile() as temp_zip_file: |
61 # We're downloading from a public bucket which does not need authentication, | 58 gs.download_from_public_bucket(gs_path, temp_zip_file.name, |
62 # but the user might have busted credential files somewhere such as ~/.boto | 59 find_depot_tools_path) |
63 # that the gsutil script will try (and fail) to use. Setting these | |
64 # environment variables convinces gsutil not to attempt to use these, but | |
65 # also generates a useless warning about failing to load the file. We want | |
66 # to discard this warning but still preserve all output in the case of an | |
67 # actual failure. So, we run the script and capture all output and then | |
68 # throw the output away if the script succeeds (return code 0). | |
69 env = os.environ.copy() | |
70 env["AWS_CREDENTIAL_FILE"] = "" | |
71 env["BOTO_CONFIG"] = "" | |
72 try: | |
73 subprocess.check_output( | |
74 [gsutil_exe, | |
75 "--bypass_prodaccess", | |
76 "cp", | |
77 gs_path, | |
78 temp_zip_file.name], | |
79 stderr=subprocess.STDOUT, | |
80 env=env) | |
81 except subprocess.CalledProcessError as e: | |
82 print e.output | |
83 sys.exit(1) | |
84 | |
85 binary_name = BINARY_FOR_PLATFORM[platform] | 60 binary_name = BINARY_FOR_PLATFORM[platform] |
86 output_dir = os.path.join(PREBUILT_FILE_PATH, platform) | 61 output_dir = os.path.join(PREBUILT_FILE_PATH, platform) |
87 with zipfile.ZipFile(temp_zip_file.name) as z: | 62 with zipfile.ZipFile(temp_zip_file.name) as z: |
88 zi = z.getinfo(binary_name) | 63 zi = z.getinfo(binary_name) |
89 mode = zi.external_attr >> 16 | 64 mode = zi.external_attr >> 16 |
90 z.extract(zi, output_dir) | 65 z.extract(zi, output_dir) |
91 os.chmod(os.path.join(output_dir, binary_name), mode) | 66 os.chmod(os.path.join(output_dir, binary_name), mode) |
92 | 67 |
93 | 68 |
94 def main(): | 69 def main(): |
95 parser = argparse.ArgumentParser(description="Download mojo_shell binaries " | 70 parser = argparse.ArgumentParser(description="Download mojo_shell binaries " |
96 "from google storage") | 71 "from google storage") |
97 parser.add_argument("--tools-directory", | 72 parser.add_argument("--tools-directory", |
98 dest="tools_directory", | 73 dest="tools_directory", |
99 metavar="<tools-directory>", | 74 metavar="<tools-directory>", |
100 type=str, | 75 type=str, |
101 help="Path to the directory containing" | 76 required=True, |
102 " find_depot_tools.py, specified as a relative path" | 77 help="Path to the directory containing " |
103 " from the location of this file.") | 78 "find_depot_tools.py, specified as a relative path " |
| 79 "from the location of this file.") |
104 args = parser.parse_args() | 80 args = parser.parse_args() |
105 if not args.tools_directory: | |
106 print "Must specify --tools_directory; please see help message." | |
107 sys.exit(1) | |
108 return download(args.tools_directory) | 81 return download(args.tools_directory) |
109 | 82 |
110 if __name__ == "__main__": | 83 if __name__ == "__main__": |
111 sys.exit(main()) | 84 sys.exit(main()) |
OLD | NEW |