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