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
|
#! /usr/bin/env nix-shell
#! nix-shell -i python3 --packages python3 nix-prefetch-git
import argparse
import json
import subprocess
from pathlib import Path
def fetch_git_hash(url: str, rev: str) -> str:
result = subprocess.run(
["nix-prefetch-git", "--fetch-submodules", "--url", url, "--rev", rev],
capture_output=True,
text=True,
check=True,
)
return json.loads(result.stdout)["hash"]
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"-i",
"--input",
type=Path,
default=Path(__file__).parent.resolve() / "pubspec.lock.json",
)
parser.add_argument("-o", "--output", type=Path, default=None)
args = parser.parse_args()
output_file: Path | None = args.output
output: dict[str, str] = {}
for name, info in json.loads(args.input.read_text()).get("packages", {}).items():
if info.get("source") != "git":
continue
desc = info.get("description")
if not isinstance(desc, dict):
continue
url = desc.get("url")
rev = desc.get("resolved-ref")
if not (isinstance(url, str) and isinstance(rev, str)):
continue
output[name] = fetch_git_hash(url, rev)
output_json = json.dumps(output, indent=2, sort_keys=True) + "\n"
if output_file:
output_file.write_text(output_json)
else:
print(output_json, end="")
if __name__ == "__main__":
main()
|