Skip to content

Commit 8da8195

Browse files
committed
ENH: add image helper CLI tool
Copied from the demo repo
1 parent a5fb373 commit 8da8195

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

tools/manage_baseline_images.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import argparse
2+
import json
3+
from pathlib import Path
4+
import sys
5+
import time
6+
7+
import matplotlib.testing.decorators as mtd
8+
9+
10+
def _mod_to_path(libpath, mod):
11+
return Path(libpath) / Path(*mod.split("."))
12+
13+
14+
def _find_imagelist(libpath, mod, imagelist_name="image_list.txt"):
15+
return Path(libpath) / Path(*mod.split(".")[:-1]) / imagelist_name
16+
17+
18+
def _add(args):
19+
image_list_path = _find_imagelist(args.libpath, args.module)
20+
data = mtd._load_imagelist(image_list_path)
21+
fname = Path(args.module.split(".")[-1]) / args.fname
22+
if fname in data:
23+
raise RuntimeError("Trying to add as existing file, did you mean to use 'rev'?")
24+
data[fname] = {"rev": 0, "ts": time.time()}
25+
mtd._write_imagelist(data, target_file=image_list_path)
26+
27+
28+
def _rev(args):
29+
image_list_path = _find_imagelist(args.libpath, args.module)
30+
data = mtd._load_imagelist(image_list_path)
31+
fname = Path(args.module.split(".")[-1]) / args.fname
32+
if fname not in data:
33+
raise RuntimeError(
34+
"Trying to rev a non-existing file, did you mean to use 'add'?"
35+
)
36+
data[fname]["rev"] += 1
37+
data[fname]["ts"] = time.time()
38+
mtd._write_imagelist(data, target_file=image_list_path)
39+
40+
41+
def _validate(args):
42+
image_list_path = _find_imagelist(args.libpath, args.package + ".a")
43+
data = mtd._load_blame(image_list_path)
44+
json_path = (
45+
Path(args.baseline_path)
46+
/ Path(*args.package.split("."))
47+
/ "baseline_images"
48+
/ "metadata.json"
49+
)
50+
with open(json_path) as fin:
51+
md = {Path(k): v for k, v in json.load(fin).items()}
52+
53+
if extra := set(md) ^ set(data):
54+
# TODO good error messages about where the extra files are
55+
print(f"{extra=}")
56+
sys.exit(1)
57+
58+
mismatch = set()
59+
for k in md:
60+
if md[k]["sha"] != data[k]["sha"]:
61+
mismatch.add(k)
62+
if mismatch:
63+
print(f"{mismatch=}")
64+
sys.exit(1)
65+
66+
67+
if __name__ == "__main__":
68+
# create the top-level parser
69+
parser = argparse.ArgumentParser(prog="manage baseline images")
70+
parser.add_argument(
71+
"--libpath",
72+
help="Relative path to package source.",
73+
default="",
74+
required=False,
75+
)
76+
77+
subparsers = parser.add_subparsers(help="sub-command help", dest="cmd")
78+
79+
# create the parser for the "rev" command
80+
parser_rev = subparsers.add_parser("rev", help="Version rev a test file.")
81+
parser_rev.add_argument("module", type=str, help="The dotted name of the module.")
82+
parser_rev.add_argument(
83+
"fname", type=str, help="The (relative) name of the file to version rev."
84+
)
85+
86+
# create the parser for the "add" command
87+
parser_add = subparsers.add_parser("add", help="Add a new baseline image.")
88+
parser_add.add_argument("module", type=str, help="The dotted name of the module.")
89+
parser_add.add_argument(
90+
"fname", type=str, help="The (relative) name of the file to version rev."
91+
)
92+
93+
# create the parser for the "add" command
94+
parser_add = subparsers.add_parser("validate", help="Check if the baseline dir .")
95+
parser_add.add_argument(
96+
"package", type=str, help="The dotted name of the test (sub-)package."
97+
)
98+
parser_add.add_argument(
99+
"baseline_path",
100+
type=str,
101+
help="The dotted name of the test (sub-)package.",
102+
)
103+
104+
# parse some argument lists
105+
args = parser.parse_args()
106+
107+
{"add": _add, "rev": _rev, "validate": _validate}[args.cmd](args)

0 commit comments

Comments
 (0)