frigate.web: fix build on Node 22.12, add missing meta (#375967)
[NixPkgs.git] / pkgs / applications / editors / emacs / elisp-packages / manual-packages / tree-sitter-langs / update-defaults.py
blob128ce66dda8bcc3f16d61a2e2a611901e70d998a
1 #!/usr/bin/env nix-shell
2 #! nix-shell ../../../../../../../. -i python3 -p python3 -p nix
4 from os.path import (
5 dirname,
6 abspath,
7 join,
9 from typing import (
10 List,
11 Any,
13 import subprocess
14 import json
15 import sys
16 import os
19 def fmt_grammar(grammar: str) -> str:
20 return "tree-sitter-" + grammar
23 def eval_expr(nixpkgs: str, expr: str) -> Any:
24 p = subprocess.run(
26 "nix-instantiate",
27 "--json",
28 "--eval",
29 "--expr",
30 ("with import %s {}; %s" % (nixpkgs, expr)),
32 check=True,
33 stdout=subprocess.PIPE,
35 return json.loads(p.stdout)
38 def check_grammar_exists(nixpkgs: str, grammar: str) -> bool:
39 return eval_expr(
40 nixpkgs, f'lib.hasAttr "{fmt_grammar(grammar)}" tree-sitter-grammars'
44 def build_attr(nixpkgs, attr: str) -> str:
45 return (
46 subprocess.run(
47 ["nix-build", "--no-out-link", nixpkgs, "-A", attr],
48 check=True,
49 stdout=subprocess.PIPE,
51 .stdout.decode()
52 .strip()
56 if __name__ == "__main__":
57 cwd = dirname(abspath(__file__))
58 nixpkgs = abspath(join(cwd, "../../../../../.."))
60 src_dir = build_attr(nixpkgs, "emacs.pkgs.tree-sitter-langs.src")
62 existing: List[str] = []
64 grammars = os.listdir(join(src_dir, "repos"))
65 for g in grammars:
66 exists = check_grammar_exists(nixpkgs, g)
67 if exists:
68 existing.append(fmt_grammar(g))
69 else:
70 sys.stderr.write("Missing grammar: " + fmt_grammar(g) + "\n")
71 sys.stderr.flush()
73 with open(join(cwd, "default-grammars.json"), mode="w") as f:
74 json.dump(sorted(existing), f, indent=2)
75 f.write("\n")