forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_links.py
More file actions
48 lines (38 loc) · 1.53 KB
/
Copy pathcheck_links.py
File metadata and controls
48 lines (38 loc) · 1.53 KB
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
#! /usr/bin/env python3
import re
import requests
import os
from pathlib import Path
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
timeout = 10
links_file = Path('LINKS.md')
content = links_file.read_text(encoding='utf-8')
link_pattern = re.compile(r'\[([^\]]+)\]\(([^)]+)\)')
links = link_pattern.findall(content)
broken_links = []
for text, url in links:
try:
print(f"Checking: {url}")
response = requests.head(url, headers=headers, timeout=timeout, allow_redirects=True)
if response.status_code >= 400:
response = requests.get(url, headers=headers, timeout=timeout, allow_redirects=True)
if response.status_code >= 400:
broken_links.append((text, url, response.status_code))
except Exception as e:
broken_links.append((text, url, str(e)))
if broken_links:
report = "# Broken Links Report\n\n"
report += "The following links in LINKS.md are broken:\n\n"
report += "| Link Text | URL | Error |\n"
report += "|-----------|-----|-------|\n"
for text, url, error in broken_links:
report += f"| {text} | {url} | {error} |\n"
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
delimiter = "_REPORT_DELIMITER_"
f.write(f"broken_links=true\n")
f.write(f"report<<{delimiter}\n{report}\n{delimiter}\n")
else:
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
f.write("broken_links=false\n")