67 lines
2.7 KiB
Python
67 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Build an experimental native Winbox/WebFig descriptor (not release-enabled)."""
|
|
import argparse
|
|
import gzip
|
|
import hashlib
|
|
import json
|
|
import re
|
|
from pathlib import Path
|
|
|
|
|
|
def checksum(data):
|
|
# crcmod: poly=0x104c11db7, initCrc=0xffffffff, rev=False, xorOut=0xffffffff.
|
|
crc = 0
|
|
for byte in data:
|
|
crc ^= byte << 24
|
|
for _ in range(8):
|
|
crc = ((crc << 1) ^ (0x04c11db7 if crc & 0x80000000 else 0)) & 0xffffffff
|
|
return crc ^ 0xffffffff
|
|
|
|
|
|
def literal(value):
|
|
"""Emit the object-literal dialect used by RouterOS GUI resources."""
|
|
if isinstance(value, dict):
|
|
pairs = []
|
|
for key, item in value.items():
|
|
if not re.fullmatch(r"[A-Za-z_$][A-Za-z0-9_$]*|[0-9]+", key):
|
|
raise ValueError(f"Unsupported descriptor property {key!r}")
|
|
pairs.append(key + ':' + literal(item))
|
|
return '{' + ','.join(pairs) + '}'
|
|
if isinstance(value, list):
|
|
return '[' + ','.join(map(literal, value)) + ']'
|
|
return json.dumps(value, ensure_ascii=True, separators=(',', ':'))
|
|
|
|
|
|
def build(root, version):
|
|
# Use existing RouterOS handlers and field IDs. Adding a field here does
|
|
# not create backend storage or a new RouterOS command handler.
|
|
# Do not use a row predicate as a menu visibility predicate.
|
|
descriptor = [{'name': 'OLSR', 'title': 'OLSR', 'prio': 1900, 'icon': 30, 'c': [
|
|
{'name': 'OLSR Service', 'title': 'Service', 'type': 'map', 'path': [48, 3],
|
|
'nameval': 'Name', 'nonaddable': 1, 'autorefresh': 2000,
|
|
'c': [{'name': 'Name', 'type': 'string', 'id': 's66', 'ro': 1, 'width': 160},
|
|
{'type': 'enable'}, {'type': 'comment'}]},
|
|
]}]
|
|
data = literal(descriptor).encode()
|
|
unique = 'olsr-' + hashlib.sha1(data).hexdigest()[:12] + '.jg'
|
|
directory = Path(root) / 'home/web/webfig'
|
|
directory.mkdir(parents=True, exist_ok=True)
|
|
for old in directory.glob('olsr-*.jg.gz'):
|
|
old.unlink()
|
|
compressed = gzip.compress(data, compresslevel=0, mtime=0)
|
|
(directory / (unique + '.gz')).write_bytes(compressed)
|
|
link = directory / 'olsr.jg.gz'
|
|
link.unlink(missing_ok=True)
|
|
link.symlink_to(unique + '.gz')
|
|
info = {'crc': checksum(data), 'size': len(compressed), 'name': 'olsr.jg', 'unique': unique, 'version': version}
|
|
(directory / 'olsr.info').write_text(literal(info) + ',\n')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--experimental', action='store_true', required=True, help='Unvalidated native UI; excludes interface/config/HNA controls')
|
|
parser.add_argument('--root', required=True)
|
|
parser.add_argument('--version', required=True)
|
|
args = parser.parse_args()
|
|
build(args.root, args.version)
|