Commit 02d5e831 by Michael DeHaan

have apt module raise an error if apt cannot be imported

parent 2cb89c12
...@@ -23,7 +23,6 @@ except ImportError: ...@@ -23,7 +23,6 @@ except ImportError:
import simplejson as json import simplejson as json
import os import os
import sys import sys
import apt
import shlex import shlex
import subprocess import subprocess
import traceback import traceback
...@@ -44,6 +43,11 @@ def fail_json(**kwargs): ...@@ -44,6 +43,11 @@ def fail_json(**kwargs):
kwargs['failed'] = True kwargs['failed'] = True
exit_json(rc=1, **kwargs) exit_json(rc=1, **kwargs)
try:
import apt
except ImportError:
fail_json(msg="could not import apt")
def run_apt(command): def run_apt(command):
try: try:
cmd = subprocess.Popen(command, shell=True, cmd = subprocess.Popen(command, shell=True,
...@@ -117,24 +121,29 @@ purge = params.get('purge', 'no') ...@@ -117,24 +121,29 @@ purge = params.get('purge', 'no')
if state not in ['installed', 'latest', 'removed']: if state not in ['installed', 'latest', 'removed']:
fail_json(msg='invalid state') fail_json(msg='invalid state')
if update_cache not in ['yes', 'no']: if update_cache not in ['yes', 'no']:
fail_json(msg='invalid value for update_cache (requires yes or no -- default is no') fail_json(msg='invalid value for update_cache (requires yes or no -- default is no')
if purge not in ['yes', 'no']: if purge not in ['yes', 'no']:
fail_json(msg='invalid value for purge (requires yes or no -- default is no)') fail_json(msg='invalid value for purge (requires yes or no -- default is no)')
if package is None and update-cache != 'yes': if package is None and update-cache != 'yes':
fail_json(msg='pkg=name and/or update-cache=yes is required') fail_json(msg='pkg=name and/or update-cache=yes is required')
cache = apt.Cache() cache = apt.Cache()
if update_cache == 'yes': if update_cache == 'yes':
cache.update() cache.update()
cache.open() cache.open()
if state == 'latest': if state == 'latest':
changed = install(package, cache, upgrade=True) changed = install(package, cache, upgrade=True)
if state == 'installed': elif state == 'installed':
changed = install(package, cache) changed = install(package, cache)
elif state == 'removed': elif state == 'removed':
changed = remove(package, cache, purge == 'yes') changed = remove(package, cache, purge == 'yes')
exit_json(changed=changed) exit_json(changed=changed)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment