Remove py2 support
Change-Id: Ieeaeb3b8efcae004e94aea6c1d2703c9782a8650 Reviewed-on: https://forge.frm2.tum.de/review/c/sine2020/secop/playground/+/21320 Tested-by: Enrico Faulhaber <enrico.faulhaber@frm2.tum.de> Reviewed-by: Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
This commit is contained in:
@ -21,12 +21,12 @@
|
||||
#
|
||||
# *****************************************************************************
|
||||
"""Define helpers"""
|
||||
from __future__ import division, print_function
|
||||
|
||||
import ast
|
||||
import os
|
||||
import threading
|
||||
import ast
|
||||
import time
|
||||
import threading
|
||||
import configparser
|
||||
from collections import OrderedDict
|
||||
|
||||
from daemon import DaemonContext
|
||||
@ -34,19 +34,12 @@ from daemon import DaemonContext
|
||||
from secop.errors import ConfigError
|
||||
from secop.lib import formatException, get_class, getGeneralConfig
|
||||
|
||||
try:
|
||||
import configparser # py3
|
||||
except ImportError:
|
||||
import ConfigParser as configparser # py2
|
||||
|
||||
try:
|
||||
import daemon.pidlockfile as pidlockfile
|
||||
except ImportError:
|
||||
import daemon.pidfile as pidlockfile
|
||||
|
||||
|
||||
|
||||
|
||||
class Server(object):
|
||||
# list allowed section prefixes
|
||||
# if mapped dict does not exist -> section need a 'class' option
|
||||
@ -66,12 +59,12 @@ class Server(object):
|
||||
if os.path.abspath(name) == name and os.path.exists(name) and \
|
||||
name.endswith('.cfg'):
|
||||
self._cfgfile = name
|
||||
self._pidfile = os.path.join(cfg[u'piddir'],
|
||||
name[:-4].replace(os.path.sep, u'_') + u'.pid')
|
||||
self._pidfile = os.path.join(cfg['piddir'],
|
||||
name[:-4].replace(os.path.sep, '_') + '.pid')
|
||||
name = os.path.basename(name[:-4])
|
||||
else:
|
||||
self._cfgfile = os.path.join(cfg[u'confdir'], name + u'.cfg')
|
||||
self._pidfile = os.path.join(cfg[u'piddir'], name + u'.pid')
|
||||
self._cfgfile = os.path.join(cfg['confdir'], name + '.cfg')
|
||||
self._pidfile = os.path.join(cfg['piddir'], name + '.pid')
|
||||
|
||||
self._name = name
|
||||
|
||||
@ -87,7 +80,7 @@ class Server(object):
|
||||
pidfile = pidlockfile.TimeoutPIDLockFile(self._pidfile)
|
||||
|
||||
if pidfile.is_locked():
|
||||
self.log.error(u'Pidfile already exists. Exiting')
|
||||
self.log.error('Pidfile already exists. Exiting')
|
||||
|
||||
with DaemonContext(
|
||||
pidfile=pidfile,
|
||||
@ -101,10 +94,10 @@ class Server(object):
|
||||
print(formatException(verbose=True))
|
||||
raise
|
||||
|
||||
self.log.info(u'startup done, handling transport messages')
|
||||
self.log.info('startup done, handling transport messages')
|
||||
self._threads = set()
|
||||
for ifname, ifobj in self.interfaces.items():
|
||||
self.log.debug(u'starting thread for interface %r' % ifname)
|
||||
self.log.debug('starting thread for interface %r' % ifname)
|
||||
t = threading.Thread(target=ifobj.serve_forever)
|
||||
t.daemon = True
|
||||
t.start()
|
||||
@ -113,78 +106,78 @@ class Server(object):
|
||||
time.sleep(1)
|
||||
for t in self._threads:
|
||||
if not t.is_alive():
|
||||
self.log.debug(u'thread %r died (%d still running)' %
|
||||
self.log.debug('thread %r died (%d still running)' %
|
||||
(t, len(self._threads)))
|
||||
t.join()
|
||||
self._threads.discard(t)
|
||||
|
||||
def _processCfg(self):
|
||||
self.log.debug(u'Parse config file %s ...' % self._cfgfile)
|
||||
self.log.debug('Parse config file %s ...' % self._cfgfile)
|
||||
|
||||
parser = configparser.SafeConfigParser()
|
||||
parser.optionxform = str
|
||||
|
||||
if not parser.read([self._cfgfile]):
|
||||
self.log.error(u'Couldn\'t read cfg file !')
|
||||
raise ConfigError(u'Couldn\'t read cfg file %r' % self._cfgfile)
|
||||
self.log.error('Couldn\'t read cfg file !')
|
||||
raise ConfigError('Couldn\'t read cfg file %r' % self._cfgfile)
|
||||
|
||||
|
||||
|
||||
for kind, devtype, classmapping in self.CFGSECTIONS:
|
||||
kinds = u'%ss' % kind
|
||||
kinds = '%ss' % kind
|
||||
objs = OrderedDict()
|
||||
self.__dict__[kinds] = objs
|
||||
for section in parser.sections():
|
||||
prefix = u'%s ' % kind
|
||||
prefix = '%s ' % kind
|
||||
if section.lower().startswith(prefix):
|
||||
name = section[len(prefix):]
|
||||
opts = dict(item for item in parser.items(section))
|
||||
if u'class' in opts:
|
||||
cls = opts.pop(u'class')
|
||||
if 'class' in opts:
|
||||
cls = opts.pop('class')
|
||||
else:
|
||||
if not classmapping:
|
||||
self.log.error(u'%s %s needs a class option!' % (kind.title(), name))
|
||||
raise ConfigError(u'cfgfile %r: %s %s needs a class option!' %
|
||||
self.log.error('%s %s needs a class option!' % (kind.title(), name))
|
||||
raise ConfigError('cfgfile %r: %s %s needs a class option!' %
|
||||
(self._cfgfile, kind.title(), name))
|
||||
type_ = opts.pop(u'type', devtype)
|
||||
type_ = opts.pop('type', devtype)
|
||||
cls = classmapping.get(type_, None)
|
||||
if not cls:
|
||||
self.log.error(u'%s %s needs a type option (select one of %s)!' %
|
||||
self.log.error('%s %s needs a type option (select one of %s)!' %
|
||||
(kind.title(), name, ', '.join(repr(r) for r in classmapping)))
|
||||
raise ConfigError(u'cfgfile %r: %s %s needs a type option (select one of %s)!' %
|
||||
raise ConfigError('cfgfile %r: %s %s needs a type option (select one of %s)!' %
|
||||
(self._cfgfile, kind.title(), name, ', '.join(repr(r) for r in classmapping)))
|
||||
# MAGIC: transform \n.\n into \n\n which are normally stripped
|
||||
# by the ini parser
|
||||
for k in opts:
|
||||
v = opts[k]
|
||||
while u'\n.\n' in v:
|
||||
v = v.replace(u'\n.\n', u'\n\n')
|
||||
while '\n.\n' in v:
|
||||
v = v.replace('\n.\n', '\n\n')
|
||||
try:
|
||||
opts[k] = ast.literal_eval(v)
|
||||
except Exception:
|
||||
opts[k] = v
|
||||
|
||||
# try to import the class, raise if this fails
|
||||
self.log.debug(u'Creating %s %s ...' % (kind.title(), name))
|
||||
self.log.debug('Creating %s %s ...' % (kind.title(), name))
|
||||
# cls.__init__ should pop all used args from options!
|
||||
logname = u'dispatcher' if kind == u'node' else u'%s_%s' % (kind, name.lower())
|
||||
logname = 'dispatcher' if kind == 'node' else '%s_%s' % (kind, name.lower())
|
||||
obj = get_class(cls)(name, self.log.getChild(logname), opts, self)
|
||||
if opts:
|
||||
raise ConfigError(u'%s %s: class %s: don\'t know how to handle option(s): %s' %
|
||||
(kind, name, cls, u', '.join(opts)))
|
||||
raise ConfigError('%s %s: class %s: don\'t know how to handle option(s): %s' %
|
||||
(kind, name, cls, ', '.join(opts)))
|
||||
|
||||
# all went well so far
|
||||
objs[name] = obj
|
||||
|
||||
# following line is the reason for 'node' beeing the first entry in CFGSECTIONS
|
||||
if len(self.nodes) != 1:
|
||||
raise ConfigError(u'cfgfile %r: needs exactly one node section!' % self._cfgfile)
|
||||
raise ConfigError('cfgfile %r: needs exactly one node section!' % self._cfgfile)
|
||||
self.dispatcher, = tuple(self.nodes.values())
|
||||
|
||||
pollTable = dict()
|
||||
# all objs created, now start them up and interconnect
|
||||
for modname, modobj in self.modules.items():
|
||||
self.log.info(u'registering module %r' % modname)
|
||||
self.log.info('registering module %r' % modname)
|
||||
self.dispatcher.register_module(modobj, modname, modobj.properties['export'])
|
||||
try:
|
||||
modobj.pollerClass.add_to_table(pollTable, modobj)
|
||||
@ -208,8 +201,8 @@ class Server(object):
|
||||
# poller.start must return either a timeout value or None (default 30 sec)
|
||||
timeout = poller.start(started_callback=event.set) or 30
|
||||
start_events.append((time.time() + timeout, repr(poller), event))
|
||||
self.log.info(u'waiting for modules and pollers being started')
|
||||
self.log.info('waiting for modules and pollers being started')
|
||||
for deadline, name, event in sorted(start_events):
|
||||
if not event.wait(timeout=max(0, deadline - time.time())):
|
||||
self.log.info('WARNING: timeout when starting %s' % name)
|
||||
self.log.info(u'all modules and pollers started')
|
||||
self.log.info('all modules and pollers started')
|
||||
|
Reference in New Issue
Block a user