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 16import os 17import re 18import subprocess 19import sys 20 21 22def create_updater(metadata, proj_path, updaters): 23 """Creates corresponding updater object for a project. 24 25 Args: 26 metadata: Parsed proto for METADATA file. 27 proj_path: Absolute path for the project. 28 29 Returns: 30 An updater object. 31 32 Raises: 33 ValueError: Occurred when there's no updater for all urls. 34 """ 35 for url in metadata.third_party.url: 36 for updater in updaters: 37 try: 38 return updater(url, proj_path, metadata) 39 except ValueError: 40 pass 41 42 raise ValueError('No supported URL.') 43 44 45def replace_package(source_dir, target_dir): 46 """Invokes a shell script to prepare and update a project. 47 48 Args: 49 source_dir: Path to the new downloaded and extracted package. 50 target_dir: The path to the project in Android source tree. 51 """ 52 53 print('Updating {} using {}.'.format(target_dir, source_dir)) 54 script_path = os.path.join( 55 os.path.dirname( 56 sys.argv[0]), 57 'update_package.sh') 58 subprocess.check_call(['bash', script_path, source_dir, target_dir]) 59 60VERSION_SPLITTER_PATTERN = r'[\.\-_]' 61VERSION_PATTERN = (r'^(?P<prefix>[^\d]*)' + 62 r'(?P<version>\d+(' + VERSION_SPLITTER_PATTERN + r'\d+)*)' + 63 r'(?P<suffix>.*)$') 64VERSION_RE = re.compile(VERSION_PATTERN) 65VERSION_SPLITTER_RE = re.compile(VERSION_SPLITTER_PATTERN) 66 67 68def _parse_version(version): 69 match = VERSION_RE.match(version) 70 if match is None: 71 raise ValueError('Invalid version.') 72 try: 73 return match.group('prefix', 'version', 'suffix') 74 except IndexError: 75 raise ValueError('Invalid version.') 76 77 78def _match_and_get_version(prefix, suffix, version): 79 try: 80 version_prefix, version, version_suffix = _parse_version(version) 81 except ValueError: 82 return [] 83 84 right_format = (version_prefix == prefix and version_suffix == suffix) 85 86 return [right_format] + [int(v) for v in VERSION_SPLITTER_RE.split(version)] 87 88 89def get_latest_version(current_version, version_list): 90 """Gets the latest version name from a list of versions. 91 92 The new version must have the same prefix and suffix with old version. 93 If no matched version is newer, current version name will be returned. 94 """ 95 prefix, _, suffix = _parse_version(current_version) 96 97 latest = max(version_list, 98 key=lambda ver: _match_and_get_version( 99 prefix, suffix, ver), 100 default=[]) 101 if not latest: 102 raise ValueError('No matching version.') 103 return latest 104