Skip to content

Commit 8e5e2c4

Browse files
committed
Add 'git-obs meta' commands for managing the local metadata
1 parent 95d605a commit 8e5e2c4

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed

osc/commands_git/meta.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import osc.commandline_git
2+
3+
4+
class MetaCommand(osc.commandline_git.GitObsCommand):
5+
"""
6+
Manage metadata in .git/obs store
7+
"""
8+
9+
name = "meta"
10+
11+
def init_arguments(self):
12+
pass
13+
14+
def run(self, args):
15+
self.parser.print_help()

osc/commands_git/meta_list.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import osc.commandline_git
2+
3+
4+
class MetaListCommand(osc.commandline_git.GitObsCommand):
5+
"""
6+
List metadata in store
7+
"""
8+
9+
name = "list"
10+
parent = "MetaCommand"
11+
12+
def init_arguments(self):
13+
self.parser.add_argument(
14+
"--branch",
15+
help="Manage values for the specified branch (default: current branch)",
16+
)
17+
18+
def run(self, args):
19+
from osc.git_scm import GitStore
20+
from osc.output import KeyValueTable
21+
22+
store = GitStore(".")
23+
branch = args.branch or store._git.current_branch
24+
meta = store._read_meta(branch=branch).dict()
25+
meta.pop("header", None)
26+
27+
table = KeyValueTable(min_key_length=10)
28+
table.add("Branch", branch, color="bold")
29+
for key, value in meta.items():
30+
table.add(key, value)
31+
print(str(table))

osc/commands_git/meta_pull.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import osc.commandline_git
2+
3+
4+
class MetaPullCommand(osc.commandline_git.GitObsCommand):
5+
"""
6+
Pull metadata about the project or package from Gitea.
7+
"""
8+
9+
name = "pull"
10+
parent = "MetaCommand"
11+
12+
def init_arguments(self):
13+
pass
14+
15+
def run(self, args):
16+
from osc import gitea_api
17+
from osc.git_scm.configuration import Configuration
18+
from osc.git_scm.manifest import Manifest
19+
from osc.git_scm.store import LocalGitStore
20+
from osc.output import KeyValueTable
21+
22+
self.print_gitea_settings()
23+
24+
store = LocalGitStore(".")
25+
26+
apiurl = None
27+
project = None
28+
29+
# read apiurl and project from _manifest that lives in <owner>/_ObsPrj, matching <branch>
30+
# XXX: when the target branch doesn't exist, file from the default branch is returned
31+
if store.is_package:
32+
try:
33+
owner, _ = store._git.get_owner_repo()
34+
repo = "_ObsPrj"
35+
branch = store._git.current_branch
36+
37+
url = self.gitea_conn.makeurl("repos", owner, repo, "raw", "_manifest", query={"ref": branch})
38+
response = self.gitea_conn.request("GET", url)
39+
if response.data:
40+
manifest = Manifest.from_string(response.data.decode("utf-8"))
41+
if manifest.obs_apiurl:
42+
apiurl = manifest.obs_apiurl
43+
if manifest.obs_project:
44+
project = manifest.obs_project
45+
except gitea_api.GiteaException as e:
46+
if e.status != 404:
47+
raise
48+
49+
# read apiurl from the global configuration in obs/configuration. branch
50+
if not apiurl:
51+
try:
52+
url = self.gitea_conn.makeurl("repos", "obs", "configuration", "raw", "configuration.yaml", query={"ref": "main"})
53+
response = self.gitea_conn.request("GET", url)
54+
if response.data:
55+
configuration = Configuration.from_string(response.data.decode("utf-8"))
56+
if configuration.obs_apiurl:
57+
apiurl = configuration.obs_apiurl
58+
except gitea_api.GiteaException as e:
59+
if e.status != 404:
60+
raise
61+
62+
if apiurl:
63+
store.apiurl = apiurl
64+
65+
if project:
66+
store.project = project
67+
68+
branch = store._git.current_branch
69+
meta = store._read_meta(branch=branch).dict()
70+
meta.pop("header", None)
71+
72+
table = KeyValueTable(min_key_length=10)
73+
table.add("Branch", branch, color="bold")
74+
for key, value in meta.items():
75+
table.add(key, value)
76+
print(str(table))

osc/commands_git/meta_reset.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import osc.commandline_git
2+
3+
4+
class MetaFooCommand(osc.commandline_git.GitObsCommand):
5+
"""
6+
Reset metadata in store
7+
"""
8+
9+
name = "reset"
10+
parent = "MetaCommand"
11+
12+
def init_arguments(self):
13+
self.add_argument(
14+
"--branch",
15+
help="Manage values for the specified branch (default: current branch)",
16+
)
17+
18+
def run(self, args):
19+
from osc.git_scm.store import LocalGitStore
20+
21+
store = LocalGitStore(".")
22+
branch = args.branch or store._git.current_branch
23+
print(f"Resetting meta for branch '{branch}' ...")
24+
store.reset(branch=branch)

osc/commands_git/meta_set.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import osc.commandline_git
2+
3+
4+
class MetaSetCommand(osc.commandline_git.GitObsCommand):
5+
"""
6+
Set metadata in store
7+
"""
8+
9+
name = "set"
10+
parent = "MetaCommand"
11+
12+
def init_arguments(self):
13+
self.add_argument(
14+
"--apiurl",
15+
help="Set 'apiurl'",
16+
)
17+
self.add_argument(
18+
"--project",
19+
help="Set 'project'",
20+
)
21+
self.add_argument(
22+
"--package",
23+
help="Set 'package'",
24+
)
25+
self.add_argument(
26+
"--branch",
27+
help="Manage values for the specified branch (default: current branch)",
28+
)
29+
30+
def run(self, args):
31+
from osc.git_scm import LocalGitStore
32+
33+
store = LocalGitStore(".")
34+
branch = args.branch or store._git.current_branch
35+
36+
# just retrieve keys from an authoritative source
37+
keys = list(store._read_meta(branch=branch).dict().keys())
38+
keys.remove("header")
39+
40+
for key in keys:
41+
value = getattr(args, key, None)
42+
if value is None:
43+
continue
44+
# translate an empty string to None to unset the value
45+
setattr(store, key, value or None)

0 commit comments

Comments
 (0)