forked from linuxmint/cinnamon-spices-desklets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate-spice
executable file
·130 lines (110 loc) · 4.75 KB
/
validate-spice
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
#!/usr/bin/python3
import json
import os
import sys
import subprocess
from PIL import Image
if len(sys.argv) != 2:
print("Usage:")
print("./validate-spice uuid")
print("./validate-spice --all")
sys.exit(1)
SPICE_TYPE="desklet"
# function with checks for an xlet
def validate_xlet(uuid):
valid = True
os.chdir(uuid)
try:
# Check mandatory files
for file in ["info.json", "screenshot.png", "files/%s/metadata.json" % uuid, "files/%s/icon.png" % uuid]:
if not os.path.exists(file):
print("[%s] Missing file: %s" % (uuid, file))
valid = False
found = False
for root, dirs, files in os.walk("files/%s" % uuid):
for file in files:
if file == "%s.js" % SPICE_TYPE:
found = True
if not found:
print("[%s] Missing main %s.js" % (uuid, SPICE_TYPE))
# Check forbidden files
for file in ["icon.png"]:
if os.path.exists(file):
print("[%s] Forbidden file: %s" % (uuid, file))
valid = False
# Check mandatory directories
for directory in ["files", "files/%s" % uuid]:
if not os.path.isdir(directory):
print("[%s] Missing directory: %s" % (uuid, directory))
valid = False
return
# Check that there are no files in files other than the uuid directory
if len(os.listdir("files")) != 1:
print("[%s] The files directory should ONLY contain the $uuid directory!" % (uuid))
valid = False
# check json
with open("files/%s/metadata.json" % uuid) as file:
metadata = json.load(file)
# check forbidden fields
for field in ['icon', 'dangerous', 'last-edited']:
if field in metadata.keys():
print("[%s] Forbidden field '%s' in %s" % (uuid, field, "files/%s/metadata.json" % uuid))
valid = False
# check mandatory fields
for field in ['uuid', 'name', 'description']:
if field not in metadata.keys():
print("[%s] Missing field '%s' in %s" % (uuid, field, "files/%s/metadata.json" % uuid))
valid = False
return
# check uuid value
if metadata['uuid'] != uuid:
print("[%s] Wrong uuid in %s" % (uuid, "files/%s/metadata.json" % uuid))
valid = False
# check for unicode characters in metadata
for field in metadata.keys():
strvalue = str(metadata[field])
if len(strvalue.encode()) != len(strvalue):
print("[%s] Forbidden unicode characters in field '%s' in %s" % (uuid, field, "files/%s/metadata.json" % uuid))
valid = False
# check if icon is square
im = Image.open("files/%s/icon.png" % uuid)
(width, height) = im.size
if width != height:
print("[%s] icon.png has to be square." % uuid)
valid = False
# check po and pot files
podir = "files/%s/po" % uuid
if os.path.isdir(podir):
for file in os.listdir(podir):
if file.endswith(".po"):
pocheck = subprocess.run(["msgfmt", "-c", os.path.join(podir, file), "-o", "-"], stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True)
if pocheck.stderr:
print("[%s] The following errors were found in translation file '%s':\n%s"
"HINT: Most errors in translation file `%s` can usually be prevented and solved by using Poedit.\n"
% (uuid, file, pocheck.stderr, file)
)
valid = False
elif file.endswith(".pot"):
potcheck = subprocess.run(["xgettext", os.path.join(podir, file), "-o", "-"], stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True)
if potcheck.stderr:
print("[%s] The following errors were found in translation template '%s':\n%s" % (uuid, file, potcheck.stderr))
valid = False
except Exception as error:
print("[%s] Unknown error. %s" % (uuid, error))
os.chdir("..")
return (valid)
def quit(valid):
if (valid):
print ("No errors found. Everything looks good.")
sys.exit(0)
else:
print ("Errors were found!")
sys.exit(1)
if sys.argv[1] == "--all":
valid = True
for uuid in os.listdir("."):
if os.path.isdir(uuid) and not uuid.startswith("."):
valid = validate_xlet(uuid) and valid
else:
valid = validate_xlet(sys.argv[1])
quit(valid)