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 25# pylint: disable=import-error 26import metadata_pb2 # type: ignore 27 28 29def create_updater(metadata: metadata_pb2.MetaData, proj_path: Path, 30 updaters: List[Type[Updater]]) -> Updater: 31 """Creates corresponding updater object for a project. 32 33 Args: 34 metadata: Parsed proto for METADATA file. 35 proj_path: Absolute path for the project. 36 37 Returns: 38 An updater object. 39 40 Raises: 41 ValueError: Occurred when there's no updater for all urls. 42 """ 43 for url in metadata.third_party.url: 44 if url.type != metadata_pb2.URL.HOMEPAGE: 45 for updater_cls in updaters: 46 updater = updater_cls(proj_path, url, metadata.third_party.version) 47 if updater.is_supported_url(): 48 return updater 49 50 raise ValueError('No supported URL.') 51 52 53def replace_package(source_dir, target_dir, temp_file=None) -> None: 54 """Invokes a shell script to prepare and update a project. 55 56 Args: 57 source_dir: Path to the new downloaded and extracted package. 58 target_dir: The path to the project in Android source tree. 59 """ 60 61 print(f'Updating {target_dir} using {source_dir}.') 62 script_path = os.path.join(os.path.dirname(sys.argv[0]), 63 'update_package.sh') 64 subprocess.check_call(['bash', script_path, source_dir, target_dir, 65 "" if temp_file is None else temp_file]) 66 67 68def run_post_update(source_dir: Path, target_dir: Path) -> None: 69 """ 70 source_dir: Path to the new downloaded and extracted package. 71 target_dir: The path to the project in Android source tree. 72 """ 73 post_update_path = os.path.join(source_dir, 'post_update.sh') 74 if os.path.isfile(post_update_path): 75 cmd: Sequence[str | Path] = ['bash', post_update_path, source_dir, target_dir] 76 print(f'Running {post_update_path}') 77 subprocess.check_call(cmd) 78 79 80VERSION_SPLITTER_PATTERN: str = r'[\.\-_]' 81VERSION_PATTERN: str = (r'^(?P<prefix>[^\d]*)' + r'(?P<version>\d+(' + 82 VERSION_SPLITTER_PATTERN + r'\d+)*)' + 83 r'(?P<suffix>.*)$') 84VERSION_RE: re.Pattern = re.compile(VERSION_PATTERN) 85VERSION_SPLITTER_RE: re.Pattern = re.compile(VERSION_SPLITTER_PATTERN) 86 87ParsedVersion = Tuple[List[int], str, str] 88 89 90def _parse_version(version: str) -> ParsedVersion: 91 match = VERSION_RE.match(version) 92 if match is None: 93 raise ValueError('Invalid version.') 94 try: 95 prefix, version, suffix = match.group('prefix', 'version', 'suffix') 96 versions = [int(v) for v in VERSION_SPLITTER_RE.split(version)] 97 return (versions, str(prefix), str(suffix)) 98 except IndexError: 99 # pylint: disable=raise-missing-from 100 raise ValueError('Invalid version.') 101 102 103def _match_and_get_version(old_ver: ParsedVersion, 104 version: str) -> Tuple[bool, bool, List[int]]: 105 try: 106 new_ver = _parse_version(version) 107 except ValueError: 108 return (False, False, []) 109 110 right_format = (new_ver[1:] == old_ver[1:]) 111 right_length = len(new_ver[0]) == len(old_ver[0]) 112 113 return (right_format, right_length, new_ver[0]) 114 115 116def get_latest_version(current_version: str, version_list: List[str]) -> str: 117 """Gets the latest version name from a list of versions. 118 119 The new version must have the same prefix and suffix with old version. 120 If no matched version is newer, current version name will be returned. 121 """ 122 parsed_current_ver = _parse_version(current_version) 123 124 latest = max( 125 version_list, 126 key=lambda ver: _match_and_get_version(parsed_current_ver, ver), 127 default=None) 128 if not latest: 129 raise ValueError('No matching version.') 130 return latest 131 132 133def build(proj_path: Path) -> None: 134 cmd = ['build/soong/soong_ui.bash', "--build-mode", "--modules-in-a-dir-no-deps", f"--dir={str(proj_path)}"] 135 print('Building...') 136 return subprocess.run(cmd, check=True, text=True) 137