Checks if a Python file contains hardcoded absolute paths or usernames.
(filepath)
| 3 | import sys |
| 4 | |
| 5 | def check_file(filepath): |
| 6 | """ |
| 7 | Checks if a Python file contains hardcoded absolute paths or usernames. |
| 8 | """ |
| 9 | issues = [] |
| 10 | with open(filepath, 'r', encoding='utf-8') as f: |
| 11 | lines = f.readlines() |
| 12 | |
| 13 | # Regex patterns to detect hardcoded paths |
| 14 | # Matches 'C:\Users', '/home/user', or specific 'Straightheart' |
| 15 | patterns = [ |
| 16 | (re.compile(r'c:\\Users', re.IGNORECASE), "Windows User Path"), |
| 17 | (re.compile(r'Straightheart', re.IGNORECASE), "Specific Username"), |
| 18 | (re.compile(r'/home/[a-z]+', re.IGNORECASE), "Linux Home Path"), |
| 19 | # More specific Windows drive pattern to avoid false positives in valid strings if any |
| 20 | (re.compile(r'[a-zA-Z]:\\[^ \t\n\r\f\v"\']*'), "Absolute Windows Path"), |
| 21 | ] |
| 22 | |
| 23 | for i, line in enumerate(lines): |
| 24 | line = line.strip() |
| 25 | # Skip comments? Maybe not strictly necessary if we want to be paranoid |
| 26 | if line.startswith('#'): |
| 27 | continue |
| 28 | |
| 29 | for pattern, desc in patterns: |
| 30 | if pattern.search(line): |
| 31 | # Filter out intentional strings if they are part of a library call? |
| 32 | # For now, let's keep it strict. |
| 33 | issues.append(f"Line {i+1}: {desc} found: {line}") |
| 34 | |
| 35 | return issues |
| 36 | |
| 37 | def main(): |
| 38 | # Script is in tests/e2e/ — project root is two levels up |