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

Jose Ricardo Ziviani joserz at linux.vnet.ibm.com
Thu Sep 10 15:59:15 UTC 2015


 - 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    | 60 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 63 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..0c9de77 100644
--- a/src/kimchi/yumparser.py
+++ b/src/kimchi/yumparser.py
@@ -17,9 +17,15 @@
 # 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
+
+try:
+    import rpm
+except ImportError:
+    pass
 
 
 class YumRepoObject(object):
@@ -208,6 +214,58 @@ def delete_repo_from_file(repo):
         f.writelines(data)
 
 
+def _get_releasever():
+    release_file = glob.glob('/etc/*-release')[0]
+    transaction = rpm.TransactionSet()
+    match_iter = transaction.dbMatch('basenames', release_file)
+
+    ret = '%releasever'
+    try:
+        ret = match_iter.next()['version']
+
+    except StopIteration:
+        pass
+
+    return ret
+
+
+def _get_basearch():
+    cmd = ['uname', '-i']
+    uname = subprocess.Popen(cmd, stdout=subprocess.PIPE)
+    return uname.communicate()[0].strip('"\n')
+
+
+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):
-- 
1.9.1




More information about the Kimchi-devel mailing list