#!/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, the rpcd methods (dawnline.rpc.*) must match the plugin and its ACL, and regexes stay POSIX (rules/backend.md). Imports against the module layering are reported as notes, not failures. 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/bin/dawnline", f"{root}/usr/share/dawnline/portal/cgi.uc", ] 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 blank_comments_templates(text): """Comments and template literals replaced by their newlines, strings kept: line numbers stay, and a `}` that starts a line of a template (nft rules) ends no function. A template inside another one's `${…}` reads as code: good enough for how Dawnline writes them.""" return re.sub(r"//[^\n]*|/\*.*?\*/|'(?:\\.|[^'\\\n])*'|\"(?:\\.|[^\"\\\n])*\"|`(?:\\.|[^`\\])*`", lambda m: m.group(0) if m.group(0)[0] in '\'"' else '\n' * m.group(0).count('\n'), text, flags=re.S) # A regex literal on one line: `/…/` with no slash inside, followed by flags or # a delimiter (so a division keeps out). Good enough for how Dawnline writes them. REGEX_LITERAL = re.compile(r'(? mine or (theirs == mine and mine < 4 and dep != mod): print(f"note: {f}: {mod} ({LAYER_NAME[mine]}) imports dawnline.{dep} ({LAYER_NAME[theirs]}), a layer above it") notes += 1 if notes: print(f"lint-ucode: {notes} layering notes (not failures)") print(f"lint-ucode: {len(files)} files checked" + (", problems found" if bad else ", ok")) sys.exit(bad)