00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 import ConfigParser
00020 import logging
00021 import os
00022
00023 _SECTION = 'MAIN'
00024
00025
00026 def _get_option_name(hw_uuid, host):
00027 return '%s__%s' % (hw_uuid, host)
00028
00029
00030 class _UuidDb:
00031 def __init__(self, database_filename):
00032 self._database_filename = database_filename
00033 self._config = ConfigParser.RawConfigParser()
00034 self._config.read(self._database_filename)
00035 if not self._config.has_section(_SECTION):
00036 self._config.add_section(_SECTION)
00037
00038 def _flush(self):
00039 try:
00040 smolt_user_config_dir = os.path.expanduser('~/.smolt/')
00041 if not os.path.exists(smolt_user_config_dir):
00042 os.mkdir(smolt_user_config_dir, 0700)
00043 f = open(self._database_filename, 'w')
00044 self._config.write(f)
00045 f.close()
00046 except:
00047 logging.error('Flushing UUID database failed')
00048
00049 def get_pub_uuid(self, hw_uuid, host):
00050 try:
00051 pub_uuid = self._config.get(_SECTION, _get_option_name(hw_uuid, host))
00052 logging.info('Public UUID "%s" read from database' % pub_uuid)
00053 return pub_uuid
00054 except ConfigParser.NoOptionError:
00055 return None
00056
00057 def set_pub_uuid(self, hw_uuid, host, pub_uuid):
00058 for i in (hw_uuid, host, pub_uuid):
00059 if not i:
00060 raise Exception('No paramater allowed to be None.')
00061 self._config.set(_SECTION, _get_option_name(hw_uuid, host), pub_uuid)
00062 logging.info('Public UUID "%s" written to database' % pub_uuid)
00063 self._flush()
00064
00065
00066 def create_default_uuiddb():
00067 from smolt_config import get_config_attr
00068 _database_filename = get_config_attr("UUID_DB", os.path.expanduser('~/.smolt/uuiddb.cfg'))
00069 return _UuidDb(_database_filename)