[Kimchi-devel] [PATCH] Github #657 - fixing yum check-update parsing

Ramon Medeiros ramonn at linux.vnet.ibm.com
Fri Jun 12 15:39:47 UTC 2015


Reviewed-By: Ramon Medeiros <ramonn at br.ibm.com>
Tested-By: Ramon Medeiros <ramonn at br.ibm.com>

On 06/11/2015 04:23 PM, Daniel Henrique Barboza wrote:
> Enhancing the parsing of yum check-update command in the
> yumparser module.
>
> A new unit test was included to simulate the error condition.
>
> Signed-off-by: Daniel Henrique Barboza <dhbarboza82 at gmail.com>
> ---
>   src/kimchi/yumparser.py | 44 +++++++++++++++++++++++++++++++++-----------
>   tests/test_yumparser.py | 27 +++++++++++++++++++++++++++
>   2 files changed, 60 insertions(+), 11 deletions(-)
>
> diff --git a/src/kimchi/yumparser.py b/src/kimchi/yumparser.py
> index ce6ba9d..7612bbc 100644
> --- a/src/kimchi/yumparser.py
> +++ b/src/kimchi/yumparser.py
> @@ -216,6 +216,27 @@ class YumUpdatePackageObject(object):
>           self.ui_from_repo = repo
>
>
> +def _include_line_checkupdate_output(line):
> +    tokens = line.split()
> +
> +    if len(tokens) != 3:
> +        return False
> +
> +    if '.' not in tokens[0]:
> +        return False
> +
> +    return True
> +
> +
> +def _filter_lines_checkupdate_output(output):
> +    if output is None:
> +        return []
> +
> +    out = [l for l in output.split('\n')
> +           if _include_line_checkupdate_output(l)]
> +    return out
> +
> +
>   def _get_yum_checkupdate_output():
>       cmd = ['yum', 'check-update', '-d0']
>       yum_update_cmd = subprocess.Popen(cmd,
> @@ -225,23 +246,24 @@ def _get_yum_checkupdate_output():
>       if error != '':
>           return None
>
> -    return out.split()
> +    return out
>
>
> -def get_yum_packages_list_update():
> -    yum_checkupdate_output = _get_yum_checkupdate_output()
> -    if yum_checkupdate_output is None:
> -        return None
> +def get_yum_packages_list_update(checkupdate_output=None):
> +    if checkupdate_output is None:
> +        checkupdate_output = _get_yum_checkupdate_output()
> +
> +    filtered_output = _filter_lines_checkupdate_output(checkupdate_output)
>
>       packages = []
> -    index = 0
> -    while index < len(yum_checkupdate_output):
> -        name_arch = yum_checkupdate_output[index]
> -        index += 1
> -        version = yum_checkupdate_output[index]
> +    for line in filtered_output:
> +        line = line.split()
> +        index = 0
> +        name_arch = line[index]
>           index += 1
> -        repo = yum_checkupdate_output[index]
> +        version = line[index]
>           index += 1
> +        repo = line[index]
>           name, arch = name_arch.rsplit('.', 1)
>           packages.append(YumUpdatePackageObject(name, arch, version, repo))
>
> diff --git a/tests/test_yumparser.py b/tests/test_yumparser.py
> index 8645a65..4ec84ae 100644
> --- a/tests/test_yumparser.py
> +++ b/tests/test_yumparser.py
> @@ -23,6 +23,7 @@ import unittest
>
>   from kimchi.model import model
>   from kimchi.yumparser import delete_repo_from_file, get_repo_files
> +from kimchi.yumparser import get_yum_packages_list_update
>   from kimchi.yumparser import get_yum_repositories, write_repo_to_file
>   from kimchi.yumparser import YumRepoObject
>
> @@ -60,6 +61,22 @@ def _create_fake_repos_file():
>       return tmp_file_name
>
>
> +def _generate_yumcheckupdate_output():
> +    output = """
> +Repository 'REPOSITORY1' is missing name in configuration, using id
> +Repository 'REPOSITORY1-OPTIONAL' is missing name in configuration, using id
> +
> +PACKAGE1.noarch         20150611.-gg-FAKE1       REPOSITORY1
> +PACKAGE2.x86_64         20150611.-no-FAKE2       REPOSITORY2
> +PACKAGE3.dot.dot.i386   20150611.-re-FAKE3       REPOSITORY3
> +
> +Obsoleting Packages
> +OBSOLETE4.dot.dot.i386       20150611.FAKE4       REPOSITORY4
> +OBSOLETE5.dot.dot.fakearch   20150611.FAKE5       REPOSITORY5
> +    """
> +    return output
> +
> +
>   @unittest.skipIf(not _is_yum_distro(), 'Skipping: YUM exclusive test')
>   def setUpModule():
>       global TEMP_REPO_FILE
> @@ -109,3 +126,13 @@ class YumParserTests(unittest.TestCase):
>           repos = get_yum_repositories()
>           repos_id = repos.keys()
>           self.assertNotIn('fake-repo-3', repos_id)
> +
> +    def test_yum_checkupdate_parsing(self):
> +        output = _generate_yumcheckupdate_output()
> +        packages = get_yum_packages_list_update(output)
> +        self.assertEqual(len(packages), 5)
> +        self.assertEqual(packages[0].ui_from_repo, 'REPOSITORY1')
> +        self.assertEqual(packages[1].version, '20150611.-no-FAKE2')
> +        self.assertEqual(packages[2].name, 'PACKAGE3.dot.dot')
> +        self.assertEqual(packages[3].arch, 'i386')
> +        self.assertEqual(packages[4].arch, 'fakearch')

-- 

Ramon Nunes Medeiros
Kimchi Developer
Linux Technology Center Brazil
IBM Systems & Technology Group
Phone : +55 19 2132 7878
ramonn at br.ibm.com




More information about the Kimchi-devel mailing list