Pylint fixes for yum module

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2017-04-10 17:11:12 +05:30 committed by Matt Clay
parent db6d818865
commit ba3295dd3f

View file

@ -258,8 +258,8 @@ rpmbin = None
def yum_base(conf_file=None, installroot='/'): def yum_base(conf_file=None, installroot='/'):
my = yum.YumBase() my = yum.YumBase()
my.preconf.debuglevel=0 my.preconf.debuglevel = 0
my.preconf.errorlevel=0 my.preconf.errorlevel = 0
my.preconf.plugins = True my.preconf.plugins = True
#my.preconf.releasever = '/' #my.preconf.releasever = '/'
if installroot != '/': if installroot != '/':
@ -343,7 +343,7 @@ def is_installed(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=None, di
e = get_exception() e = get_exception()
module.fail_json(msg="Failure talking to yum: %s" % e) module.fail_json(msg="Failure talking to yum: %s" % e)
return [ po_to_nevra(p) for p in pkgs ] return [po_to_nevra(p) for p in pkgs]
else: else:
global rpmbin global rpmbin
@ -396,7 +396,7 @@ def is_available(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=None, di
for rid in en_repos: for rid in en_repos:
my.repos.enableRepo(rid) my.repos.enableRepo(rid)
e,m,u = my.pkgSack.matchPackageNames([pkgspec]) e, m, u = my.pkgSack.matchPackageNames([pkgspec])
pkgs = e + m pkgs = e + m
if not pkgs: if not pkgs:
pkgs.extend(my.returnPackagesByDep(pkgspec)) pkgs.extend(my.returnPackagesByDep(pkgspec))
@ -404,7 +404,7 @@ def is_available(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=None, di
e = get_exception() e = get_exception()
module.fail_json(msg="Failure talking to yum: %s" % e) module.fail_json(msg="Failure talking to yum: %s" % e)
return [ po_to_nevra(p) for p in pkgs ] return [po_to_nevra(p) for p in pkgs]
else: else:
myrepoq = list(repoq) myrepoq = list(repoq)
@ -416,9 +416,9 @@ def is_available(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=None, di
myrepoq.extend(r_cmd) myrepoq.extend(r_cmd)
cmd = myrepoq + ["--qf", qf, pkgspec] cmd = myrepoq + ["--qf", qf, pkgspec]
rc,out,err = module.run_command(cmd) rc, out, err = module.run_command(cmd)
if rc == 0: if rc == 0:
return [ p for p in out.split('\n') if p.strip() ] return [p for p in out.split('\n') if p.strip()]
else: else:
module.fail_json(msg='Error from repoquery: %s: %s' % (cmd, err)) module.fail_json(msg='Error from repoquery: %s: %s' % (cmd, err))
@ -445,7 +445,7 @@ def is_update(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=None, dis_r
pkgs = my.returnPackagesByDep(pkgspec) + my.returnInstalledPackagesByDep(pkgspec) pkgs = my.returnPackagesByDep(pkgspec) + my.returnInstalledPackagesByDep(pkgspec)
if not pkgs: if not pkgs:
e,m,u = my.pkgSack.matchPackageNames([pkgspec]) e, m, u = my.pkgSack.matchPackageNames([pkgspec])
pkgs = e + m pkgs = e + m
updates = my.doPackageLists(pkgnarrow='updates').updates updates = my.doPackageLists(pkgnarrow='updates').updates
except Exception: except Exception:
@ -456,7 +456,7 @@ def is_update(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=None, dis_r
if pkg in updates: if pkg in updates:
retpkgs.append(pkg) retpkgs.append(pkg)
return set([ po_to_nevra(p) for p in retpkgs ]) return set([po_to_nevra(p) for p in retpkgs])
else: else:
myrepoq = list(repoq) myrepoq = list(repoq)
@ -467,16 +467,16 @@ def is_update(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=None, dis_r
myrepoq.extend(r_cmd) myrepoq.extend(r_cmd)
cmd = myrepoq + ["--pkgnarrow=updates", "--qf", qf, pkgspec] cmd = myrepoq + ["--pkgnarrow=updates", "--qf", qf, pkgspec]
rc,out,err = module.run_command(cmd) rc, out, err = module.run_command(cmd)
if rc == 0: if rc == 0:
return set([ p for p in out.split('\n') if p.strip() ]) return set([p for p in out.split('\n') if p.strip()])
else: else:
module.fail_json(msg='Error from repoquery: %s: %s' % (cmd, err)) module.fail_json(msg='Error from repoquery: %s: %s' % (cmd, err))
return set() return set()
def what_provides(module, repoq, req_spec, conf_file, qf=def_qf, en_repos=None, dis_repos=None, installroot='/'): def what_provides(module, repoq, req_spec, conf_file, qf=def_qf, en_repos=None, dis_repos=None, installroot='/'):
if en_repos is None: if en_repos is None:
en_repos = [] en_repos = []
if dis_repos is None: if dis_repos is None:
@ -501,17 +501,17 @@ def what_provides(module, repoq, req_spec, conf_file, qf=def_qf, en_repos=None,
pkgs = my.returnPackagesByDep(req_spec) + my.returnInstalledPackagesByDep(req_spec) pkgs = my.returnPackagesByDep(req_spec) + my.returnInstalledPackagesByDep(req_spec)
if not pkgs: if not pkgs:
e,m,u = my.pkgSack.matchPackageNames([req_spec]) e, m, u = my.pkgSack.matchPackageNames([req_spec])
pkgs.extend(e) pkgs.extend(e)
pkgs.extend(m) pkgs.extend(m)
e,m,u = my.rpmdb.matchPackageNames([req_spec]) e, m, u = my.rpmdb.matchPackageNames([req_spec])
pkgs.extend(e) pkgs.extend(e)
pkgs.extend(m) pkgs.extend(m)
except Exception: except Exception:
e = get_exception() e = get_exception()
module.fail_json(msg="Failure talking to yum: %s" % e) module.fail_json(msg="Failure talking to yum: %s" % e)
return set([ po_to_nevra(p) for p in pkgs ]) return set([po_to_nevra(p) for p in pkgs])
else: else:
myrepoq = list(repoq) myrepoq = list(repoq)
@ -522,12 +522,12 @@ def what_provides(module, repoq, req_spec, conf_file, qf=def_qf, en_repos=None,
myrepoq.extend(r_cmd) myrepoq.extend(r_cmd)
cmd = myrepoq + ["--qf", qf, "--whatprovides", req_spec] cmd = myrepoq + ["--qf", qf, "--whatprovides", req_spec]
rc,out,err = module.run_command(cmd) rc, out, err = module.run_command(cmd)
cmd = myrepoq + ["--qf", qf, req_spec] cmd = myrepoq + ["--qf", qf, req_spec]
rc2,out2,err2 = module.run_command(cmd) rc2, out2, err2 = module.run_command(cmd)
if rc == 0 and rc2 == 0: if rc == 0 and rc2 == 0:
out += out2 out += out2
pkgs = set([ p for p in out.split('\n') if p.strip() ]) pkgs = set([p for p in out.split('\n') if p.strip()])
if not pkgs: if not pkgs:
pkgs = is_installed(module, repoq, req_spec, conf_file, qf=qf, installroot=installroot) pkgs = is_installed(module, repoq, req_spec, conf_file, qf=qf, installroot=installroot)
return pkgs return pkgs
@ -561,17 +561,17 @@ def transaction_exists(pkglist):
# the action is install/erase/etc., but we only # the action is install/erase/etc., but we only
# care about the package spec contained in the step # care about the package spec contained in the step
(action, step_spec) = step (action, step_spec) = step
(n,v,r,e,a) = splitFilename(step_spec) (n, v, r, e, a) = splitFilename(step_spec)
# and see if that spec is in the list of packages # and see if that spec is in the list of packages
# requested for installation/updating # requested for installation/updating
for pkg in pkglist_nvreas: for pkg in pkglist_nvreas:
# if the name and arch match, we're going to assume # if the name and arch match, we're going to assume
# this package is part of a pending transaction # this package is part of a pending transaction
# the label is just for display purposes # the label is just for display purposes
label = "%s-%s" % (n,a) label = "%s-%s" % (n, a)
if n == pkg[0] and a == pkg[4]: if n == pkg[0] and a == pkg[4]:
if label not in conflicts: if label not in conflicts:
conflicts.append("%s-%s" % (n,a)) conflicts.append("%s-%s" % (n, a))
break break
return conflicts return conflicts
@ -594,7 +594,7 @@ def local_nvra(module, path):
def pkg_to_dict(pkgstr): def pkg_to_dict(pkgstr):
if pkgstr.strip(): if pkgstr.strip():
n,e,v,r,a,repo = pkgstr.split('|') n, e, v, r, a, repo = pkgstr.split('|')
else: else:
return {'error_parsing': pkgstr} return {'error_parsing': pkgstr}
@ -605,7 +605,7 @@ def pkg_to_dict(pkgstr):
'release':r, 'release':r,
'version':v, 'version':v,
'repo':repo, 'repo':repo,
'nevra': '%s:%s-%s-%s.%s' % (e,n,v,r,a) 'nevra': '%s:%s-%s-%s.%s' % (e, n, v, r, a)
} }
if repo == 'installed': if repo == 'installed':
@ -618,10 +618,10 @@ def pkg_to_dict(pkgstr):
def repolist(module, repoq, qf="%{repoid}"): def repolist(module, repoq, qf="%{repoid}"):
cmd = repoq + ["--qf", qf, "-a"] cmd = repoq + ["--qf", qf, "-a"]
rc,out,err = module.run_command(cmd) rc, out, err = module.run_command(cmd)
ret = [] ret = []
if rc == 0: if rc == 0:
ret = set([ p for p in out.split('\n') if p.strip() ]) ret = set([p for p in out.split('\n') if p.strip()])
return ret return ret
def list_stuff(module, repoquerybin, conf_file, stuff, installroot='/', disablerepo='', enablerepo=''): def list_stuff(module, repoquerybin, conf_file, stuff, installroot='/', disablerepo='', enablerepo=''):
@ -640,15 +640,15 @@ def list_stuff(module, repoquerybin, conf_file, stuff, installroot='/', disabler
repoq += ['-c', conf_file] repoq += ['-c', conf_file]
if stuff == 'installed': if stuff == 'installed':
return [ pkg_to_dict(p) for p in sorted(is_installed(module, repoq, '-a', conf_file, qf=is_installed_qf, installroot=installroot)) if p.strip() ] return [pkg_to_dict(p) for p in sorted(is_installed(module, repoq, '-a', conf_file, qf=is_installed_qf, installroot=installroot)) if p.strip()]
elif stuff == 'updates': elif stuff == 'updates':
return [ pkg_to_dict(p) for p in sorted(is_update(module, repoq, '-a', conf_file, qf=qf, installroot=installroot)) if p.strip() ] return [pkg_to_dict(p) for p in sorted(is_update(module, repoq, '-a', conf_file, qf=qf, installroot=installroot)) if p.strip()]
elif stuff == 'available': elif stuff == 'available':
return [ pkg_to_dict(p) for p in sorted(is_available(module, repoq, '-a', conf_file, qf=qf, installroot=installroot)) if p.strip() ] return [pkg_to_dict(p) for p in sorted(is_available(module, repoq, '-a', conf_file, qf=qf, installroot=installroot)) if p.strip()]
elif stuff == 'repos': elif stuff == 'repos':
return [ dict(repoid=name, state='enabled') for name in sorted(repolist(module, repoq)) if name.strip() ] return [dict(repoid=name, state='enabled') for name in sorted(repolist(module, repoq)) if name.strip()]
else: else:
return [ pkg_to_dict(p) for p in sorted(is_installed(module,repoq, stuff, conf_file, qf=is_installed_qf, installroot=installroot)+ return [pkg_to_dict(p) for p in sorted(is_installed(module, repoq, stuff, conf_file, qf=is_installed_qf, installroot=installroot)+
is_available(module, repoq, stuff, conf_file, qf=qf, installroot=installroot)) if p.strip()] is_available(module, repoq, stuff, conf_file, qf=qf, installroot=installroot)) if p.strip()]
def install(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos, installroot='/'): def install(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos, installroot='/'):
@ -702,7 +702,7 @@ def install(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos, i
# most common case is the pkg is already installed and done # most common case is the pkg is already installed and done
# short circuit all the bs - and search for it as a pkg in is_installed # short circuit all the bs - and search for it as a pkg in is_installed
# if you find it then we're done # if you find it then we're done
if not set(['*','?']).intersection(set(spec)): if not set(['*', '?']).intersection(set(spec)):
installed_pkgs = is_installed(module, repoq, spec, conf_file, en_repos=en_repos, dis_repos=dis_repos, is_pkg=True, installroot=installroot) installed_pkgs = is_installed(module, repoq, spec, conf_file, en_repos=en_repos, dis_repos=dis_repos, is_pkg=True, installroot=installroot)
if installed_pkgs: if installed_pkgs:
res['results'].append('%s providing %s is already installed' % (installed_pkgs[0], spec)) res['results'].append('%s providing %s is already installed' % (installed_pkgs[0], spec))
@ -772,10 +772,10 @@ def install(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos, i
lang_env = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C') lang_env = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C')
rc, out, err = module.run_command(cmd, environ_update=lang_env) rc, out, err = module.run_command(cmd, environ_update=lang_env)
if (rc == 1): if rc == 1:
for spec in items: for spec in items:
# Fail on invalid urls: # Fail on invalid urls:
if ('://' in spec and ('No package %s available.' % spec in out or 'Cannot open: %s. Skipping.' % spec in err)): if '://' in spec and ('No package %s available.' % spec in out or 'Cannot open: %s. Skipping.' % spec in err):
module.fail_json(msg='Package at %s could not be installed' % spec, rc=1, changed=False) module.fail_json(msg='Package at %s could not be installed' % spec, rc=1, changed=False)
if (rc != 0 and 'Nothing to do' in err) or 'Nothing to do' in out: if (rc != 0 and 'Nothing to do' in err) or 'Nothing to do' in out:
# avoid failing in the 'Nothing To Do' case # avoid failing in the 'Nothing To Do' case
@ -852,7 +852,7 @@ def remove(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos, in
for pkg in pkgs: for pkg in pkgs:
if not pkg.startswith('@'): # we can't sensibly check for a group being uninstalled reliably if not pkg.startswith('@'): # we can't sensibly check for a group being uninstalled reliably
# look to see if the pkg shows up from is_installed. If it doesn't # look to see if the pkg shows up from is_installed. If it doesn't
if not is_installed(module, repoq, pkg, conf_file, en_repos=en_repos, dis_repos=dis_repos,installroot=installroot): if not is_installed(module, repoq, pkg, conf_file, en_repos=en_repos, dis_repos=dis_repos, installroot=installroot):
res['changed'] = True res['changed'] = True
else: else:
module.fail_json(**res) module.fail_json(**res)
@ -1076,7 +1076,7 @@ def ensure(module, state, pkgs, conf_file, enablerepo, disablerepo,
if repoq: if repoq:
repoq += ['-c', conf_file] repoq += ['-c', conf_file]
dis_repos =[] dis_repos = []
en_repos = [] en_repos = []
if skip_broken: if skip_broken:
@ -1163,8 +1163,8 @@ def ensure(module, state, pkgs, conf_file, enablerepo, disablerepo,
res = latest(module, pkgs, repoq, yum_basecmd, conf_file, en_repos, dis_repos, installroot=installroot) res = latest(module, pkgs, repoq, yum_basecmd, conf_file, en_repos, dis_repos, installroot=installroot)
else: else:
# should be caught by AnsibleModule argument_spec # should be caught by AnsibleModule argument_spec
module.fail_json(msg="we should never get here unless this all" module.fail_json(msg="we should never get here unless this all failed",
" failed", changed=False, results='', errors='unexpected state') changed=False, results='', errors='unexpected state')
return res return res
@ -1181,13 +1181,14 @@ def main():
# list=repos # list=repos
# list=pkgspec # list=pkgspec
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec=dict(
name=dict(aliases=['pkg'], type="list"), name=dict(aliases=['pkg'], type="list"),
exclude=dict(required=False, default=None), exclude=dict(required=False, default=None),
# removed==absent, installed==present, these are accepted as aliases # removed==absent, installed==present, these are accepted as aliases
state=dict(default='installed', choices=['absent','present','installed','removed','latest']), state=dict(default='installed', choices=['absent', 'present',
'installed', 'removed',
'latest']),
enablerepo=dict(), enablerepo=dict(),
disablerepo=dict(), disablerepo=dict(),
list=dict(), list=dict(),
@ -1200,9 +1201,9 @@ def main():
# this should not be needed, but exists as a failsafe # this should not be needed, but exists as a failsafe
install_repoquery=dict(required=False, default="yes", type='bool'), install_repoquery=dict(required=False, default="yes", type='bool'),
), ),
required_one_of = [['name','list']], required_one_of=[['name', 'list']],
mutually_exclusive = [['name','list']], mutually_exclusive=[['name', 'list']],
supports_check_mode = True supports_check_mode=True
) )
error_msgs = [] error_msgs = []
@ -1246,7 +1247,7 @@ def main():
if params['installroot'] != '/': if params['installroot'] != '/':
repoquery.extend(['--installroot', params['installroot']]) repoquery.extend(['--installroot', params['installroot']])
pkg = [ p.strip() for p in params['name']] pkg = [p.strip() for p in params['name']]
exclude = params['exclude'] exclude = params['exclude']
state = params['state'] state = params['state']
enablerepo = params.get('enablerepo', '') enablerepo = params.get('enablerepo', '')
@ -1254,10 +1255,10 @@ def main():
disable_gpg_check = params['disable_gpg_check'] disable_gpg_check = params['disable_gpg_check']
skip_broken = params['skip_broken'] skip_broken = params['skip_broken']
results = ensure(module, state, pkg, params['conf_file'], enablerepo, results = ensure(module, state, pkg, params['conf_file'], enablerepo,
disablerepo, disable_gpg_check, exclude, repoquery, skip_broken, disablerepo, disable_gpg_check, exclude, repoquery,
params['installroot']) skip_broken, params['installroot'])
if repoquery: if repoquery:
results['msg'] = '%s %s' % (results.get('msg',''), results['msg'] = '%s %s' % (results.get('msg', ''),
'Warning: Due to potential bad behaviour with rhnplugin and certificates, used slower repoquery calls instead of Yum API.') 'Warning: Due to potential bad behaviour with rhnplugin and certificates, used slower repoquery calls instead of Yum API.')
module.exit_json(**results) module.exit_json(**results)