1 |
commit: 030a76d75fe5cdad175355c90680361cd5b59723 |
2 |
Author: Pavlos Ratis <dastergon <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon May 19 23:38:01 2014 +0000 |
4 |
Commit: Brian Dolbec <brian.dolbec <AT> gmail <DOT> com> |
5 |
CommitDate: Tue May 20 12:27:15 2014 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/gentoo-keys.git;a=commit;h=030a76d7 |
7 |
|
8 |
misc improvements |
9 |
|
10 |
* Rename some variables to a more easy-to-understand name |
11 |
* Improve some strings and fix typos |
12 |
|
13 |
--- |
14 |
gkeyldap/actions.py | 105 +++++++++++++++++++++++++-------------------------- |
15 |
gkeyldap/search.py | 9 ++--- |
16 |
gkeys/actions.py | 10 ++--- |
17 |
gkeys/config.py | 2 +- |
18 |
gkeys/seed.py | 1 - |
19 |
gkeys/seedhandler.py | 30 +++++++-------- |
20 |
6 files changed, 77 insertions(+), 80 deletions(-) |
21 |
|
22 |
diff --git a/gkeyldap/actions.py b/gkeyldap/actions.py |
23 |
index 27b35bf..55c3e13 100644 |
24 |
--- a/gkeyldap/actions.py |
25 |
+++ b/gkeyldap/actions.py |
26 |
@@ -13,7 +13,7 @@ |
27 |
import os |
28 |
import re |
29 |
|
30 |
-from gkeys.config import GKEY, KEYID, LONGKEYID, FINGERPRINT, KEY_LEN |
31 |
+from gkeys.config import GKEY, KEYID, LONGKEYID, FINGERPRINT, KEYLEN_MAP |
32 |
from gkeys.seed import Seeds |
33 |
from gkeyldap.search import (LdapSearch, UID, gkey2ldap_map, gkey2SEARCH) |
34 |
|
35 |
@@ -21,22 +21,21 @@ from gkeyldap.search import (LdapSearch, UID, gkey2ldap_map, gkey2SEARCH) |
36 |
Available_Actions = ['ldapsearch', 'updateseeds'] |
37 |
|
38 |
|
39 |
-def get_key_ids(key, info): |
40 |
+def get_key_ids(key_len, keyids): |
41 |
'''Small utility function to return only keyid (short) |
42 |
or longkeyid's |
43 |
|
44 |
- @param key: string, the key length desired |
45 |
- @param info: list of keysid's to process |
46 |
+ @param key_len: string, the key length desired |
47 |
+ @param keyids: list of keysid's to process |
48 |
@return list of the desired key length id's |
49 |
''' |
50 |
result = [] |
51 |
- for x in info: |
52 |
- if x.startswith('0x'): |
53 |
- mylen = KEY_LEN[key] + 2 |
54 |
- else: |
55 |
- mylen = KEY_LEN[key] |
56 |
- if len(x) == mylen: |
57 |
- result.append(x) |
58 |
+ for keyid in keyids: |
59 |
+ target_len = KEYLEN_MAP[key_len] |
60 |
+ if keyid.startswith('0x'): |
61 |
+ target_len = KEYLEN_MAP[key_len] + 2 |
62 |
+ if len(keyid) == target_len: |
63 |
+ result.append(keyid) |
64 |
return result |
65 |
|
66 |
|
67 |
@@ -53,11 +52,11 @@ class Actions(object): |
68 |
|
69 |
def ldapsearch(self, args): |
70 |
l = LdapSearch() |
71 |
- self.logger.info("Search...establishing connection") |
72 |
- self.output("Search...establishing connection") |
73 |
+ self.logger.info("Search... Establishing connection") |
74 |
+ self.output("Search... Establishing connection") |
75 |
if not l.connect(): |
76 |
- self.logger.info("Aborting Search...Connection failed") |
77 |
- self.output("Aborting Search...Connection failed") |
78 |
+ self.logger.info("Aborting search... Connection failed") |
79 |
+ self.output("Aborting search... Connection failed") |
80 |
return False |
81 |
self.logger.debug("MAIN: _action_ldapsearch; args = %s" % str(args)) |
82 |
x, target, search_field = self.get_args(args) |
83 |
@@ -66,26 +65,26 @@ class Actions(object): |
84 |
for dev in sorted(devs): |
85 |
self.output(dev, devs[dev]) |
86 |
self.output("============================================") |
87 |
- self.output("Total number of devs in results:", len(devs)) |
88 |
+ self.output("Total number of developers in results:", len(devs)) |
89 |
self.logger.info("============================================") |
90 |
- self.logger.info("Total number of devs in results: %d" % len(devs)) |
91 |
+ self.logger.info("Total number of developers in results: %d" % len(devs)) |
92 |
return True |
93 |
|
94 |
|
95 |
def updateseeds(self, args): |
96 |
- self.logger.info("Beginning ldap search...") |
97 |
- self.output("Beginning ldap search...") |
98 |
+ self.logger.info("Beginning LDAP search...") |
99 |
+ self.output("Beginning LDAP search...") |
100 |
l = LdapSearch() |
101 |
if not l.connect(): |
102 |
- self.output("Aborting Update...Connection failed") |
103 |
- self.logger.info("Aborting Update...Connection failed") |
104 |
+ self.output("Aborting update... Connection failed") |
105 |
+ self.logger.info("Aborting update... Connection failed") |
106 |
return False |
107 |
results = l.search('*', UID) |
108 |
info = l.result2dict(results, 'uid') |
109 |
self.logger.debug( |
110 |
"MAIN: _action_updateseeds; got results :) converted to info") |
111 |
if not self.create_seedfile(info): |
112 |
- self.logger.error("Dev seed file update failure: " |
113 |
+ self.logger.error("Developer seed file update failure: " |
114 |
"Original seed file is intact & untouched.") |
115 |
filename = self.config['dev-seedfile'] |
116 |
old = filename + '.old' |
117 |
@@ -108,12 +107,12 @@ class Actions(object): |
118 |
os.rename(filename + '.new', filename) |
119 |
except IOError: |
120 |
raise |
121 |
- self.output("Developer Seed file updated") |
122 |
+ self.output("Developer seed file updated!") |
123 |
return True |
124 |
|
125 |
|
126 |
def create_seedfile(self, devs): |
127 |
- self.output("Creating seeds from ldap data...") |
128 |
+ self.output("Creating seeds from LDAP data...") |
129 |
filename = self.config['dev-seedfile'] + '.new' |
130 |
self.seeds = Seeds(filename) |
131 |
count = 0 |
132 |
@@ -131,22 +130,22 @@ class Actions(object): |
133 |
else: |
134 |
error_count += 1 |
135 |
self.output("Total number of seeds created:", count) |
136 |
- self.output("Seeds created...saving file: %s" % filename) |
137 |
- self.output("Total number of Dev's with gpg errors:", error_count) |
138 |
+ self.output("Seeds created... Saving file: %s" % filename) |
139 |
+ self.output("Total number of Dev's with GPG errors:", error_count) |
140 |
self.logger.info("Total number of seeds created: %d" % count) |
141 |
- self.logger.info("Seeds created...saving file: %s" % filename) |
142 |
- self.logger.info("Total number of Dev's with gpg errors: %d" % error_count) |
143 |
+ self.logger.info("Seeds created... Saving file: %s" % filename) |
144 |
+ self.logger.info("Total number of Dev's with GPG errors: %d" % error_count) |
145 |
return self.seeds.save() |
146 |
|
147 |
|
148 |
@staticmethod |
149 |
def get_args(args): |
150 |
- for x in ['nick', 'name', 'gpgkey', 'fingerprint', 'status']: |
151 |
- if x: |
152 |
- target = getattr(args, x) |
153 |
- search_field = gkey2SEARCH[x] |
154 |
+ for attr in ['nick', 'name', 'gpgkey', 'fingerprint', 'status']: |
155 |
+ if attr: |
156 |
+ target = getattr(args, attr) |
157 |
+ search_field = gkey2SEARCH[attr] |
158 |
break |
159 |
- return (x, target, search_field) |
160 |
+ return (attr, target, search_field) |
161 |
|
162 |
|
163 |
def build_gkeylist(self, info): |
164 |
@@ -156,8 +155,8 @@ class Actions(object): |
165 |
# assume it's good until an error is found |
166 |
is_good = True |
167 |
#self.logger.debug("Actions: build_gkeylist; info = %s" % str(info)) |
168 |
- for x in GKEY._fields: |
169 |
- field = gkey2ldap_map[x] |
170 |
+ for attr in GKEY._fields: |
171 |
+ field = gkey2ldap_map[attr] |
172 |
if not field: |
173 |
keyinfo.append(None) |
174 |
continue |
175 |
@@ -167,24 +166,24 @@ class Actions(object): |
176 |
if values and field in ['uid', 'cn' ]: |
177 |
value = values[0] |
178 |
# separate out short/long key id's |
179 |
- elif values and x in ['keyid', 'longkeyid']: |
180 |
- value = get_key_ids(x, values) |
181 |
+ elif values and attr in ['keyid', 'longkeyid']: |
182 |
+ value = get_key_ids(attr, values) |
183 |
if len(value): |
184 |
keyid_found = True |
185 |
- elif values and x in ['fingerprint']: |
186 |
+ elif values and attr in ['fingerprint']: |
187 |
value = [v.replace(' ', '') for v in values] |
188 |
else: |
189 |
value = values |
190 |
if 'undefined' in values: |
191 |
- self.logger.error('ERROR in ldap info for: %s, %s' |
192 |
+ self.logger.error('ERROR in LDAP info for: %s, %s' |
193 |
%(info['uid'][0],info['cn'][0])) |
194 |
self.logger.error(' %s = "undefined"' %(field)) |
195 |
is_good = False |
196 |
keyinfo.append(value) |
197 |
except KeyError: |
198 |
- self.logger.debug('Ldap info for: %s, %s' |
199 |
+ self.logger.debug('LDAP info for: %s, %s' |
200 |
%(info['uid'][0],info['cn'][0])) |
201 |
- self.logger.debug(' MISSING or EMPTY ldap field ' + |
202 |
+ self.logger.debug(' MISSING or EMPTY LDAP field ' + |
203 |
'[%s] GPGKey field [%s]' %(field, x)) |
204 |
if x in ['keyid', 'longkeyid']: |
205 |
keyid_missing = True |
206 |
@@ -195,21 +194,21 @@ class Actions(object): |
207 |
fingerprint = None |
208 |
try: |
209 |
fingerprint = info[gkey2ldap_map['fingerprint']] |
210 |
- self.logger.debug(' Generate gpgkey, Found ldap fingerprint field') |
211 |
+ self.logger.debug(' Generate gpgkey, Found LDAP fingerprint field') |
212 |
except KeyError: |
213 |
- gpgkey = 'Missing fingerprint from ldap info' |
214 |
- self.logger.debug(' Generate gpgkey, ldap fingerprint KeyError') |
215 |
+ gpgkey = 'Missing fingerprint from LDAP info' |
216 |
+ self.logger.debug(' Generate gpgkey, LDAP fingerprint KeyError') |
217 |
if fingerprint: |
218 |
values = [y.strip('\n') for y in fingerprint] |
219 |
value = [v.replace(' ', '') for v in values] |
220 |
# assign it to gpgkey to prevent a possible |
221 |
# "gpgkey" undefined error |
222 |
- gpgkey = ['0x' + x[-KEY_LEN['longkeyid']:] for x in value] |
223 |
+ gpgkey = ['0x' + x[-KEYLEN_MAP['longkeyid']:] for x in value] |
224 |
keyinfo[LONGKEYID] = gpgkey |
225 |
self.logger.debug(' Generate gpgkey, NEW keyinfo[LONGKEYID] = %s' |
226 |
% str(keyinfo[LONGKEYID])) |
227 |
else: |
228 |
- gpgkey = 'Missing or Bad fingerprint from ldap info' |
229 |
+ gpgkey = 'Missing or Bad fingerprint from LDAP info' |
230 |
is_good = False |
231 |
if not keyinfo[LONGKEYID]: |
232 |
self.logger.error('ERROR in ldap info for: %s, %s' |
233 |
@@ -239,7 +238,7 @@ class Actions(object): |
234 |
index = len(y.lstrip('0x')) |
235 |
if y.lstrip('0x').upper() not in \ |
236 |
[x[-index:].upper() for x in keyinfo[FINGERPRINT]]: |
237 |
- self.logger.error('ERROR in ldap info for: %s, %s' |
238 |
+ self.logger.error('ERROR in LDAP info for: %s, %s' |
239 |
%(info['uid'][0],info['cn'][0])) |
240 |
self.logger.error(' ' + str(keyinfo)) |
241 |
self.logger.error(' GPGKey id %s not found in the ' |
242 |
@@ -251,19 +250,19 @@ class Actions(object): |
243 |
def _check_fingerprint_integrity(self, info, keyinfo): |
244 |
# assume it's good until found an error is found |
245 |
is_good = True |
246 |
- for x in keyinfo[FINGERPRINT]: |
247 |
+ for fingerprint in keyinfo[FINGERPRINT]: |
248 |
# check fingerprint integrity |
249 |
- if len(x) != 40: |
250 |
- self.logger.error('ERROR in ldap info for: %s, %s' |
251 |
+ if len(fingerprint) != 40: |
252 |
+ self.logger.error('ERROR in LDAP info for: %s, %s' |
253 |
%(info['uid'][0],info['cn'][0])) |
254 |
self.logger.error(' GPGKey incorrect fingerprint ' + |
255 |
- 'length (%s) for fingerprint: %s' %(len(x), x)) |
256 |
+ 'length (%s) for fingerprint: %s' %(len(fingerprint), fingerprint)) |
257 |
is_good = False |
258 |
continue |
259 |
- if not self.fingerprint_re.match(x): |
260 |
+ if not self.fingerprint_re.match(fingerprint): |
261 |
self.logger.error('ERROR in ldap info for: %s, %s' |
262 |
%(info['uid'][0],info['cn'][0])) |
263 |
self.logger.error(' GPGKey: Non hexadecimal digits in ' + |
264 |
- 'fingerprint for fingerprint: ' + x) |
265 |
+ 'fingerprint for fingerprint: ' + fingerprint) |
266 |
is_good = False |
267 |
return is_good |
268 |
|
269 |
diff --git a/gkeyldap/search.py b/gkeyldap/search.py |
270 |
index 2cbc4b9..087bee6 100644 |
271 |
--- a/gkeyldap/search.py |
272 |
+++ b/gkeyldap/search.py |
273 |
@@ -60,7 +60,7 @@ gkey2SEARCH = { |
274 |
|
275 |
|
276 |
class LdapSearch(object): |
277 |
- '''Class to perform searches on the configured ldap server |
278 |
+ '''Class to perform searches on the configured LDAP server |
279 |
''' |
280 |
|
281 |
def __init__(self, server=None, fields=None, criteria=None): |
282 |
@@ -74,9 +74,9 @@ class LdapSearch(object): |
283 |
|
284 |
|
285 |
def connect(self, server=None,): |
286 |
- '''Creates our ldap server connection |
287 |
- |
288 |
+ '''Creates our LDAP server connection |
289 |
''' |
290 |
+ |
291 |
if server: |
292 |
self.server = server |
293 |
logger.debug('LdapSearch: connect; new server: %s' % self.server) |
294 |
@@ -93,9 +93,8 @@ class LdapSearch(object): |
295 |
return True |
296 |
|
297 |
|
298 |
- |
299 |
def search(self, target, search_field=UID, fields=None, criteria=None): |
300 |
- '''Perform the ldap search |
301 |
+ '''Perform the LDAP search |
302 |
''' |
303 |
if not target: |
304 |
logger.debug('LdapSearch: search; invalid target: "%s"' % target) |
305 |
|
306 |
diff --git a/gkeys/actions.py b/gkeys/actions.py |
307 |
index 85c0e0e..79690fc 100644 |
308 |
--- a/gkeys/actions.py |
309 |
+++ b/gkeys/actions.py |
310 |
@@ -76,7 +76,7 @@ class Actions(object): |
311 |
success = self.seeds.add(gkey) |
312 |
if success: |
313 |
success = self.seeds.save() |
314 |
- return ["Successfully Added new seed: %s" % str(success), gkey] |
315 |
+ return ["Successfully added new seed: %s" % str(success), gkey] |
316 |
else: |
317 |
messages = ["Matching seeds found in seeds file", |
318 |
"Aborting... \nMatching seeds:"] |
319 |
@@ -91,20 +91,20 @@ class Actions(object): |
320 |
self.logger.debug("ACTIONS: removeseed; gkey: %s" % str(searchkey)) |
321 |
gkeys = self.listseed(args) |
322 |
if not gkeys: |
323 |
- return ["Failed to Removed seed: No gkeys returned from listseed()", |
324 |
+ return ["Failed to remove seed: No gkeys returned from listseed()", |
325 |
None] |
326 |
if len(gkeys) == 1: |
327 |
self.logger.debug("ACTIONS: removeseed; now deleting gkey: %s" % str(gkeys[0])) |
328 |
success = self.seeds.delete(gkeys[0]) |
329 |
if success: |
330 |
success = self.seeds.save() |
331 |
- return ["Successfully Removed seed: %s" % str(success), |
332 |
+ return ["Successfully removed seed: %s" % str(success), |
333 |
gkeys[0]] |
334 |
elif len(gkeys): |
335 |
messages = ["Too many seeds found to remove"] |
336 |
messages.extend(gkeys) |
337 |
return messages |
338 |
- return ["Failed to Remove seed:", searchkey, |
339 |
+ return ["Failed to remove seed:", searchkey, |
340 |
"No matching seed found"] |
341 |
|
342 |
|
343 |
@@ -138,7 +138,7 @@ class Actions(object): |
344 |
messages = ["Too many seeds found to move"] |
345 |
messages.extend(sourcekeys) |
346 |
return messages |
347 |
- messages.append("Failed to Move seed:") |
348 |
+ messages.append("Failed to move seed:") |
349 |
messages.append(searchkey) |
350 |
messages.append('\n') |
351 |
messages.append("Source seeds found...") |
352 |
|
353 |
diff --git a/gkeys/config.py b/gkeys/config.py |
354 |
index 1ff5a18..b20d5cd 100644 |
355 |
--- a/gkeys/config.py |
356 |
+++ b/gkeys/config.py |
357 |
@@ -120,7 +120,7 @@ KEYDIR = 4 |
358 |
FINGERPRINT = 5 |
359 |
|
360 |
# set some defaults |
361 |
-KEY_LEN = { |
362 |
+KEYLEN_MAP = { |
363 |
'keyid': 8, |
364 |
'longkeyid': 16, |
365 |
} |
366 |
|
367 |
diff --git a/gkeys/seed.py b/gkeys/seed.py |
368 |
index 1dd8fc4..d5a92e3 100644 |
369 |
--- a/gkeys/seed.py |
370 |
+++ b/gkeys/seed.py |
371 |
@@ -92,7 +92,6 @@ class Seeds(object): |
372 |
return False |
373 |
|
374 |
|
375 |
- |
376 |
def delete(self, gkey=None, index=None): |
377 |
'''Delete the key from the seeds in memory |
378 |
|
379 |
|
380 |
diff --git a/gkeys/seedhandler.py b/gkeys/seedhandler.py |
381 |
index 2506277..7f73857 100644 |
382 |
--- a/gkeys/seedhandler.py |
383 |
+++ b/gkeys/seedhandler.py |
384 |
@@ -13,7 +13,7 @@ |
385 |
import re |
386 |
|
387 |
from gkeys.config import (GKEY, NICK, NAME, KEYID, LONGKEYID, FINGERPRINT, |
388 |
- KEY_LEN) |
389 |
+ KEYLEN_MAP) |
390 |
|
391 |
|
392 |
class SeedHandler(object): |
393 |
@@ -40,11 +40,11 @@ class SeedHandler(object): |
394 |
@staticmethod |
395 |
def build_gkeydict(args): |
396 |
keyinfo = {} |
397 |
- for x in GKEY._fields: |
398 |
+ for attr in GKEY._fields: |
399 |
try: |
400 |
- value = getattr(args, x) |
401 |
+ value = getattr(args, attr) |
402 |
if value: |
403 |
- keyinfo[x] = value |
404 |
+ keyinfo[attr] = value |
405 |
except AttributeError: |
406 |
pass |
407 |
return keyinfo |
408 |
@@ -56,20 +56,20 @@ class SeedHandler(object): |
409 |
# assume it's good until an error is found |
410 |
is_good = True |
411 |
#self.logger.debug("SeedHandler: build_gkeylist; args = %s" % str(args)) |
412 |
- for x in GKEY._fields: |
413 |
- if GKEY.field_types[x] is str: |
414 |
+ for attr in GKEY._fields: |
415 |
+ if GKEY.field_types[attr] is str: |
416 |
try: |
417 |
- value = getattr(args, x) |
418 |
+ value = getattr(args, attr) |
419 |
except AttributeError: |
420 |
value = None |
421 |
- elif GKEY.field_types[x] is list: |
422 |
+ elif GKEY.field_types[attr] is list: |
423 |
try: |
424 |
- values = [y for y in getattr(args, x).split(':')] |
425 |
+ values = [y for y in getattr(args, attr).split(':')] |
426 |
value = [v.replace(' ', '') for v in values] |
427 |
except AttributeError: |
428 |
value = None |
429 |
keyinfo.append(value) |
430 |
- if x in ["keyid", "longkeyid"] and value: |
431 |
+ if attr in ["keyid", "longkeyid"] and value: |
432 |
keyid_found = True |
433 |
if not keyid_found and needkeyid: |
434 |
fingerprint = keyinfo[FINGERPRINT] |
435 |
@@ -78,7 +78,7 @@ class SeedHandler(object): |
436 |
'fingerprint in args') |
437 |
# assign it to gpgkey to prevent a possible |
438 |
# "gpgkey" undefined error |
439 |
- gpgkey = ['0x' + x[-KEY_LEN['longkeyid']:] for x in fingerprint] |
440 |
+ gpgkey = ['0x' + x[-KEYLEN_MAP['longkeyid']:] for x in fingerprint] |
441 |
keyinfo[LONGKEYID] = gpgkey |
442 |
self.logger.debug(' Generate gpgkey longkeyid, NEW ' |
443 |
'keyinfo[LONGKEYID] = %s' % str(keyinfo[LONGKEYID])) |
444 |
@@ -139,19 +139,19 @@ class SeedHandler(object): |
445 |
def _check_fingerprint_integrity(self, keyinfo): |
446 |
# assume it's good until an error is found |
447 |
is_good = True |
448 |
- for x in keyinfo[FINGERPRINT]: |
449 |
+ for fingerprint in keyinfo[FINGERPRINT]: |
450 |
# check fingerprint integrity |
451 |
- if len(x) != 40: |
452 |
+ if len(fingerprint) != 40: |
453 |
self.logger.error('ERROR in keyinfo for: %s, %s' |
454 |
%(keyinfo[NICK], keyinfo[NAME])) |
455 |
self.logger.error(' GPGKey incorrect fingerprint ' + |
456 |
'length (%s) for fingerprint: %s' %(len(x), x)) |
457 |
is_good = False |
458 |
continue |
459 |
- if not self.fingerprint_re.match(x): |
460 |
+ if not self.fingerprint_re.match(fingerprint): |
461 |
self.logger.error('ERROR in keyinfo info for: %s, %s' |
462 |
%(keyinfo[NICK], keyinfo[NAME])) |
463 |
self.logger.error(' GPGKey: Non hexadecimal digits in ' + |
464 |
- 'fingerprint for fingerprint: ' + x) |
465 |
+ 'fingerprint for fingerprint: ' + fingerprint) |
466 |
is_good = False |
467 |
return is_good |