| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2017 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 """Tool to help developers rebase branches across the Blink rename.""" |
| 6 |
| 7 import json |
| 8 import os |
| 9 import subprocess |
| 10 import shutil |
| 11 import sys |
| 12 import tempfile |
| 13 |
| 14 |
| 15 class _DepotToolsNotFoundException(Exception): |
| 16 pass |
| 17 |
| 18 |
| 19 def _whereis(name): |
| 20 """Find and return the first entry in $PATH containing a file named |name|. |
| 21 |
| 22 Returns the path if found; otherwise returns nothing. |
| 23 """ |
| 24 for path in os.environ['PATH'].split(os.pathsep): |
| 25 if os.path.exists(os.path.join(path, name)): |
| 26 return path |
| 27 |
| 28 |
| 29 def _find_depot_tools(): |
| 30 """Attempts to configure and return a wrapper for invoking depot tools. |
| 31 |
| 32 Returns: |
| 33 A helper object for invoking depot tools. |
| 34 |
| 35 Raises: |
| 36 _DepotToolsNotFoundException: An error occurred trying to find depot tools. |
| 37 """ |
| 38 |
| 39 class DepotToolsWrapper(object): |
| 40 |
| 41 def __init__(self, path): |
| 42 self.__download_from_google_storage = os.path.join( |
| 43 path, 'download_from_google_storage.py') |
| 44 self.__gsutil = os.path.join(path, 'gsutil.py') |
| 45 |
| 46 def call_download_from_google_storage(self, *args): |
| 47 """Runs download_from_google_storage with the given args.""" |
| 48 subprocess.check_call(['python', self.__download_from_google_storage] + |
| 49 list(args)) |
| 50 |
| 51 def call_gsutil(self, *args): |
| 52 """Runs gsutil with the given args.""" |
| 53 subprocess.check_call(['python', self.__gsutil] + list(args)) |
| 54 |
| 55 # Attempt to find download_from_google_storage.py from depot_tools |
| 56 path = _whereis('download_from_google_storage.py') |
| 57 if not path: |
| 58 raise _DepotToolsNotFoundException( |
| 59 'download_from_google_storage.py not found. Make sure depot_tools is ' |
| 60 'in $PATH.') |
| 61 |
| 62 # Make sure gsutil.py is in the same location |
| 63 path2 = _whereis('download_from_google_storage.py') |
| 64 if not path2: |
| 65 raise _DepotToolsNotFoundException( |
| 66 'gsutil.py not found. Make sure depot_tools is in $PATH.') |
| 67 |
| 68 if path != path2: |
| 69 raise _DepotToolsNotFoundException( |
| 70 'download_from_google_storage.py found in %s but gsutil.py found in %s.' |
| 71 % (path, path2)) |
| 72 |
| 73 return DepotToolsWrapper(path) |
| 74 |
| 75 |
| 76 class Bootstrapper(object): |
| 77 """Helper class for bootstrapping startup of the rebase helper. |
| 78 |
| 79 Performs update checks and stages any required binaries.""" |
| 80 |
| 81 def __init__(self, depot_tools): |
| 82 """Bootstrapper constructor. |
| 83 |
| 84 Args: |
| 85 depot_tools: a wrapper for invoking depot_tools. |
| 86 """ |
| 87 self.__depot_tools = depot_tools |
| 88 self.__tmpdir = None |
| 89 |
| 90 def __enter__(self): |
| 91 self.__tmpdir = tempfile.mkdtemp() |
| 92 return self |
| 93 |
| 94 def __exit__(self, exc_type, exc_value, traceback): |
| 95 shutil.rmtree(self.__tmpdir, ignore_errors=True) |
| 96 |
| 97 def update(self): |
| 98 """Performs an update check for various components.""" |
| 99 components = self._get_latest_components() |
| 100 for name, sha1_hash in components.iteritems(): |
| 101 args = [ |
| 102 '--no_auth', '--no_resume', '-b', 'chromium-blink-rename', |
| 103 '--extract', sha1_hash |
| 104 ] |
| 105 if '-' in name: |
| 106 name, platform = name.split('-', 1) |
| 107 args.append('-p') |
| 108 args.append(platform) |
| 109 args.append('-o') |
| 110 args.append(os.path.join('staging', '%s.tar.gz' % name)) |
| 111 self.__depot_tools.call_download_from_google_storage(*args) |
| 112 |
| 113 def _get_latest_components(self): |
| 114 """Fetches info about the latest components from google storage. |
| 115 |
| 116 The return value should be a dict of component names to SHA1 hashes.""" |
| 117 hashes_path = os.path.join(self.__tmpdir, 'COMPONENTS') |
| 118 self.__depot_tools.call_gsutil( |
| 119 'cp', 'gs://chromium-blink-rename/COMPONENTS', hashes_path) |
| 120 with open(hashes_path) as f: |
| 121 return json.loads(f.read()) |
| 122 |
| 123 |
| 124 def main(): |
| 125 script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 126 os.chdir(script_dir) |
| 127 |
| 128 try: |
| 129 depot_tools = _find_depot_tools() |
| 130 except _DepotToolsNotFoundException as e: |
| 131 print e.message |
| 132 return 1 |
| 133 |
| 134 print 'Checking for updates...' |
| 135 with Bootstrapper(depot_tools) as bootstrapper: |
| 136 bootstrapper.update() |
| 137 |
| 138 # Import stage 2 and launch it. |
| 139 tool_pylib = os.path.abspath(os.path.join(script_dir, 'staging/pylib')) |
| 140 sys.path.insert(0, tool_pylib) |
| 141 from blink_rename_merge_helper import driver |
| 142 driver.run() |
| 143 |
| 144 |
| 145 if __name__ == '__main__': |
| 146 sys.exit(main()) |
| OLD | NEW |