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) |
| 36 | url = re.sub(r'^persistent-(https?://)', r'\1', url) |
| 37 | return url |
| 38 | |
| 39 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 40 | def init(repo_dir, |
| 41 | manifest_url, |
| 42 | manifest_branch=None, |
| 43 | manifest_name=None, |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 44 | repo_url=None, |
| 45 | reference=None): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 46 | """Repo init. |
| 47 | |
| 48 | Args: |
| 49 | repo_dir: root directory of repo |
| 50 | manifest_url: manifest repository location |
| 51 | manifest_branch: manifest branch or revision |
| 52 | manifest_name: initial manifest file name |
| 53 | repo_url: repo repository location |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 54 | reference: location of mirror directory |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 55 | """ |
| 56 | cmd = ['repo', 'init', '--manifest-url', manifest_url] |
| 57 | if manifest_name: |
| 58 | cmd += ['--manifest-name', manifest_name] |
| 59 | if manifest_branch: |
| 60 | cmd += ['--manifest-branch', manifest_branch] |
| 61 | if repo_url: |
| 62 | cmd += ['--repo-url', repo_url] |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 63 | if reference: |
| 64 | cmd += ['--reference', reference] |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 65 | util.check_call(*cmd, cwd=repo_dir) |
| 66 | |
| 67 | |
| 68 | def sync(repo_dir, jobs=16, manifest_name=None, current_branch=False): |
| 69 | """Repo sync. |
| 70 | |
| 71 | Args: |
| 72 | repo_dir: root directory of repo |
| 73 | jobs: projects to fetch simultaneously |
| 74 | manifest_name: filename of manifest |
| 75 | current_branch: fetch only current branch |
| 76 | """ |
| 77 | cmd = ['repo', 'sync', '-q', '--force-sync'] |
| 78 | if jobs: |
| 79 | cmd += ['-j', str(jobs)] |
| 80 | if manifest_name: |
| 81 | cmd += ['--manifest-name', manifest_name] |
| 82 | if current_branch: |
| 83 | cmd += ['--current-branch'] |
| 84 | util.check_call(*cmd, cwd=repo_dir) |
| 85 | |
| 86 | |
| 87 | def abandon(repo_dir, branch_name): |
| 88 | """Repo abandon. |
| 89 | |
| 90 | Args: |
| 91 | repo_dir: root directory of repo |
| 92 | branch_name: branch name to abandon |
| 93 | """ |
| 94 | # Ignore errors if failed, which means the branch didn't exist beforehand. |
| 95 | util.call('repo', 'abandon', branch_name, cwd=repo_dir) |
| 96 | |
| 97 | |
| 98 | def info(repo_dir, query): |
| 99 | """Repo info. |
| 100 | |
| 101 | Args: |
| 102 | repo_dir: root directory of repo |
| 103 | query: key to query |
| 104 | """ |
| 105 | for line in util.check_output('repo', 'info', '.', cwd=repo_dir).splitlines(): |
| 106 | key, value = map(str.strip, line.split(':')) |
| 107 | if key == query: |
| 108 | return value |
Kuang-che Wu | 89ac2e7 | 2018-07-25 17:39:07 +0800 | [diff] [blame] | 109 | |
| 110 | return None |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 111 | |
| 112 | |
| 113 | def get_current_branch(repo_dir): |
| 114 | """Get manifest branch of existing repo directory.""" |
| 115 | return info(repo_dir, 'Manifest branch') |
| 116 | |
| 117 | |
| 118 | def get_manifest_groups(repo_dir): |
| 119 | """Get manifest group of existing repo directory.""" |
| 120 | return info(repo_dir, 'Manifest groups') |
| 121 | |
| 122 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 123 | class ManifestParser(object): |
| 124 | """Enumerates historical manifest files and parses them.""" |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 125 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 126 | def __init__(self, manifest_dir): |
| 127 | self.manifest_dir = manifest_dir |
| 128 | self.manifest_url = get_manifest_url(self.manifest_dir) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 129 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 130 | def parse_single_xml(self, content, allow_include=False): |
| 131 | root = xml.etree.ElementTree.fromstring(content) |
| 132 | if not allow_include and root.find('include') is not None: |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 133 | raise ValueError( |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 134 | 'Expects self-contained manifest. <include> is not allowed') |
| 135 | return root |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 136 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 137 | def parse_xml_recursive(self, git_rev, path): |
| 138 | content = git_util.get_file_from_revision(self.manifest_dir, git_rev, path) |
| 139 | root = self.parse_single_xml(content, allow_include=True) |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 140 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 141 | result = xml.etree.ElementTree.Element('manifest') |
| 142 | for node in root: |
| 143 | if node.tag == 'include': |
| 144 | for subnode in self.parse_xml_recursive(git_rev, node.get('name')): |
| 145 | result.append(subnode) |
| 146 | else: |
| 147 | result.append(node) |
| 148 | return result |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 149 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 150 | def process_parsed_result(self, root): |
| 151 | result = {} |
| 152 | default = root.find('default') |
| 153 | if default is None: |
| 154 | default = {} |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 155 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 156 | remote_fetch_map = {} |
| 157 | for remote in root.findall('.//remote'): |
| 158 | name = remote.get('name') |
| 159 | fetch_url = urlparse.urljoin(self.manifest_url, remote.get('fetch')) |
| 160 | if urlparse.urlparse(fetch_url).path not in ('', '/'): |
| 161 | # TODO(kcwu): support remote url with sub folders |
| 162 | raise ValueError( |
| 163 | 'only support git repo at root path of remote server: %s' % |
| 164 | fetch_url) |
| 165 | remote_fetch_map[name] = fetch_url |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 166 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 167 | assert root.find('include') is None |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 168 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 169 | for project in root.findall('.//project'): |
| 170 | if 'notdefault' in project.get('groups', ''): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 171 | continue |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 172 | for subproject in project.findall('.//project'): |
| 173 | logger.warning('nested project %s.%s is not supported and ignored', |
| 174 | project.get('name'), subproject.get('name')) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 175 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 176 | # default path is its name |
| 177 | path = project.get('path', project.get('name')) |
| 178 | revision = project.get('revision', default.get('revision')) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 179 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 180 | remote_name = project.get('remote', default.get('remote')) |
| 181 | if remote_name not in remote_fetch_map: |
| 182 | raise ValueError('unknown remote name=%s' % remote_name) |
| 183 | fetch_url = remote_fetch_map.get(remote_name) |
| 184 | repo_url = urlparse.urljoin(fetch_url, project.get('name')) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 185 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 186 | result[path] = codechange.PathSpec(path, repo_url, revision) |
| 187 | return result |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 188 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 189 | def enumerate_manifest_commits(self, start_time, end_time, path): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 190 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 191 | def parse_dependencies(path, content): |
| 192 | del path # unused |
| 193 | root = self.parse_single_xml(content, allow_include=True) |
| 194 | for include in root.findall('.//include'): |
| 195 | yield include.get('name') |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 196 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 197 | return git_util.get_history_recursively(self.manifest_dir, path, start_time, |
| 198 | end_time, parse_dependencies) |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 199 | |
| 200 | |
| 201 | class RepoMirror(codechange.CodeStorage): |
| 202 | """Repo git mirror.""" |
| 203 | |
| 204 | def __init__(self, mirror_dir): |
| 205 | self.mirror_dir = mirror_dir |
| 206 | |
| 207 | def _url_to_cache_dir(self, url): |
| 208 | # Here we assume remote fetch url is always at root of server url, so we can |
| 209 | # simply treat whole path as repo project name. |
| 210 | path = urlparse.urlparse(url).path |
| 211 | assert path[0] == '/' |
| 212 | return '%s.git' % path[1:] |
| 213 | |
| 214 | def cached_git_root(self, repo_url): |
| 215 | cache_path = self._url_to_cache_dir(repo_url) |
| 216 | return os.path.join(self.mirror_dir, cache_path) |
Kuang-che Wu | 6948ecc | 2018-09-11 17:43:49 +0800 | [diff] [blame] | 217 | |
| 218 | def _load_project_list(self, project_root): |
| 219 | repo_project_list = os.path.join(project_root, '.repo', 'project.list') |
| 220 | return open(repo_project_list).readlines() |
| 221 | |
| 222 | def _save_project_list(self, project_root, lines): |
| 223 | repo_project_list = os.path.join(project_root, '.repo', 'project.list') |
| 224 | with open(repo_project_list, 'w') as f: |
| 225 | f.write(''.join(sorted(lines))) |
| 226 | |
| 227 | def add_to_project_list(self, project_root, path, repo_url): |
| 228 | lines = self._load_project_list(project_root) |
| 229 | |
| 230 | line = path + '\n' |
| 231 | if line not in lines: |
| 232 | lines.append(line) |
| 233 | |
| 234 | self._save_project_list(project_root, lines) |
| 235 | |
| 236 | def remove_from_project_list(self, project_root, path): |
| 237 | lines = self._load_project_list(project_root) |
| 238 | |
| 239 | line = path + '\n' |
| 240 | if line in lines: |
| 241 | lines.remove(line) |
| 242 | |
| 243 | self._save_project_list(project_root, lines) |