-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink_checker.py
More file actions
83 lines (72 loc) · 2.51 KB
/
Copy pathlink_checker.py
File metadata and controls
83 lines (72 loc) · 2.51 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
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
#!/usr/bin/env python3
"""
Check for broken links in services.
"""
import os
import glob
import yaml
import requests
import sys
from concurrent.futures import ThreadPoolExecutor, as_completed
def load_services():
script_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(os.path.dirname(script_dir))
services_dir = os.path.join(project_root, 'services')
services = []
for yaml_file in glob.glob(os.path.join(services_dir, '**', '*.yml'), recursive=True):
try:
with open(yaml_file, 'r') as f:
data = yaml.safe_load(f)
services.append((yaml_file, data['service']))
except Exception as e:
print(f"Error loading {yaml_file}: {e}")
for yaml_file in glob.glob(os.path.join(services_dir, '**', '*.yaml'), recursive=True):
try:
with open(yaml_file, 'r') as f:
data = yaml.safe_load(f)
services.append((yaml_file, data['service']))
except Exception as e:
print(f"Error loading {yaml_file}: {e}")
return services
def extract_urls(service):
urls = []
if 'url' in service:
urls.append(service['url'])
resources = service.get('resources', {})
for key in ['official_docs', 'free_tier_page', 'tutorial', 'community']:
if key in resources and resources[key]:
urls.append(resources[key])
return urls
def check_url(url):
try:
response = requests.head(url, timeout=10, allow_redirects=True)
return url, response.status_code < 400
except requests.RequestException:
return url, False
def main():
services = load_services()
all_urls = []
for yaml_file, service in services:
urls = extract_urls(service)
for url in urls:
all_urls.append((yaml_file, url))
broken = []
with ThreadPoolExecutor(max_workers=10) as executor:
futures = {executor.submit(check_url, url): (file, url) for file, url in all_urls}
for future in as_completed(futures):
file, url = futures[future]
try:
checked_url, is_ok = future.result()
if not is_ok:
broken.append((file, checked_url))
except Exception as e:
broken.append((file, url))
if broken:
print("Broken links found:")
for file, url in broken:
print(f" {file}: {url}")
sys.exit(1)
else:
print("All links are accessible.")
if __name__ == "__main__":
main()