Merge pull request #1 from gentoo/week1_small_improvements
authorPavlos Ratis <dastergon@gentoo.org>
Tue, 20 May 2014 12:33:29 +0000 (15:33 +0300)
committerPavlos Ratis <dastergon@gentoo.org>
Tue, 20 May 2014 12:33:29 +0000 (15:33 +0300)
GSoC 2014 Week1: small improvements

.gitignore
etc/gkeys.conf.sample [moved from etc/gkeys.conf with 100% similarity]
gkeyldap/actions.py
gkeyldap/search.py
gkeys/actions.py
gkeys/config.py
gkeys/seed.py
gkeys/seedhandler.py
testpath

index 539da7411f1d5a03e01b75c76fb3a76d16fbdc2f..14b5d1da7cd53494caff06050e88fe1dea267545 100644 (file)
@@ -1 +1,2 @@
+*.conf
 *.py[co]
similarity index 100%
rename from etc/gkeys.conf
rename to etc/gkeys.conf.sample
index 5b336000984118fc446bfea872115b3b8c86ce09..55c3e13d5471457393feea38b3b718a22bca9b0c 100644 (file)
@@ -13,7 +13,7 @@
 import os
 import re
 
-from gkeys.config import GKEY, KEYID, LONGKEYID, FINGERPRINT, KEY_LEN
+from gkeys.config import GKEY, KEYID, LONGKEYID, FINGERPRINT, KEYLEN_MAP
 from gkeys.seed import Seeds
 from gkeyldap.search import (LdapSearch, UID, gkey2ldap_map, gkey2SEARCH)
 
@@ -21,22 +21,21 @@ from gkeyldap.search import (LdapSearch, UID, gkey2ldap_map, gkey2SEARCH)
 Available_Actions = ['ldapsearch', 'updateseeds']
 
 
-def get_key_ids(key, info):
+def get_key_ids(key_len, keyids):
     '''Small utility function to return only keyid (short)
     or longkeyid's
 
-    @param key: string, the key length desired
-    @param info: list of keysid's to process
+    @param key_len: string, the key length desired
+    @param keyids: list of keysid's to process
     @return list of the desired key length id's
     '''
     result = []
-    for x in info:
-        if x.startswith('0x'):
-            mylen = KEY_LEN[key] + 2
-        else:
-            mylen = KEY_LEN[key]
-        if len(x) == mylen:
-            result.append(x)
+    for keyid in keyids:
+        target_len = KEYLEN_MAP[key_len]
+        if keyid.startswith('0x'):
+            target_len = KEYLEN_MAP[key_len] + 2
+        if len(keyid) == target_len:
+            result.append(keyid)
     return result
 
 
@@ -53,11 +52,11 @@ class Actions(object):
 
     def ldapsearch(self, args):
         l = LdapSearch()
-        self.logger.info("Search...establishing connection")
-        self.output("Search...establishing connection")
+        self.logger.info("Search... Establishing connection")
+        self.output("Search... Establishing connection")
         if not l.connect():
-            self.logger.info("Aborting Search...Connection failed")
-            self.output("Aborting Search...Connection failed")
+            self.logger.info("Aborting search... Connection failed")
+            self.output("Aborting search... Connection failed")
             return False
         self.logger.debug("MAIN: _action_ldapsearch; args = %s" % str(args))
         x, target, search_field = self.get_args(args)
@@ -66,26 +65,26 @@ class Actions(object):
         for dev in sorted(devs):
             self.output(dev, devs[dev])
         self.output("============================================")
-        self.output("Total number of devs in results:", len(devs))
+        self.output("Total number of developers in results:", len(devs))
         self.logger.info("============================================")
-        self.logger.info("Total number of devs in results: %d" % len(devs))
+        self.logger.info("Total number of developers in results: %d" % len(devs))
         return True
 
 
     def updateseeds(self, args):
-        self.logger.info("Beginning ldap search...")
-        self.output("Beginning ldap search...")
+        self.logger.info("Beginning LDAP search...")
+        self.output("Beginning LDAP search...")
         l = LdapSearch()
         if not l.connect():
-            self.output("Aborting Update...Connection failed")
-            self.logger.info("Aborting Update...Connection failed")
+            self.output("Aborting update... Connection failed")
+            self.logger.info("Aborting update... Connection failed")
             return False
         results = l.search('*', UID)
         info = l.result2dict(results, 'uid')
         self.logger.debug(
             "MAIN: _action_updateseeds; got results :) converted to info")
         if not self.create_seedfile(info):
