OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """ |
| 7 Downloads Go binaries from Google Cloud Storage and extracts them to |
| 8 INSTALL_DIR, updating INSTALL_DIR/VERSION stamp file with current version. |
| 9 Does nothing if INSTALL_DIR/VERSION is already up to date. |
| 10 """ |
| 11 |
| 12 import os |
| 13 import shutil |
| 14 import subprocess |
| 15 import sys |
| 16 import tarfile |
| 17 |
| 18 # Path constants. (All of these should be absolute paths.) |
| 19 THIS_DIR = os.path.abspath(os.path.dirname(__file__)) |
| 20 MOJO_DIR = os.path.abspath(os.path.join(THIS_DIR, '..', '..')) |
| 21 # Should be the same as in upload.py. |
| 22 INSTALL_DIR = os.path.join(MOJO_DIR, 'third_party', 'go', 'tool') |
| 23 |
| 24 sys.path.insert(0, os.path.join(MOJO_DIR, 'tools')) |
| 25 import find_depot_tools |
| 26 |
| 27 DEPOT_PATH = find_depot_tools.add_depot_tools_to_path() |
| 28 GSUTIL_PATH = os.path.join(DEPOT_PATH, 'gsutil.py') |
| 29 |
| 30 def RunCommand(command): |
| 31 """Run command and return success (True) or failure.""" |
| 32 |
| 33 print 'Running %s' % (str(command)) |
| 34 if subprocess.call(command, shell=False) == 0: |
| 35 return True |
| 36 print 'Failed.' |
| 37 return False |
| 38 |
| 39 def VersionFileName(): |
| 40 if sys.platform.startswith('linux'): |
| 41 platform_suffix = 'LINUX' |
| 42 elif sys.platform == 'darwin': |
| 43 platform_suffix = 'MACOSX' |
| 44 else: |
| 45 raise Exception('unsupported platform: ' + sys.platform) |
| 46 return 'VERSION_' + platform_suffix |
| 47 |
| 48 def GetInstalledVersion(): |
| 49 version_file = os.path.join(INSTALL_DIR, VersionFileName()) |
| 50 if not os.path.exists(version_file): |
| 51 return None |
| 52 with open(version_file) as f: |
| 53 return f.read().strip() |
| 54 |
| 55 def InstallGoBinaries(version): |
| 56 """Downloads zipped go binaries from Google Cloud Storage and extracts them, |
| 57 stamping current version.""" |
| 58 |
| 59 # Remove current installation. |
| 60 if os.path.exists(INSTALL_DIR): |
| 61 shutil.rmtree(INSTALL_DIR) |
| 62 os.makedirs(INSTALL_DIR) |
| 63 # Download go tool binaries from GCS. |
| 64 archive_path = os.path.join(INSTALL_DIR, 'go.tar.gz') |
| 65 download_cmd = ['python', GSUTIL_PATH, 'cp', |
| 66 'gs://mojo/go/tool/%s.tar.gz' % version, |
| 67 archive_path] |
| 68 if not RunCommand(download_cmd): |
| 69 print ('WARNING: Failed to download Go tool binaries.') |
| 70 return |
| 71 |
| 72 print "Extracting Go binaries." |
| 73 with tarfile.open(archive_path) as arch: |
| 74 arch.extractall(INSTALL_DIR) |
| 75 os.remove(archive_path) |
| 76 # Write version as the last step. |
| 77 with open(os.path.join(INSTALL_DIR, VersionFileName()), 'w+') as f: |
| 78 f.write('%s\n' % version) |
| 79 |
| 80 def main(): |
| 81 # Read latest version. |
| 82 version = '' |
| 83 with open(os.path.join(THIS_DIR, VersionFileName())) as f: |
| 84 version = f.read().strip() |
| 85 # Return if installed binaries are up to date. |
| 86 if version == GetInstalledVersion(): |
| 87 return |
| 88 InstallGoBinaries(version) |
| 89 |
| 90 if __name__ == '__main__': |
| 91 sys.exit(main()) |
OLD | NEW |