-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathverify.py
85 lines (72 loc) · 2.51 KB
/
verify.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
""" Simple audit script for carousel slides, CI friendly. """
from __future__ import print_function
import glob
import json
import os
import pprint
import sys
import traceback
def verify():
"""
Loads master list of all slides, verifies that the JSON is sane and
the images are not missing.
"""
# Python 2/3 compatibility for FileNotFoundError.
try:
FileNotFoundError
except:
FileNotFoundError = IOError
try:
file = json.load(open("json/slides.json", "r"))
except FileNotFoundError:
print("Error: Main slide file not found.")
sys.exit(1)
except json.decoder.JSONDecodeError:
print("Error: Main slide file has a JSON error, fix file.")
print(traceback.format_exc())
sys.exit(1)
except Exception:
print("Error: Unknown error")
sys.exit(1)
missing_images = []
missing_metadata = []
invalid_mask = []
slide_files = glob.glob("images/*.*")
for slide in file:
# Check if we're missing files
if "image" in slide and not os.path.isfile("images/" + slide["image"]):
missing_images.append(slide["image"])
if "video" in slide and not os.path.isfile("images/" + slide["video"]):
missing_images.append(slide["video"])
# Check if we've got messed up metadata
if not "image" in slide and not "video" in slide:
missing_metadata.append(slide)
elif any([not x in slide for x in ["title", "caption", "weight"]]):
missing_metadata.append(slide)
elif "mask" in slide and slide["mask"] not in ["light", "medium", "strong"]:
invalid_mask.append(slide["title"])
# Remove files from list of files -- if any are left, they are unused
try:
if "image" in slide:
slide_files.remove("images/%s" % slide["image"])
elif "video" in slide:
slide_files.remove("images/%s" % slide["video"])
except ValueError:
pass
if slide_files:
print("Warning: Some images are unused.")
pprint.pprint(slide_files)
if missing_images:
print("Error: Some missing images")
pprint.pprint(missing_images)
sys.exit(1)
if missing_metadata:
print("Error: Some missing metadata")
pprint.pprint(missing_metadata)
sys.exit(1)
if invalid_mask:
print("Error: invalid visual masks")
pprint.pprint(invalid_mask)
sys.exit(1)
if __name__ == "__main__":
verify()