about summary refs log tree commit diff
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2022-09-06 21:11:50 +0200
committerProfpatsch <mail@profpatsch.de>2022-11-04 18:14:33 +0100
commit0d067c8603cde7118fbe11b806b718d00c78d493 (patch)
tree88fc6f95b2746b797a8f501462785574b3c09418
parent673676319bfd3850ed6e4697f62d7969421ef47b (diff)
tree-sitter/update: move pyhon impl into its own file
This helps with syntax highlighting & editor support
-rw-r--r--pkgs/development/tools/parsing/tree-sitter/update.nix86
-rw-r--r--pkgs/development/tools/parsing/tree-sitter/update_impl.py83
2 files changed, 84 insertions, 85 deletions
diff --git a/pkgs/development/tools/parsing/tree-sitter/update.nix b/pkgs/development/tools/parsing/tree-sitter/update.nix
index d4b66f83beff6..8181280df7edc 100644
--- a/pkgs/development/tools/parsing/tree-sitter/update.nix
+++ b/pkgs/development/tools/parsing/tree-sitter/update.nix
@@ -410,91 +410,7 @@ let
   # implementation of the fetching of repo information from github
   fetchImpl = writers.writePython3 "fetchImpl" {
     flakeIgnore = ["E501"];
-  } ''
-    from urllib.parse import quote
-    import json
-    import subprocess as sub
-    import os
-    import sys
-
-    debug = True if os.environ.get("DEBUG", False) else False
-
-    mode = sys.argv[1]
-    jsonArg = json.loads(sys.argv[2])
-
-
-    def curl_github_args(token, url):
-        """Query the github API via curl"""
-        yield "curl"
-        if not debug:
-            yield "--silent"
-        if token:
-            yield "-H"
-            yield f"Authorization: token {token}"
-        yield url
-
-
-    def curl_result(orga, repo, output):
-        """Parse the curl result of the github API"""
-        res = json.loads(output)
-        message = res.get("message", "")
-        if "rate limit" in message:
-            sys.exit("Rate limited by the Github API")
-        if "Not Found" in message:
-            # repository not there or no releases; if the repo is missing,
-            # we’ll notice when we try to clone it
-            return {}
-        return res
-
-
-    def nix_prefetch_args(url, version_rev):
-        """Prefetch a git repository"""
-        yield "nix-prefetch-git"
-        if not debug:
-            yield "--quiet"
-        yield "--no-deepClone"
-        yield "--url"
-        yield url
-        yield "--rev"
-        yield version_rev
-
-
-    def fetchRepo():
-        """fetch the given repo and print its nix-prefetch output to stdout"""
-        match jsonArg:
-            case {"orga": orga, "repo": repo}:
-                token = os.environ.get("GITHUB_TOKEN", None)
-                curl_cmd = list(curl_github_args(
-                    token,
-                    url=f"https://api.github.com/repos/{quote(orga)}/{quote(repo)}/releases/latest"
-                ))
-                if debug:
-                    print(curl_cmd, file=sys.stderr)
-                out = sub.check_output(curl_cmd)
-                release = curl_result(orga, repo, out).get("tag_name", None)
-
-                # github sometimes returns an empty list even tough there are releases
-                if not release:
-                    print(f"uh-oh, latest for {orga}/{repo} is not there, using HEAD", file=sys.stderr)
-                    release = "HEAD"
-
-                print(f"Fetching latest release ({release}) of {orga}/{repo} …", file=sys.stderr)
-                sub.check_call(
-                    list(nix_prefetch_args(
-                        url=f"https://github.com/{quote(orga)}/{quote(repo)}",
-                        version_rev=release
-                    ))
-                )
-            case _:
-                sys.exit("input json must have `orga` and `repo` keys")
-
-
-    match mode:
-        case "fetch-repo":
-            fetchRepo()
-        case _:
-            sys.exit(f"mode {mode} unknown")
-  '';
+  } ./update_impl.py;
 
   # find the latest repos of a github organization
   latestGithubRepos = { orga }: writeShellScript "latest-github-repos" ''
diff --git a/pkgs/development/tools/parsing/tree-sitter/update_impl.py b/pkgs/development/tools/parsing/tree-sitter/update_impl.py
new file mode 100644
index 0000000000000..63ace17c3583a
--- /dev/null
+++ b/pkgs/development/tools/parsing/tree-sitter/update_impl.py
@@ -0,0 +1,83 @@
+from urllib.parse import quote
+import json
+import subprocess as sub
+import os
+import sys
+
+debug = True if os.environ.get("DEBUG", False) else False
+
+mode = sys.argv[1]
+jsonArg = json.loads(sys.argv[2])
+
+
+def curl_github_args(token, url):
+    """Query the github API via curl"""
+    yield "curl"
+    if not debug:
+        yield "--silent"
+    if token:
+        yield "-H"
+        yield f"Authorization: token {token}"
+    yield url
+
+
+def curl_result(orga, repo, output):
+    """Parse the curl result of the github API"""
+    res = json.loads(output)
+    message = res.get("message", "")
+    if "rate limit" in message:
+        sys.exit("Rate limited by the Github API")
+    if "Not Found" in message:
+        # repository not there or no releases; if the repo is missing,
+        # we’ll notice when we try to clone it
+        return {}
+    return res
+
+
+def nix_prefetch_args(url, version_rev):
+    """Prefetch a git repository"""
+    yield "nix-prefetch-git"
+    if not debug:
+        yield "--quiet"
+    yield "--no-deepClone"
+    yield "--url"
+    yield url
+    yield "--rev"
+    yield version_rev
+
+
+def fetchRepo():
+    """fetch the given repo and print its nix-prefetch output to stdout"""
+    match jsonArg:
+        case {"orga": orga, "repo": repo}:
+            token = os.environ.get("GITHUB_TOKEN", None)
+            curl_cmd = list(curl_github_args(
+                token,
+                url=f"https://api.github.com/repos/{quote(orga)}/{quote(repo)}/releases/latest"
+            ))
+            if debug:
+                print(curl_cmd, file=sys.stderr)
+            out = sub.check_output(curl_cmd)
+            release = curl_result(orga, repo, out).get("tag_name", None)
+
+            # github sometimes returns an empty list even tough there are releases
+            if not release:
+                print(f"uh-oh, latest for {orga}/{repo} is not there, using HEAD", file=sys.stderr)
+                release = "HEAD"
+
+            print(f"Fetching latest release ({release}) of {orga}/{repo} …", file=sys.stderr)
+            sub.check_call(
+                list(nix_prefetch_args(
+                    url=f"https://github.com/{quote(orga)}/{quote(repo)}",
+                    version_rev=release
+                ))
+            )
+        case _:
+            sys.exit("input json must have `orga` and `repo` keys")
+
+
+match mode:
+    case "fetch-repo":
+        fetchRepo()
+    case _:
+        sys.exit(f"mode {mode} unknown")