about summary refs log tree commit diff
path: root/nixpkgs/pkgs/tools/graphics/vulkan-cts/vk-cts-sources.py
blob: f3e42bd82e7a0c56481ac532d72cada2e9e45ab7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env nix-shell
#!nix-shell -i python3 -p nix-prefetch-github -p git
#nix-shell -I nixpkgs=../../../../ -i python3 -p "python3.withPackages (ps: with ps; [ nix-prefetch-github ])" -p "git"

import json
import re
import subprocess
import sys

import fetch_sources

def get_github_hash(owner, repo, revision):
    result = subprocess.run(
        ["nix-prefetch-github", owner, repo, "--json", "--rev", revision],
        check=True,
        capture_output=True,
        text=True,
    )
    j = json.loads(result.stdout)
    # Remove False values
    return {k: v for k, v in j.items() if v}

def main():
    pkgs = fetch_sources.PACKAGES
    pkgs.sort(key = lambda pkg: pkg.baseDir)
    existing_sources = {}

    # Fetch hashes from existing sources file
    with open("sources.nix") as f:
        existing_file = f.read()

    source_re = re.compile("(?P<name>[^ ]+) = fetchFromGitHub[^\n]*\n"
        "[^\n]+\n" # owner
        "[^\n]+\n" # repo
        " *rev = \"(?P<rev>[^\"]+)\";\n"
        " *hash = \"(?P<hash>[^\"]+)\";\n"
    )

    for m in source_re.finditer(existing_file):
        if m.group("hash").startswith("sha"):
            print(f"Found {m.group('name')}: {m.group('rev')} -> {m.group('hash')}")
            existing_sources[m.group("name")] = (m.group("rev"), m.group("hash"))
    print()


    # Write new sources file
    with open("sources.nix", "w") as f:
        f.write("# Autogenerated from vk-cts-sources.py\n")
        f.write("{ fetchurl, fetchFromGitHub }:\n")
        f.write("rec {");

        github_re = re.compile("https://github.com/(?P<owner>[^/]+)/(?P<repo>[^/]+).git")

        for pkg in pkgs:
            if isinstance(pkg, fetch_sources.GitRepo):
                ms = github_re.match(pkg.httpsUrl)

                # Check for known hash
                hash = None
                if pkg.baseDir in existing_sources:
                    existing_src = existing_sources[pkg.baseDir]
                    if existing_src[0] == pkg.revision:
                        hash = existing_src[1]

                if hash is None:
                    print(f"Fetching {pkg.baseDir}: {pkg.revision}")
                    hash = get_github_hash(ms.group("owner"), ms.group("repo"), pkg.revision)["hash"]
                    print(f"Got {pkg.baseDir}: {pkg.revision} -> {hash}")

                f.write(f"\n  {pkg.baseDir} = fetchFromGitHub {{\n");
                f.write(f"    owner = \"{ms.group('owner')}\";\n");
                f.write(f"    repo = \"{ms.group('repo')}\";\n");
                f.write(f"    rev = \"{pkg.revision}\";\n");
                f.write(f"    hash = \"{hash}\";\n");
                f.write(f"  }};\n");

        f.write("\n\n  prePatch = ''\n");
        f.write("    mkdir -p");
        for pkg in pkgs:
            if isinstance(pkg, fetch_sources.GitRepo):
                f.write(f" external/{pkg.baseDir}")
        f.write("\n\n");

        for pkg in pkgs:
            if isinstance(pkg, fetch_sources.GitRepo):
                f.write(f"    cp -r ${{{pkg.baseDir}}} external/{pkg.baseDir}/{pkg.extractDir}\n");

        f.write("  '';\n");

        f.write("}\n");

if __name__ == "__main__":
    main()