|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Link Checker for awesome-codeql repository |
| 4 | +Checks all links in markdown files to ensure they are functional |
| 5 | +""" |
| 6 | + |
| 7 | +import re |
| 8 | +import os |
| 9 | +import sys |
| 10 | +import json |
| 11 | +import requests |
| 12 | +from typing import List, Dict, Tuple |
| 13 | +from urllib.parse import urlparse |
| 14 | +from concurrent.futures import ThreadPoolExecutor, as_completed |
| 15 | +from collections import defaultdict |
| 16 | + |
| 17 | +# Configuration |
| 18 | +TIMEOUT = 10 # seconds |
| 19 | +MAX_WORKERS = 10 # parallel requests |
| 20 | +USER_AGENT = 'Mozilla/5.0 (compatible; LinkChecker/1.0)' |
| 21 | + |
| 22 | +# Known patterns that might cause false positives |
| 23 | +SKIP_PATTERNS = [ |
| 24 | + r'localhost', |
| 25 | + r'127\.0\.0\.1', |
| 26 | + r'example\.com', |
| 27 | + r'\{.*\}', # Template variables |
| 28 | +] |
| 29 | + |
| 30 | +class LinkChecker: |
| 31 | + def __init__(self): |
| 32 | + self.session = requests.Session() |
| 33 | + self.session.headers.update({'User-Agent': USER_AGENT}) |
| 34 | + self.checked_urls = {} # Cache for checked URLs |
| 35 | + |
| 36 | + def extract_links_from_file(self, filepath: str) -> List[Tuple[str, int]]: |
| 37 | + """Extract all URLs from a markdown file""" |
| 38 | + links = [] |
| 39 | + |
| 40 | + with open(filepath, 'r', encoding='utf-8') as f: |
| 41 | + content = f.read() |
| 42 | + |
| 43 | + # Find markdown links [text](url) |
| 44 | + markdown_links = re.finditer(r'\[([^\]]+)\]\(([^)]+)\)', content) |
| 45 | + for match in markdown_links: |
| 46 | + url = match.group(2) |
| 47 | + # Get line number |
| 48 | + line_num = content[:match.start()].count('\n') + 1 |
| 49 | + links.append((url, line_num)) |
| 50 | + |
| 51 | + # Find plain URLs (http/https) |
| 52 | + plain_urls = re.finditer(r'https?://[^\s\)]+', content) |
| 53 | + for match in plain_urls: |
| 54 | + url = match.group(0) |
| 55 | + line_num = content[:match.start()].count('\n') + 1 |
| 56 | + # Avoid duplicates from markdown links |
| 57 | + if (url, line_num) not in links: |
| 58 | + links.append((url, line_num)) |
| 59 | + |
| 60 | + return links |
| 61 | + |
| 62 | + def should_skip_url(self, url: str) -> bool: |
| 63 | + """Check if URL should be skipped""" |
| 64 | + for pattern in SKIP_PATTERNS: |
| 65 | + if re.search(pattern, url): |
| 66 | + return True |
| 67 | + |
| 68 | + # Skip anchors and fragments within documents |
| 69 | + if url.startswith('#'): |
| 70 | + return True |
| 71 | + |
| 72 | + # Skip non-http(s) URLs |
| 73 | + parsed = urlparse(url) |
| 74 | + if parsed.scheme and parsed.scheme not in ['http', 'https']: |
| 75 | + return True |
| 76 | + |
| 77 | + return False |
| 78 | + |
| 79 | + def check_url(self, url: str) -> Dict: |
| 80 | + """Check if a URL is accessible""" |
| 81 | + # Remove anchor/fragment |
| 82 | + url_without_fragment = url.split('#')[0] |
| 83 | + |
| 84 | + # Check cache |
| 85 | + if url_without_fragment in self.checked_urls: |
| 86 | + return self.checked_urls[url_without_fragment] |
| 87 | + |
| 88 | + result = { |
| 89 | + 'url': url, |
| 90 | + 'status': 'unknown', |
| 91 | + 'status_code': None, |
| 92 | + 'error': None, |
| 93 | + 'redirected_to': None |
| 94 | + } |
| 95 | + |
| 96 | + try: |
| 97 | + # First try HEAD request (faster) |
| 98 | + response = self.session.head( |
| 99 | + url_without_fragment, |
| 100 | + timeout=TIMEOUT, |
| 101 | + allow_redirects=True |
| 102 | + ) |
| 103 | + |
| 104 | + # Some servers don't support HEAD, try GET if HEAD fails |
| 105 | + if response.status_code in [405, 404]: |
| 106 | + response = self.session.get( |
| 107 | + url_without_fragment, |
| 108 | + timeout=TIMEOUT, |
| 109 | + allow_redirects=True |
| 110 | + ) |
| 111 | + |
| 112 | + result['status_code'] = response.status_code |
| 113 | + |
| 114 | + if response.status_code == 200: |
| 115 | + result['status'] = 'ok' |
| 116 | + elif response.status_code in [301, 302, 307, 308]: |
| 117 | + result['status'] = 'redirect' |
| 118 | + result['redirected_to'] = response.url |
| 119 | + elif response.status_code == 404: |
| 120 | + result['status'] = 'not_found' |
| 121 | + elif response.status_code >= 400: |
| 122 | + result['status'] = 'error' |
| 123 | + else: |
| 124 | + result['status'] = 'warning' |
| 125 | + |
| 126 | + if url_without_fragment != response.url: |
| 127 | + result['redirected_to'] = response.url |
| 128 | + |
| 129 | + except requests.exceptions.Timeout: |
| 130 | + result['status'] = 'timeout' |
| 131 | + result['error'] = 'Request timeout' |
| 132 | + except requests.exceptions.SSLError as e: |
| 133 | + result['status'] = 'ssl_error' |
| 134 | + result['error'] = f'SSL Error: {str(e)}' |
| 135 | + except requests.exceptions.ConnectionError as e: |
| 136 | + result['status'] = 'connection_error' |
| 137 | + result['error'] = f'Connection Error: {str(e)}' |
| 138 | + except requests.exceptions.TooManyRedirects: |
| 139 | + result['status'] = 'too_many_redirects' |
| 140 | + result['error'] = 'Too many redirects' |
| 141 | + except Exception as e: |
| 142 | + result['status'] = 'error' |
| 143 | + result['error'] = str(e) |
| 144 | + |
| 145 | + # Cache the result |
| 146 | + self.checked_urls[url_without_fragment] = result |
| 147 | + return result |
| 148 | + |
| 149 | + def check_links_parallel(self, links: List[Tuple[str, str, int]]) -> List[Dict]: |
| 150 | + """Check multiple links in parallel""" |
| 151 | + results = [] |
| 152 | + |
| 153 | + # Filter out skipped URLs |
| 154 | + urls_to_check = [ |
| 155 | + (filepath, url, line_num) |
| 156 | + for filepath, url, line_num in links |
| 157 | + if not self.should_skip_url(url) |
| 158 | + ] |
| 159 | + |
| 160 | + print(f"Checking {len(urls_to_check)} links (skipped {len(links) - len(urls_to_check)})...") |
| 161 | + |
| 162 | + with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor: |
| 163 | + future_to_link = { |
| 164 | + executor.submit(self.check_url, url): (filepath, url, line_num) |
| 165 | + for filepath, url, line_num in urls_to_check |
| 166 | + } |
| 167 | + |
| 168 | + for i, future in enumerate(as_completed(future_to_link), 1): |
| 169 | + filepath, url, line_num = future_to_link[future] |
| 170 | + try: |
| 171 | + result = future.result() |
| 172 | + result['file'] = filepath |
| 173 | + result['line'] = line_num |
| 174 | + results.append(result) |
| 175 | + |
| 176 | + # Progress indicator |
| 177 | + if i % 10 == 0: |
| 178 | + print(f"Checked {i}/{len(urls_to_check)} links...") |
| 179 | + |
| 180 | + except Exception as e: |
| 181 | + results.append({ |
| 182 | + 'file': filepath, |
| 183 | + 'url': url, |
| 184 | + 'line': line_num, |
| 185 | + 'status': 'error', |
| 186 | + 'error': str(e) |
| 187 | + }) |
| 188 | + |
| 189 | + return results |
| 190 | + |
| 191 | +def main(): |
| 192 | + # Find all markdown files |
| 193 | + md_files = [] |
| 194 | + repo_root = '/home/runner/work/awesome-codeql/awesome-codeql' |
| 195 | + |
| 196 | + for root, dirs, files in os.walk(repo_root): |
| 197 | + # Skip .git directory |
| 198 | + if '.git' in root: |
| 199 | + continue |
| 200 | + for file in files: |
| 201 | + if file.endswith('.md'): |
| 202 | + md_files.append(os.path.join(root, file)) |
| 203 | + |
| 204 | + print(f"Found {len(md_files)} markdown files") |
| 205 | + |
| 206 | + # Extract all links |
| 207 | + checker = LinkChecker() |
| 208 | + all_links = [] |
| 209 | + |
| 210 | + for filepath in md_files: |
| 211 | + rel_path = os.path.relpath(filepath, repo_root) |
| 212 | + print(f"Extracting links from {rel_path}...") |
| 213 | + links = checker.extract_links_from_file(filepath) |
| 214 | + for url, line_num in links: |
| 215 | + all_links.append((rel_path, url, line_num)) |
| 216 | + |
| 217 | + print(f"\nFound {len(all_links)} total links\n") |
| 218 | + |
| 219 | + # Check all links |
| 220 | + results = checker.check_links_parallel(all_links) |
| 221 | + |
| 222 | + # Categorize results |
| 223 | + categorized = defaultdict(list) |
| 224 | + for result in results: |
| 225 | + categorized[result['status']].append(result) |
| 226 | + |
| 227 | + # Print summary |
| 228 | + print("\n" + "="*80) |
| 229 | + print("LINK CHECK SUMMARY") |
| 230 | + print("="*80) |
| 231 | + |
| 232 | + print(f"\nTotal links checked: {len(results)}") |
| 233 | + print(f" ✓ OK: {len(categorized['ok'])}") |
| 234 | + print(f" ⚠ Redirects: {len(categorized['redirect'])}") |
| 235 | + print(f" ✗ Not Found (404): {len(categorized['not_found'])}") |
| 236 | + print(f" ✗ Errors: {len(categorized['error'])}") |
| 237 | + print(f" ⏱ Timeouts: {len(categorized['timeout'])}") |
| 238 | + print(f" 🔒 SSL Errors: {len(categorized['ssl_error'])}") |
| 239 | + print(f" 🔌 Connection Errors: {len(categorized['connection_error'])}") |
| 240 | + |
| 241 | + # Report broken links |
| 242 | + broken_statuses = ['not_found', 'error', 'timeout', 'ssl_error', 'connection_error'] |
| 243 | + broken_links = [] |
| 244 | + for status in broken_statuses: |
| 245 | + broken_links.extend(categorized[status]) |
| 246 | + |
| 247 | + if broken_links: |
| 248 | + print("\n" + "="*80) |
| 249 | + print("BROKEN LINKS REPORT") |
| 250 | + print("="*80) |
| 251 | + |
| 252 | + for result in sorted(broken_links, key=lambda x: (x['file'], x['line'])): |
| 253 | + print(f"\n{result['file']}:{result['line']}") |
| 254 | + print(f" URL: {result['url']}") |
| 255 | + print(f" Status: {result['status']}") |
| 256 | + if result.get('status_code'): |
| 257 | + print(f" HTTP Status: {result['status_code']}") |
| 258 | + if result.get('error'): |
| 259 | + print(f" Error: {result['error']}") |
| 260 | + |
| 261 | + # Report redirects (informational) |
| 262 | + if categorized['redirect']: |
| 263 | + print("\n" + "="*80) |
| 264 | + print("REDIRECTED LINKS (Informational)") |
| 265 | + print("="*80) |
| 266 | + |
| 267 | + for result in sorted(categorized['redirect'], key=lambda x: (x['file'], x['line'])): |
| 268 | + print(f"\n{result['file']}:{result['line']}") |
| 269 | + print(f" URL: {result['url']}") |
| 270 | + print(f" Redirected to: {result.get('redirected_to', 'Unknown')}") |
| 271 | + |
| 272 | + # Save detailed results to JSON |
| 273 | + output_file = os.path.join(repo_root, 'link_check_results.json') |
| 274 | + with open(output_file, 'w') as f: |
| 275 | + json.dump({ |
| 276 | + 'summary': { |
| 277 | + 'total': len(results), |
| 278 | + 'ok': len(categorized['ok']), |
| 279 | + 'redirects': len(categorized['redirect']), |
| 280 | + 'not_found': len(categorized['not_found']), |
| 281 | + 'errors': len(categorized['error']), |
| 282 | + 'timeouts': len(categorized['timeout']), |
| 283 | + 'ssl_errors': len(categorized['ssl_error']), |
| 284 | + 'connection_errors': len(categorized['connection_error']), |
| 285 | + }, |
| 286 | + 'broken_links': broken_links, |
| 287 | + 'redirects': categorized['redirect'], |
| 288 | + 'all_results': results |
| 289 | + }, f, indent=2) |
| 290 | + |
| 291 | + print(f"\n\nDetailed results saved to: {output_file}") |
| 292 | + |
| 293 | + # Exit with error code if there are broken links |
| 294 | + if broken_links: |
| 295 | + print(f"\n❌ Found {len(broken_links)} broken links!") |
| 296 | + return 1 |
| 297 | + else: |
| 298 | + print("\n✅ All links are functional!") |
| 299 | + return 0 |
| 300 | + |
| 301 | +if __name__ == '__main__': |
| 302 | + sys.exit(main()) |
0 commit comments