Browse code

have apt module raise an error if apt cannot be imported

Michael DeHaan authored on 2012/04/03 09:54:31
Showing 1 changed files
... ...
@@ -23,7 +23,6 @@ except ImportError:
23 23
     import simplejson as json
24 24
 import os
25 25
 import sys
26
-import apt
27 26
 import shlex
28 27
 import subprocess
29 28
 import traceback
... ...
@@ -44,6 +43,11 @@ def fail_json(**kwargs):
44 44
     kwargs['failed'] = True
45 45
     exit_json(rc=1, **kwargs)
46 46
 
47
+try:
48
+    import apt
49
+except ImportError:
50
+    fail_json(msg="could not import apt")
51
+
47 52
 def run_apt(command):
48 53
     try:
49 54
         cmd = subprocess.Popen(command, shell=True, 
... ...
@@ -117,24 +121,29 @@ purge        = params.get('purge', 'no')
117 117
 
118 118
 if state not in ['installed', 'latest', 'removed']:
119 119
     fail_json(msg='invalid state')
120
+
120 121
 if update_cache not in ['yes', 'no']:
121 122
     fail_json(msg='invalid value for update_cache (requires yes or no -- default is no')
123
+
122 124
 if purge not in ['yes', 'no']:
123 125
     fail_json(msg='invalid value for purge (requires yes or no -- default is no)')
126
+
124 127
 if package is None and update-cache != 'yes':
125 128
     fail_json(msg='pkg=name and/or update-cache=yes is required')
126 129
 
127 130
 cache = apt.Cache()
131
+
128 132
 if update_cache == 'yes':
129 133
     cache.update()
130 134
     cache.open()
131 135
 
132 136
 if state == 'latest':
133 137
     changed = install(package, cache, upgrade=True)
134
-if state == 'installed':
138
+elif state == 'installed':
135 139
     changed = install(package, cache)
136 140
 elif state == 'removed':
137 141
     changed = remove(package, cache, purge == 'yes')
142
+
138 143
 exit_json(changed=changed)
139 144
 
140 145