MythTV  master
distros/mythtv_data/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 
19 from builtins import map
20 from builtins import str
21 from builtins import object
22 import configparser
23 import logging
24 import os , sys
25 sys.path.extend(list(map(os.path.abspath, ['../../'])))
26 from smolt_config import get_config_attr
27 from request import Request
28 
29 _SECTION = 'MAIN'
30 
31 
32 def _get_option_name(hw_uuid, host):
33  return '%s__%s' % (hw_uuid, host)
34 
35 class UUIDError(Exception):
36  def __init__(self, message):
37  self.msg = message
38 
39  def __str__(self):
40  return str(self.msg)
41 
42 class PubUUIDError(Exception):
43  def __init__(self, message):
44  self.msg = message
45 
46  def __str__(self):
47  return str(self.msg)
48 
49 class _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
130 def 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
hardwareprofile.distros.mythtv_data.uuiddb.UUIDError.__init__
def __init__(self, message)
Definition: distros/mythtv_data/uuiddb.py:36
hardwareprofile.distros.mythtv_data.uuiddb._UuidDb.get_pub_uuid
def get_pub_uuid(self, hw_uuid, host)
Definition: distros/mythtv_data/uuiddb.py:71
error
static void error(const char *str,...)
Definition: vbi.cpp:36
discid.disc.read
def read(device=None, features=[])
Definition: disc.py:35
mythburn.write
def write(text, progress=True)
Definition: mythburn.py:308
hardwareprofile.distros.mythtv_data.uuiddb.PubUUIDError.msg
msg
Definition: distros/mythtv_data/uuiddb.py:44
hardwareprofile.distros.mythtv_data.uuiddb.UUIDError
Definition: distros/mythtv_data/uuiddb.py:35
hardwareprofile.distros.mythtv_data.uuiddb.UUIDError.msg
msg
Definition: distros/mythtv_data/uuiddb.py:37
hardwareprofile.distros.mythtv_data.uuiddb.PubUUIDError.__str__
def __str__(self)
Definition: distros/mythtv_data/uuiddb.py:46
hardwareprofile.distros.mythtv_data.uuiddb._UuidDb.set_pub_uuid
def set_pub_uuid(self, hw_uuid, host, pub_uuid)
Definition: distros/mythtv_data/uuiddb.py:88
hardwareprofile.distros.mythtv_data.uuiddb.UUIDError.__str__
def __str__(self)
Definition: distros/mythtv_data/uuiddb.py:39
hardwareprofile.distros.mythtv_data.uuiddb._UuidDb.get_priv_uuid
def get_priv_uuid(self)
Definition: distros/mythtv_data/uuiddb.py:108
hardwareprofile.i18n._
_
Definition: i18n.py:44
hardwareprofile.distros.mythtv_data.uuiddb.PubUUIDError
Definition: distros/mythtv_data/uuiddb.py:42
hardwareprofile.smolt_config.get_config_attr
def get_config_attr(attr, default="")
Definition: smolt_config.py:22
hardwareprofile.distros.mythtv_data.request.Request
def Request(url=None)
Definition: distros/mythtv_data/request.py:62
hardwareprofile.distros.mythtv_data.uuiddb._UuidDb
Definition: distros/mythtv_data/uuiddb.py:49
hardwareprofile.distros.mythtv_data.uuiddb.PubUUIDError.__init__
def __init__(self, message)
Definition: distros/mythtv_data/uuiddb.py:43
hardwareprofile.distros.mythtv_data.uuiddb._UuidDb.hw_uuid_file
hw_uuid_file
Definition: distros/mythtv_data/uuiddb.py:58
hardwareprofile.distros.mythtv_data.uuiddb._UuidDb.hw_uuid
hw_uuid
Definition: distros/mythtv_data/uuiddb.py:50
hardwareprofile.distros.mythtv_data.uuiddb._UuidDb._flush
def _flush(self)
Definition: distros/mythtv_data/uuiddb.py:60
hardwareprofile.distros.all.get
def get()
Definition: all.py:22
hardwareprofile.distros.mythtv_data.uuiddb._UuidDb._config
_config
Definition: distros/mythtv_data/uuiddb.py:54
hardwareprofile.distros.mythtv_data.uuiddb._UuidDb.__init__
def __init__(self, database_filename)
Definition: distros/mythtv_data/uuiddb.py:52
hardwareprofile.distros.mythtv_data.uuiddb._UuidDb._database_filename
_database_filename
Definition: distros/mythtv_data/uuiddb.py:53
hardwareprofile.distros.mythtv_data.uuiddb.UuidDb
def UuidDb()
Definition: distros/mythtv_data/uuiddb.py:130
hardwareprofile.distros.mythtv_data.uuiddb._UuidDb.gen_uuid
def gen_uuid(self)
Definition: distros/mythtv_data/uuiddb.py:96
hardwareprofile.distros.mythtv_data.uuiddb._get_option_name
def _get_option_name(hw_uuid, host)
Definition: distros/mythtv_data/uuiddb.py:32