From bbc8dae00694386240cd5ff95ac02373be9110c6 Mon Sep 17 00:00:00 2001 From: Igor Gnatenko Date: Fri, 22 May 2015 23:04:16 +0300 Subject: [PATCH] add ability to install packages Signed-off-by: Igor Gnatenko --- packaging/os/dnf.py | 60 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/packaging/os/dnf.py b/packaging/os/dnf.py index b3fa60bef42..32ae442042c 100644 --- a/packaging/os/dnf.py +++ b/packaging/os/dnf.py @@ -22,14 +22,11 @@ import traceback import os +import operator +import functools import dnf - -try: - from dnf import find_unfinished_transactions, find_ts_remaining - from rpmUtils.miscutils import splitFilename - transaction_helpers = True -except: - transaction_helpers = False +import dnf.cli +import dnf.util DOCUMENTATION = ''' --- @@ -174,6 +171,54 @@ def list_stuff(module, conf_file, stuff): else: return [pkg_to_dict(p) for p in dnf.subject.Subject(stuff).get_best_query(my.sack)] +def ensure(module, state, pkgspec, conf_file, enablerepo, disablerepo, disable_gpg_check): + my = dnf_base(conf_file) + items = pkgspec.split(',') + if disablerepo: + for repo in disablerepo.split(','): + [r.disable() for r in b.repos.get_matching(repo)] + if enablerepo: + for repo in enablerepo.split(','): + [r.enable() for r in b.repos.get_matching(repo)] + my.conf.gpgcheck = disable_gpg_check + + res = {} + res['results'] = [] + res['msg'] = '' + res['rc'] = 0 + res['changed'] = False + + if not dnf.util.am_i_root(): + res['msg'] = 'This command has to be run under the root user.' + res['rc'] = 1 + + pkg_specs, grp_specs, filenames = dnf.cli.commands.parse_spec_group_file(items) + if state in ['installed', 'present']: + # Install files. + local_pkgs = map(my.add_remote_rpm, filenames) + map(my.package_install, local_pkgs) + # Install groups. + if grp_specs: + my.read_comps() + my.env_group_install(grp_specs, dnf.const.GROUP_PACKAGE_TYPES) + # Install packages. + for pkg_spec in pkg_specs: + try: + my.install(pkg_spec) + except dnf.exceptions.MarkingError: + res['results'].append('No package %s available.' % pkg_spec) + res['rc'] = 1 + if not my.resolve() and res['rc'] == 0: + res['msg'] += 'Nothing to do' + res['changed'] = False + else: + my.download_packages(my.transaction.install_set) + my.do_transaction() + [res['results'].append('Installed: %s' % pkg) for pkg in my.transaction.install_set) + [res['results'].append('Removed: %s' % pkg) for pkg in my.transaction.remove_set) + + module.exit_json(**res) + def main(): # state=installed name=pkgspec @@ -212,7 +257,6 @@ def main(): module.exit_json(**results) else: - return pkg = params['name'] state = params['state'] enablerepo = params.get('enablerepo', '')