1# Copyright (C) 2018 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Helper functions for updaters.""" 15 16from collections.abc import Sequence 17import os 18import re 19import subprocess 20import sys 21from pathlib import Path 22from typing import List, Tuple, Type 23 24from base_updater import Updater 25import fileutils 26# pylint: disable=import-error 27import metadata_pb2 # type: ignore 28 29 30def create_updater(metadata: metadata_pb2.MetaData, proj_path: Path, 31 updaters: List[Type[Updater]]) -> Updater: 32 """Creates corresponding updater object for a project. 33 34 Args: 35 metadata: Parsed proto for METADATA file. 36 proj_path: Absolute path for the project. 37 38 Returns: 39 An updater object. 40 41 Raises: 42 ValueError: Occurred when there's no updater for all urls. 43 """ 44 for identifier in metadata.third_party.identifier: 45 if identifier.type.lower() != 'homepage': 46 for updater_cls in updaters: 47 updater = updater_cls(proj_path, identifier, metadata.third_party.version) 48 if updater.is_supported_url(): 49 return updater 50 51 raise ValueError('No supported URL.') 52 53 54def replace_package(source_dir, target_dir, temp_file=None) -> None: 55 """Invokes a shell script to prepare and update a project. 56 57 Args: 58 source_dir: Path to the new downloaded and extracted package. 59 target_dir: The path to the project in Android source tree. 60 """ 61 62 print(f'Updating {target_dir} using {source_dir}.') 63 script_path = os.path.join(os.path.dirname(sys.argv[0]), 64 'update_package.sh') 65 subprocess.check_call(['bash', script_path, source_dir, target_dir, 66 "" if temp_file is None else temp_file]) 67 68 69def run_post_update(proj_path: Path, old_project_path: Path | None = None) -> None: 70 """ Runs the post_update.sh script if exists. 71 72 Args: 73 proj_path: The path to the project in Android source tree. Note that this 74 project is now updated. 75 old_project_path: Temp dir to where it stored the old version after 76 upgrading the project. 77 """ 78 post_update_path = os.path.join(proj_path, 'post_update.sh') 79 if os.path.isfile(post_update_path): 80 print(f"Running post update script {post_update_path}") 81 cmd: Sequence[str | Path] 82 if old_project_path: 83 cmd = ['bash', post_update_path, proj_path, old_project_path] 84 else: 85 cmd = ['bash', post_update_path, proj_path] 86 subprocess.check_call(cmd) 87 88 89VERSION_SPLITTER_PATTERN: str = r'[\.\-_]' 90VERSION_PATTERN: str = (r'^(?P<prefix>[^\d]*)' + r'(?P<version>\d+(' + 91 VERSION_SPLITTER_PATTERN + r'\d+)*)' + 92 r'(?P<suffix>.*)$') 93TAG_PATTERN: str = r".*refs/tags/(?P<tag>[^\^]*).*" 94TAG_RE: re.Pattern = re.compile(TAG_PATTERN) 95VERSION_RE: re.Pattern = re.compile(VERSION_PATTERN) 96VERSION_SPLITTER_RE: re.Pattern = re.compile(VERSION_SPLITTER_PATTERN) 97 98ParsedVersion = Tuple[List[int], str, str] 99 100 101def _parse_version(version: str) -> ParsedVersion: 102 match = VERSION_RE.match(version) 103 if match is None: 104 raise ValueError('Invalid version.') 105 try: 106 prefix, version, suffix = match.group('prefix', 'version', 'suffix') 107 versions = [int(v) for v in VERSION_SPLITTER_RE.split(version)] 108 return versions, str(prefix), str(suffix) 109 except IndexError: 110 # pylint: disable=raise-missing-from 111 raise ValueError('Invalid version.') 112 113 114def _match_and_get_version(old_ver: ParsedVersion, 115 version: str) -> Tuple[bool, bool, List[int]]: 116 try: 117 new_ver = _parse_version(version) 118 except ValueError: 119 return False, False, [] 120 121 right_format = new_ver[1:] == old_ver[1:] 122 right_length = len(new_ver[0]) == len(old_ver[0]) 123 124 return right_format, right_length, new_ver[0] 125 126 127def get_latest_stable_release_tag(current_version: str, version_list: List[str]) -> str: 128 """Gets the latest version name from a list of versions. 129 130 The new version must have the same prefix and suffix with old version. 131 If no matched version is newer, current version name will be returned. 132 """ 133 parsed_current_ver = _parse_version(current_version) 134 135 latest = max( 136 version_list, 137 key=lambda ver: _match_and_get_version(parsed_current_ver, ver), 138 default=None) 139 if not latest: 140 raise ValueError('No matching version.') 141 return latest 142 143 144def parse_remote_tag(line: str) -> str: 145 if (m := TAG_RE.match(line)) is not None: 146 return m.group("tag") 147 raise ValueError(f"Could not parse tag from {line}") 148 149 150def build(proj_path: Path) -> None: 151 tree = fileutils.find_tree_containing(proj_path) 152 cmd = [ 153 str(tree / 'build/soong/soong_ui.bash'), 154 "--build-mode", 155 "--modules-in-a-dir", 156 f"--dir={str(proj_path)}", 157 ] 158 print('Building...') 159 subprocess.run(cmd, check=True, text=True) 160