#3440 Parse_arches allows string and list of arches
Merged 2 years ago by tkopecek. Opened 2 years ago by jcupova.
jcupova/koji issue-3434  into  master

file modified
+11 -10
@@ -1220,14 +1220,14 @@ 

  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 @@ 

                ['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:

@@ -3,8 +3,21 @@ 

  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 @@ 

          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'ěšč')

Please, just update docstring here to reflect on new behaviour. :thumbsup:

rebased onto 497b5a1

2 years ago

Metadata Update from @tkopecek:
- Pull-request tagged with: testing-ready

2 years ago

pretty please pagure-ci rebuild

2 years ago

Metadata Update from @jobrauer:
- Pull-request tagged with: testing-done

2 years ago

Commit 9d99bbe fixes this pull-request

Pull-Request has been merged by tkopecek

2 years ago