/
bundle-tree.py
40 lines (30 loc) · 1.02 KB
/
bundle-tree.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
#!/usr/bin/env python
import argparse
import os.path
import shutil
import json
import sys
import os
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
"structured_attrs", help="Path to structured attrs file as exported by Nix __structuredAttrs"
)
arg_parser.add_argument("output_dir", help="Output directory")
def write_tree(output_dir: str, tree: dict) -> None:
os.mkdir(output_dir)
for name, node in tree.items():
output_file = os.path.join(output_dir, name)
if node["type"] == "derivation":
shutil.copy(node["value"], output_file)
elif node["type"] == "string":
with open(output_file, "w") as f:
f.write(node["value"])
elif node["type"] == "set":
write_tree(output_file, node["value"])
else:
raise ValueError(node["type"])
if __name__ == "__main__":
args = arg_parser.parse_args()
with open(args.structured_attrs) as f:
attrs = json.load(f)
write_tree(args.output_dir, attrs["tree"])