From 18852d81f9ced6db40b9a31f0cdd48e9f7d78f0f Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Mon, 2 Apr 2012 20:54:31 -0400 Subject: [PATCH] have apt module raise an error if apt cannot be imported --- apt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/apt b/apt index b5418903b9d..b8d441660eb 100755 --- a/apt +++ b/apt @@ -23,7 +23,6 @@ except ImportError: import simplejson as json import os import sys -import apt import shlex import subprocess import traceback @@ -44,6 +43,11 @@ def fail_json(**kwargs): kwargs['failed'] = True exit_json(rc=1, **kwargs) +try: + import apt +except ImportError: + fail_json(msg="could not import apt") + def run_apt(command): try: cmd = subprocess.Popen(command, shell=True, @@ -117,24 +121,29 @@ purge = params.get('purge', 'no') if state not in ['installed', 'latest', 'removed']: fail_json(msg='invalid state') + if update_cache not in ['yes', 'no']: fail_json(msg='invalid value for update_cache (requires yes or no -- default is no') + if purge not in ['yes', 'no']: fail_json(msg='invalid value for purge (requires yes or no -- default is no)') + if package is None and update-cache != 'yes': fail_json(msg='pkg=name and/or update-cache=yes is required') cache = apt.Cache() + if update_cache == 'yes': cache.update() cache.open() if state == 'latest': changed = install(package, cache, upgrade=True) -if state == 'installed': +elif state == 'installed': changed = install(package, cache) elif state == 'removed': changed = remove(package, cache, purge == 'yes') + exit_json(changed=changed)