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
19import metadata_pb2  # type: ignore
20
21
22class Updater:
23    """Base Updater that defines methods common for all updaters."""
24    def __init__(self, proj_path: Path, old_url: metadata_pb2.URL,
25                 old_ver: str) -> None:
26        self._proj_path = fileutils.get_absolute_project_path(proj_path)
27        self._old_url = old_url
28        self._old_ver = old_ver
29
30        self._new_url = metadata_pb2.URL()
31        self._new_url.CopyFrom(old_url)
32        self._new_ver = old_ver
33
34    def is_supported_url(self) -> bool:
35        """Returns whether the url is supported."""
36        raise NotImplementedError()
37
38    def check(self) -> None:
39        """Checks whether a new version is available."""
40        raise NotImplementedError()
41
42    def update(self) -> None:
43        """Updates the package.
44
45        Has to call check() before this function.
46        """
47        raise NotImplementedError()
48
49    @property
50    def project_path(self) -> Path:
51        """Gets absolute path to the project."""
52        return self._proj_path
53
54    @property
55    def current_version(self) -> str:
56        """Gets the current version."""
57        return self._old_ver
58
59    @property
60    def current_url(self) -> metadata_pb2.URL:
61        """Gets the current url."""
62        return self._old_url
63
64    @property
65    def latest_version(self) -> str:
66        """Gets latest version."""
67        return self._new_ver
68
69    @property
70    def latest_url(self) -> metadata_pb2.URL:
71        """Gets URL for latest version."""
72        return self._new_url
73