| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 2015 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 sys | 8 import sys |
| 9 import tempfile | 9 import tempfile |
| 10 import zipfile | 10 import zipfile |
| 11 | 11 |
| 12 _PLATFORMS = ["linux-x64", "android-arm"] | 12 _PLATFORMS = ["linux-x64", "android-arm"] |
| 13 _APPS = ["network_service", "network_service_apptests"] | 13 _APPS = ["network_service", "network_service_apptests"] |
| 14 _CURRENT_PATH = os.path.dirname(os.path.realpath(__file__)) |
| 15 sys.path.insert(0, os.path.join(_CURRENT_PATH, "pylib")) |
| 16 import gs |
| 14 | 17 |
| 15 if not sys.platform.startswith("linux"): | 18 if not sys.platform.startswith("linux"): |
| 16 print "Not supported for your platform" | 19 print "Not supported for your platform" |
| 17 sys.exit(0) | 20 sys.exit(0) |
| 18 | 21 |
| 19 # Add //tools to the system path so that we can import the gs helper. | |
| 20 script_dir = os.path.dirname(os.path.realpath(__file__)) | 22 script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 21 | 23 |
| 22 # TODO(blundell): Determine whether to move gs.py into the SDK or to inline the | |
| 23 # contents of that function in this file as is done in download_shell_binary.py. | |
| 24 tools_path = os.path.join(script_dir, os.pardir, os.pardir, os.pardir, "tools") | |
| 25 sys.path.insert(0, tools_path) | |
| 26 # pylint: disable=F0401 | |
| 27 import gs | |
| 28 | 24 |
| 29 | 25 def download_app(app, version, tools_directory): |
| 30 def download_app(app, version): | |
| 31 prebuilt_directory = os.path.join(script_dir, "prebuilt/%s" % app) | 26 prebuilt_directory = os.path.join(script_dir, "prebuilt/%s" % app) |
| 32 stamp_path = os.path.join(prebuilt_directory, "VERSION") | 27 stamp_path = os.path.join(prebuilt_directory, "VERSION") |
| 33 | 28 |
| 34 try: | 29 try: |
| 35 with open(stamp_path) as stamp_file: | 30 with open(stamp_path) as stamp_file: |
| 36 current_version = stamp_file.read().strip() | 31 current_version = stamp_file.read().strip() |
| 37 if current_version == version: | 32 if current_version == version: |
| 38 return # Already have the right version. | 33 return # Already have the right version. |
| 39 except IOError: | 34 except IOError: |
| 40 pass # If the stamp file does not exist we need to download a new binary. | 35 pass # If the stamp file does not exist we need to download a new binary. |
| 41 | 36 |
| 42 for platform in _PLATFORMS: | 37 for platform in _PLATFORMS: |
| 43 download_app_for_platform(app, version, platform) | 38 download_app_for_platform(app, version, platform, tools_directory) |
| 44 | 39 |
| 45 with open(stamp_path, 'w') as stamp_file: | 40 with open(stamp_path, 'w') as stamp_file: |
| 46 stamp_file.write(version) | 41 stamp_file.write(version) |
| 47 | 42 |
| 48 def download_app_for_platform(app, version, platform): | 43 def download_app_for_platform(app, version, platform, tools_directory): |
| 44 find_depot_tools_path = os.path.join(_CURRENT_PATH, tools_directory) |
| 45 sys.path.insert(0, find_depot_tools_path) |
| 46 # pylint: disable=F0401 |
| 47 import find_depot_tools |
| 48 depot_tools_path = find_depot_tools.add_depot_tools_to_path() |
| 49 |
| 49 binary_name = app + ".mojo" | 50 binary_name = app + ".mojo" |
| 50 gs_path = "gs://mojo/%s/%s/%s/%s.zip" % (app, version, platform, binary_name) | 51 gs_path = "gs://mojo/%s/%s/%s/%s.zip" % (app, version, platform, binary_name) |
| 51 output_directory = os.path.join(script_dir, | 52 output_directory = os.path.join(script_dir, |
| 52 "prebuilt/%s/%s" % (app, platform)) | 53 "prebuilt/%s/%s" % (app, platform)) |
| 53 | 54 |
| 54 with tempfile.NamedTemporaryFile() as temp_zip_file: | 55 with tempfile.NamedTemporaryFile() as temp_zip_file: |
| 55 gs.download_from_public_bucket(gs_path, temp_zip_file.name) | 56 gs.download_from_public_bucket(gs_path, temp_zip_file.name, |
| 57 depot_tools_path) |
| 56 with zipfile.ZipFile(temp_zip_file.name) as z: | 58 with zipfile.ZipFile(temp_zip_file.name) as z: |
| 57 zi = z.getinfo(binary_name) | 59 zi = z.getinfo(binary_name) |
| 58 mode = zi.external_attr >> 16 | 60 mode = zi.external_attr >> 16 |
| 59 z.extract(zi, output_directory) | 61 z.extract(zi, output_directory) |
| 60 os.chmod(os.path.join(output_directory, binary_name), mode) | 62 os.chmod(os.path.join(output_directory, binary_name), mode) |
| 61 | 63 |
| 62 def main(): | 64 def main(): |
| 63 parser = argparse.ArgumentParser( | 65 parser = argparse.ArgumentParser( |
| 64 description="Download prebuilt network service binaries from google " + | 66 description="Download prebuilt network service binaries from google " + |
| 65 "storage") | 67 "storage") |
| 66 parser.parse_args() | 68 parser.add_argument("--tools-directory", |
| 69 dest="tools_directory", |
| 70 metavar="<tools-directory>", |
| 71 type=str, |
| 72 required=True, |
| 73 help="Path to the directory containing " |
| 74 "find_depot_tools.py, specified as a relative path " |
| 75 "from the location of this file.") |
| 76 args = parser.parse_args() |
| 67 | 77 |
| 68 version_path = os.path.join(script_dir, "NETWORK_SERVICE_VERSION") | 78 version_path = os.path.join(script_dir, "NETWORK_SERVICE_VERSION") |
| 69 with open(version_path) as version_file: | 79 with open(version_path) as version_file: |
| 70 version = version_file.read().strip() | 80 version = version_file.read().strip() |
| 71 | 81 |
| 72 for app in _APPS: | 82 for app in _APPS: |
| 73 download_app(app, version) | 83 download_app(app, version, args.tools_directory) |
| 74 | 84 |
| 75 return 0 | 85 return 0 |
| 76 | 86 |
| 77 | 87 |
| 78 if __name__ == "__main__": | 88 if __name__ == "__main__": |
| 79 sys.exit(main()) | 89 sys.exit(main()) |
| OLD | NEW |