From 77ab04fd8aade18fe385d83963016ef5cb12cbd3 Mon Sep 17 00:00:00 2001 From: Brian Dolbec Date: Sun, 9 Dec 2012 07:16:01 -0800 Subject: [PATCH] New file seed.py, class Seeds for all seed file handling, config fixes and extend GKEY capability. --- gkeys/config.py | 20 ++++++- gkeys/seed.py | 154 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 gkeys/seed.py diff --git a/gkeys/config.py b/gkeys/config.py index 1f6610c..a15ec55 100644 --- a/gkeys/config.py +++ b/gkeys/config.py @@ -42,7 +42,7 @@ class GKeysConfig(GPGConfig): if config: self.defaults['config'] = config self.defaults['configdir'] = os.path.dirname(config) - else + else: self.defaults['configdir'] = path([self.root, EPREFIX, '/etc/gentoo-keys']) self.defaults['config'] = '%(configdir)s/gkeys.conf' self.configparser = None @@ -54,8 +54,10 @@ class GKeysConfig(GPGConfig): def _add_gkey_defaults(self): self.defaults['keysdir'] = path([self.root, EPREFIX, '/var/gentoo/gkeys']) self.defaults['devkeydir'] = '%(keysdir)s/devs' - self.defaults['releasekeydir'] = '%(keysdir)s/release') + self.defaults['releasekeydir'] = '%(keysdir)s/release' self.defaults['knownkeysfile'] = '%(keysdir)s/knownkeys' + self.fedualts['releaseseedfile'] = '%(configdir)s/release.seeds' + self.fedualts['devseedfile'] = '%(configdir)s/developer.seeds' @@ -79,7 +81,19 @@ class GKeysConfig(GPGConfig): class GKEY(namedtuple('GKEY', ['name', 'keyid', 'longkeyid', - 'fingerprint', 'keyring']): + 'fingerprint', 'keyring'])): '''Class to hold the relavent info about a key''' + __slots__ = () + + def values(self): + '''Returns a list of the field values''' + v = [] + for f in self._fields: + v.append(getattr(self, f)) + return v + + def value_string(self): + '''Returns a space separated string of the field values''' + return ' '.join([str(x) for x in self.values()]) diff --git a/gkeys/seed.py b/gkeys/seed.py new file mode 100644 index 0000000..f473293 --- /dev/null +++ b/gkeys/seed.py @@ -0,0 +1,154 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- + +'''Gentoo-keys - seed.py +This is gentoo-keys superclass which wraps the pyGPG lib +with gentoo-keys specific convienience functions. + + Distributed under the terms of the GNU General Public License v2 + + Copyright: + (c) 2011 Brian Dolbec + Distributed under the terms of the GNU General Public License v2 + + Author(s): + Brian Dolbec + +''' + +from gkeys.log import logger +from gkeys.config import GKEY + + +class Seeds(object): + '''Handles all seed key file operations''' + + def __init__(self, filepath=None): + '''Seeds class init function + + @param filepath: string of the file to load + ''' + self.filename = filepath + self.seeds = [] + + + def load(self, filename=None): + '''Load the seed file into memory''' + if filename: + self.filename = filename + if not self.filename: + logger.debug("Seed.load() Not a valid filename: '%s'" % str(self.filename)) + return False + logger.debug("Begin loading seed file %s" % self.filename) + seedlines = None + self.seeds = [] + try: + with open(self.filename) as seedfile: + seedlines = seedfile.readlines() + except IOError as err: + self._error(err) + return False + + for seed in seedlines: + try: + parts = self._split_seed(seed) + self.seeds.append(GKEY._make(parts)) + except Exception as err: + self._error(err) + logger.debug("Completed loading seed file %s" % self.filename) + return True + + + def save(self, filename=None): + '''Save the seeds to the file''' + if filename: + self.filename = filename + if not self.filename: + logger.debug("Seed.load() Not a valid filename: '%s'" % str(self.filename)) + return False + logger.debug("Begin loading seed file %s" % self.filename) + try: + with open(self.filename, 'w') as seedfile: + seedlines = [x.value_string() for x in self.seeds] + seedfile.write('\n'.join(seedlines)) + seedfile.write("\n") + except IOError as err: + self._error(err) + return + + + def add(self, gkey): + '''Add a new seed key to memory''' + if isinstance(gkey, GKEY): + self.seeds.append(gkey) + return True + return False + + + + def delete(self, gkey=None, index=None): + '''Delete the key from the seeds in memory + + @param gkey: GKEY, the matching GKEY to delete + @param index: int, ''' + if gkey: + try: + self.seeds.remove(gkey) + except ValueError: + return False + return True + elif index: + self.seeds.pop(index) + return True + + + def list(self, **kwargs): + '''List the key or keys matching the kwargs argument or all + + @param kwargs: dict of GKEY._fields and values + @returns list + ''' + if not kwargs: + return self.seeds + # discard any invalid keys + keys = set(list(kwargs)).intersection(GKEY._fields) + result = self.seeds[:] + for key in keys: + result = [x for x in result if getattr(x , key) == kwargs[key]] + return result + + + def search(self, pattern): + '''Search for the keys matching the regular expression pattern''' + pass + + + def index(self, gkey): + '''The index of the gkey in the seeds list + + @param gkey: GKEY, the matching GKEY to delete + @return int + ''' + try: + index = self.seeds.index(gkey) + except ValueError: + return None + return index + + + def _error(self, err): + '''Class error logging function''' + logger.error("Error processing seed file %s" % self.filename) + logger.error("Error was: %s" % str(err)) + + + @staticmethod + def _split_seed(seed): + '''Splits the seed string and + replaces all occurances of 'None' with the python type None''' + iterable = seed.split() + for i in range(len(iterable)): + if iterable[i] == 'None': + iterable[i] = None + return iterable + -- 2.26.2