-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
85 lines (75 loc) · 2.47 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import sys
import dill
import subprocess
import io
from functools import wraps
def to_stdout(f_py=None, kill_if_exception=True):
def globals_from_dict(di, name):
for key, item in di.items():
setattr(sys.modules[name], key, item)
def _decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
stdout = sys.stdout
sys.stdout = io.StringIO()
image_data = sys.stdin.buffer.read()
f = dill.loads(image_data)
globals_from_dict(f, "__main__")
allb = func(*args, **kwargs)
output_data = io.BytesIO()
output_data.write(
b"STARTSTARTSTARTSTARTSTARTSTARTSTARTSTARTSTARTSTARTSTARTSTARTSTARTSTARTSTARTSTART"
+ dill.dumps(allb)
+ b"ENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDEND"
)
sys.stdout = stdout
sys.stdout.flush()
sys.stdout.buffer.write(output_data.getvalue())
return None
return wrapper
try:
return _decorator(f_py) if callable(f_py) else _decorator
except Exception as fe:
if kill_if_exception:
os._exit()
def run_py_file(
variables,
pyfile,
activate_env_command="",
pythonexe=sys.executable,
raise_exception=False,
):
pyfile = os.path.normpath(pyfile)
pythonexe = os.path.normpath(pythonexe)
executecommand = [pythonexe, pyfile]
if activate_env_command:
if isinstance(activate_env_command, tuple):
activate_env_command = list(activate_env_command)
executecommand = activate_env_command + ["&"] + executecommand
ii = dill.dumps(variables)
p = None
try:
DEVNULL = open(os.devnull, "wb")
p = subprocess.run(
executecommand, input=ii, stdout=subprocess.PIPE, stderr=DEVNULL
)
finally:
try:
DEVNULL.close()
except Exception as fe:
print(fe)
try:
output_data = p.stdout
output_data = output_data.split(
b"STARTSTARTSTARTSTARTSTARTSTARTSTARTSTARTSTARTSTARTSTARTSTARTSTARTSTARTSTARTSTART"
)[-1]
output_data = output_data.split(
b"ENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDEND"
)[0]
return dill.loads(output_data)
except Exception as fa:
if raise_exception:
raise fa
print(fa)
return None