MythTV master
uuiddb.py
Go to the documentation of this file.
1# smolt - Fedora hardware profiler
2#
3# Copyright (C) 2009 Sebastian Pipping <sebastian@pipping.org>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18
19from builtins import map
20from builtins import str
21from builtins import object
22import configparser
23import logging
24import os , sys
25sys.path.extend(list(map(os.path.abspath, ['../../'])))
26from smolt_config import get_config_attr
27from request import Request
28
29_SECTION = 'MAIN'
30
31
32def _get_option_name(hw_uuid, host):
33 return '%s__%s' % (hw_uuid, host)
34
35class UUIDError(Exception):
36 def __init__(self, message):
37 self.msg = message
38
39 def __str__(self):
40 return str(self.msg)
41
42class PubUUIDError(Exception):
43 def __init__(self, message):
44 self.msg = message
45
46 def __str__(self):
47 return str(self.msg)
48
49class _UuidDb(object):
50 hw_uuid = None
51
52 def __init__(self, database_filename):
53 self._database_filename = database_filename
54 self._config = configparser.RawConfigParser()
56 if not self._config.has_section(_SECTION):
57 self._config.add_section(_SECTION)
58 self.hw_uuid_file = get_config_attr('HW_UUID', '/etc/smolt/hw-uuid')
59
60 def _flush(self):
61 try:
62 smolt_user_config_dir = os.path.expanduser('~/.smolt/')
63 if not os.path.exists(smolt_user_config_dir):
64 os.mkdir(smolt_user_config_dir, 0o700)
65 f = open(self._database_filename, 'w')
66 self._config.write(f)
67 f.close()
68 except:
69 logging.error('Flushing UUID database failed')
70
71 def get_pub_uuid(self, hw_uuid, host):
72 try:
73 pub_uuid = self._config.get(_SECTION, _get_option_name(hw_uuid, host))
74 logging.info('Public UUID "%s" read from database' % pub_uuid)
75 return pub_uuid
76 except configparser.NoOptionError:
77 try:
78 req = Request('/client/pub_uuid/%s' % self.get_priv_uuid())
79 pudict = json.loads(req.open().read())
80 self.set_pub_uuid(self.hw_uuid, Request(), pudict['pub_uuid'])
81 return pudict['pub_uuid']
82 except Exception as e:
83 error(_('Error determining public UUID: %s') % e)
84 sys.stderr.write(_("Unable to determine Public UUID! This could be a network error or you've\n"))
85 sys.stderr.write(_("not submitted your profile yet.\n"))
86 raise PubUUIDError('Could not determine Public UUID!\n')
87
88 def set_pub_uuid(self, hw_uuid, host, pub_uuid):
89 for i in (hw_uuid, host, pub_uuid):
90 if not i:
91 raise Exception('No parameter is allowed to be None')
92 self._config.set(_SECTION, _get_option_name(hw_uuid, host), pub_uuid)
93 logging.info('Public UUID "%s" written to database' % pub_uuid)
94 self._flush()
95
96 def gen_uuid(self):
97 try:
98 with open('/proc/sys/kernel/random/uuid') as rand_uuid:
99 rand_uuid_string = rand_uuid.read().strip()
100 return rand_uuid_string
101 except IOError:
102 try:
103 import uuid
104 return uuid.uuid4()
105 except:
106 raise UUIDError('Could not generate new UUID!')
107
108 def get_priv_uuid(self):
109 if self.hw_uuid:
110 return self.hw_uuid
111
112 try:
113 with open(self.hw_uuid_file) as hwuuidfile:
114 self.hw_uuid = hwuuidfile.read().strip()
115 except IOError:
116 try:
117 self.hw_uuid = self.genUUID()
118 except UUIDError:
119 raise UUIDError('Unable to determine UUID of system!')
120 try:
121 # make sure directory exists, create if not
122 with open(self.hw_uuid_file, 'w') as hwuuidfile:
123 hwuuidfile.write(self.hw_uuid)
124 except Exception as e:
125 raise UUIDError('Unable to save UUID to %s. Please run once as root.' % self.hw_uuid_file)
126
127 return self.hw_uuid
128
129_uuid_db_instance = None
130def UuidDb():
131 """Simple singleton wrapper with lazy initialization"""
132 global _uuid_db_instance
133 if _uuid_db_instance is None:
134 import config
135 from smolt import get_config_attr
136 _uuid_db_instance = _UuidDb(get_config_attr("UUID_DB", os.path.expanduser('~/.smolt/uuiddb.cfg')))
137 return _uuid_db_instance
def set_pub_uuid(self, hw_uuid, host, pub_uuid)
Definition: uuiddb.py:88
def __init__(self, database_filename)
Definition: uuiddb.py:52
def read(device=None, features=[])
Definition: disc.py:35
def get_config_attr(attr, default="")
Definition: smolt_config.py:22
def error(message)
Definition: smolt.py:409
def write(text, progress=True)
Definition: mythburn.py:307