#!/usr/bin/env python3 """Static checks for the ucode sources that need no router. ucode resolves names where they are used: calling a function that is declared further down the same file fails at runtime ("access to undeclared variable"). Also: calls and named imports must match what a Dawnline module exports, and the rpcd methods (dawnline.rpc.*) must match the plugin and its ACL. Run: scripts/lint-ucode.py [package root] """ import glob import json import os import re import sys root = sys.argv[1] if len(sys.argv) > 1 else os.path.join(os.path.dirname(__file__), '..', 'package', 'luci-app-dawnline', 'root') MODDIR = f"{root}/usr/share/ucode/dawnline" PLUGIN = f"{root}/usr/share/rpcd/ucode/luci.dawnline" ACL = f"{root}/usr/share/rpcd/acl.d/luci-app-dawnline.json" modules = sorted(glob.glob(f"{MODDIR}/*.uc") + glob.glob(f"{MODDIR}/rpc/*.uc")) files = modules + [ PLUGIN, f"{root}/usr/libexec/dawnline/agent", f"{root}/usr/libexec/dawnline/updater", f"{root}/usr/libexec/dawnline/resolver", f"{root}/usr/bin/dawnline", f"{root}/usr/share/dawnline/portal/www/cgi-bin/portal", ] files = [f for f in files if os.path.exists(f)] bad = 0 def problem(msg): global bad print(msg) bad = 1 def strip_comments(text): return re.sub(r'//[^\n]*|/\*.*?\*/', '', text, flags=re.S) def modname(path): """dawnline/rpc/people.uc → 'rpc.people' (as in import … from 'dawnline.rpc.people').""" rel = os.path.relpath(path, MODDIR)[:-3] return rel.replace(os.sep, '.') for f in files: code = strip_comments(open(f, encoding='utf-8').read()) defs = {m.group(1): m.start() for m in re.finditer(r'^(?:export )?function (\w+)\(', code, re.M)} for name, pos in defs.items(): for m in re.finditer(r'(?