Chromium Code Reviews| 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 tool helpers. | |
| 31 | |
| 32 Returns: | |
| 33 A helper object for invoking depot tool utils or None on failure.""" | |
| 34 | |
| 35 class DepotToolsWrapper(object): | |
| 36 | |
| 37 def __init__(self, path): | |
| 38 self.__download_from_google_storage = os.path.join( | |
| 39 path, 'download_from_google_storage.py') | |
| 40 self.__gsutil = os.path.join(path, 'gsutil.py') | |
| 41 | |
| 42 def call_download_from_google_storage(self, *args): | |
| 43 """Runs download_from_google_storage with the given args.""" | |
| 44 subprocess.check_call(['python', self.__download_from_google_storage] + | |
| 45 list(args)) | |
| 46 | |
| 47 def call_gsutil(self, *args): | |
| 48 """Runs gsutil with the given args.""" | |
| 49 subprocess.check_call(['python', self.__gsutil] + list(args)) | |
| 50 | |
| 51 # Attempt to find download_from_google_storage.py from depot_tools | |
| 52 path = _whereis('download_from_google_storage.py') | |
| 53 if not path: | |
| 54 print('error: download_from_google_storage.py not found. Make sure ' | |
| 55 'depot_tools is in $PATH.') | |
| 56 return | |
| 57 return DepotToolsWrapper(path) | |
| 58 | |
| 59 | |
| 60 class Bootstrapper(object): | |
| 61 """Helper class for bootstrapping startup of the rebase helper. | |
| 62 | |
| 63 Performs update checks and stages any required binaries.""" | |
| 64 | |
| 65 def __init__(self, depot_tools): | |
| 66 """Bootstrapper constructor. | |
| 67 | |
| 68 Args: | |
| 69 depot_tools: a wrapper for invoking depot_tools. | |
| 70 """ | |
| 71 self.__depot_tools = depot_tools | |
| 72 self.__tmpdir = None | |
| 73 | |
| 74 def __enter__(self): | |
| 75 self.__tmpdir = tempfile.mkdtemp() | |
| 76 return self | |
| 77 | |
| 78 def __exit__(self, exc_type, exc_value, traceback): | |
| 79 shutil.rmtree(self.__tmpdir, ignore_errors=True) | |
| 80 | |
| 81 def update(self): | |
| 82 """Performs an update check for various components.""" | |
| 83 components = self._get_latest_components() | |
| 84 for name, sha1_hash in components.iteritems(): | |
| 85 args = [ | |
| 86 '--no_auth', '--no_resume', '-b', 'chromium-blink-rename', | |
| 87 '--extract', sha1_hash | |
| 88 ] | |
| 89 if '-' in name: | |
| 90 name, platform = name.split('-', 1) | |
| 91 args.append('-p') | |
| 92 args.append(platform) | |
| 93 args.append('-o') | |
| 94 args.append(os.path.join('staging', '%s.tar.gz' % name)) | |
| 95 self.__depot_tools.call_download_from_google_storage(*args) | |
| 96 | |
| 97 def _get_latest_components(self): | |
| 98 """Fetches info about the latest components from google storage. | |
| 99 | |
| 100 The return value should be a dict of component names to SHA1 hashes.""" | |
| 101 hashes_path = os.path.join(self.__tmpdir, 'COMPONENTS') | |
| 102 self.__depot_tools.call_gsutil( | |
| 103 'cp', 'gs://chromium-blink-rename/COMPONENTS', hashes_path) | |
|
Nico
2017/04/05 20:24:43
Aha!
dcheng
2017/04/05 20:28:28
Yeah, sorry, I guess it wasn't clear enough from t
| |
| 104 with open(hashes_path) as f: | |
| 105 return json.loads(f.read()) | |
| 106 | |
| 107 | |
| 108 def main(): | |
| 109 script_dir = os.path.dirname(os.path.realpath(__file__)) | |
| 110 os.chdir(script_dir) | |
| 111 | |
| 112 try: | |
| 113 depot_tools = _find_depot_tools() | |
| 114 except _DepotToolsNotFoundException: | |
| 115 return 1 | |
| 116 | |
| 117 print 'Checking for updates...' | |
| 118 with Bootstrapper(depot_tools) as bootstrapper: | |
| 119 bootstrapper.update() | |
| 120 | |
| 121 # Import stage 2 and launch it. | |
| 122 tool_pylib = os.path.abspath(os.path.join(script_dir, 'staging/pylib')) | |
| 123 sys.path.insert(0, tool_pylib) | |
| 124 from blink_rename_merge_helper import driver | |
| 125 driver.run() | |
| 126 | |
| 127 | |
| 128 if __name__ == '__main__': | |
| 129 sys.exit(main()) | |
| OLD | NEW |