|
| 1 | +import ast |
| 2 | +import os |
| 3 | +from typing import List, Dict, Any |
| 4 | + |
| 5 | + |
| 6 | +ALLOWED_FILE = os.path.normpath("litellm/_uuid.py") |
| 7 | + |
| 8 | + |
| 9 | +def _to_module_path(relative_path: str) -> str: |
| 10 | + module = os.path.splitext(relative_path)[0].replace(os.sep, ".") |
| 11 | + if module.endswith(".__init__"): |
| 12 | + return module[: -len(".__init__")] |
| 13 | + return module |
| 14 | + |
| 15 | + |
| 16 | +def _find_fastuuid_imports_in_file( |
| 17 | + file_path: str, base_dir: str |
| 18 | +) -> List[Dict[str, Any]]: |
| 19 | + results: List[Dict[str, Any]] = [] |
| 20 | + try: |
| 21 | + with open(file_path, "r", encoding="utf-8") as f: |
| 22 | + source = f.read() |
| 23 | + tree = ast.parse(source, filename=file_path) |
| 24 | + except Exception: |
| 25 | + return results |
| 26 | + |
| 27 | + relative = os.path.normpath(os.path.relpath(file_path, base_dir)) |
| 28 | + if relative == ALLOWED_FILE: |
| 29 | + return results |
| 30 | + |
| 31 | + module = _to_module_path(relative) |
| 32 | + for node in ast.walk(tree): |
| 33 | + if isinstance(node, ast.Import): |
| 34 | + for alias in node.names: |
| 35 | + if alias.name == "fastuuid": |
| 36 | + results.append( |
| 37 | + { |
| 38 | + "file": relative, |
| 39 | + "line": getattr(node, "lineno", 0), |
| 40 | + "import": f"import {alias.name}", |
| 41 | + "module": module, |
| 42 | + } |
| 43 | + ) |
| 44 | + elif isinstance(node, ast.ImportFrom) and node.module == "fastuuid": |
| 45 | + names = ", ".join([a.name for a in node.names]) |
| 46 | + results.append( |
| 47 | + { |
| 48 | + "file": relative, |
| 49 | + "line": getattr(node, "lineno", 0), |
| 50 | + "import": f"from fastuuid import {names}", |
| 51 | + "module": module, |
| 52 | + } |
| 53 | + ) |
| 54 | + |
| 55 | + return results |
| 56 | + |
| 57 | + |
| 58 | +def scan_directory_for_fastuuid(base_dir: str) -> List[Dict[str, Any]]: |
| 59 | + violations: List[Dict[str, Any]] = [] |
| 60 | + scan_root = os.path.join(base_dir, "litellm") |
| 61 | + for root, _, files in os.walk(scan_root): |
| 62 | + for filename in files: |
| 63 | + if filename.endswith(".py"): |
| 64 | + file_path = os.path.join(root, filename) |
| 65 | + violations.extend(_find_fastuuid_imports_in_file(file_path, base_dir)) |
| 66 | + return violations |
| 67 | + |
| 68 | + |
| 69 | +def main() -> None: |
| 70 | + base_dir = "." # tests run from repo root in CI |
| 71 | + violations = scan_directory_for_fastuuid(base_dir) |
| 72 | + if violations: |
| 73 | + print( |
| 74 | + "\n🚨 fastuuid must only be imported inside litellm/_uuid.py. Found violations:" |
| 75 | + ) |
| 76 | + for v in violations: |
| 77 | + print(f"* {v['module']} ({v['file']}:{v['line']}) -> {v['import']}") |
| 78 | + print("\n") |
| 79 | + raise Exception( |
| 80 | + "Found fastuuid imports outside litellm/_uuid.py. Use litellm._uuid.uuid or litellm._uuid.uuid4 instead." |
| 81 | + ) |
| 82 | + else: |
| 83 | + print("✅ No invalid fastuuid imports found.") |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
0 commit comments