invert sea_info dict

This commit is contained in:
zolliker 2024-04-03 13:24:26 +02:00
parent 9fed0ead11
commit 0d026c91de

View File

@ -70,12 +70,12 @@ def summarize_server_state(givencfgs, ourcfgs, sealist, sea_info, strict=False):
:param givencfgs: dict <service> of given configuration (from nicos cache) :param givencfgs: dict <service> of given configuration (from nicos cache)
:param outcfgs: dict <service> of running configuration (frappy) :param outcfgs: dict <service> of running configuration (frappy)
:param sealist: list of running sea configuration :param sealist: list of running sea configuration [<config>, <stick>, <addon1>, <addon2> ...]
:param sea_info: dict <frappycfg> of <seacfg> with info about sea configs :param sea_info: dict <seacfg> of <frappycfg>
:param strict: when True return empty cfg result on error :param strict: when True return empty cfg result on error
:return: tuple (<error>, <proposed>, (<frappyitems>, <seaitems>), <remarks> where: :return: tuple (<error>, <proposed>, (<frappyitems>, <seaitems>), <remarks> where:
<error>: proposed config not sure <error>: proposed config not sure
<proposed>: dict <service> of proposed cfg <proposed>: dict <service> of proposed cfg (cfg is True: no restart needed)
<frappyitems>: dict of items runnning in frappy servers (addons are separated) <frappyitems>: dict of items runnning in frappy servers (addons are separated)
<seaitems>: dict of items running on the sea server <seaitems>: dict of items running on the sea server
<remarks>: dict of actions / remarks <remarks>: dict of actions / remarks
@ -95,7 +95,6 @@ def summarize_server_state(givencfgs, ourcfgs, sealist, sea_info, strict=False):
seacfgfiles = [(c or '') + SEAEXT.get(s, '.addon') seacfgfiles = [(c or '') + SEAEXT.get(s, '.addon')
for c, s in zip_longest(sealist, FrappyManager.services)] for c, s in zip_longest(sealist, FrappyManager.services)]
seacfgset = set(seacfgfiles)
error = False error = False
result = {} result = {}
addons = set() addons = set()
@ -143,6 +142,10 @@ def summarize_server_state(givencfgs, ourcfgs, sealist, sea_info, strict=False):
remarks[pkey] = 'missing frappy config' remarks[pkey] = 'missing frappy config'
error = True error = True
restart = set() restart = set()
available = {s: set() for s in ('.config', '.stick', '.addon')}
for seacfg, cfg in sea_info.items():
ext = seacfg.rsplit('.', 1)[-1]
available[ext].add(cfg)
for key, running in frappycfgs.items(): for key, running in frappycfgs.items():
if running: if running:
if key in ('main', 'stick'): if key in ('main', 'stick'):
@ -151,7 +154,7 @@ def summarize_server_state(givencfgs, ourcfgs, sealist, sea_info, strict=False):
service = 'addons' service = 'addons'
addons.add(running) addons.add(running)
if not seacfgs.get(key): if not seacfgs.get(key):
if sea_info.get(running): if running in available[SEAEXT.get(key, '.addon')]:
restart.add(service) restart.add(service)
remarks[key] = 'restart to start sea' remarks[key] = 'restart to start sea'
elif givencfgs.get(key) != running: elif givencfgs.get(key) != running:
@ -334,7 +337,8 @@ class FrappyManager(ServiceManager):
desc = repr(e) desc = repr(e)
if cfg not in all_cfg: if cfg not in all_cfg:
if sea_cfg and sea_info is not None: if sea_cfg and sea_info is not None:
sea_info.setdefault(sea_cfg, set()).add(cfg) # sea_info.setdefault(sea_cfg, set()).add(cfg)
sea_info[cfg] = sea_cfg
all_cfg.add(cfg) all_cfg.add(cfg)
if list_info is not None: if list_info is not None:
list_info.setdefault(cfgdir, {})[cfg] = desc.split('\n', 1)[0] list_info.setdefault(cfgdir, {})[cfg] = desc.split('\n', 1)[0]
@ -351,35 +355,39 @@ class FrappyManager(ServiceManager):
for service in self.services: for service in self.services:
self.all_cfg(ins, service, list_info, sea_info) self.all_cfg(ins, service, list_info, sea_info)
seacfgpat = re.compile(r'(.*)(\.config|\.stick|\.addon)') seacfgpat = re.compile(r'(.*)(\.config|\.stick|\.addon)')
inverted_sea_info = {} # inverted_sea_info = {}
for seacfg, cfgset in sea_info.items(): # for seacfg, cfgset in sea_info.items():
for cfg in cfgset: # for cfg in cfgset:
inverted_sea_info[cfg] = seacfg # inverted_sea_info[cfg] = seacfg
ambiguous = 0 cfgset = set()
ambiguous = {}
for cfg, seacfg in sea_info.items():
if cfg in cfgset:
ambiguous[cfg] = ambiguous.get(cfg, 1) + 1
else:
cfgset.add(cfg)
keylen = max(max(len(k) for k in cfgs) for cfgs in list_info.values()) keylen = max(max(len(k) for k in cfgs) for cfgs in list_info.values())
for cfgdir, cfgs in list_info.items(): for cfgdir, cfgs in list_info.items():
if cfgs: if cfgs:
prt('') prt('')
prt('--- %s:' % cfgdir) prt('--- %s:' % cfgdir)
for cfg, desc in sorted(cfgs.items(), key=lambda v: (v[0].lower(), v)): for cfg, desc in sorted(cfgs.items(), key=lambda v: (v[0].lower(), v)):
seacfg = inverted_sea_info.get(cfg) seacfg = sea_info.get(cfg)
if seacfg: if seacfg:
name, ext = seacfgpat.match(seacfg).groups() name, ext = seacfgpat.match(seacfg).groups()
if name == cfg or name + 'stick' == cfg: if name == cfg or name + 'stick' == cfg:
prefix = '* ' prefix = '* '
else: else:
prefix = f'* ({name}{ext}) ' prefix = f'* ({name}{ext}) '
n = len(sea_info[seacfg]) if cfg in ambiguous:
if n > 1:
prefix = '!' + prefix[1:] prefix = '!' + prefix[1:]
ambiguous += 1.0 / n
desc = prefix + desc desc = prefix + desc
prt('%s %s' % (cfg.ljust(keylen), desc)) prt('%s %s' % (cfg.ljust(keylen), desc))
prt(' ') prt(' ')
gap = ' ' * keylen gap = ' ' * keylen
prt(f'{gap} * need sea') prt(f'{gap} * need sea')
if ambiguous: if ambiguous:
print(f'{gap} ! {round(ambiguous)} ambiguous mappings sea -> frappy') print(f'{gap} ! {len(ambiguous)} ambiguous mappings sea -> frappy')
def treat_args(self, argdict, unknown=(), extra=()): def treat_args(self, argdict, unknown=(), extra=()):
cfg = None cfg = None