-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkgreleaser.py
executable file
·93 lines (75 loc) · 2.8 KB
/
pkgreleaser.py
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 python3
import argparse
import json
import re
import subprocess
import os
from pathlib import Path
from typing import NamedTuple
class Package(NamedTuple):
name: str
version: str
revision: str
def run_nvchecker(entry: str) -> list[str]:
result = subprocess.run(
[
"uv",
"run",
"--with=git+https://github.com/lilydjwg/nvchecker@2722ccc7fef71fccf9f031d8299bc3c36736fdda",
"nvchecker",
"--entry",
entry,
"--logger=json",
"-c",
"nvchecker.toml"
],
check=True,
text=True,
capture_output=True,
)
return result.stdout.splitlines()
def parse_nvchecker_output(lines: list[str]) -> list[Package]:
nv_data = []
for line in lines:
data = json.loads(line)
nv_data.append(
Package(name=data["name"], version=data["version"], revision=data["revision"])
)
return nv_data
def process_package(package: Package) -> None:
dir_path = Path(package.name)
pkgbuild_path = dir_path / "PKGBUILD"
srcinfo_path = dir_path / ".SRCINFO"
content = pkgbuild_path.read_text()
if not content:
raise RuntimeError(f"Failed to read PKGBUILD for package {package.name}")
match = re.search(r"(?m)^pkgver=(.+)$", content)
if not match:
raise RuntimeError(f"pkgver not found in PKGBUILD for package {package.name}")
current_version = match.group(1).strip()
if current_version == package.version:
return
updated_content = re.sub(r"(?m)^pkgver=(.+)$", f"pkgver={package.version}", content)
updated_content = re.sub(r"(?m)^pkgrel=(.+)$", f"pkgrel=1", updated_content)
updated_content = re.sub(r"(?m)^_commit=(.+)$", f"_commit='{package.revision}'", updated_content)
pkgbuild_path.write_text(updated_content)
with srcinfo_path.open(mode="w") as f:
subprocess.run(["makepkg", "--printsrcinfo"], stdout=f, check=True, cwd=dir_path)
if "sums=('SKIP')" not in updated_content:
subprocess.run(["updpkgsums"], check=True, capture_output=True, cwd=dir_path)
print(f"Bump {package.name} from {current_version} to {package.version}")
def _directory(value):
if not os.path.isdir(value):
raise argparse.ArgumentTypeError(f"'{value}' is not a valid directory path.")
return value
def main():
parser = argparse.ArgumentParser(description="Process package version updates")
parser.add_argument("package", type=_directory, help="The name of the package to process")
args = parser.parse_args()
lines = run_nvchecker(args.package)
packages = parse_nvchecker_output(lines)
package = next((p for p in packages if p.name == args.package), None)
if package:
process_package(package)
if __name__ == "__main__":
main()