From 4634690176778bee1a60c991a10493ac4121e8f6 Mon Sep 17 00:00:00 2001 From: Daniel Mach Date: Wed, 9 Nov 2022 10:31:20 +0100 Subject: [PATCH] Move showlinked code from commandline to _private --- .../features/showlinked-pkgcheckout.feature | 17 +++++++++++++ .../features/showlinked-prjcheckout.feature | 17 +++++++++++++ .../showlinked-project-package.feature | 23 ++++++++++++++++++ osc/_private/__init__.py | 1 + osc/_private/api_source.py | 16 +++++++++++++ osc/commandline.py | 24 +++++++------------ 6 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 behave/features/showlinked-pkgcheckout.feature create mode 100644 behave/features/showlinked-prjcheckout.feature create mode 100644 behave/features/showlinked-project-package.feature diff --git a/behave/features/showlinked-pkgcheckout.feature b/behave/features/showlinked-pkgcheckout.feature new file mode 100644 index 00000000..5fced7af --- /dev/null +++ b/behave/features/showlinked-pkgcheckout.feature @@ -0,0 +1,17 @@ +Feature: `osc showlinked` command + + +# common steps for all scenarios +Background: + Given I set working directory to "{context.osc.temp}" + And I execute osc with args "checkout test:factory/test-pkgA" + And I set working directory to "{context.osc.temp}/test:factory/test-pkgA" + And I execute osc with args "linkpac test:factory/test-pkgA home:Admin --force" + + +Scenario: Run `osc showlinked` + When I execute osc with args "showlinked" + Then stdout is + """ + home:Admin/test-pkgA + """ diff --git a/behave/features/showlinked-prjcheckout.feature b/behave/features/showlinked-prjcheckout.feature new file mode 100644 index 00000000..8753ac77 --- /dev/null +++ b/behave/features/showlinked-prjcheckout.feature @@ -0,0 +1,17 @@ +Feature: `osc showlinked` command + + +# common steps for all scenarios +Background: + Given I set working directory to "{context.osc.temp}" + And I execute osc with args "checkout test:factory" + And I set working directory to "{context.osc.temp}/test:factory" + + +Scenario: Run `osc showlinked` + When I execute osc with args "showlinked" + Then the exit code is 1 + And stderr is + """ + Directory '{context.osc.temp}/test:factory' is not a working copy of a package + """ diff --git a/behave/features/showlinked-project-package.feature b/behave/features/showlinked-project-package.feature new file mode 100644 index 00000000..141206b2 --- /dev/null +++ b/behave/features/showlinked-project-package.feature @@ -0,0 +1,23 @@ +Feature: `osc showlinked` command + + +# common steps for all scenarios +Background: + Given I set working directory to "{context.osc.temp}" + And I execute osc with args "linkpac test:factory/test-pkgA home:Admin --force" + + +Scenario: Run `osc showlinked ` + When I execute osc with args "showlinked test:factory test-pkgA" + Then stdout is + """ + home:Admin/test-pkgA + """ + + +Scenario: Run `osc showlinked /` + When I execute osc with args "showlinked test:factory/test-pkgA" + Then stdout is + """ + home:Admin/test-pkgA + """ diff --git a/osc/_private/__init__.py b/osc/_private/__init__.py index 21581a7d..843b162f 100644 --- a/osc/_private/__init__.py +++ b/osc/_private/__init__.py @@ -6,6 +6,7 @@ from .api_source import add_channels from .api_source import add_containers from .api_source import enable_channels +from .api_source import get_linked_packages from .common import print_msg from .common import format_msg_project_package_options from .package import ApiPackage diff --git a/osc/_private/api_source.py b/osc/_private/api_source.py index eaa7a81e..f49f4c4c 100644 --- a/osc/_private/api_source.py +++ b/osc/_private/api_source.py @@ -66,3 +66,19 @@ def enable_channels(apiurl, project, package=None, print_to="debug"): url_query = {"cmd": "modifychannels", "mode": "enable_all"} return api.post(apiurl, url_path, url_query) + + +def get_linked_packages(apiurl, project, package): + url_path = ["source", project, package] + url_query = {"cmd": "showlinked"} + root = api.post(apiurl, url_path, url_query) + + result = [] + nodes = api.find_nodes(root, "collection", "package") + for node in nodes: + item = { + "project": node.get("project"), + "name": node.get("name"), + } + result.append(item) + return result diff --git a/osc/commandline.py b/osc/commandline.py index 2427f5dd..1cf7bdd0 100644 --- a/osc/commandline.py +++ b/osc/commandline.py @@ -826,24 +826,16 @@ class Osc(cmdln.Cmdln): osc showlinked [PROJECT PACKAGE] """ - args = slash_split(args) apiurl = self.get_api_url() - localdir = Path.cwd() - project = package = None - if len(args) == 2: - project = self._process_project_name(args[0]) - package = args[1] - elif is_package_dir(localdir): - project = store_read_project(localdir) - package = store_read_package(localdir) - else: - raise oscerr.WrongArgs('Either specify project and package or call it from a package working copy') - url = makeurl(apiurl, ['source', project, package], query={'cmd': 'showlinked'}) - f = http_POST(url) - root = ET.parse(f).getroot() - for node in root.findall('package'): - print(node.get('project') + " " + node.get('name')) + args = list(args) + project, package = pop_project_package_from_args( + args, default_project=".", default_package=".", package_is_optional=False + ) + + linked_packages = _private.get_linked_packages(apiurl, project, package) + for pkg in linked_packages: + print(f"{pkg['project']}/{pkg['name']}") @cmdln.option('-c', '--create', action='store_true', help='Create a new token')