| 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 | 14 |
| 15 if not sys.platform.startswith("linux"): | 15 if not sys.platform.startswith("linux"): |
| 16 print "Not supported for your platform" | 16 print "Not supported for your platform" |
| 17 sys.exit(0) | 17 sys.exit(0) |
| 18 | 18 |
| 19 # Add //tools to the system path so that we can import the gs helper. | 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__)) | 20 script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 21 |
| 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. |
| 21 tools_path = os.path.join(script_dir, os.pardir, os.pardir, os.pardir, "tools") | 24 tools_path = os.path.join(script_dir, os.pardir, os.pardir, os.pardir, "tools") |
| 22 sys.path.insert(0, tools_path) | 25 sys.path.insert(0, tools_path) |
| 23 # pylint: disable=F0401 | 26 # pylint: disable=F0401 |
| 24 import gs | 27 import gs |
| 25 | 28 |
| 26 | 29 |
| 27 def download_app(app, version, platform, prebuilt_directory): | 30 def download_app(app, version): |
| 31 prebuilt_directory = os.path.join(script_dir, "prebuilt/%s" % app) |
| 32 stamp_path = os.path.join(prebuilt_directory, "VERSION") |
| 33 |
| 34 try: |
| 35 with open(stamp_path) as stamp_file: |
| 36 current_version = stamp_file.read().strip() |
| 37 if current_version == version: |
| 38 return # Already have the right version. |
| 39 except IOError: |
| 40 pass # If the stamp file does not exist we need to download a new binary. |
| 41 |
| 42 for platform in _PLATFORMS: |
| 43 download_app_for_platform(app, version, platform) |
| 44 |
| 45 with open(stamp_path, 'w') as stamp_file: |
| 46 stamp_file.write(version) |
| 47 |
| 48 def download_app_for_platform(app, version, platform): |
| 28 binary_name = app + ".mojo" | 49 binary_name = app + ".mojo" |
| 29 gs_path = "gs://mojo/%s/%s/%s/%s.zip" % (app, version, platform, binary_name) | 50 gs_path = "gs://mojo/%s/%s/%s/%s.zip" % (app, version, platform, binary_name) |
| 30 output_directory = os.path.join(prebuilt_directory, platform) | 51 output_directory = os.path.join(script_dir, |
| 52 "prebuilt/%s/%s" % (app, platform)) |
| 31 | 53 |
| 32 with tempfile.NamedTemporaryFile() as temp_zip_file: | 54 with tempfile.NamedTemporaryFile() as temp_zip_file: |
| 33 gs.download_from_public_bucket(gs_path, temp_zip_file.name) | 55 gs.download_from_public_bucket(gs_path, temp_zip_file.name) |
| 34 with zipfile.ZipFile(temp_zip_file.name) as z: | 56 with zipfile.ZipFile(temp_zip_file.name) as z: |
| 35 zi = z.getinfo(binary_name) | 57 zi = z.getinfo(binary_name) |
| 36 mode = zi.external_attr >> 16 | 58 mode = zi.external_attr >> 16 |
| 37 z.extract(zi, output_directory) | 59 z.extract(zi, output_directory) |
| 38 os.chmod(os.path.join(output_directory, binary_name), mode) | 60 os.chmod(os.path.join(output_directory, binary_name), mode) |
| 39 | 61 |
| 40 | |
| 41 def main(): | 62 def main(): |
| 42 parser = argparse.ArgumentParser( | 63 parser = argparse.ArgumentParser( |
| 43 description="Download prebuilt network service binaries from google " + | 64 description="Download prebuilt network service binaries from google " + |
| 44 "storage") | 65 "storage") |
| 45 parser.parse_args() | 66 parser.parse_args() |
| 46 | 67 |
| 47 prebuilt_directory_path = os.path.join(script_dir, "prebuilt") | 68 version_path = os.path.join(script_dir, "NETWORK_SERVICE_VERSION") |
| 48 stamp_path = os.path.join(prebuilt_directory_path, "STAMP") | |
| 49 version_path = os.path.join(script_dir, "VERSION") | |
| 50 with open(version_path) as version_file: | 69 with open(version_path) as version_file: |
| 51 version = version_file.read().strip() | 70 version = version_file.read().strip() |
| 52 | 71 |
| 53 try: | 72 for app in _APPS: |
| 54 with open(stamp_path) as stamp_file: | 73 download_app(app, version) |
| 55 current_version = stamp_file.read().strip() | |
| 56 if current_version == version: | |
| 57 return 0 # Already have the right version. | |
| 58 except IOError: | |
| 59 pass # If the stamp file does not exist we need to download a new binary. | |
| 60 | 74 |
| 61 for platform in _PLATFORMS: | |
| 62 for app in _APPS: | |
| 63 download_app(app, version, platform, prebuilt_directory_path) | |
| 64 | |
| 65 with open(stamp_path, 'w') as stamp_file: | |
| 66 stamp_file.write(version) | |
| 67 return 0 | 75 return 0 |
| 68 | 76 |
| 69 | 77 |
| 70 if __name__ == "__main__": | 78 if __name__ == "__main__": |
| 71 sys.exit(main()) | 79 sys.exit(main()) |
| OLD | NEW |