Kuang-che Wu | 6e4beca | 2018-06-27 17:45:02 +0800 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 2 | # Copyright 2018 The Chromium OS 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 | """Repo utility. |
| 6 | |
| 7 | This module provides wrapper for "repo" (a Google-built repository management |
| 8 | tool that runs on top of git) and related utility functions. |
| 9 | """ |
| 10 | |
| 11 | from __future__ import print_function |
| 12 | import logging |
| 13 | import os |
| 14 | import re |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 15 | import urlparse |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 16 | import xml.etree.ElementTree |
| 17 | |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 18 | from bisect_kit import codechange |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 19 | from bisect_kit import git_util |
| 20 | from bisect_kit import util |
| 21 | |
| 22 | logger = logging.getLogger(__name__) |
| 23 | |
| 24 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 25 | def get_manifest_url(manifest_dir): |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 26 | """Get manifest URL of repo project. |
| 27 | |
| 28 | Args: |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 29 | manifest_dir: path of manifest directory |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 30 | |
| 31 | Returns: |
| 32 | manifest URL. |
| 33 | """ |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 34 | url = util.check_output( |
| 35 | 'git', 'config', 'remote.origin.url', cwd=manifest_dir) |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 36 | return url |
| 37 | |
| 38 | |
Kuang-che Wu | 41e8b59 | 2018-09-25 17:01:30 +0800 | [diff] [blame] | 39 | def find_repo_root(path): |
| 40 | """Find the root path of a repo project |
| 41 | |
| 42 | Args: |
| 43 | path: path |
| 44 | |
| 45 | Returns: |
| 46 | project root if path is inside a repo project; otherwise None |
| 47 | """ |
| 48 | path = os.path.abspath(path) |
| 49 | while not os.path.exists(os.path.join(path, '.repo')): |
| 50 | if path == '/': |
| 51 | return None |
| 52 | path = os.path.dirname(path) |
| 53 | return path |
| 54 | |
| 55 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 56 | def init(repo_dir, |
| 57 | manifest_url, |
| 58 | manifest_branch=None, |
| 59 | manifest_name=None, |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 60 | repo_url=None, |
Kuang-che Wu | 41e8b59 | 2018-09-25 17:01:30 +0800 | [diff] [blame] | 61 | reference=None, |
| 62 | mirror=False): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 63 | """Repo init. |
| 64 | |
| 65 | Args: |
| 66 | repo_dir: root directory of repo |
| 67 | manifest_url: manifest repository location |
| 68 | manifest_branch: manifest branch or revision |
| 69 | manifest_name: initial manifest file name |
| 70 | repo_url: repo repository location |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 71 | reference: location of mirror directory |
Kuang-che Wu | 41e8b59 | 2018-09-25 17:01:30 +0800 | [diff] [blame] | 72 | mirror: indicates repo mirror |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 73 | """ |
Kuang-che Wu | 41e8b59 | 2018-09-25 17:01:30 +0800 | [diff] [blame] | 74 | root = find_repo_root(repo_dir) |
| 75 | if root and root != repo_dir: |
| 76 | raise Exception( |
| 77 | '%s should not be inside another repo project at %s' % (repo_dir, root)) |
| 78 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 79 | cmd = ['repo', 'init', '--manifest-url', manifest_url] |
| 80 | if manifest_name: |
| 81 | cmd += ['--manifest-name', manifest_name] |
| 82 | if manifest_branch: |
| 83 | cmd += ['--manifest-branch', manifest_branch] |
| 84 | if repo_url: |
| 85 | cmd += ['--repo-url', repo_url] |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 86 | if reference: |
| 87 | cmd += ['--reference', reference] |
Kuang-che Wu | 41e8b59 | 2018-09-25 17:01:30 +0800 | [diff] [blame] | 88 | if mirror: |
| 89 | cmd.append('--mirror') |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 90 | util.check_call(*cmd, cwd=repo_dir) |
| 91 | |
| 92 | |
Kuang-che Wu | ea3abce | 2018-10-04 17:50:42 +0800 | [diff] [blame] | 93 | def cleanup_repo_generated_files(repo_dir, manifest_name='default.xml'): |
| 94 | """Cleanup files generated by <copyfile> <linkfile> tags. |
| 95 | |
| 96 | Args: |
| 97 | repo_dir: root directory of repo |
| 98 | manifest_name: filename of manifest |
| 99 | """ |
| 100 | manifest_dir = os.path.join(repo_dir, '.repo', 'manifests') |
Kuang-che Wu | 35080a7 | 2018-10-05 14:14:33 +0800 | [diff] [blame] | 101 | manifest_path = os.path.join(manifest_dir, manifest_name) |
| 102 | if os.path.islink(manifest_path): |
| 103 | manifest_name = os.readlink(manifest_path) |
Kuang-che Wu | ea3abce | 2018-10-04 17:50:42 +0800 | [diff] [blame] | 104 | parser = ManifestParser(manifest_dir) |
| 105 | manifest = parser.parse_xml_recursive('HEAD', manifest_name) |
| 106 | |
| 107 | for copyfile in manifest.findall('.//copyfile'): |
| 108 | dest = copyfile.get('dest') |
| 109 | if not dest: |
| 110 | continue |
| 111 | # `dest` is relative to the top of the tree |
| 112 | dest_path = os.path.join(repo_dir, dest) |
| 113 | if not os.path.isfile(dest_path): |
| 114 | continue |
| 115 | logger.debug('delete file %r', dest_path) |
| 116 | os.unlink(dest_path) |
| 117 | |
| 118 | for linkfile in manifest.findall('.//linkfile'): |
| 119 | dest = linkfile.get('dest') |
| 120 | if not dest: |
| 121 | continue |
| 122 | # `dest` is relative to the top of the tree |
| 123 | dest_path = os.path.join(repo_dir, dest) |
| 124 | if not os.path.islink(dest_path): |
| 125 | continue |
| 126 | logger.debug('delete link %r', dest_path) |
| 127 | os.unlink(dest_path) |
| 128 | |
| 129 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 130 | def sync(repo_dir, jobs=16, manifest_name=None, current_branch=False): |
| 131 | """Repo sync. |
| 132 | |
| 133 | Args: |
| 134 | repo_dir: root directory of repo |
| 135 | jobs: projects to fetch simultaneously |
| 136 | manifest_name: filename of manifest |
| 137 | current_branch: fetch only current branch |
| 138 | """ |
Kuang-che Wu | ea3abce | 2018-10-04 17:50:42 +0800 | [diff] [blame] | 139 | # Workaround to prevent garbage files left between repo syncs |
| 140 | # (http://crbug.com/881783). |
| 141 | cleanup_repo_generated_files(repo_dir) |
| 142 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 143 | cmd = ['repo', 'sync', '-q', '--force-sync'] |
| 144 | if jobs: |
| 145 | cmd += ['-j', str(jobs)] |
| 146 | if manifest_name: |
| 147 | cmd += ['--manifest-name', manifest_name] |
| 148 | if current_branch: |
| 149 | cmd += ['--current-branch'] |
| 150 | util.check_call(*cmd, cwd=repo_dir) |
| 151 | |
| 152 | |
| 153 | def abandon(repo_dir, branch_name): |
| 154 | """Repo abandon. |
| 155 | |
| 156 | Args: |
| 157 | repo_dir: root directory of repo |
| 158 | branch_name: branch name to abandon |
| 159 | """ |
| 160 | # Ignore errors if failed, which means the branch didn't exist beforehand. |
| 161 | util.call('repo', 'abandon', branch_name, cwd=repo_dir) |
| 162 | |
| 163 | |
| 164 | def info(repo_dir, query): |
| 165 | """Repo info. |
| 166 | |
| 167 | Args: |
| 168 | repo_dir: root directory of repo |
| 169 | query: key to query |
| 170 | """ |
| 171 | for line in util.check_output('repo', 'info', '.', cwd=repo_dir).splitlines(): |
| 172 | key, value = map(str.strip, line.split(':')) |
| 173 | if key == query: |
| 174 | return value |
Kuang-che Wu | 89ac2e7 | 2018-07-25 17:39:07 +0800 | [diff] [blame] | 175 | |
| 176 | return None |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 177 | |
| 178 | |
| 179 | def get_current_branch(repo_dir): |
| 180 | """Get manifest branch of existing repo directory.""" |
| 181 | return info(repo_dir, 'Manifest branch') |
| 182 | |
| 183 | |
| 184 | def get_manifest_groups(repo_dir): |
| 185 | """Get manifest group of existing repo directory.""" |
| 186 | return info(repo_dir, 'Manifest groups') |
| 187 | |
| 188 | |
Kuang-che Wu | bfa6448 | 2018-10-16 11:49:49 +0800 | [diff] [blame^] | 189 | def _urljoin(base, url): |
| 190 | # urlparse.urljoin doesn't recognize "persistent-https://" protocol. |
| 191 | # Following hack replaces "persistent-https" by obsolete protocol "gopher" |
| 192 | # before urlparse.urljoin and replaces back after urlparse.urljoin calls. |
| 193 | dummy_scheme = 'gopher://' |
| 194 | new_scheme = 'persistent-https://' |
| 195 | assert not base.startswith(dummy_scheme) |
| 196 | assert not url.startswith(dummy_scheme) |
| 197 | base = re.sub('^' + new_scheme, dummy_scheme, base) |
| 198 | url = re.sub('^' + new_scheme, dummy_scheme, url) |
| 199 | result = urlparse.urljoin(base, url) |
| 200 | result = re.sub('^' + dummy_scheme, new_scheme, result) |
| 201 | return result |
| 202 | |
| 203 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 204 | class ManifestParser(object): |
| 205 | """Enumerates historical manifest files and parses them.""" |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 206 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 207 | def __init__(self, manifest_dir): |
| 208 | self.manifest_dir = manifest_dir |
| 209 | self.manifest_url = get_manifest_url(self.manifest_dir) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 210 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 211 | def parse_single_xml(self, content, allow_include=False): |
| 212 | root = xml.etree.ElementTree.fromstring(content) |
| 213 | if not allow_include and root.find('include') is not None: |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 214 | raise ValueError( |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 215 | 'Expects self-contained manifest. <include> is not allowed') |
| 216 | return root |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 217 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 218 | def parse_xml_recursive(self, git_rev, path): |
| 219 | content = git_util.get_file_from_revision(self.manifest_dir, git_rev, path) |
| 220 | root = self.parse_single_xml(content, allow_include=True) |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 221 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 222 | result = xml.etree.ElementTree.Element('manifest') |
| 223 | for node in root: |
| 224 | if node.tag == 'include': |
| 225 | for subnode in self.parse_xml_recursive(git_rev, node.get('name')): |
| 226 | result.append(subnode) |
| 227 | else: |
| 228 | result.append(node) |
| 229 | return result |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 230 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 231 | def process_parsed_result(self, root): |
| 232 | result = {} |
| 233 | default = root.find('default') |
| 234 | if default is None: |
| 235 | default = {} |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 236 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 237 | remote_fetch_map = {} |
| 238 | for remote in root.findall('.//remote'): |
| 239 | name = remote.get('name') |
Kuang-che Wu | bfa6448 | 2018-10-16 11:49:49 +0800 | [diff] [blame^] | 240 | fetch_url = _urljoin(self.manifest_url, remote.get('fetch')) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 241 | if urlparse.urlparse(fetch_url).path not in ('', '/'): |
| 242 | # TODO(kcwu): support remote url with sub folders |
| 243 | raise ValueError( |
| 244 | 'only support git repo at root path of remote server: %s' % |
| 245 | fetch_url) |
| 246 | remote_fetch_map[name] = fetch_url |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 247 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 248 | assert root.find('include') is None |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 249 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 250 | for project in root.findall('.//project'): |
| 251 | if 'notdefault' in project.get('groups', ''): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 252 | continue |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 253 | for subproject in project.findall('.//project'): |
| 254 | logger.warning('nested project %s.%s is not supported and ignored', |
| 255 | project.get('name'), subproject.get('name')) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 256 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 257 | # default path is its name |
| 258 | path = project.get('path', project.get('name')) |
| 259 | revision = project.get('revision', default.get('revision')) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 260 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 261 | remote_name = project.get('remote', default.get('remote')) |
| 262 | if remote_name not in remote_fetch_map: |
| 263 | raise ValueError('unknown remote name=%s' % remote_name) |
| 264 | fetch_url = remote_fetch_map.get(remote_name) |
Kuang-che Wu | bfa6448 | 2018-10-16 11:49:49 +0800 | [diff] [blame^] | 265 | repo_url = _urljoin(fetch_url, project.get('name')) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 266 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 267 | result[path] = codechange.PathSpec(path, repo_url, revision) |
| 268 | return result |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 269 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 270 | def enumerate_manifest_commits(self, start_time, end_time, path): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 271 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 272 | def parse_dependencies(path, content): |
| 273 | del path # unused |
| 274 | root = self.parse_single_xml(content, allow_include=True) |
| 275 | for include in root.findall('.//include'): |
| 276 | yield include.get('name') |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 277 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 278 | return git_util.get_history_recursively(self.manifest_dir, path, start_time, |
| 279 | end_time, parse_dependencies) |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 280 | |
| 281 | |
| 282 | class RepoMirror(codechange.CodeStorage): |
| 283 | """Repo git mirror.""" |
| 284 | |
| 285 | def __init__(self, mirror_dir): |
| 286 | self.mirror_dir = mirror_dir |
| 287 | |
| 288 | def _url_to_cache_dir(self, url): |
| 289 | # Here we assume remote fetch url is always at root of server url, so we can |
| 290 | # simply treat whole path as repo project name. |
| 291 | path = urlparse.urlparse(url).path |
| 292 | assert path[0] == '/' |
| 293 | return '%s.git' % path[1:] |
| 294 | |
| 295 | def cached_git_root(self, repo_url): |
| 296 | cache_path = self._url_to_cache_dir(repo_url) |
| 297 | return os.path.join(self.mirror_dir, cache_path) |
Kuang-che Wu | 6948ecc | 2018-09-11 17:43:49 +0800 | [diff] [blame] | 298 | |
| 299 | def _load_project_list(self, project_root): |
| 300 | repo_project_list = os.path.join(project_root, '.repo', 'project.list') |
| 301 | return open(repo_project_list).readlines() |
| 302 | |
| 303 | def _save_project_list(self, project_root, lines): |
| 304 | repo_project_list = os.path.join(project_root, '.repo', 'project.list') |
| 305 | with open(repo_project_list, 'w') as f: |
| 306 | f.write(''.join(sorted(lines))) |
| 307 | |
| 308 | def add_to_project_list(self, project_root, path, repo_url): |
| 309 | lines = self._load_project_list(project_root) |
| 310 | |
| 311 | line = path + '\n' |
| 312 | if line not in lines: |
| 313 | lines.append(line) |
| 314 | |
| 315 | self._save_project_list(project_root, lines) |
| 316 | |
| 317 | def remove_from_project_list(self, project_root, path): |
| 318 | lines = self._load_project_list(project_root) |
| 319 | |
| 320 | line = path + '\n' |
| 321 | if line in lines: |
| 322 | lines.remove(line) |
| 323 | |
| 324 | self._save_project_list(project_root, lines) |