Create analysis_tools.py
Browse files- analysis_tools.py +78 -0
analysis_tools.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Nástroje pro bezpečné spuštění a validaci Python skriptů.
|
| 3 |
+
"""
|
| 4 |
+
import io
|
| 5 |
+
import sys
|
| 6 |
+
from contextlib import redirect_stdout, redirect_stderr
|
| 7 |
+
|
| 8 |
+
def safe_execute_script(script_code, context=None):
|
| 9 |
+
"""
|
| 10 |
+
Safely execute Python script with limited context
|
| 11 |
+
Returns script output or error message
|
| 12 |
+
"""
|
| 13 |
+
if context is None:
|
| 14 |
+
context = {
|
| 15 |
+
'video_path': None,
|
| 16 |
+
'frame_data': None,
|
| 17 |
+
'metadata': None
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
output = io.StringIO()
|
| 21 |
+
error = io.StringIO()
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
with redirect_stdout(output), redirect_stderr(error):
|
| 25 |
+
exec_globals = {
|
| 26 |
+
'__builtins__': {
|
| 27 |
+
'print': print,
|
| 28 |
+
'str': str,
|
| 29 |
+
'int': int,
|
| 30 |
+
'float': float,
|
| 31 |
+
'list': list,
|
| 32 |
+
'dict': dict,
|
| 33 |
+
'tuple': tuple,
|
| 34 |
+
'range': range,
|
| 35 |
+
'len': len,
|
| 36 |
+
'enumerate': enumerate,
|
| 37 |
+
'zip': zip,
|
| 38 |
+
'min': min,
|
| 39 |
+
'max': max,
|
| 40 |
+
'sum': sum,
|
| 41 |
+
'abs': abs,
|
| 42 |
+
'round': round
|
| 43 |
+
},
|
| 44 |
+
'context': context
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
exec(script_code, exec_globals)
|
| 48 |
+
|
| 49 |
+
if error.getvalue():
|
| 50 |
+
return f"Error: {error.getvalue()}"
|
| 51 |
+
else:
|
| 52 |
+
return output.getvalue()
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return f"Error executing script: {str(e)}"
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def validate_python_script(script_code):
|
| 59 |
+
"""
|
| 60 |
+
Validate Python script syntax and restricted functions
|
| 61 |
+
Returns (is_valid, error_message)
|
| 62 |
+
"""
|
| 63 |
+
import ast
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
tree = ast.parse(script_code)
|
| 67 |
+
|
| 68 |
+
for node in ast.walk(tree):
|
| 69 |
+
if isinstance(node, ast.Import) or isinstance(node, ast.ImportFrom):
|
| 70 |
+
return (False, "Import statements are not allowed")
|
| 71 |
+
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
|
| 72 |
+
if node.func.id in ['eval', 'exec', 'open', 'execfile']:
|
| 73 |
+
return (False, f"Function {node.func.id}() is not allowed")
|
| 74 |
+
|
| 75 |
+
return (True, "Script is valid")
|
| 76 |
+
except SyntaxError as e:
|
| 77 |
+
return (False, f"Syntax error: {str(e)}")
|
| 78 |
+
|