5 Gentoo-keys - config.py
7 Holds configuration keys and values
9 @copyright: 2012 by Brian Dolbec <dol-sen@gentoo.org>
10 @license: GNU GNU GPL2, see COPYING for details.
15 from collections import namedtuple
18 from pygpg.config import GPGConfig
21 from gkeys.utils import path
26 # establish the eprefix, initially set so eprefixify can
28 EPREFIX = "@GENTOO_PORTAGE_EPREFIX@"
30 # check and set it if it wasn't
31 if "GENTOO_PORTAGE_EPREFIX" in EPREFIX:
36 class GKeysConfig(GPGConfig):
37 """ Configuration superclass which holds our gentoo-keys
38 config settings for pygpg """
40 def __init__ (self, config=None, root=None, read_configfile=False):
41 """ Class initialiser """
42 GPGConfig.__init__(self)
44 self.root = root or ''
46 self.defaults['config'] = config
47 self.defaults['configdir'] = os.path.dirname(config)
49 self.defaults['configdir'] = path([self.root, EPREFIX, '/etc/gentoo-keys'])
50 self.defaults['config'] = '%(configdir)s/gkeys.conf'
51 self.configparser = None
56 def _add_gkey_defaults(self):
57 self.defaults['keysdir'] = path([self.root, EPREFIX, '/var/gentoo/gkeys'])
58 self.defaults['dev-keydir'] = '%(keysdir)s/devs'
59 self.defaults['release-keydir'] = '%(keysdir)s/release'
60 self.defaults['overlays-keydir'] = '%(keysdir)s/overlays'
61 self.defaults['known-keysfile'] = '%(keysdir)s/knownkeys'
62 self.defaults['release-seedfile'] = '%(configdir)s/release.seeds'
63 self.defaults['dev-seedfile'] = '%(configdir)s/developer.seeds'
67 def read_config(self):
68 '''Reads the config file into memory
70 if "%(configdir)s" in self.defaults['config']:
72 self.defaults['config'] = self.defaults['config'] \
73 % {'configdir': self.defaults['configdir']}
74 defaults = self.get_defaults()
75 # remove some defaults from being entered into the configparser
76 for key in ['gpg_defaults', 'only_usable', 'refetch', 'tasks']:
78 self.configparser = ConfigParser.ConfigParser(defaults)
79 self.configparser.add_section('MAIN')
80 self.configparser.read(defaults['config'])
83 def get_key(self, key, subkey=None):
84 return self._get_(key, subkey)
87 def _get_(self, key, subkey=None):
88 if self.configparser and self.configparser.has_option('MAIN', key):
90 logger.debug("Found %s in configparser... %s"
91 % (key, str(self.configparser.get('MAIN', key))))
92 #logger.debug("type(key)= %s"
93 # % str(type(self.configparser.get('MAIN', key))))
94 return self.configparser.get('MAIN', key)
96 return super(GKeysConfig, self)._get_(key, subkey)
99 class GKEY(namedtuple('GKEY', ['nick', 'name', 'keyid', 'longkeyid',
100 'keyring', 'fingerprint'])):
101 '''Class to hold the relavent info about a key'''
103 field_types = {'nick': str, 'name': str, 'keyid': list,
104 'longkeyid': list, 'keyring': str, 'fingerprint': list}
105 field_separator = "|"
109 def _packed_values(self):
110 '''Returns a list of the field values'''
112 for f in self._fields:
113 v.append(self._pack(f))
117 def packed_string(self):
118 '''Returns a separator joined string of the field values'''
119 return self.field_separator.join([x for x in self._packed_values()])
121 def _unpack_string(self, packed_data):
122 '''Returns a list of the separator joined string of the field values'''
124 data = packed_data.split(self.field_separator)
125 for x in self._fields:
126 values.append(self._unpack(x, data.pop(0)))
129 def _pack(self, field):
130 '''pack field data into a string'''
131 if self.field_types[field] == str:
132 return getattr(self, field)
133 elif self.field_types[field] == list:
134 info = getattr(self, field)
136 return self.list_separator.join(info)
138 # force an empty list to None
141 raise "ERROR packing %s" %str(getattr(self, field))
143 def _unpack(self, field, data):
144 '''unpack field data to the desired type'''
145 if self.field_types[field] == str:
151 # make it an empty list
154 result = data.split(self.list_separator)
157 def make_packed(self, packed_string):
158 '''Creates a new instance of Gkey from the packed
161 @param packed_string: string of data separated by field_separator
162 @return new GKEY instance containing the data
164 return GKEY._make(self._unpack_string(packed_string))