-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime2py.py
More file actions
44 lines (38 loc) · 1.04 KB
/
time2py.py
File metadata and controls
44 lines (38 loc) · 1.04 KB
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
import sys
import os
from io import StringIO
import contextlib
import subprocess
@contextlib.contextmanager
def stdoutIO(stdout=None):
old = sys.stdout
if stdout is None:
stdout = StringIO()
sys.stdout = stdout
yield stdout
sys.stdout = old
def get_stdin():
buf = ""
for line in sys.stdin:
buf = buf + line
return buf
if __name__ == "__main__":
st = get_stdin()
# print(st)
with stdoutIO() as s:
try:
tmpfold = os.getenv("TMPDIR", "/tmp")
tmpfile = "%s/%s" % (tmpfold, "script.py")
# print(tmpfile)
f = open(tmpfile, 'w')
print(st, file=f)
f.close()
p1 = subprocess.Popen(["python3", tmpfile], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p2, p3 = p1.stdout.read().decode('utf-8'), p1.stderr.read().decode('utf-8')
if len(p2) > 0:
print(p2)
else:
print(p3)
except BaseException as e:
print(e)
print(s.getvalue())