From 497b5a1fbe2f2ef4e81f416b0fa46ccb97f55dd1 Mon Sep 17 00:00:00 2001 From: Jana Cupova Date: Jul 13 2022 12:26:53 +0000 Subject: Parse_arches allows string and list of arches Fixes: https://pagure.io/koji/issue/3434 --- diff --git a/koji/__init__.py b/koji/__init__.py index 55f9c43..7d8bcd2 100644 --- a/koji/__init__.py +++ b/koji/__init__.py @@ -1220,14 +1220,14 @@ def canonArch(arch): def parse_arches(arches, to_list=False, strict=False, allow_none=False): """Normalize user input for a list of arches. - This method parses a single comma- or space-separated string of arches and + This method parses a single comma-, space-separated string or list of arches and returns a space-separated string. Raise an error if arches string contain non-allowed characters. In strict version allow only space-separated strings (db input). - :param str arches: comma- or space-separated string of arches, eg. - "x86_64,ppc64le", or "x86_64 ppc64le" + :param str|list arches: comma- or space-separated string of arches or list of arches, eg. + "x86_64,ppc64le", "x86_64 ppc64le", or "['x86_64', 'ppc64le']" :param bool to_list: return a list of each arch, instead of a single string. This is False by default. :param bool allow_none: convert None to "" @@ -1235,13 +1235,14 @@ def parse_arches(arches, to_list=False, strict=False, allow_none=False): ['x86_64', 'ppc64le']. """ if allow_none and arches is None: - arches = '' - if not strict: - arches = arches.replace(',', ' ') - if not re.match(r'^[a-zA-Z0-9_\- ]*$', arches): - raise GenericError("Architecture can be only [a-zA-Z0-9_-]") - - arches = arches.split() + arches = [] + if not isinstance(arches, list): + if not strict: + arches = arches.replace(',', ' ') + arches = arches.split() + for arch in arches: + if not re.match(r'^[a-zA-Z0-9_\-]*$', arch): + raise GenericError("Architecture can be only [a-zA-Z0-9_-]") if to_list: return arches else: diff --git a/tests/test_lib/test_parse_arches.py b/tests/test_lib/test_parse_arches.py index 00d0d77..59f2aaa 100644 --- a/tests/test_lib/test_parse_arches.py +++ b/tests/test_lib/test_parse_arches.py @@ -3,8 +3,21 @@ from __future__ import absolute_import import unittest import koji + class TestParseArchesString(unittest.TestCase): def test_parse_valid_arches(self): + r = koji.parse_arches('i386 x86_64') + self.assertEqual('i386 x86_64', r) + + r = koji.parse_arches('i386') + self.assertEqual('i386', r) + + r = koji.parse_arches('i386 x86_64 ') + self.assertEqual('i386 x86_64', r) + + r = koji.parse_arches('i386,x86_64') + self.assertEqual('i386 x86_64', r) + r = koji.parse_arches('i386', to_list=True) self.assertEqual(['i386'], r) @@ -17,6 +30,18 @@ class TestParseArchesString(unittest.TestCase): r = koji.parse_arches('i386,x86_64', to_list=True) self.assertEqual(['i386', 'x86_64'], r) + r = koji.parse_arches(['i386']) + self.assertEqual('i386', r) + + r = koji.parse_arches(['i386', 'x86_64']) + self.assertEqual('i386 x86_64', r) + + r = koji.parse_arches(['i386'], to_list=True) + self.assertEqual(['i386'], r) + + r = koji.parse_arches(['i386', 'x86_64'], to_list=True) + self.assertEqual(['i386', 'x86_64'], r) + def test_parse_invalid_arches(self): with self.assertRaises(koji.GenericError): koji.parse_arches(u'ěšč')