2021-11-18 23:26:27 +00:00
|
|
|
import collections
|
|
|
|
import json
|
2023-01-02 13:09:29 +00:00
|
|
|
import os
|
2021-11-18 23:26:27 +00:00
|
|
|
import sys
|
2021-12-18 17:57:45 +00:00
|
|
|
from typing import Any, Dict, List
|
2022-08-02 22:44:34 +00:00
|
|
|
|
2021-12-18 17:57:45 +00:00
|
|
|
JSON = Dict[str, Any]
|
2021-11-18 23:26:27 +00:00
|
|
|
|
|
|
|
class Key:
|
2021-12-18 17:57:45 +00:00
|
|
|
def __init__(self, path: List[str]):
|
2021-11-18 23:26:27 +00:00
|
|
|
self.path = path
|
|
|
|
def __hash__(self):
|
|
|
|
result = 0
|
|
|
|
for id in self.path:
|
|
|
|
result ^= hash(id)
|
|
|
|
return result
|
|
|
|
def __eq__(self, other):
|
|
|
|
return type(self) is type(other) and self.path == other.path
|
|
|
|
|
|
|
|
Option = collections.namedtuple('Option', ['name', 'value'])
|
|
|
|
|
|
|
|
# pivot a dict of options keyed by their display name to a dict keyed by their path
|
2021-12-18 17:57:45 +00:00
|
|
|
def pivot(options: Dict[str, JSON]) -> Dict[Key, Option]:
|
|
|
|
result: Dict[Key, Option] = dict()
|
2021-11-18 23:26:27 +00:00
|
|
|
for (name, opt) in options.items():
|
|
|
|
result[Key(opt['loc'])] = Option(name, opt)
|
|
|
|
return result
|
|
|
|
|
|
|
|
# pivot back to indexed-by-full-name
|
|
|
|
# like the docbook build we'll just fail if multiple options with differing locs
|
|
|
|
# render to the same option name.
|
2021-12-18 17:57:45 +00:00
|
|
|
def unpivot(options: Dict[Key, Option]) -> Dict[str, JSON]:
|
|
|
|
result: Dict[str, Dict] = dict()
|
2021-11-18 23:26:27 +00:00
|
|
|
for (key, opt) in options.items():
|
|
|
|
if opt.name in result:
|
|
|
|
raise RuntimeError(
|
|
|
|
'multiple options with colliding ids found',
|
|
|
|
opt.name,
|
|
|
|
result[opt.name]['loc'],
|
|
|
|
opt.value['loc'],
|
|
|
|
)
|
|
|
|
result[opt.name] = opt.value
|
|
|
|
return result
|
|
|
|
|
2022-09-01 17:23:59 +00:00
|
|
|
warningsAreErrors = False
|
|
|
|
optOffset = 0
|
|
|
|
for arg in sys.argv[1:]:
|
|
|
|
if arg == "--warnings-are-errors":
|
|
|
|
optOffset += 1
|
|
|
|
warningsAreErrors = True
|
|
|
|
|
2021-12-18 18:37:41 +00:00
|
|
|
options = pivot(json.load(open(sys.argv[1 + optOffset], 'r')))
|
|
|
|
overrides = pivot(json.load(open(sys.argv[2 + optOffset], 'r')))
|
2021-11-18 23:26:27 +00:00
|
|
|
|
|
|
|
# fix up declaration paths in lazy options, since we don't eval them from a full nixpkgs dir
|
|
|
|
for (k, v) in options.items():
|
2022-03-24 01:44:43 +00:00
|
|
|
# The _module options are not declared in nixos/modules
|
|
|
|
if v.value['loc'][0] != "_module":
|
2022-06-27 18:03:34 +00:00
|
|
|
v.value['declarations'] = list(map(lambda s: f'nixos/modules/{s}' if isinstance(s, str) else s, v.value['declarations']))
|
2021-11-18 23:26:27 +00:00
|
|
|
|
|
|
|
# merge both descriptions
|
|
|
|
for (k, v) in overrides.items():
|
|
|
|
cur = options.setdefault(k, v).value
|
|
|
|
for (ok, ov) in v.value.items():
|
|
|
|
if ok == 'declarations':
|
|
|
|
decls = cur[ok]
|
|
|
|
for d in ov:
|
|
|
|
if d not in decls:
|
|
|
|
decls += [d]
|
|
|
|
elif ok == "type":
|
|
|
|
# ignore types of placeholder options
|
|
|
|
if ov != "_unspecified" or cur[ok] == "_unspecified":
|
|
|
|
cur[ok] = ov
|
|
|
|
elif ov is not None or cur.get(ok, None) is None:
|
|
|
|
cur[ok] = ov
|
|
|
|
|
2021-06-16 10:27:47 +00:00
|
|
|
severity = "error" if warningsAreErrors else "warning"
|
|
|
|
|
2021-11-18 23:26:27 +00:00
|
|
|
# check that every option has a description
|
2021-12-18 18:37:41 +00:00
|
|
|
hasWarnings = False
|
2022-09-01 17:23:59 +00:00
|
|
|
hasErrors = False
|
2021-11-18 23:26:27 +00:00
|
|
|
for (k, v) in options.items():
|
|
|
|
if v.value.get('description', None) is None:
|
2021-12-18 18:37:41 +00:00
|
|
|
hasWarnings = True
|
|
|
|
print(f"\x1b[1;31m{severity}: option {v.name} has no description\x1b[0m", file=sys.stderr)
|
2021-11-18 23:26:27 +00:00
|
|
|
v.value['description'] = "This option has no description."
|
2021-06-16 10:27:47 +00:00
|
|
|
if v.value.get('type', "unspecified") == "unspecified":
|
|
|
|
hasWarnings = True
|
|
|
|
print(
|
|
|
|
f"\x1b[1;31m{severity}: option {v.name} has no type. Please specify a valid type, see " +
|
|
|
|
"https://nixos.org/manual/nixos/stable/index.html#sec-option-types\x1b[0m", file=sys.stderr)
|
|
|
|
|
2022-09-01 17:23:59 +00:00
|
|
|
if hasErrors:
|
|
|
|
sys.exit(1)
|
2021-12-18 18:37:41 +00:00
|
|
|
if hasWarnings and warningsAreErrors:
|
|
|
|
print(
|
|
|
|
"\x1b[1;31m" +
|
|
|
|
"Treating warnings as errors. Set documentation.nixos.options.warningsAreErrors " +
|
|
|
|
"to false to ignore these warnings." +
|
|
|
|
"\x1b[0m",
|
|
|
|
file=sys.stderr)
|
|
|
|
sys.exit(1)
|
2021-11-18 23:26:27 +00:00
|
|
|
|
2023-01-23 17:37:33 +00:00
|
|
|
json.dump(unpivot(options), fp=sys.stdout)
|