aboutsummaryrefslogtreecommitdiff
path: root/vcpkg/scripts/update_suitesparse.py
blob: 419683d53ae0ad67b0c2b62fc4950acb9835c27f (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3

# Usage: ./update_suitesparse.py <new_version>
#
# Updates the `suitesparse` port and all of its `suitesparse-*` sub-packages
# based on the source archive automatically downloaded of the given version.

import hashlib
import io
import json
import re
import sys
import tarfile
from pathlib import Path

import requests

ports_root = Path(__file__).resolve().parent.parent / "ports"


def download(url):
    print(f"Downloading {url}...")
    r = requests.get(url)
    r.raise_for_status()
    return r.content


def sha512(data):
    sha = hashlib.sha512()
    sha.update(data)
    return sha.hexdigest()


def extract_version(content):
    major = re.search(r"^set *\( *(\w+)_VERSION_MAJOR +(\d+) ", content, re.M).group(2)
    minor = re.search(r"^set *\( *(\w+)_VERSION_MINOR +(\d+) ", content, re.M).group(2)
    sub = re.search(r"^set *\( *(\w+)_VERSION_(?:SUB|PATCH|UPDATE) +(\d+) ", content, re.M).group(2)
    return f"{major}.{minor}.{sub}"


def load_versions(tar_gz_bytes):
    versions = {}
    tar_gz_file = io.BytesIO(tar_gz_bytes)
    with tarfile.open(fileobj=tar_gz_file, mode="r:gz") as tar:
        for member in tar.getmembers():
            if not member.isfile():
                continue
            if m := re.fullmatch(r"SuiteSparse-[^/]+/(\w+)/CMakeLists.txt", member.name):
                name = m.group(1)
                if name in ["Example", "GraphBLAS", "CSparse"]:
                    continue
                content = tar.extractfile(member).read().decode("utf8")
                versions[name] = extract_version(content)
            elif member.name.endswith("GraphBLAS_version.cmake"):
                content = tar.extractfile(member).read().decode("utf8")
                versions["GraphBLAS"] = extract_version(content)
    return versions


def update_manifest(pkg_name, version):
    port_dir = ports_root / pkg_name
    manifest_path = port_dir / "vcpkg.json"
    manifest = json.loads(manifest_path.read_text("utf8"))
    if manifest["version-semver"] == version:
        return False
    manifest["version-semver"] = version
    manifest_path.write_text(json.dumps(manifest, indent=2) + "\n")
    return True


def update_portfile(pkg_name, new_version, new_hash):
    port_dir = ports_root / pkg_name
    portfile_path = port_dir / "portfile.cmake"
    content = portfile_path.read_text("utf8")
    content, n = re.subn(r"\bREF v\S+", f"REF v{new_version}", content, re.M)
    if n != 1:
        raise Exception(f"Updating {pkg_name} portfile ref failed!")
    content, n = re.subn(r"\bSHA512 \S+", f"SHA512 {new_hash}", content, re.M)
    if n != 1:
        raise Exception(f"Updating {pkg_name} portfile hash failed!")
    portfile_path.write_text(content)


def update_port(pkg_name, new_version, suitesparse_hash):
    port_dir = ports_root / pkg_name
    if not port_dir.exists():
        raise Exception(f"'{pkg_name}' does not exist!")
    update_manifest(pkg_name, new_version)
    # Always update the tag in vcpkg_from_github() even if version has not changed
    # to avoid having to download multiple versions of the source archive.
    print(f"{pkg_name}: updating...")
    if pkg_name == "suitesparse-graphblas":
        url = f"https://github.com/DrTimothyAldenDavis/GraphBLAS/archive/refs/tags/v{new_version}.tar.gz"
        graphblas_hash = sha512(download(url))
        update_portfile(pkg_name, new_version, graphblas_hash)
    else:
        update_portfile(pkg_name, suitesparse_version, suitesparse_hash)


def main(suitesparse_version):
    suitesparse_url = (
        f"https://github.com/DrTimothyAldenDavis/SuiteSparse/archive/refs/tags/v{suitesparse_version}.tar.gz"
    )
    tar_gz_bytes = download(suitesparse_url)
    suitesparse_hash = sha512(tar_gz_bytes)
    print("Reading versions from CMakeLists.txt files...")
    versions = load_versions(tar_gz_bytes)
    for lib, new_version in versions.items():
        pkg_name = "suitesparse-config" if lib == "SuiteSparse_config" else "suitesparse-" + lib.lower()
        update_port(pkg_name, new_version, suitesparse_hash)
    update_manifest("suitesparse", suitesparse_version)
    print("Done!")


if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: ./update_suitesparse.py <new_version>")
        sys.exit(1)
    suitesparse_version = sys.argv[1]
    main(suitesparse_version)