Download bots/dependances.py from RDS777/bluster2: direct link, hf CLI and curl.
- Browser
- Download file 2.17 kB
-
https://huggingface.co/spaces/RDS777/bluster2/resolve/main/bots/dependances.py
- Command line
-
hf download hf://spaces/RDS777/bluster2/bots/dependances.py
-
curl -L -o dependances.py https://huggingface.co/spaces/RDS777/bluster2/resolve/main/bots/dependances.py
2.17 kB
| """Bot Dépendances — extrait imports, use, appels externes depuis du code. | |
| Entrée : {"code": str, "fichier": str} | |
| Sortie : {"imports": [str], "appels_externes": [str]} | |
| """ | |
| from __future__ import annotations | |
| import ast | |
| import re | |
| from typing import Any | |
| def _imports_python(code: str) -> list[str]: | |
| imports: list[str] = [] | |
| try: | |
| tree = ast.parse(code) | |
| except SyntaxError: | |
| return _imports_regex(code) | |
| for node in ast.walk(tree): | |
| if isinstance(node, ast.Import): | |
| for alias in node.names: | |
| imports.append(alias.name) | |
| elif isinstance(node, ast.ImportFrom): | |
| module = node.module or "" | |
| for alias in node.names: | |
| imports.append(f"{module}.{alias.name}" if module else alias.name) | |
| return sorted(set(imports)) | |
| def _imports_regex(code: str) -> list[str]: | |
| found = [] | |
| for ligne in code.splitlines(): | |
| m = re.match(r"^\s*(?:import|from)\s+([\w.]+)", ligne) | |
| if m: | |
| found.append(m.group(1)) | |
| return sorted(set(found)) | |
| def _use_rust(code: str) -> list[str]: | |
| found = [] | |
| for ligne in code.splitlines(): | |
| m = re.match(r"^\s*use\s+([\w:{}*]+)", ligne) | |
| if m: | |
| found.append(m.group(1)) | |
| return sorted(set(found)) | |
| def _appels_externes(code: str) -> list[str]: | |
| """Détecte HTTP, subprocess, os.system, etc.""" | |
| patterns = [ | |
| r"https?://[^\s\"']+", | |
| r"subprocess\.\w+", | |
| r"os\.system", | |
| r"os\.popen", | |
| r"urllib\.\w+", | |
| r"requests\.\w+", | |
| r"httpx\.\w+", | |
| ] | |
| found = [] | |
| for pat in patterns: | |
| found.extend(re.findall(pat, code)) | |
| return sorted(set(found))[:30] | |
| def run(payload: dict[str, Any]) -> dict[str, Any]: | |
| code: str = payload.get("code", "") | |
| fichier: str = payload.get("fichier", "") | |
| if fichier.endswith(".py"): | |
| imports = _imports_python(code) | |
| elif fichier.endswith(".rs"): | |
| imports = _use_rust(code) | |
| else: | |
| imports = _imports_regex(code) | |
| return { | |
| "bot": "dependances", | |
| "imports": imports[:60], | |
| "appels_externes": _appels_externes(code), | |
| } | |