-            self.logger.error("Dev seed file update failure: "
+            self.logger.error("Developer seed file update failure: "
                 "Original seed file is intact & untouched.")
         filename = self.config['dev-seedfile']
         old = filename + '.old'
@@ -108,12 +107,12 @@ class Actions(object):
             os.rename(filename + '.new', filename)
         except IOError:
             raise
-        self.output("Developer Seed file updated")
+        self.output("Developer seed file updated!")
         return True
 
 
     def create_seedfile(self, devs):
-        self.output("Creating seeds from ldap data...")
+        self.output("Creating seeds from LDAP data...")
         filename = self.config['dev-seedfile'] + '.new'
         self.seeds = Seeds(filename)
         count = 0
@@ -131,49 +130,22 @@ class Actions(object):
             else:
                 error_count += 1
         self.output("Total number of seeds created:", count)
-        self.output("Seeds created...saving file: %s" % filename)
-        self.output("Total number of Dev's with gpg errors:", error_count)
+        self.output("Seeds created... Saving file: %s" % filename)
+        self.output("Total number of Dev's with GPG errors:", error_count)
         self.logger.info("Total number of seeds created: %d" % count)
-        self.logger.info("Seeds created...saving file: %s" % filename)
-        self.logger.info("Total number of Dev's with gpg errors: %d" % error_count)
+        self.logger.info("Seeds created... Saving file: %s" % filename)
+        self.logger.info("Total number of Dev's with GPG errors: %d" % error_count)
         return self.seeds.save()
 
 
     @staticmethod
     def get_args(args):
-        for x in ['nick', 'name', 'gpgkey', 'fingerprint', 'status']:
-            if x:
-                target = getattr(args, x)
-                search_field = gkey2SEARCH[x]
+        for attr in ['nick', 'name', 'gpgkey', 'fingerprint', 'status']:
+            if attr:
+                target = getattr(args, attr)
+                search_field = gkey2SEARCH[attr]
                 break
-        return (x, target, search_field)
-
-
-
-    def build_gkeydict(self, info):
-        keyinfo = {}
-        for x in GKEY._fields:
-            field = gkey2ldap_map[x]
-            if not field:
-                continue
-            try:
-                # strip errant line feeds
-                values = [y.strip('\n') for y in info[field]]
-                if values and values in ['uid', 'cn' ]:
-                    value = values[0]
-                # separate out short/long key id's
-                elif values and x in ['keyid', 'longkeyid']:
-                    value = get_key_ids(x, values)
-                else:
-                    value = values
-                if 'undefined' in values:
-                    self.logger.error('%s = "undefined" for %s, %s'
-                        %(field, info['uid'][0], info['cn'][0]))
-                if value:
-                    keyinfo[x] = value
-            except KeyError:
-                pass
-        return keyinfo
+        return (attr, target, search_field)
 
 
     def build_gkeylist(self, info):
@@ -183,8 +155,8 @@ class Actions(object):
         # assume it's good until an error is found
         is_good = True
         #self.logger.debug("Actions: build_gkeylist; info = %s" % str(info))
-        for x in GKEY._fields:
-            field = gkey2ldap_map[x]
+        for attr in GKEY._fields:
+            field = gkey2ldap_map[attr]
             if not field:
                 keyinfo.append(None)
                 continue
@@ -194,24 +166,24 @@ class Actions(object):
                 if values and field in ['uid', 'cn' ]:
                     value = values[0]
                 # separate out short/long key id's
-                elif values and x in ['keyid', 'longkeyid']:
-                    value = get_key_ids(x, values)
+                elif values and attr in ['keyid', 'longkeyid']:
+                    value = get_key_ids(attr, values)
                     if len(value):
                         keyid_found = True
-                elif values and x in ['fingerprint']:
+                elif values and attr in ['fingerprint']:
                     value = [v.replace(' ', '') for v in values]
                 else:
                     value = values
                 if 'undefined' in values:
-                    self.logger.error('ERROR in ldap info for: %s, %s'
+                    self.logger.error('ERROR in LDAP info for: %s, %s'
                         %(info['uid'][0],info['cn'][0]))
                     self.logger.error('  %s = "undefined"' %(field))
                     is_good = False
                 keyinfo.append(value)
             except KeyError:
-                self.logger.debug('Ldap info for: %s, %s'
+                self.logger.debug('LDAP info for: %s, %s'
                     %(info['uid'][0],info['cn'][0]))
-                self.logger.debug('  MISSING or EMPTY ldap field ' +
+                self.logger.debug('  MISSING or EMPTY LDAP field ' +
                     '[%s] GPGKey field [%s]' %(field, x))
                 if x in ['keyid', 'longkeyid']:
                     keyid_missing = True
@@ -222,21 +194,21 @@ class Actions(object):
             fingerprint = None
             try:
                 fingerprint = info[gkey2ldap_map['fingerprint']]
-                self.logger.debug('  Generate gpgkey, Found ldap fingerprint field')
+                self.logger.debug('  Generate gpgkey, Found LDAP fingerprint field')
             except KeyError:
-                gpgkey = 'Missing fingerprint from ldap info'
-                self.logger.debug('  Generate gpgkey, ldap fingerprint KeyError')
+                gpgkey = 'Missing fingerprint from LDAP info'
+                self.logger.debug('  Generate gpgkey, LDAP fingerprint KeyError')
             if fingerprint:
                 values = [y.strip('\n') for y in fingerprint]
                 value = [v.replace(' ', '') for v in values]
                 # assign it to gpgkey to prevent a possible
                 # "gpgkey" undefined error
-                gpgkey = ['0x' + x[-KEY_LEN['longkeyid']:] for x in value]
+                gpgkey = ['0x' + x[-KEYLEN_MAP['longkeyid']:] for x in value]
                 keyinfo[LONGKEYID] = gpgkey
                 self.logger.debug('  Generate gpgkey, NEW keyinfo[LONGKEYID] = %s'
                     % str(keyinfo[LONGKEYID]))
             else:
-                gpgkey = 'Missing or Bad fingerprint from ldap info'
+                gpgkey = 'Missing or Bad fingerprint from LDAP info'
                 is_good = False
             if not keyinfo[LONGKEYID]:
                 self.logger.error('ERROR in ldap info for: %s, %s'
@@ -266,7 +238,7 @@ class Actions(object):
                 index = len(y.lstrip('0x'))
                 if y.lstrip('0x').upper() not in \
                         [x[-index:].upper() for x in keyinfo[FINGERPRINT]]:
-                    self.logger.error('ERROR in ldap info for: %s, %s'
+                    self.logger.error('ERROR in LDAP info for: %s, %s'
                         %(info['uid'][0],info['cn'][0]))
                     self.logger.error('  ' + str(keyinfo))
                     self.logger.error('  GPGKey id %s not found in the '
@@ -278,19 +250,19 @@ class Actions(object):
     def _check_fingerprint_integrity(self, info, keyinfo):
         # assume it's good until found an error is found
         is_good = True
-        for x in keyinfo[FINGERPRINT]:
+        for fingerprint in keyinfo[FINGERPRINT]:
             # check fingerprint integrity
-            if len(x) != 40:
-                self.logger.error('ERROR in ldap info for: %s, %s'
+            if len(fingerprint) != 40:
+                self.logger.error('ERROR in LDAP info for: %s, %s'
                     %(info['uid'][0],info['cn'][0]))
                 self.logger.error('  GPGKey incorrect fingerprint ' +
-                    'length (%s) for fingerprint: %s' %(len(x), x))
+                    'length (%s) for fingerprint: %s' %(len(fingerprint), fingerprint))
                 is_good = False
                 continue
-            if not self.fingerprint_re.match(x):
+            if not self.fingerprint_re.match(fingerprint):
                 self.logger.error('ERROR in ldap info for: %s, %s'
                     %(info['uid'][0],info['cn'][0]))
                 self.logger.error('  GPGKey: Non hexadecimal digits in ' +
-                    'fingerprint for fingerprint: ' + x)
+                    'fingerprint for fingerprint: ' + fingerprint)
                 is_good = False
         return is_good
index 2cbc4b95743bffb9cee631ada144f87e43bbb032..087bee6d90ea03a8261da3650e069d3be4e58efd 100644 (file)
@@ -60,7 +60,7 @@ gkey2SEARCH = {
 
 
 class LdapSearch(object):
-    '''Class to perform searches on the configured ldap server
+    '''Class to perform searches on the configured LDAP server
     '''
 
     def __init__(self, server=None, fields=None, criteria=None):
@@ -74,9 +74,9 @@ class LdapSearch(object):
 
 
     def connect(self, server=None,):
-        '''Creates our ldap server connection
-
+        '''Creates our LDAP server connection
         '''
+
         if server:
             self.server = server
             logger.debug('LdapSearch: connect; new server: %s' % self.server)
@@ -93,9 +93,8 @@ class LdapSearch(object):
         return True
 
 
-
     def search(self, target, search_field=UID, fields=None, criteria=None):
-        '''Perform the ldap search
+        '''Perform the LDAP search
         '''
         if not target:
             logger.debug('LdapSearch: search; invalid target: "%s"' % target)
index 85c0e0ebad1aa40f98d77d60fccb3bb681c331de..79690fc73e57bdae7d89c6926b7d023ccae452ed 100644 (file)
@@ -76,7 +76,7 @@ class Actions(object):
             success = self.seeds.add(gkey)
             if success:
                 success = self.seeds.save()
-                return ["Successfully Added new seed: %s" % str(success), gkey]
+                return ["Successfully added new seed: %s" % str(success), gkey]
         else:
             messages = ["Matching seeds found in seeds file",
                 "Aborting... \nMatching seeds:"]
@@ -91,20 +91,20 @@ class Actions(object):
         self.logger.debug("ACTIONS: removeseed; gkey: %s" % str(searchkey))
         gkeys = self.listseed(args)
         if not gkeys:
-            return ["Failed to Removed seed: No gkeys returned from listseed()",
+            return ["Failed to remove seed: No gkeys returned from listseed()",
                 None]
         if len(gkeys) == 1:
             self.logger.debug("ACTIONS: removeseed; now deleting gkey: %s" % str(gkeys[0]))
             success = self.seeds.delete(gkeys[0])
             if success:
                 success = self.seeds.save()
-            return ["Successfully Removed seed: %s" % str(success),
+            return ["Successfully removed seed: %s" % str(success),
                 gkeys[0]]
         elif len(gkeys):
             messages = ["Too many seeds found to remove"]
             messages.extend(gkeys)
             return messages
-        return ["Failed to Remove seed:", searchkey,
+        return ["Failed to remove seed:", searchkey,
             "No matching seed found"]
 
 
@@ -138,7 +138,7 @@ class Actions(object):
             messages = ["Too many seeds found to move"]
             messages.extend(sourcekeys)
             return messages
-        messages.append("Failed to Move seed:")
+        messages.append("Failed to move seed:")
         messages.append(searchkey)
         messages.append('\n')
         messages.append("Source seeds found...")
index 3edc463c5d7eac3021a699a362e84028d1490d09..b20d5cdc0321f0ac6af330a42325096077a01563 100644 (file)
@@ -120,7 +120,7 @@ KEYDIR = 4
 FINGERPRINT = 5
 
 # set some defaults
-KEY_LEN = {
+KEYLEN_MAP = {
     'keyid': 8,
     'longkeyid': 16,
 }
@@ -146,7 +146,7 @@ class GKEY(namedtuple('GKEY', ['nick', 'name', 'keyid', 'longkeyid',
     @property
     def packed_string(self):
         '''Returns a separator joined string of the field values'''
-        return self.field_separator.join([x for x in self._packed_values()])
+        return self.field_separator.join([str(x) for x in self._packed_values()])
 
     def _unpack_string(self, packed_data):
         '''Returns a list of the separator joined string of the field values'''
index 1dd8fc4fc5e84c600a9d5511dea5e62d1c2384f6..d5a92e3bd3b5b465eb140bd6643b3fd20da2d4eb 100644 (file)
@@ -92,7 +92,6 @@ class Seeds(object):
         return False
 
 
-
     def delete(self, gkey=None, index=None):
         '''Delete the key from the seeds in memory
 
index 25062771a34befce321692aa493de95583093f02..7f738576e28f79bab4ab2b353b3a12b6345d6550 100644 (file)
@@ -13,7 +13,7 @@
 import re
 
 from gkeys.config import (GKEY, NICK, NAME, KEYID, LONGKEYID, FINGERPRINT,
-    KEY_LEN)
+    KEYLEN_MAP)
 
 
 class SeedHandler(object):
@@ -40,11 +40,11 @@ class SeedHandler(object):
     @staticmethod
     def build_gkeydict(args):
         keyinfo = {}
-        for x in GKEY._fields:
+        for attr in GKEY._fields:
             try:
-                value = getattr(args, x)
+                value = getattr(args, attr)
                 if value:
-                    keyinfo[x] = value
+                    keyinfo[attr] = value
             except AttributeError:
                 pass
         return keyinfo
@@ -56,20 +56,20 @@ class SeedHandler(object):
         # assume it's good until an error is found
         is_good = True
         #self.logger.debug("SeedHandler: build_gkeylist; args = %s" % str(args))
-        for x in GKEY._fields:
-            if GKEY.field_types[x] is str:
+        for attr in GKEY._fields:
+            if GKEY.field_types[attr] is str:
                 try:
-                    value = getattr(args, x)
+                    value = getattr(args, attr)
                 except AttributeError:
                     value = None
-            elif GKEY.field_types[x] is list:
+            elif GKEY.field_types[attr] is list:
                 try:
-                    values = [y for y in getattr(args, x).split(':')]
+                    values = [y for y in getattr(args, attr).split(':')]
                     value = [v.replace(' ', '') for v in values]
                 except AttributeError:
                     value = None
             keyinfo.append(value)
-            if x in ["keyid", "longkeyid"] and value:
+            if attr in ["keyid", "longkeyid"] and value:
                 keyid_found = True
         if not keyid_found and needkeyid:
             fingerprint = keyinfo[FINGERPRINT]
@@ -78,7 +78,7 @@ class SeedHandler(object):
                     'fingerprint in args')
                 # assign it to gpgkey to prevent a possible
                 # "gpgkey" undefined error
-                gpgkey = ['0x' + x[-KEY_LEN['longkeyid']:] for x in fingerprint]
+                gpgkey = ['0x' + x[-KEYLEN_MAP['longkeyid']:] for x in fingerprint]
                 keyinfo[LONGKEYID] = gpgkey
                 self.logger.debug('  Generate gpgkey longkeyid, NEW '
                     'keyinfo[LONGKEYID] = %s' % str(keyinfo[LONGKEYID]))
@@ -139,19 +139,19 @@ class SeedHandler(object):
     def _check_fingerprint_integrity(self, keyinfo):
         # assume it's good until an error is found
         is_good = True
-        for x in keyinfo[FINGERPRINT]:
+        for fingerprint in keyinfo[FINGERPRINT]:
             # check fingerprint integrity
-            if len(x) != 40:
+            if len(fingerprint) != 40:
                 self.logger.error('ERROR in keyinfo for: %s, %s'
                     %(keyinfo[NICK], keyinfo[NAME]))
                 self.logger.error('  GPGKey incorrect fingerprint ' +
                     'length (%s) for fingerprint: %s' %(len(x), x))
                 is_good = False
                 continue
-            if not self.fingerprint_re.match(x):
+            if not self.fingerprint_re.match(fingerprint):
                 self.logger.error('ERROR in keyinfo info for: %s, %s'
                     %(keyinfo[NICK], keyinfo[NAME]))
                 self.logger.error('  GPGKey: Non hexadecimal digits in ' +
-                    'fingerprint for fingerprint: ' + x)
+                    'fingerprint for fingerprint: ' + fingerprint)
                 is_good = False
         return is_good
index b1bc173298b2c2010c5b8959e8d073cae3dab16c..3bf8bde1eaf76bd4c9b6efb781707b571762eee3 100644 (file)
--- a/testpath
+++ b/testpath
 # $ source ./testpath
 # $ esearch some-package
 
-
-export PATH="$(dirname $BASH_SOURCE[0])/../pyGPG/bin:$(dirname $BASH_SOURCE[0])/bin:${PATH}"
-
-export PYTHONPATH="$(dirname $BASH_SOURCE[0])/../pyGPG/:$(dirname $BASH_SOURCE[0])/../ssl-fetch/:$(dirname $BASH_SOURCE[0])/:${PYTHONPATH}"
+if [[ "$SHELL" =~ "zsh" ]]; then
+    export PATH="$(dirname $0)/../pyGPG/bin:$(dirname $0)/bin:${PATH}"
+    export PYTHONPATH="$(dirname $0)/../pyGPG/:$(dirname $0)/../ssl-fetch/:$(dirname $0)/:${PYTHONPATH}"
+else
+    export PATH="$(dirname $BASH_SOURCE[0])/../pyGPG/bin:$(dirname $BASH_SOURCE[0])/bin:${PATH}"
+    export PYTHONPATH="$(dirname $BASH_SOURCE[0])/../pyGPG/:$(dirname $BASH_SOURCE[0])/../ssl-fetch/:$(dirname $BASH_SOURCE[0])/:${PYTHONPATH}"
+fi