# -*- coding: utf-8 -*- # ***************************************************************************** # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # details. # # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Module authors: # Markus Zolliker # # ***************************************************************************** import sys import os from glob import glob from os.path import join, isdir, basename, expanduser, exists from configparser import ConfigParser from servicemanager.base import ServiceManager, ServiceDown, UsageError class FrappyManager(ServiceManager): group = 'frappy' services = ('main', 'stick', 'addons') USAGE = """ Usage: frappy list [] * frappy start frappy restart [] [] * frappy stop [] * frappy listcfg [ | develop] # list available cfg files is one of main, stick, addons %s * wildcards allowed, using '.' to replace 0 or more arbitrary characters in """ def config_dirs(self, ins, service): cfgpaths = [] cfgparser = ConfigParser() cfgparser.optionxform = str cfgfile = self.env[ins].get('FRAPPY_CONFIG_FILE') confdir = self.env[ins].get('FRAPPY_CONFDIR') if cfgfile is not None: cfgfile = self.env[ins]['FRAPPY_CONFIG_FILE'].replace('', service) cfgparser.read(cfgfile) try: section = cfgparser['FRAPPY'] except KeyError: raise ValueError('%s does not exist or has no FRAPPY section' % cfgfile) confdir = section.get('confdir', confdir) for cfgpath in confdir.split(os.pathsep): if cfgpath.endswith(''): cfgpaths.append(expanduser(cfgpath[:-6] + service)) else: scfg = join(cfgpath, service) if isdir(scfg): cfgpaths.append(scfg) cfgpaths.append(expanduser(cfgpath)) return cfgpaths def prepare_start(self, ins, service, cfg=''): start_dir, env = super().prepare_start(ins, service) for key, value in env.items(): if '' in value: env[key] = value.replace('', service) os.environ[key] = env[key] cfgpaths = self.config_dirs(ins, service) if cfgpaths: env['FRAPPY_CONFDIR'] = os.pathsep.join(cfgpaths) # merge PYTHONPATH from servicemanager.cfg with the one from environment pypathlist = [] for pypath in env.get('PYTHONPATH'), os.environ.get('PYTHONPATH'): if pypath is not None: pypathlist.extend(p for p in pypath.split(':') if p not in pypathlist) if pypathlist: env['PYTHONPATH'] = os.environ['PYTHONPATH'] = ':'.join(pypathlist) return start_dir, env def do_start(self, ins, service=None, cfg='', restart=False, wait=False, logger=None): if self.wildcard(ins) is not None: raise UsageError('no wildcards allowed with %s start' % self.group) if cfg and not service and len(self.services) != 1: raise UsageError('need service to start (one of %s)' % ', '.join(self.services)) super().do_start(ins, service, cfg, restart, wait, logger) def do_gui(self, ins='', service='main'): start_dir, env = self.prepare_start(ins, service) sys.path.insert(0, start_dir) try: self.check_running(ins, service) except ServiceDown as e: raise UsageError('frappy %s %s is not running' % (ins, service)) return except KeyError: if not ins: raise UsageError('missing instance') raise UsageError('unknown instance %s' % ins) print('starting frappy gui %s' % ins) import mlzlog from secop.gui.qt import QApplication from secop.gui.mainwindow import MainWindow mlzlog.initLogging('gui', 'info') app = QApplication([]) win = MainWindow(['localhost:%d' % self.info[ins][service]]) win.show() return app.exec_() def all_cfg(self, ins, service, by_dir=False): result = {} all_cfg = {} for ins in [ins] if ins else self.info: for service in [service] if service else self.services: for cfgdir in self.config_dirs(ins, service): result.setdefault(cfgdir, {}) cfgs = result[cfgdir] for cfgfile in glob(join(cfgdir, '*.cfg')): desc = '' try: parser = ConfigParser() parser.read(cfgfile) for s in parser.sections(): if s == 'NODE' or s.startswith('node '): desc = parser[s].get('description', '').split('\n')[0] break except Exception: pass cfg = basename(cfgfile)[:-4] if cfg not in all_cfg: all_cfg[cfg] = desc cfgs[cfg] = desc return result if by_dir else all_cfg def do_listcfg(self, ins='', service=''): if service: all_cfg = self.all_cfg(ins, service, by_dir=True) else: all_cfg = {} for service in self.services: all_cfg.update(self.all_cfg(ins, service, by_dir=True)) for cfgdir, cfgs in all_cfg.items(): if cfgs: print('\n--- %s:\n' % cfgdir) keylen = max(len(k) for k in cfgs) for cfg, desc in cfgs.items(): print('%s %s' % (cfg.ljust(keylen), desc)) def treat_args(self, argdict, unknown=(), extra=()): if len(unknown) == 1: cfg = unknown[0] if cfg == 'develop': argdict['service'] = cfg return super().treat_args(argdict, (), ()) if (',' in cfg or cfg.endswith('.cfg') or cfg in self.all_cfg(argdict.get('ins'), argdict.get('service'))): return super().treat_args(argdict, (), unknown) return super().treat_args(argdict, unknown, extra)