[Kimchi-devel] [PATCH 1/2] Implement a method to display the repo name expanding variables

Jose Ricardo Ziviani joserz at linux.vnet.ibm.com
Wed Sep 9 17:43:47 UTC 2015



On 08-09-2015 22:33, Aline Manera wrote:
>
>
> On 04/09/2015 13:20, Jose Ricardo Ziviani wrote:
>>   - Yum repos can use variables instead of hardcoded values in the config
>>     files. This patch adds a new field containing the repo name with all
>>     variables expanded. It will be displayed in Kimchi host tab to
>>     improve the usability.
>>
>> Signed-off-by: Jose Ricardo Ziviani <joserz at linux.vnet.ibm.com>
>> ---
>>   src/kimchi/repositories.py |  4 ++++
>>   src/kimchi/yumparser.py    | 47
>> +++++++++++++++++++++++++++++++++++++++++++++-
>>   2 files changed, 50 insertions(+), 1 deletion(-)
>>
>> diff --git a/src/kimchi/repositories.py b/src/kimchi/repositories.py
>> index 20b7311..8f99a88 100644
>> --- a/src/kimchi/repositories.py
>> +++ b/src/kimchi/repositories.py
>> @@ -30,6 +30,7 @@ from kimchi.exception import InvalidOperation,
>> InvalidParameter
>>   from kimchi.exception import OperationFailed, NotFoundError,
>> MissingParameter
>>   from kimchi.utils import validate_repo_url
>>   from kimchi.yumparser import get_yum_repositories, write_repo_to_file
>> +from kimchi.yumparser import get_display_name
>>
>>
>>   class Repositories(object):
>> @@ -149,10 +150,13 @@ class YumRepo(object):
>>
>>           entry = repos.get(repo_id)
>>
>> +        display_name = get_display_name(entry.name)
>> +
>>           info = {}
>>           info['enabled'] = entry.enabled
>>           info['baseurl'] = entry.baseurl or ''
>>           info['config'] = {}
>> +        info['config']['display_repo_name'] = display_name
>>           info['config']['repo_name'] = entry.name or ''
>>           info['config']['gpgcheck'] = entry.gpgcheck
>>           info['config']['gpgkey'] = entry.gpgkey or ''
>> diff --git a/src/kimchi/yumparser.py b/src/kimchi/yumparser.py
>> index 792c3ae..c278711 100644
>> --- a/src/kimchi/yumparser.py
>> +++ b/src/kimchi/yumparser.py
>> @@ -17,9 +17,10 @@
>>   # License along with this library; if not, write to the Free Software
>>   # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
>> 02110-1301 USA
>>   import subprocess
>> +import glob
>>
>>   from os import listdir
>> -from os.path import isfile, splitext
>> +from os.path import isfile, splitext, basename
>>
>>
>>   class YumRepoObject(object):
>> @@ -208,6 +209,50 @@ def delete_repo_from_file(repo):
>>           f.writelines(data)
>>
>>
>> +def _get_releasever():
>> +    release_file = glob.glob('/etc/*-release')[0]
>> +    cmd = ['rpm', '-q', '--qf', '"%{version}"', '-f', release_file]
>> +    relver = subprocess.Popen(cmd, stdout=subprocess.PIPE)
>> +    return relver.communicate()[0].strip('"\n')
>> +
>
> There is a package named rpm-python which provides binding to RPM tool.
> I don't know how the RPM output change from distro to distro so probably
> it is better to use the python binding.

This output *shouldn't* change from distro to distro because --qf 
"%{version}" means I'm querying only the version of that package. In 
practice, for --qf (queryformat) rpm calls printf so we could change the 
output in our own way:

# rpm -q --qf "name:\t%{name}\nversion:\t%{version}\n" -f /etc/os-release
name:	fedora-release
version:	21

# rpm -q --qf "name:\t%{name}\nversion:\t%{version}\n" -f /etc/os-release
name:	openSUSE-release
version:	13.2

But I'm checking how can I do it using the rpm binding.

>
>> +
>> +def _get_basearch():
>> +    cmd = ['uname', '-i']
>> +    uname = subprocess.Popen(cmd, stdout=subprocess.PIPE)
>> +    return uname.communicate()[0].strip('"\n')
>> +
>> +
>
> You can use os.uname()[4] to get the arch

I cannot use os.uname() here because I need the basearch, not the arch. 
check out the difference:

# uname -i
i383

 >> import os
 >> os.uname()[4]
'i686'

>
>> +def _get_all_yum_vars():
>> +    variables = {}
>> +
>> +    def _get_var_content(varfile):
>> +        with open(varfile) as f:
>> +            variables[basename(varfile)] = f.read().strip('\n')
>> +
>> +    map(lambda vfile:
>> +        _get_var_content(vfile),
>> +        glob.glob('/etc/yum/vars/*'))
>> +
>> +    return variables
>> +
>> +
>> +def get_display_name(name):
>> +    if not name:
>> +        return ''
>> +
>> +    yum_variables = _get_all_yum_vars()
>> +    yum_variables['releasever'] = _get_releasever()
>> +    yum_variables['basearch'] = _get_basearch()
>> +
>> +    name_vars = [var for var in name.split()
>> +                 if var.startswith('$') and var.strip('$') in
>> yum_variables]
>> +
>> +    return reduce(lambda nm, var:
>> +                  nm.replace(var, yum_variables[var.strip('$')]),
>> +                  name_vars,
>> +                  name)
>> +
>> +
>>   class YumUpdatePackageObject(object):
>>
>>       def __init__(self, name, arch, version, repo):
>

-- 
Jose Ricardo Ziviani
-----------------------------
Software Engineer
Linux Technology Center - IBM




More information about the Kimchi-devel mailing list