[PATCH V3 0/3] Issue #662 - Display repo name without variables

V3: - add the new field in docs/API.md. V2: - get releasever from rpm api instead of command line. This patch expands the repo name of all YUM repos to values if any known variable is found in it. For example: Fedora $releasever - $basearch will (based on the running system) be displayed: Fedora 21 - x86_64 Note: this is only an user experience improvement for the host tab. If user decides to edit the repo, the original variables will be displayed. Jose Ricardo Ziviani (3): Implement a method to display the repo name expanding variables Display the expanded repo name instead of variables Adds the new display_repo_name field in the JSON API docs/API.md | 2 ++ src/kimchi/repositories.py | 4 ++++ src/kimchi/yumparser.py | 60 +++++++++++++++++++++++++++++++++++++++++++++- ui/js/src/kimchi.host.js | 2 +- 4 files changed, 66 insertions(+), 2 deletions(-) -- 1.9.1

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

- In Kimchi host tab, if variables are found in the repo name this patch will actually display the repo name with the variables expanded to real values. Such values will not be displayed in the edit fields because that must follow the real config files. Signed-off-by: Jose Ricardo Ziviani <joserz@linux.vnet.ibm.com> --- ui/js/src/kimchi.host.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/js/src/kimchi.host.js b/ui/js/src/kimchi.host.js index b75b9e4..a85d74f 100644 --- a/ui/js/src/kimchi.host.js +++ b/ui/js/src/kimchi.host.js @@ -33,7 +33,7 @@ kimchi.host_main = function() { label: i18n['KCHREPO6004M'], 'class': 'repository-id' }, { - name: 'config[repo_name]', + name: 'config[display_repo_name]', label: i18n['KCHREPO6005M'], 'class': 'repository-name' }, { -- 1.9.1

Signed-off-by: Jose Ricardo Ziviani <joserz@linux.vnet.ibm.com> --- docs/API.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/API.md b/docs/API.md index 8b7e9ca..18d15ac 100644 --- a/docs/API.md +++ b/docs/API.md @@ -1116,6 +1116,8 @@ http://, ftp:// or file:// URL. * config: A dictionary that contains specific data according to repository type. * repo_name: Human-readable string describing the YUM repository. + * display_repo_name: The same name retrieve from repo_name with any + possible variable expanded. * mirrorlist: Specifies a URL to a file containing a list of baseurls for YUM repository * gpgcheck: True, to enable GPG signature verification; False, otherwise. -- 1.9.1
participants (2)
-
Aline Manera
-
Jose Ricardo Ziviani