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"""Base class for all updaters.""" 15 16from pathlib import Path 17 18import fileutils 19# pylint: disable=import-error 20import metadata_pb2 # type: ignore 21 22 23class Updater: 24 """Base Updater that defines methods common for all updaters.""" 25 def __init__(self, proj_path: Path, old_url: metadata_pb2.URL, 26 old_ver: str) -> None: 27 self._proj_path = fileutils.get_absolute_project_path(proj_path) 28 self._old_url = old_url 29 self._old_ver = old_ver 30 31 self._new_url = metadata_pb2.URL() 32 self._new_url.CopyFrom(old_url) 33 self._new_ver = old_ver 34 35 self._has_errors = False 36 37 def is_supported_url(self) -> bool: 38 """Returns whether the url is supported.""" 39 raise NotImplementedError() 40 41 def check(self) -> None: 42 """Checks whether a new version is available.""" 43 raise NotImplementedError() 44 45 def update(self) -> None: 46 """Updates the package. 47 48 Has to call check() before this function. 49 """ 50 raise NotImplementedError() 51 52 def rollback(self) -> bool: 53 """Rolls the current update back. 54 55 This is an optional operation. Returns whether the rollback succeeded. 56 """ 57 return False 58 59 @property 60 def project_path(self) -> Path: 61 """Gets absolute path to the project.""" 62 return self._proj_path 63 64 @property 65 def current_version(self) -> str: 66 """Gets the current version.""" 67 return self._old_ver 68 69 @property 70 def current_url(self) -> metadata_pb2.URL: 71 """Gets the current url.""" 72 return self._old_url 73 74 @property 75 def latest_version(self) -> str: 76 """Gets latest version.""" 77 return self._new_ver 78 79 @property 80 def latest_url(self) -> metadata_pb2.URL: 81 """Gets URL for latest version.""" 82 return self._new_url 83 84 @property 85 def has_errors(self) -> bool: 86 """Gets whether this update had an error.""" 87 return self._has_errors 88 89 def use_current_as_latest(self): 90 """Uses current version/url as the latest to refresh project.""" 91 self._new_ver = self._old_ver 92 self._new_url = self._old_url 93