auto helper mode added

pull/1/head
alex 2017-12-24 08:21:11 +01:00
parent d0bddf7953
commit 843233987a
2 changed files with 27 additions and 19 deletions

View File

@ -10,7 +10,7 @@ Ansible module to use some AUR helpers. The following helpers are supported:
|--- |--- |--- |--- |---| |--- |--- |--- |--- |---|
|name |no | | |Name or list of names of the package(s) to install or upgrade.| |name |no | | |Name or list of names of the package(s) to install or upgrade.|
|upgrade |no |no |yes, no |Whether or not to upgrade whole system.| |upgrade |no |no |yes, no |Whether or not to upgrade whole system.|
|helper |no |pacaur |pacaur, trizen, yaourt, yay |Helper to use.| |use |no |auto |auto, pacaur, trizen, yaourt, yay |The helper to use, 'auto' uses the first helper found in the list.|
### Note ### Note
Either *name* or *upgrade* is required, both can not be used together. Either *name* or *upgrade* is required, both can not be used together.
@ -43,7 +43,7 @@ Use it in a task, as in the following examples:
# Install (using trizen) # Install (using trizen)
- aur: - aur:
helper: trizen use: trizen
name: name:
- package_name_1 - package_name_1
- package_name_2 - package_name_2

34
aur.py
View File

@ -4,32 +4,32 @@ from ansible.module_utils.basic import *
helper_cmd = { helper_cmd = {
'pacaur': ['env', 'LC_ALL=C', 'pacaur', '-S', '--noconfirm', '--noedit', '--needed', '--aur'],
'trizen': ['env', 'LC_ALL=C', 'trizen', '-S', '--noconfirm', '--noedit', '--needed', '--aur'], 'trizen': ['env', 'LC_ALL=C', 'trizen', '-S', '--noconfirm', '--noedit', '--needed', '--aur'],
'pacaur': ['env', 'LC_ALL=C', 'pacaur', '-S', '--noconfirm', '--noedit', '--needed', '--aur'],
'yaourt': ['env', 'LC_ALL=C', 'yaourt', '-S', '--noconfirm', '--needed'], 'yaourt': ['env', 'LC_ALL=C', 'yaourt', '-S', '--noconfirm', '--needed'],
'yay': ['env', 'LC_ALL=C', 'yay', '-S', '--noconfirm'], 'yay': ['env', 'LC_ALL=C', 'yay', '-S', '--noconfirm'],
} }
def upgrade(module, helper): def upgrade(module, use):
assert helper in helper_cmd assert use in helper_cmd
cmd = helper_cmd[helper] + ['-u'] cmd = helper_cmd[use] + ['-u']
rc, out, err = module.run_command(cmd, check_rc=True) rc, out, err = module.run_command(cmd, check_rc=True)
module.exit_json( module.exit_json(
changed=not (out == '' or 'there is nothing to do' in out or 'No AUR update found' in out), changed=not (out == '' or 'there is nothing to do' in out or 'No AUR updates found' in out),
msg='upgraded system', msg='upgraded system',
) )
def install_packages(module, packages, helper): def install_packages(module, packages, use):
assert helper in helper_cmd assert use in helper_cmd
changed_iter = False changed_iter = False
for package in packages: for package in packages:
cmd = helper_cmd[helper] + [package] cmd = helper_cmd[use] + [package]
rc, out, err = module.run_command(cmd, check_rc=True) rc, out, err = module.run_command(cmd, check_rc=True)
changed_iter = changed_iter or not (out == '' or '-- skipping' in out) changed_iter = changed_iter or not (out == '' or '-- skipping' in out)
@ -50,9 +50,9 @@ def main():
'default': False, 'default': False,
'type': 'bool', 'type': 'bool',
}, },
'helper': { 'use': {
'default': 'pacaur', 'default': 'auto',
'choices': ['pacaur', 'trizen', 'yaourt', 'yay'], 'choices': ['auto', 'pacaur', 'trizen', 'yaourt', 'yay'],
}, },
}, },
required_one_of=[['name', 'upgrade']], required_one_of=[['name', 'upgrade']],
@ -60,12 +60,20 @@ def main():
params = module.params params = module.params
if params['use'] == 'auto':
for k in helper_cmd:
if module.get_bin_path(k, False):
helper = k
break
else:
helper = params['use']
if params['upgrade'] and params['name']: if params['upgrade'] and params['name']:
module.fail_json(msg="Upgrade and install must be requested separately") module.fail_json(msg="Upgrade and install must be requested separately")
if params['upgrade']: if params['upgrade']:
upgrade(module, params['helper']) upgrade(module, helper)
else: else:
install_packages(module, params['name'], params['helper']) install_packages(module, params['name'], helper)
if __name__ == '__main__': if __name__ == '__main__':