@@ -2,14 +2,16 @@
22#!nix-shell -p nix -p python3 -p git -i python
33# USAGE - just run the script: ./update.py
44# When editing this file, make also sure it passes the mypy typecheck
5−# and is formatted with yapf.
6−import urllib.request
7−import json
8−import tempfile
9−import subprocess
5+# and is formatted with black.
106import fileinput
7+import json
118import re
9+import subprocess
10+import tempfile
11+import urllib.request
12+from datetime import datetime
1213from pathlib import Path
14+from typing import Dict
1315
1416
1517def sh(*args: str) -> str:
@@ -18,50 +20,107 @@ def sh(*args: str) -> str:
1820
1921
2022def prefetch_github(owner: str, repo: str, ref: str) -> str:
21− return sh("nix-prefetch-url", "--unpack",
22− f"https://github.com/{owner}/{repo}/archive/{ref}.tar.gz")
23+ return sh(
24+ "nix-prefetch-url",
25+ "--unpack",
26+ f"https://github.com/{owner}/{repo}/archive/{ref}.tar.gz",
27+ )
2328
2429
25−def main() -> None:
30+def get_radare2_rev() -> str:
2631 url = "https://api.github.com/repos/radare/radare2/releases/latest"
2732 with urllib.request.urlopen(url) as response:
2833 release = json.load(response) # type: ignore
29− version = release["tag_name"]
30− with tempfile.TemporaryDirectory() as dirname:
34+ return release["tag_name"]
35+
36+
37+def get_r2_cutter_rev() -> str:
38+ url = "https://api.github.com/repos/radareorg/cutter/contents/"
39+ with urllib.request.urlopen(url) as response:
40+ data = json.load(response) # type: ignore
41+ for entry in data:
42+ if entry["name"] == "radare2":
43+ return entry["sha"]
44+ raise Exception("no radare2 submodule found in github.com/radareorg/cutter")
45+
3146
32− def git(*args: str) -> str:
33− return sh("git", "-C", dirname, *args)
47+def git(dirname: str, *args: str) -> str:
48+ return sh("git", "-C", dirname, *args)
3449
35− git("clone", "--branch", version, "https://github.com/radare/radare2",
36− ".")
37− sha256 = prefetch_github("radare", "radare2", version)
50+
51+def get_repo_info(dirname: str, rev: str) -> Dict[str, str]:
52+ sha256 = prefetch_github("radare", "radare2", rev)
53+
54+ cs_tip = None
55+ with open(Path(dirname).joinpath("shlr", "Makefile")) as makefile:
56+ for l in makefile:
57+ match = re.match("CS_TIP=(\S+)", l)
58+ if match:
59+ cs_tip = match.group(1)
60+ assert cs_tip is not None
61+
62+ cs_sha256 = prefetch_github("aquynh", "capstone", cs_tip)
63+
64+ return dict(
65+ rev=rev,
66+ sha256=sha256,
67+ version_commit=git(dirname, "rev-list", "--all", "--count"),
68+ gittap=git(dirname, "describe", "--tags", "--match", "[0-9]*"),
69+ gittip=git(dirname, "rev-parse", "HEAD"),
70+ cs_tip=cs_tip,
71+ cs_sha256=cs_sha256,
72+ )
73+
74+
75+def write_package_expr(version: str, info: Dict[str, str]) -> str:
76+ return f"""generic {{
77+ version_commit = "{info["version_commit"]}";
78+ gittap = "{info["gittap"]}";
79+ gittip = "{info["gittip"]}";
80+ rev = "{info["rev"]}";
81+ version = "{version}";
82+ sha256 = "{info["sha256"]}";
83+ cs_tip = "{info["cs_tip"]}";
84+ cs_sha256 = "{info["cs_sha256"]}";
85+ }}"""
86+
87+
88+def main() -> None:
89+ radare2_rev = get_radare2_rev()
90+ r2_cutter_rev = get_r2_cutter_rev()
91+
92+ with tempfile.TemporaryDirectory() as dirname:
93+ git(
94+ dirname,
95+ "clone",
96+ "--branch",
97+ radare2_rev,
98+ "https://github.com/radare/radare2",
99+ ".",
100+ )
38101 nix_file = str(Path(__file__).parent.joinpath("default.nix"))
39102
40− cs_tip = None
41− with open(Path(dirname).joinpath("shlr", "Makefile")) as makefile:
42− for l in makefile:
43− match = re.match("CS_TIP=(\S+)", l)
44− if match:
45− cs_tip = match.group(1)
46− assert cs_tip is not None
103+ radare2_info = get_repo_info(dirname, radare2_rev)
104+
105+ git(dirname, "checkout", r2_cutter_rev)
106+
107+ timestamp = git(dirname, "log", "-n1", "--format=%at")
108+ r2_cutter_version = datetime.fromtimestamp(int(timestamp)).strftime("%Y-%m-%d")
47109
48− cs_sha256 = prefetch_github("aquynh", "capstone", cs_tip)
110+ r2_cutter_info = get_repo_info(dirname, r2_cutter_rev)
49111
50112 in_block = False
51113 with fileinput.FileInput(nix_file, inplace=True) as f:
52114 for l in f:
53115 if "#<generated>" in l:
54116 in_block = True
55− print(f""" #<generated>
117+ print(
118+ f""" #<generated>
56119 # DO NOT EDIT! Automatically generated by ./update.py
57− version_commit = "{git("rev-list", "--all", "--count")}";
58− gittap = "{git("describe", "--tags", "--match", "[0-9]*")}";
59− gittip = "{git("rev-parse", "HEAD")}";
60− version = "{version}";
61− sha256 = "{sha256}";
62− cs_tip = "{cs_tip}";
63− cs_sha256 = "{cs_sha256}";
64− #</generated>""")
120+ radare2 = {write_package_expr(radare2_rev, radare2_info)};
121+ r2-for-cutter = {write_package_expr(r2_cutter_version, r2_cutter_info)};
122+ #</generated>"""
123+ )
65124 elif "#</generated>" in l:
66125 in_block = False
67126 elif not in_block: