-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffing.py
executable file
·194 lines (167 loc) · 5.41 KB
/
diffing.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""
usage:
diffing ".#bash"
"""
import argparse
import json
import os
import subprocess
import sys
import textwrap
from pathlib import Path
def is_strict_already(attr: str) -> bool:
"""check if strictDeps is enabled already"""
strict_nixpkgs = Path.cwd()
os.chdir(strict_nixpkgs)
strict_deps_status = (
subprocess.check_output(
[
"nix",
"eval",
"--impure",
"--expr",
"with import ./. {}; "
f"if ({attr} ? strictDeps) then {attr}.strictDeps else false",
]
)
.decode()
.strip()
)
if strict_deps_status == "true":
return True
return False
def get_outputs_strict(p_r: int, attr: str) -> bytes:
strict_expr = Path("@strictexpr@")
if p_r != 0:
try:
output_strict_1 = subprocess.run(
[
"nixpkgs-review",
"pr",
str(p_r),
"-p",
attr,
"--run",
f"echo -n 'STOREPATH=' && readlink -f ./results/{attr}",
],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
except subprocess.CalledProcessError as exc:
print("nixpkgs-review failed", exc.returncode, exc.output)
sys.exit(1)
output_strict_2 = subprocess.run(
["grep", "^STOREPATH"],
check=True,
input=output_strict_1.stdout,
stdout=subprocess.PIPE,
)
output_strict_3 = subprocess.run(
["sed", "s/STOREPATH=//"],
check=True,
input=output_strict_2.stdout,
stdout=subprocess.PIPE,
)
output_strict_4 = subprocess.run(
["nix", "derivation", "show", "--stdin"],
check=True,
input=output_strict_3.stdout,
stdout=subprocess.PIPE,
)
# for converting 'nix derivation show' output to 'nix build --json' compatible output
# derivation show '.[] | .outputs'
# { "out": { "path": "/nix/store/..."} }
# nix build '.[] | .outputs'
# { "out": "/nix/store/..." }
output_strict = subprocess.check_output(
["jq", ".[] | .outputs | [{ outputs: map_values(.path)}]"],
input=output_strict_4.stdout,
)
else:
output_strict = subprocess.check_output(
[
"nix",
"build",
"--json",
"--impure",
"-f",
strict_expr,
]
)
return output_strict
def get_outputs(
attr: str, nixgits: Path, p_r: int
) -> tuple[dict[str, str], dict[str, str]]:
"""get json from nix build and convert it class objects"""
nixpkgs = Path(f"{nixgits}/nixpkgs")
strict_nixpkgs = Path.cwd()
os.environ["diffNixpkgs"] = str(nixpkgs)
os.environ["diffStrictNixpkgs"] = str(strict_nixpkgs)
os.environ["diffAttr"] = attr
expr = Path("@expr@")
output_strict = get_outputs_strict(p_r, attr)
output = subprocess.check_output(
[
"nix",
"build",
"--json",
"--impure",
"-f",
expr,
]
)
output_strict_dict: dict[str, str] = json.loads(output_strict.strip())[0]["outputs"]
output_dict: dict[str, str] = json.loads(output.strip())[0]["outputs"]
return output_dict, output_strict_dict
def main() -> None:
"""main"""
nixgits = os.getenv("NIXGITS") or f"{os.getenv('HOME')}/nixgits"
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent(
"""\
usage:
diffing ".#bash"
diffing --file filecontainingattrs
diffing ".#bash" --pr *prnumber*
"""
),
)
parser.add_argument("attrs", nargs="*")
parser.add_argument("--file", help="a file containing attrs")
parser.add_argument("--pr", help="pr to check, diffed as the strict nixpkgs")
parser.add_argument(
"--force",
help="diff even if strictDeps is already enabled",
action="store_true",
)
args = parser.parse_args()
attrs: list[str] = []
if args.file:
attrs = Path(args.file).read_text(encoding="UTF-8").splitlines()
else:
attrs = [a.replace(".#", "") for a in args.attrs]
p_r = args.pr or 0
print(f"{attrs}\n")
for attr in attrs:
# when pr is specified there should be no checking
if p_r == 0 and not args.force:
if is_strict_already(attr):
txt = f"{attr} has strictDeps enabled already!".center(100, "-")
print(txt)
print()
continue
outputs, outputs_strict = get_outputs(attr, Path(nixgits), p_r)
for output_name, output_path in outputs.items():
outputs_strict_path = outputs_strict[output_name]
txt = f"diffing output {output_name} of {attr}".center(100, "-")
print(txt)
print(f"normal: {output_path}")
print(f"strict: {outputs_strict_path}")
print()
subprocess.run(
["@diffoscope@", output_path, outputs_strict_path], check=False
)
if __name__ == "__main__":
main()