Skip to content

Commit 95d605a

Browse files
committed
Refactor GitStore
1 parent eabb48b commit 95d605a

File tree

6 files changed

+550
-395
lines changed

6 files changed

+550
-395
lines changed

osc/git_scm/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22

33
from .store import GitStore
4+
from .store import LocalGitStore
45

56

67
def warn_experimental():

osc/git_scm/configuration.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import Optional
2+
3+
4+
class Configuration:
5+
"""
6+
A wrapper to configuration.yaml file that lives in obs/configuration, in the main branch.
7+
"""
8+
9+
@classmethod
10+
def from_file(cls, path: str) -> "Configuration":
11+
from ..util import yaml as osc_yaml
12+
13+
with open(path, "r", encoding="utf-8") as f:
14+
data = osc_yaml.yaml_load(f)
15+
obj = cls(data)
16+
return obj
17+
18+
@classmethod
19+
def from_string(cls, text: str) -> "Configuration":
20+
from ..util import yaml as osc_yaml
21+
22+
data = osc_yaml.yaml_loads(text)
23+
obj = cls(data)
24+
return obj
25+
26+
def __init__(self, data: dict):
27+
self._data = data
28+
29+
@property
30+
def obs_apiurl(self) -> Optional[str]:
31+
return self._data.get("obs_apiurl", None)

osc/git_scm/manifest.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)