forked from Delegation-numerique-en-sante/mesconseilscovid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.py
executable file
·247 lines (215 loc) · 7.76 KB
/
check.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python3
import json
import time
from html.parser import HTMLParser
from http import HTTPStatus
from pathlib import Path
import httpx
from minicli import cli, run
from build import each_folder_from, each_file_from
HERE = Path(__file__).parent
SRC_DIR = HERE / "src"
DIAGRAMMES_DIR = HERE / "diagrammes"
CONTENUS_DIR = HERE / "contenus"
class LinkExtractor(HTMLParser):
def reset(self):
HTMLParser.reset(self)
self.links = set()
def handle_starttag(self, tag, attrs):
if tag == "a":
attrs = dict(attrs)
url = attrs["href"]
if url.startswith("http"):
if url.startswith(
(
"https://www.facebook.com/",
"https://twitter.com/",
"https://github.com/",
)
):
return
self.links.add(url)
@cli
def links(timeout: int = 10, delay: float = 0.1):
dynamically_generated_links_via_injection = {
"https://www.sante.fr/cf/centres-vaccination-covid/departement-22-cotes-d'armor.html",
"https://www.sante.fr/cf/centres-vaccination-covid/departement-20A-corse-du-sud.html",
"https://www.sante.fr/cf/centres-vaccination-covid/departement-01-ain.html",
"https://www.sante.fr/cf/centres-vaccination-covid.html",
"https://www.sante.fr/cf/centres-depistage-covid/departement-01.html",
"https://www.sante.fr/cf/centres-depistage-covid/departement-2A.html",
"https://www.sante.fr/cf/centres-depistage-covid.html",
}
parser = LinkExtractor()
content = (SRC_DIR / "index.html").read_text()
parser.feed(content)
links = parser.links.union(dynamically_generated_links_via_injection)
for link in sorted(links):
print(link)
with httpx.stream(
"GET",
link,
timeout=timeout,
verify=False, # ignore SSL certificate validation errors
) as response:
if response.status_code == HTTPStatus.TOO_MANY_REQUESTS:
print("Warning: we’re being throttled, skipping link (429)")
continue
if response.status_code != HTTPStatus.OK:
raise Exception(f"{link} is broken! ({response.status_code})")
time.sleep(delay) # avoid being throttled
@cli
def versions():
content = open(HERE / "static" / "version.json").read()
data = json.loads(content)
version = data["version"]
line_prefix = "const CACHE_NAME = "
for line in open(SRC_DIR / "service-worker.js"):
if line.startswith(line_prefix):
break
if version not in line:
raise Exception(
f"Version mismatch between version.json ({version}) and service-worker.js"
)
@cli
def service_worker():
# Retrieving the list from CACHE_FILES.
sw_filenames = set()
start = False
for line in open(SRC_DIR / "service-worker.js"):
# Parsing a JS file in Python, what could potentially go wrong?
if line.startswith("const CACHE_FILES = ["):
start = True
continue
if start:
sw_filenames.add(line.strip()[1:-2])
if line.startswith("]"):
break
# Make sure the cached files exist.
for filename in sw_filenames:
if not (
(Path("src") / filename).exists() or (Path("static") / filename).exists()
):
raise Exception(f"Non-existent file in service-worker.js: {filename}")
REQUIRED_FILES = {"/", "style.css", "scripts/main.js", "favicon.ico"}
KNOWN_EXCLUDED_FILES = {
"browserconfig.xml",
"illustrations/mesconseilscovid.png",
"illustrations/isolement-sans-symptomes.png",
"illustrations/isolement-foyer-malade.png",
"index.html",
"logo.png",
"logo-favicon.png",
"service-worker.js",
"template.html",
"version.json",
}
sw_filenames |= KNOWN_EXCLUDED_FILES
# Make sure the required files are present.
if not REQUIRED_FILES.issubset(sw_filenames):
raise Exception(
f"File(s) missing in service-worker.js: {REQUIRED_FILES - sw_filenames}"
)
# Compare the list to static files.
static_file_names = {
filename
for file_path, filename in each_file_from(
HERE / "static", exclude=[".DS_Store"]
)
}
if not static_file_names.issubset(sw_filenames):
raise Exception(
f"File(s) missing in service-worker.js: {static_file_names - sw_filenames}"
)
# Compare the list to font files.
fonts_file_names = {
f"fonts/{filename}"
for file_path, filename in each_file_from(
SRC_DIR / "fonts", pattern="*.woff2", exclude=[".DS_Store"]
)
}
if not fonts_file_names.issubset(sw_filenames):
raise Exception(
f"File(s) missing in service-worker.js: {fonts_file_names - sw_filenames}"
)
# Compare the list to illustration files.
illustrations_file_names = {
f"illustrations/{filename}"
for file_path, filename in each_file_from(
SRC_DIR / "illustrations",
exclude=[".DS_Store"],
)
}
if not illustrations_file_names.issubset(sw_filenames):
raise Exception(
f"File(s) missing in service-worker.js: {illustrations_file_names - sw_filenames}"
)
# Compare the list to src files.
src_file_names = {
filename
for file_path, filename in each_file_from(
SRC_DIR,
pattern="*.*",
exclude=[".DS_Store"],
)
}
if not src_file_names.issubset(sw_filenames):
raise Exception(
f"File(s) missing in service-worker.js: {src_file_names - sw_filenames}"
)
@cli
def orphelins():
template = (SRC_DIR / "template.html").read_text()
for folder in each_folder_from(CONTENUS_DIR, exclude=["nouveaux_contenus"]):
for file_path, filename in each_file_from(
folder, pattern="*.md", exclude=["README.md"]
):
if filename.startswith("meta_") or filename.startswith("config_"):
continue
if filename[: -len(".md")] not in template:
raise Exception(f"Reference missing for {filename}")
@cli
def diagrammes():
matrice_content = (DIAGRAMMES_DIR / "matrice-statuts-conseils.md").read_text()
matrice_statuts = {
line.strip()
for line in matrice_content.split("\n")
if line.strip().startswith("statut_")
}
matrice_conseils = {
line.strip()
for line in matrice_content.split("\n")
if line.strip().startswith("conseils_")
}
statuts_filenames = {
filename
for file_path, filename in each_file_from(
CONTENUS_DIR / "statuts", pattern="*.md", exclude=["README.md"]
)
}
conseils_filenames = {
filename
for file_path, filename in each_file_from(
CONTENUS_DIR / "conseils",
pattern="conseils_personnels_*.md",
exclude=["README.md"],
)
}
if matrice_statuts - statuts_filenames:
raise Exception(
f"Statut file(s) missing for: {matrice_statuts - statuts_filenames}"
)
if matrice_conseils - conseils_filenames:
raise Exception(
f"Conseils file(s) missing for: {matrice_conseils - conseils_filenames}"
)
if statuts_filenames - matrice_statuts:
raise Exception(
f"Non-existent statut from matrice: {statuts_filenames - matrice_statuts}"
)
if conseils_filenames - matrice_conseils:
raise Exception(
f"Non-existent conseils personnels from matrice: {conseils_filenames - matrice_conseils}"
)
if __name__ == "__main__":
run()