lol

tree-sitter/update: Move json file output to python

Removes the second-to-last use of jq from the shell script.

+44 -19
+9 -3
pkgs/development/tools/parsing/tree-sitter/update.nix
··· 408 408 fetchImpl = passBinaries "fetchImpl-wrapped" { 409 409 curl = "${curl}/bin/curl"; 410 410 nix-prefetch-git = "${nix-prefetch-git}/bin/nix-prefetch-git"; 411 + inherit atomically-write; 411 412 } 412 413 (writers.writePython3 "fetchImpl" { 413 414 flakeIgnore = ["E501"]; ··· 434 435 ''; 435 436 436 437 outputDir = "${toString ./.}/grammars"; 438 + 437 439 update-all-grammars = writeShellScript "update-all-grammars.sh" '' 438 440 set -euo pipefail 439 441 echo "fetching list of grammars" 1>&2 ··· 445 447 ${forEachParallel 446 448 "repos-to-fetch" 447 449 (writeShellScript "fetch-repo" '' 448 - ${atomically-write} \ 449 - "${outputDir}/$(jq --raw-output --null-input '$ARGS.positional[0].name' --jsonargs "$1").json" \ 450 450 ${fetchImpl} fetch-repo "$1" 451 451 '') 452 - (lib.mapAttrsToList (name: attrs: attrs // { inherit name; }) allGrammars) 452 + (lib.mapAttrsToList 453 + (nixRepoAttrName: attrs: attrs // { 454 + inherit 455 + nixRepoAttrName 456 + outputDir; 457 + }) 458 + allGrammars) 453 459 } 454 460 ${atomically-write} \ 455 461 "${outputDir}/default.nix" \
+35 -16
pkgs/development/tools/parsing/tree-sitter/update_impl.py
··· 3 3 import subprocess as sub 4 4 import os 5 5 import sys 6 - from typing import Generator, Any, Literal 6 + from typing import Iterator, Any, Literal 7 7 8 8 debug: bool = True if os.environ.get("DEBUG", False) else False 9 9 Bin = str ··· 12 12 mode: str = sys.argv[1] 13 13 jsonArg: dict = json.loads(sys.argv[2]) 14 14 15 - Args = Generator[str, None, None] 15 + Args = Iterator[str] 16 16 17 17 18 18 def curl_github_args(token: str | None, url: str) -> Args: 19 19 """Query the github API via curl""" 20 + yield bins["curl"] 20 21 if not debug: 21 22 yield "--silent" 22 23 # follow redirects ··· 43 44 44 45 def nix_prefetch_git_args(url: str, version_rev: str) -> Args: 45 46 """Prefetch a git repository""" 47 + yield bins["nix-prefetch-git"] 46 48 if not debug: 47 49 yield "--quiet" 48 50 yield "--no-deepClone" ··· 52 54 yield version_rev 53 55 54 56 55 - def run_bin(cmd: str, args: Args) -> bytes: 56 - bin: Bin = bins[cmd] 57 - all = [bin] + list(args) 57 + def run_cmd(args: Args) -> bytes: 58 + all = list(args) 58 59 if debug: 59 60 print(all, file=sys.stderr) 60 61 return sub.check_output(all) 61 62 62 63 64 + Dir = str 65 + 66 + 67 + def atomically_write_args(to: Dir, cmd: Args) -> Args: 68 + yield bins["atomically-write"] 69 + yield to 70 + yield from cmd 71 + 72 + 63 73 def fetchRepo() -> None: 64 - """fetch the given repo and print its nix-prefetch output to stdout""" 74 + """fetch the given repo and write its nix-prefetch output to the corresponding grammar json file""" 65 75 match jsonArg: 66 - case {"orga": orga, "repo": repo}: 76 + case { 77 + "orga": orga, 78 + "repo": repo, 79 + "outputDir": outputDir, 80 + "nixRepoAttrName": nixRepoAttrName, 81 + }: 67 82 token: str | None = os.environ.get("GITHUB_TOKEN", None) 68 - out = run_bin( 69 - "curl", 83 + out = run_cmd( 70 84 curl_github_args( 71 85 token, 72 86 url=f"https://api.github.com/repos/{quote(orga)}/{quote(repo)}/releases/latest" ··· 84 98 sys.exit(f"git result for {orga}/{repo} did not have a `tag_name` field") 85 99 86 100 print(f"Fetching latest release ({release}) of {orga}/{repo} …", file=sys.stderr) 87 - res = run_bin( 88 - "nix-prefetch-git", 89 - nix_prefetch_git_args( 90 - url=f"https://github.com/{quote(orga)}/{quote(repo)}", 91 - version_rev=release 101 + res = run_cmd( 102 + atomically_write_args( 103 + os.path.join( 104 + outputDir, 105 + f"{nixRepoAttrName}.json" 106 + ), 107 + nix_prefetch_git_args( 108 + url=f"https://github.com/{quote(orga)}/{quote(repo)}", 109 + version_rev=release 110 + 111 + ) 92 112 ) 93 113 ) 94 114 sys.stdout.buffer.write(res) ··· 101 121 match jsonArg: 102 122 case {"orga": orga}: 103 123 token: str | None = os.environ.get("GITHUB_TOKEN", None) 104 - out = run_bin( 105 - "curl", 124 + out = run_cmd( 106 125 curl_github_args( 107 126 token, 108 127 url=f"https://api.github.com/orgs/{quote(orga)}/repos?per_page=100"