Chromium Code Reviews| 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 |
| 17 | 22 |
| 18 def download(tools_directory): | 23 def download(tools_directory): |
| 19 current_path = os.path.dirname(os.path.realpath(__file__)) | 24 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") | 25 prebuilt_file_path = os.path.join(current_path, "prebuilt") |
| 26 stamp_path = os.path.join(prebuilt_file_path, "VERSION") | 26 stamp_path = os.path.join(prebuilt_file_path, "VERSION") |
| 27 | 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") | 28 version_path = os.path.join(current_path, "../VERSION") |
| 32 with open(version_path) as version_file: | 29 with open(version_path) as version_file: |
| 33 version = version_file.read().strip() | 30 version = version_file.read().strip() |
| 34 | 31 |
| 35 try: | 32 try: |
| 36 with open(stamp_path) as stamp_file: | 33 with open(stamp_path) as stamp_file: |
| 37 current_version = stamp_file.read().strip() | 34 current_version = stamp_file.read().strip() |
| 38 if current_version == version: | 35 if current_version == version: |
| 39 return 0 # Already have the right version. | 36 return 0 # Already have the right version. |
| 40 except IOError: | 37 except IOError: |
| 41 pass # If the stamp file does not exist we need to download a new binary. | 38 pass # If the stamp file does not exist we need to download new binaries. |
| 42 | 39 |
| 43 platform = "linux-x64" # TODO: configurate | 40 for platform in ["linux-x64", "android-arm"]: |
| 41 download_version_for_platform(version, platform, tools_directory) | |
| 42 | |
| 43 with open(stamp_path, 'w') as stamp_file: | |
| 44 stamp_file.write(version) | |
|
qsr
2015/02/06 14:37:37
Are you missing a return value after this?
blundell
2015/02/06 14:41:09
Done.
| |
| 45 | |
| 46 def download_version_for_platform(version, platform, tools_directory): | |
| 47 current_path = os.path.dirname(os.path.realpath(__file__)) | |
| 48 prebuilt_file_path = os.path.join(current_path, "prebuilt") | |
| 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 | 91 |
| 79 with open(stamp_path, 'w') as stamp_file: | |
| 80 stamp_file.write(version) | |
| 81 return 0 | 92 return 0 |
|
qsr
2015/02/06 14:37:37
And you do not seem to be using this one.
blundell
2015/02/06 14:41:09
whoops, restored the return 0 to the parent functi
| |
| 82 | 93 |
| 83 | 94 |
| 84 def main(): | 95 def main(): |
| 85 parser = argparse.ArgumentParser(description="Download mojo_shell binary " | 96 parser = argparse.ArgumentParser(description="Download mojo_shell binaries " |
| 86 "from google storage") | 97 "from google storage") |
| 87 parser.add_argument("--tools-directory", | 98 parser.add_argument("--tools-directory", |
| 88 dest="tools_directory", | 99 dest="tools_directory", |
| 89 metavar="<tools-directory>", | 100 metavar="<tools-directory>", |
| 90 type=str, | 101 type=str, |
| 91 help="Path to the directory containing" | 102 help="Path to the directory containing" |
| 92 " find_depot_tools.py, specified as a relative path" | 103 " find_depot_tools.py, specified as a relative path" |
| 93 " from the location of this file.") | 104 " from the location of this file.") |
| 94 args = parser.parse_args() | 105 args = parser.parse_args() |
| 95 if not args.tools_directory: | 106 if not args.tools_directory: |
| 96 print "Must specify --tools_directory; please see help message." | 107 print "Must specify --tools_directory; please see help message." |
| 97 sys.exit(1) | 108 sys.exit(1) |
| 98 return download(args.tools_directory) | 109 return download(args.tools_directory) |
| 99 | 110 |
| 100 if __name__ == "__main__": | 111 if __name__ == "__main__": |
| 101 sys.exit(main()) | 112 sys.exit(main()) |
| OLD | NEW |