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 @property 53 def project_path(self) -> Path: 54 """Gets absolute path to the project.""" 55 return self._proj_path 56 57 @property 58 def current_version(self) -> str: 59 """Gets the current version.""" 60 return self._old_ver 61 62 @property 63 def current_url(self) -> metadata_pb2.URL: 64 """Gets the current url.""" 65 return self._old_url 66 67 @property 68 def latest_version(self) -> str: 69 """Gets latest version.""" 70 return self._new_ver 71 72 @property 73 def latest_url(self) -> metadata_pb2.URL: 74 """Gets URL for latest version.""" 75 return self._new_url 76 77 @property 78 def has_errors(self) -> bool: 79 """Gets whether this update had an error.""" 80 return self._has_errors 81 82 def use_current_as_latest(self): 83 """Uses current version/url as the latest to refresh project.""" 84 self._new_ver = self._old_ver 85 self._new_url = self._old_url 86