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 if not sys.platform.startswith("linux"): | |
14 print "Not supported for your platform" | |
15 sys.exit(0) | |
16 | |
17 | |
18 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 | |
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") | |
32 with open(version_path) as version_file: | |
33 version = version_file.read().strip() | |
34 | |
35 try: | |
36 with open(stamp_path) as stamp_file: | |
37 current_version = stamp_file.read().strip() | |
38 if current_version == version: | |
39 return 0 # Already have the right version. | |
40 except IOError: | |
41 pass # If the stamp file does not exist we need to download a new binary. | |
42 | |
43 platform = "linux-x64" # TODO: configurate | |
44 basename = platform + ".zip" | |
45 | |
46 gs_path = "gs://mojo/shell/" + version + "/" + basename | |
47 | |
48 with tempfile.NamedTemporaryFile() as temp_zip_file: | |
49 # 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 | |
51 # that the gsutil script will try (and fail) to use. Setting these | |
52 # 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 | |
54 # 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 | |
56 # throw the output away if the script succeeds (return code 0). | |
57 env = os.environ.copy() | |
58 env["AWS_CREDENTIAL_FILE"] = "" | |
59 env["BOTO_CONFIG"] = "" | |
60 try: | |
61 subprocess.check_output( | |
62 [gsutil_exe, | |
63 "--bypass_prodaccess", | |
64 "cp", | |
65 gs_path, | |
66 temp_zip_file.name], | |
67 stderr=subprocess.STDOUT, | |
68 env=env) | |
69 except subprocess.CalledProcessError as e: | |
70 print e.output | |
71 sys.exit(1) | |
72 | |
73 with zipfile.ZipFile(temp_zip_file.name) as z: | |
74 zi = z.getinfo("mojo_shell") | |
75 mode = zi.external_attr >> 16 | |
76 z.extract(zi, prebuilt_file_path) | |
77 os.chmod(os.path.join(prebuilt_file_path, "mojo_shell"), mode) | |
78 | |
79 with open(stamp_path, 'w') as stamp_file: | |
80 stamp_file.write(version) | |
81 return 0 | |
82 | |
83 | |
84 def main(): | |
85 parser = argparse.ArgumentParser(description="Download mojo_shell binary " | |
86 "from google storage") | |
87 parser.add_argument("--tools-directory", | |
88 dest="tools_directory", | |
89 metavar="<tools-directory>", | |
90 type=str, | |
91 help="Path to the directory containing" | |
92 " find_depot_tools.py, specified as a relative path" | |
93 " from the location of this file.") | |
94 args = parser.parse_args() | |
95 if not args.tools_directory: | |
96 print "Must specify --tools_directory; please see help message." | |
97 sys.exit(1) | |
98 return download(args.tools_directory) | |
99 | |
100 if __name__ == "__main__": | |
101 sys.exit(main()) | |
OLD | NEW |