forked from jaheyns/CfdOF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowsRunWrapper.py
74 lines (65 loc) · 3.31 KB
/
WindowsRunWrapper.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
# ***************************************************************************
# * *
# * Copyright (c) 2017 Oliver Oxtoby (CSIR) <ooxtoby@csir.co.za> *
# * Copyright (c) 2017 Johan Heyns (CSIR) <jheyns@csir.co.za> *
# * Copyright (c) 2017 Alfred Bogaers (CSIR) <abogaers@csir.co.za> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
from __future__ import print_function
import sys
import threading
import signal
import subprocess
def processStdin():
with sys.stdin:
for line in iter(sys.stdin.readline, ''):
if line.rstrip() == "terminate":
print("Wrapper process received terminate command")
process.send_signal(signal.CTRL_BREAK_EVENT)
def processStdout():
with process.stdout:
for output in iter(process.stdout.readline, ''):
sys.stdout.write(output)
sys.stdout.flush()
def processStderr():
with process.stderr:
for output in iter(process.stderr.readline, ''):
sys.stderr.write(output)
sys.stdout.flush()
# Run program, return its exit code, while awaiting quit instruction on stdin pipe
argv = sys.argv
process = subprocess.Popen(argv[1:],
# Although we don't access stdin of subprocess, without stdin=PIPE,
# delivery to our (the parent's) stdin from outside seems very unreliable
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP,
universal_newlines=True)
# Start threads to await input/output.
t1 = threading.Thread(target=processStdin)
t1.daemon = True
t1.start()
t2 = threading.Thread(target=processStdout)
t2.daemon = True
t2.start()
t3 = threading.Thread(target=processStderr)
t3.daemon = True
t3.start()
process.wait()
sys.exit(process.returncode)