|
| 1 | +import os |
| 2 | +import pathlib |
| 3 | + |
| 4 | +from typing import List |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | + |
| 8 | +class Manifest: |
| 9 | + """ |
| 10 | + A wrapper to _manifest file that lives in a project, such as _ObsPrj repo. |
| 11 | + """ |
| 12 | + |
| 13 | + @classmethod |
| 14 | + def from_file(cls, path: str) -> "Manifest": |
| 15 | + from ..util import yaml as osc_yaml |
| 16 | + |
| 17 | + with open(path, "r", encoding="utf-8") as f: |
| 18 | + data = osc_yaml.yaml_load(f) |
| 19 | + obj = cls(data) |
| 20 | + return obj |
| 21 | + |
| 22 | + @classmethod |
| 23 | + def from_string(cls, text: str) -> "Manifest": |
| 24 | + from ..util import yaml as osc_yaml |
| 25 | + |
| 26 | + data = osc_yaml.yaml_loads(text) |
| 27 | + obj = cls(data) |
| 28 | + return obj |
| 29 | + |
| 30 | + def __init__(self, data: dict): |
| 31 | + self._data = data |
| 32 | + |
| 33 | + @property |
| 34 | + def obs_apiurl(self) -> Optional[str]: |
| 35 | + return self._data.get("obs_apiurl", None) |
| 36 | + |
| 37 | + @property |
| 38 | + def obs_project(self) -> Optional[str]: |
| 39 | + return self._data.get("obs_project", None) |
| 40 | + |
| 41 | + @property |
| 42 | + def packages(self) -> List[str]: |
| 43 | + return self._data.get("packages", []) |
| 44 | + |
| 45 | + @property |
| 46 | + def package_directories(self) -> List[str]: |
| 47 | + result = self._data.get("subdirectories", []) |
| 48 | + if not result and not self.packages: |
| 49 | + return ["."] |
| 50 | + return result |
| 51 | + |
| 52 | + def resolve_package_path(self, project_path: str, package_path: str) -> Optional[str]: |
| 53 | + """ |
| 54 | + Return package topdir or `None` if it cannot be resolved. |
| 55 | + The `package_path` argument may point inside the directory tree under the package's location. |
| 56 | + """ |
| 57 | + project_path = os.path.abspath(project_path) |
| 58 | + package_path = os.path.abspath(package_path) |
| 59 | + |
| 60 | + # package path must not be equal to project path |
| 61 | + if package_path == project_path: |
| 62 | + return None |
| 63 | + |
| 64 | + # package path must be under project path |
| 65 | + if os.path.commonpath([project_path, package_path]) != project_path: |
| 66 | + return None |
| 67 | + |
| 68 | + packages_abspath = [os.path.abspath(os.path.join(project_path, i)) for i in self.packages] |
| 69 | + for i in packages_abspath: |
| 70 | + if os.path.commonpath([package_path, i]) == i: |
| 71 | + return i |
| 72 | + |
| 73 | + package_directories_abspath = [os.path.abspath(os.path.join(project_path, i)) for i in self.package_directories] |
| 74 | + package_path_obj = pathlib.Path(package_path) |
| 75 | + for i in package_directories_abspath: |
| 76 | + if os.path.commonpath([package_path, i]) == i: |
| 77 | + i_obj = pathlib.Path(i) |
| 78 | + if i_obj in package_path_obj.parents: |
| 79 | + i_obj /= package_path_obj.parts[len(i_obj.parts)] |
| 80 | + return i_obj.as_posix() |
| 81 | + |
| 82 | + return None |
| 83 | + |
| 84 | + |
| 85 | +class Subdirs(Manifest): |
| 86 | + """ |
| 87 | + A wrapper to _subdirs file that has been deprecated by _manifest. |
| 88 | + """ |
| 89 | + |
| 90 | + @property |
| 91 | + def package_directories(self) -> List[str]: |
| 92 | + result = self._data.get("subdirs", []) |
| 93 | + if self._data.get("toplevel", None) == "include": |
| 94 | + result.append(".") |
| 95 | + return result |
0 commit comments