1*3c875a21SAndroid Build Coastguard Worker# Copyright (C) 2018 The Android Open Source Project 2*3c875a21SAndroid Build Coastguard Worker# 3*3c875a21SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 4*3c875a21SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 5*3c875a21SAndroid Build Coastguard Worker# You may obtain a copy of the License at 6*3c875a21SAndroid Build Coastguard Worker# 7*3c875a21SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 8*3c875a21SAndroid Build Coastguard Worker# 9*3c875a21SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*3c875a21SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 11*3c875a21SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*3c875a21SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 13*3c875a21SAndroid Build Coastguard Worker# limitations under the License. 14*3c875a21SAndroid Build Coastguard Worker"""Base class for all updaters.""" 15*3c875a21SAndroid Build Coastguard Worker 16*3c875a21SAndroid Build Coastguard Workerfrom pathlib import Path 17*3c875a21SAndroid Build Coastguard Worker 18*3c875a21SAndroid Build Coastguard Workerimport git_utils 19*3c875a21SAndroid Build Coastguard Workerimport fileutils 20*3c875a21SAndroid Build Coastguard Worker# pylint: disable=import-error 21*3c875a21SAndroid Build Coastguard Workerimport metadata_pb2 # type: ignore 22*3c875a21SAndroid Build Coastguard Worker 23*3c875a21SAndroid Build Coastguard Worker 24*3c875a21SAndroid Build Coastguard Workerclass Updater: 25*3c875a21SAndroid Build Coastguard Worker """Base Updater that defines methods common for all updaters.""" 26*3c875a21SAndroid Build Coastguard Worker def __init__(self, proj_path: Path, old_identifier: metadata_pb2.Identifier, 27*3c875a21SAndroid Build Coastguard Worker old_ver: str) -> None: 28*3c875a21SAndroid Build Coastguard Worker self._proj_path = fileutils.get_absolute_project_path(proj_path) 29*3c875a21SAndroid Build Coastguard Worker self._old_identifier = old_identifier 30*3c875a21SAndroid Build Coastguard Worker self._old_identifier.version = old_identifier.version if old_identifier.version else old_ver 31*3c875a21SAndroid Build Coastguard Worker 32*3c875a21SAndroid Build Coastguard Worker self._new_identifier = metadata_pb2.Identifier() 33*3c875a21SAndroid Build Coastguard Worker self._new_identifier.CopyFrom(old_identifier) 34*3c875a21SAndroid Build Coastguard Worker 35*3c875a21SAndroid Build Coastguard Worker self._alternative_new_ver: str | None = None 36*3c875a21SAndroid Build Coastguard Worker 37*3c875a21SAndroid Build Coastguard Worker self._has_errors = False 38*3c875a21SAndroid Build Coastguard Worker 39*3c875a21SAndroid Build Coastguard Worker def is_supported_url(self) -> bool: 40*3c875a21SAndroid Build Coastguard Worker """Returns whether the url is supported.""" 41*3c875a21SAndroid Build Coastguard Worker raise NotImplementedError() 42*3c875a21SAndroid Build Coastguard Worker 43*3c875a21SAndroid Build Coastguard Worker def setup_remote(self) -> None: 44*3c875a21SAndroid Build Coastguard Worker raise NotImplementedError() 45*3c875a21SAndroid Build Coastguard Worker 46*3c875a21SAndroid Build Coastguard Worker def validate(self) -> str: 47*3c875a21SAndroid Build Coastguard Worker """Checks whether aosp version is what it claims to be.""" 48*3c875a21SAndroid Build Coastguard Worker self.setup_remote() 49*3c875a21SAndroid Build Coastguard Worker return git_utils.diff(self._proj_path, 'a', self._old_identifier.version) 50*3c875a21SAndroid Build Coastguard Worker 51*3c875a21SAndroid Build Coastguard Worker def check(self) -> None: 52*3c875a21SAndroid Build Coastguard Worker """Checks whether a new version is available.""" 53*3c875a21SAndroid Build Coastguard Worker raise NotImplementedError() 54*3c875a21SAndroid Build Coastguard Worker 55*3c875a21SAndroid Build Coastguard Worker def update(self) -> None: 56*3c875a21SAndroid Build Coastguard Worker """Updates the package. 57*3c875a21SAndroid Build Coastguard Worker 58*3c875a21SAndroid Build Coastguard Worker Has to call check() before this function. 59*3c875a21SAndroid Build Coastguard Worker """ 60*3c875a21SAndroid Build Coastguard Worker raise NotImplementedError() 61*3c875a21SAndroid Build Coastguard Worker 62*3c875a21SAndroid Build Coastguard Worker def rollback(self) -> bool: 63*3c875a21SAndroid Build Coastguard Worker """Rolls the current update back. 64*3c875a21SAndroid Build Coastguard Worker 65*3c875a21SAndroid Build Coastguard Worker This is an optional operation. Returns whether the rollback succeeded. 66*3c875a21SAndroid Build Coastguard Worker """ 67*3c875a21SAndroid Build Coastguard Worker return False 68*3c875a21SAndroid Build Coastguard Worker 69*3c875a21SAndroid Build Coastguard Worker def update_metadata(self, metadata: metadata_pb2.MetaData) -> metadata_pb2: 70*3c875a21SAndroid Build Coastguard Worker """Rewrites the metadata file.""" 71*3c875a21SAndroid Build Coastguard Worker updated_metadata = metadata_pb2.MetaData() 72*3c875a21SAndroid Build Coastguard Worker updated_metadata.CopyFrom(metadata) 73*3c875a21SAndroid Build Coastguard Worker updated_metadata.third_party.ClearField("version") 74*3c875a21SAndroid Build Coastguard Worker for identifier in updated_metadata.third_party.identifier: 75*3c875a21SAndroid Build Coastguard Worker if identifier == self.current_identifier: 76*3c875a21SAndroid Build Coastguard Worker identifier.CopyFrom(self.latest_identifier) 77*3c875a21SAndroid Build Coastguard Worker return updated_metadata 78*3c875a21SAndroid Build Coastguard Worker 79*3c875a21SAndroid Build Coastguard Worker @property 80*3c875a21SAndroid Build Coastguard Worker def project_path(self) -> Path: 81*3c875a21SAndroid Build Coastguard Worker """Gets absolute path to the project.""" 82*3c875a21SAndroid Build Coastguard Worker return self._proj_path 83*3c875a21SAndroid Build Coastguard Worker 84*3c875a21SAndroid Build Coastguard Worker @property 85*3c875a21SAndroid Build Coastguard Worker def current_version(self) -> str: 86*3c875a21SAndroid Build Coastguard Worker """Gets the current version.""" 87*3c875a21SAndroid Build Coastguard Worker return self._old_identifier.version 88*3c875a21SAndroid Build Coastguard Worker 89*3c875a21SAndroid Build Coastguard Worker @property 90*3c875a21SAndroid Build Coastguard Worker def current_identifier(self) -> metadata_pb2.Identifier: 91*3c875a21SAndroid Build Coastguard Worker """Gets the current identifier.""" 92*3c875a21SAndroid Build Coastguard Worker return self._old_identifier 93*3c875a21SAndroid Build Coastguard Worker 94*3c875a21SAndroid Build Coastguard Worker @property 95*3c875a21SAndroid Build Coastguard Worker def latest_version(self) -> str: 96*3c875a21SAndroid Build Coastguard Worker """Gets latest version.""" 97*3c875a21SAndroid Build Coastguard Worker return self._new_identifier.version 98*3c875a21SAndroid Build Coastguard Worker 99*3c875a21SAndroid Build Coastguard Worker @property 100*3c875a21SAndroid Build Coastguard Worker def latest_identifier(self) -> metadata_pb2.Identifier: 101*3c875a21SAndroid Build Coastguard Worker """Gets identifier for latest version.""" 102*3c875a21SAndroid Build Coastguard Worker return self._new_identifier 103*3c875a21SAndroid Build Coastguard Worker 104*3c875a21SAndroid Build Coastguard Worker @property 105*3c875a21SAndroid Build Coastguard Worker def has_errors(self) -> bool: 106*3c875a21SAndroid Build Coastguard Worker """Gets whether this update had an error.""" 107*3c875a21SAndroid Build Coastguard Worker return self._has_errors 108*3c875a21SAndroid Build Coastguard Worker 109*3c875a21SAndroid Build Coastguard Worker @property 110*3c875a21SAndroid Build Coastguard Worker def alternative_latest_version(self) -> str | None: 111*3c875a21SAndroid Build Coastguard Worker """Gets alternative latest version.""" 112*3c875a21SAndroid Build Coastguard Worker return self._alternative_new_ver 113*3c875a21SAndroid Build Coastguard Worker 114*3c875a21SAndroid Build Coastguard Worker def refresh_without_upgrading(self) -> None: 115*3c875a21SAndroid Build Coastguard Worker """Uses current version and url as the latest to refresh project.""" 116*3c875a21SAndroid Build Coastguard Worker self._new_identifier.version = self._old_identifier.version 117*3c875a21SAndroid Build Coastguard Worker self._new_identifier.value = self._old_identifier.value 118*3c875a21SAndroid Build Coastguard Worker 119*3c875a21SAndroid Build Coastguard Worker def set_new_version(self, version: str) -> None: 120*3c875a21SAndroid Build Coastguard Worker """Uses the passed version as the latest to upgrade project.""" 121*3c875a21SAndroid Build Coastguard Worker self._new_identifier.version = version 122*3c875a21SAndroid Build Coastguard Worker 123*3c875a21SAndroid Build Coastguard Worker def set_custom_version(self, custom_version: str) -> None: 124*3c875a21SAndroid Build Coastguard Worker """Uses the passed version as the latest to upgrade project if the 125*3c875a21SAndroid Build Coastguard Worker passed version is not older than the current version.""" 126*3c875a21SAndroid Build Coastguard Worker if git_utils.is_ancestor(self._proj_path, self._old_identifier.version, custom_version): 127*3c875a21SAndroid Build Coastguard Worker self._new_identifier.version = custom_version 128*3c875a21SAndroid Build Coastguard Worker else: 129*3c875a21SAndroid Build Coastguard Worker raise RuntimeError( 130*3c875a21SAndroid Build Coastguard Worker f"Can not upgrade to {custom_version}. The current version is newer than {custom_version}.") 131