diff --git a/osc/commands/repo.py b/osc/commands/repo.py index 33c3c190..c745adc3 100644 --- a/osc/commands/repo.py +++ b/osc/commands/repo.py @@ -5,6 +5,7 @@ class RepoCommand(osc.commandline.OscCommand): """ Manage repositories in project meta """ + name = "repo" def run(self, args): diff --git a/osc/commands/repo_add.py b/osc/commands/repo_add.py index ec9ff5ca..9c8dadb9 100644 --- a/osc/commands/repo_add.py +++ b/osc/commands/repo_add.py @@ -1,9 +1,9 @@ import difflib import osc.commandline +from .. import obs_api from .. import oscerr -from .._private.project import ProjectMeta -from ..core import raw_input +from ..output import get_user_input class RepoAddCommand(osc.commandline.OscCommand): @@ -61,22 +61,50 @@ class RepoAddCommand(osc.commandline.OscCommand): project, repo = path.split("/") paths.append({"project": project, "repository": repo}) - meta = ProjectMeta.from_api(args.apiurl, args.project) - old_meta = meta.to_string().splitlines() + project_obj = obs_api.Project.from_api(args.apiurl, args.project) + old = project_obj.to_string() + + matching_repos = [i for i in project_obj.repository_list or [] if i.name == args.repo] + if matching_repos: + raise oscerr.OscValueError(f"Repository '{args.repo}' already exists in project meta") + + project_obj.repository_list.append( + { + "name": args.repo, + "arch_list": args.arches, + "path_list": paths, + } + ) - meta.repository_add(args.repo, args.arches, paths) if args.disable_publish: - meta.publish_add_disable_repository(args.repo) - - new_meta = meta.to_string().splitlines() - diff = difflib.unified_diff(old_meta, new_meta, fromfile="old", tofile="new") - print("\n".join(diff)) + matching_publish_disable_repos = [ + i for i in project_obj.publish_list or [] if i.flag == "disable" and i.repository == args.repo + ] + if not matching_publish_disable_repos: + if project_obj.publish_list is None: + project_obj.publish_list = [] + project_obj.publish_list.append( + { + "flag": "disable", + "repository": args.repo, + } + ) if not args.yes: + new = project_obj.to_string() + diff = difflib.unified_diff(old.splitlines(), new.splitlines(), fromfile="old", tofile="new") + print("\n".join(diff)) print() - print(f"You're changing meta of project '{args.project}'") - reply = raw_input("Do you want to apply the changes? [y/N] ").lower() - if reply != "y": + + reply = get_user_input( + f""" + You're changing meta of project '{args.project}' + Do you want to apply the changes? + """, + answers={"y": "yes", "n": "no"}, + ) + + if reply == "n": raise oscerr.UserAbort() - meta.to_api(args.apiurl, args.project) + project_obj.to_api(args.apiurl) diff --git a/osc/commands/repo_list.py b/osc/commands/repo_list.py index a23ea0f2..111a7e21 100644 --- a/osc/commands/repo_list.py +++ b/osc/commands/repo_list.py @@ -1,6 +1,6 @@ import osc.commandline +from .. import obs_api from ..output import KeyValueTable -from .._private.project import ProjectMeta class RepoListCommand(osc.commandline.OscCommand): @@ -19,9 +19,9 @@ class RepoListCommand(osc.commandline.OscCommand): ) def run(self, args): - meta = ProjectMeta.from_api(args.apiurl, args.project) + project_obj = obs_api.Project.from_api(args.apiurl, args.project) + repo_flags = project_obj.resolve_repository_flags() - repo_flags = meta.resolve_repository_flags() flag_map = {} for (repo_name, arch), data in repo_flags.items(): for flag_name, flag_value in data.items(): @@ -31,18 +31,18 @@ class RepoListCommand(osc.commandline.OscCommand): flag_map.setdefault(repo_name, {}).setdefault(flag_name, {}).setdefault(action, []).append(arch) table = KeyValueTable() - for repo in meta.repository_list(): - table.add("Repository", repo["name"], color="bold") - table.add("Architectures", ", ".join(repo["archs"])) - if repo["paths"]: - paths = [f"{path['project']}/{path['repository']}" for path in repo["paths"]] + for repo in project_obj.repository_list or []: + table.add("Repository", repo.name, color="bold") + table.add("Architectures", ", ".join(repo.arch_list)) + if repo.path_list: + paths = [f"{path.project}/{path.repository}" for path in repo.path_list] table.add("Paths", paths) - if repo["name"] in flag_map: + if repo.name in flag_map: table.add("Flags", None) - for flag_name in flag_map[repo["name"]]: + for flag_name in flag_map[repo.name]: lines = [] - for action, archs in flag_map[repo["name"]][flag_name].items(): + for action, archs in flag_map[repo.name][flag_name].items(): lines.append(f"{action + ':':<8s} {', '.join(archs)}") lines.sort() table.add(flag_name, lines, indent=4) diff --git a/osc/commands/repo_remove.py b/osc/commands/repo_remove.py index 859e919a..5832a438 100644 --- a/osc/commands/repo_remove.py +++ b/osc/commands/repo_remove.py @@ -1,9 +1,9 @@ import difflib import osc.commandline +from .. import obs_api from .. import oscerr -from .._private.project import ProjectMeta -from ..core import raw_input +from ..output import get_user_input class RepoRemoveCommand(osc.commandline.OscCommand): @@ -34,22 +34,35 @@ class RepoRemoveCommand(osc.commandline.OscCommand): ) def run(self, args): - meta = ProjectMeta.from_api(args.apiurl, args.project) - old_meta = meta.to_string().splitlines() + project_obj = obs_api.Project.from_api(args.apiurl, args.project) + old = project_obj.to_string() for repo in args.repo: - meta.repository_remove(repo) - meta.publish_remove_disable_repository(repo) + if project_obj.repository_list is not None: + project_obj.repository_list = [i for i in project_obj.repository_list if i.name != repo] + if project_obj.publish_list is not None: + project_obj.publish_list = [ + i for i in project_obj.publish_list if i.flag != "disable" or i.repository != repo + ] - new_meta = meta.to_string().splitlines() - diff = difflib.unified_diff(old_meta, new_meta, fromfile="old", tofile="new") - print("\n".join(diff)) + if not project_obj.has_changed(): + return if not args.yes: + new = project_obj.to_string() + diff = difflib.unified_diff(old.splitlines(), new.splitlines(), fromfile="old", tofile="new") + print("\n".join(diff)) print() - print(f"You're changing meta of project '{args.project}'") - reply = raw_input("Do you want to apply the changes? [y/N] ").lower() - if reply != "y": + + reply = get_user_input( + f""" + You're changing meta of project '{args.project}' + Do you want to apply the changes? + """, + answers={"y": "yes", "n": "no"}, + ) + + if reply == "n": raise oscerr.UserAbort() - meta.to_api(args.apiurl, args.project) + project_obj.to_api(args.apiurl)