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