This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcheck.py
executable file
·183 lines (162 loc) · 6.18 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
#!/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 construction.utils import each_folder_from, each_file_from
HERE = Path(__file__).parent
SRC_DIR = HERE / "src"
DIAGRAMMES_DIR = HERE / "diagrammes"
CONTENUS_DIR = HERE / "contenus"
TEMPLATES_DIR = HERE / "templates"
class LinkExtractor(HTMLParser):
def reset(self):
HTMLParser.reset(self)
self.external_links = set()
self.internal_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://www.youtube.com/",
"https://twitter.com/",
"https://github.com/",
"https://wa.me/",
)
):
return
self.external_links.add(url)
elif url.startswith(("/", "#")):
if url in ("/", "#") or url.startswith(("/#", "/illustrations/")):
return
self.internal_links.add(url)
@cli
def external_links(timeout: int = 10, delay: float = 0.1):
external_links = set()
for path in each_file_from(SRC_DIR, pattern="*.html"):
parser = LinkExtractor()
content = path.read_text()
parser.feed(content)
external_links |= parser.external_links
for external_link in sorted(external_links):
print(external_link)
with httpx.stream(
"GET",
external_link,
follow_redirects=True,
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"{external_link} is broken! ({response.status_code})")
time.sleep(delay) # avoid being throttled
@cli
def internal_links():
internal_contents = {}
internal_links = {}
internal_pages = []
for path in each_file_from(SRC_DIR, pattern="*.html"):
parser = LinkExtractor()
content = path.read_text()
internal_contents[path.name] = content
parser.feed(content)
internal_links[path.name] = parser.internal_links
internal_pages.append(path.name)
for path, path_internal_links in internal_links.items():
for internal_link in path_internal_links:
# Check cross pages references.
if internal_link.startswith("/"):
if "#" in internal_link:
path_name, anchor = internal_link.split("#", 1)
else:
path_name = internal_link
anchor = None
# Check missing pages.
if path_name[1:] not in internal_pages:
raise Exception(
(
f"{path_name} referenced in {path} but "
f"does not exist in {internal_pages}."
)
)
# Check missing anchors.
if anchor is not None:
if f'id="{anchor}"' not in internal_contents[path_name[1:]]:
raise Exception(
(
f"{internal_link} referenced in {path} but "
f"does not exist in content."
)
)
# Check anchors on the same page.
elif internal_link.startswith("#"):
if f'id="{internal_link[1:]}"' not in internal_contents[path]:
raise Exception(
(
f"{internal_link} referenced in {path} but "
f"does not exist on that page."
)
)
else:
raise Exception(f"What is that link?! {internal_link}")
@cli
def orphelins():
template = (TEMPLATES_DIR / "index.html").read_text()
for folder in each_folder_from(CONTENUS_DIR, exclude=["thematiques"]):
for path in each_file_from(folder, pattern="*.md"):
if path.name.startswith("meta_") or path.name.startswith("config_"):
continue
if path.name[: -len(".md")] not in template:
raise Exception(f"Reference missing for {path.name}")
@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 = {
path.name
for path in each_file_from(CONTENUS_DIR / "statuts", pattern="*.md")
if path.name != "statut_moins_de_15_ans.md"
}
conseils_filenames = {
path.name
for path in each_file_from(
CONTENUS_DIR / "conseils", pattern="conseils_personnels_*.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()