diff --git a/inventory.py b/inventory.py new file mode 100644 index 0000000..955a0a2 --- /dev/null +++ b/inventory.py @@ -0,0 +1,117 @@ +import re +from pathlib import Path +from glob import glob +from frappy_psi.calcurve import CalCurve + + +lookup = '~/git', '~' +sea_pat = re.compile(r'\s*(lsc_sensor|mom_sensor|stick_sensors)\s+(\S+)\s+(\S+)\s+(?:.*-(sensor|curve)\s)?(\S+)?') +frappy_pat = re.compile(r'''[^#]*calcurve\s*=\s*('[^']+'|"[^"]+")''') + + +def find_dir(dirpath): + for base in lookup: + result = (Path(base) / dirpath).expanduser() + if result.is_dir(): + return result + raise ValueError(f'{dirpath} not found') + + +sea_dir = find_dir('sea/tcl') +cal_dir = find_dir('calcurves') +frappy_cfg = find_dir('frappy/cfg') + + +SPECIAL = {'raw', 'manual', 'vacuum', 'code', 'clone', 'undefined', '-sensorname'} +STD = set(Path(p).name.replace('.std', '') for p in glob(str(cal_dir / '*.std'))) +FAKECRV = STD | SPECIAL + +OPT = {'lsc_sensor': ' -sensor ', 'mom_sensor': '-curve '} + +moms = {} + + +class Collector: + def __init__(self): + self.result = {} + + def lsc_sensor(self, file, _1, _2, kwd, calcurve): + if calcurve not in SPECIAL and kwd in (None, 'sensor'): + self.result.setdefault(calcurve, []).append(file) + + def mom_sensor(self, file, _1, _2, kwd, calcurve): + if calcurve not in FAKECRV and kwd == 'curve': + self.result.setdefault(calcurve, []).append(file) + + def stick_sensors(self, file, calcurve1, calcurve2, _3, _4): + for calcurve in (calcurve1, calcurve2): + if calcurve.startswith('code=') or calcurve in SPECIAL: + continue + self.result.setdefault(calcurve, []).append(Path(file).name) + + def collect_sea(self, pattern): + """treat lines starting with lsc_sensor, mom_sensor and stick_sensors""" + for file in glob(str(sea_dir / pattern)): + with open(file) as f: + for line in f: + match = sea_pat.match(line) + if match: + getattr(self, match.group(1), None)(file, *match.groups()[1:]) + + def collect_frappy(self, pattern): + """treat lines containing 'calcurve = "" + also single quote is allowed, and no hash before + """ + for file in glob(str(frappy_cfg / pattern)): + with open(file) as f: + for line in f: + match = frappy_pat.match(line) + if match: + calcurve = match.group(1)[1:-1] + self.result.setdefault(calcurve, []).append(file) + + +col = Collector() + +col.collect_sea('*.config') +col.collect_sea('*.stick') +col.collect_sea('*.addon') +col.collect_frappy('*.py') +col.collect_frappy('*/*.py') + +all_calibs = {Path(f).stem.lower(): f for f in glob(str(cal_dir / '*.*'))} +unused = dict(all_calibs) + +found = {} +notfound = {} + +for calib, files in col.result.items(): + file = all_calibs.get(calib.lower()) + if file: + unused.pop(calib.lower(), None) + found[calib] = (file, files) + else: + notfound[calib] = files + + +print('\n--- found:') +for calib, (file, files) in found.items(): + if calib in STD: + continue + try: + CalCurve(calib) + head = f'{Path(file).name}:' + except Exception as e: + head = f'{Path(file).name} {e!r}:' + print(head, ' '.join(Path(p).name for p in files)) + +if notfound: + print('\n--- not found:') + for calib, files in notfound.items(): + print(f'{calib}:', ' '.join(Path(p).name for p in files)) + +print('\n--- unused:') +print(' '.join(Path(p).name for p in unused.values())) + +print('\nfound', len(found), 'notfound', len(notfound), 'unused', len(unused)) +