File: process.py

package info (click to toggle)
esys-particle 2.3.5%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,132 kB
  • sloc: cpp: 81,480; python: 5,872; makefile: 1,259; sh: 313; perl: 225
file content (185 lines) | stat: -rw-r--r-- 5,911 bytes parent folder | download | duplicates (4)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#############################################################
##                                                         ##
## Copyright (c) 2003-2017 by The University of Queensland ##
## Centre for Geoscience Computing                         ##
## http://earth.uq.edu.au/centre-geoscience-computing      ##
##                                                         ##
## Primary Business: Brisbane, Queensland, Australia       ##
## Licensed under the Open Software License version 3.0    ##
## http://www.apache.org/licenses/LICENSE-2.0              ##
##                                                         ##
#############################################################

from esys.lsm.util.pathSearcher import *

import esys.lsm.Logging

import sys
import subprocess
import threading

def getLogger():
    return esys.lsm.Logging.getLogger("esys.lsm.util")

class ThreadedWriter(threading.Thread):
    def __init__(self, readFrom=None, writeToList=[]):
        threading.Thread.__init__(self)
        self.readFrom = readFrom
        self.writeToList = writeToList
        self.lineList = []

    def run(self):
        if (self.readFrom != None):
          try:
            line = self.readFrom.readline()
            """
            Writing a binary line to a text stream worked under
            Python 2.x without explicit decode, probably because
            the default encoding was ascii.  But under Python 3.x
            with unicode an explicit decode is necessary.
            """
            if sys.version_info[0] > 2:
              line = bytes.decode(line)
          except:
            line = ""
          while (line != ""):
            for f in self.writeToList:
              f.write(line)
            self.lineList.append(line)
            try:
              line = self.readFrom.readline()
              if sys.version_info[0] > 2:
                line = bytes.decode(line)
            except:
              line = ""

    def getLineList(self):
        self.join()
        return self.lineList

class Process:
    def __init__(
        self,
        exePath,
        exeArgList=None,
        combineStdOutAndStdErr=True,
        printOutput=False
    ):
        self.exePath                = PathSearcher().which(exePath)
        if (exeArgList == None):
            exeArgList = []
        self.exeArgList             = exeArgList
        self.printOutput            = printOutput
        self.combineStdOutAndStdErr = combineStdOutAndStdErr
        self.popen                  = None
        self.stdOutWriter           = None
        self.stdErrWriter           = None

    def getExePath(self):
        return self.exePath

    def getExeArgList(self):
        return self.exeArgList

    def getCommandLine(self):
        return \
            str(self.getExePath()) + " " + \
            str.join(" ", (list(map(str, self.getExeArgList()))))

    def wait(self):
        try:
            getLogger().info(
                "Waiting on process with id=" + \
                str(self.popen.pid)   + \
                ", cmd="     + \
                self.getCommandLine()
            )
            if sys.version_info[0] > 2:
              return self.popen.communicate()
            else:
              return self.popen.wait()
        except:
            getLogger().error(
                "Exception while waiting for cmd=" +\
                self.getCommandLine()
            )
            raise

    def runNoWait(self):
        if (self.printOutput):
            stdOutList = [sys.stdout]
            stdErrList = [sys.stderr]
        else:
            stdOutList = []
            stdErrList = []

        try:
            getLogger().info(
                "Creating process with cmd=" + self.getCommandLine()
            )
            if (self.combineStdOutAndStdErr):
                self.popen = \
                    subprocess.Popen(
                        self.getCommandLine().split(),
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT,
                        close_fds=True
                    )
            else:
                self.popen = \
                    subprocess.Popen(
                        self.getCommandLine().split(),
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        close_fds=True
                    
                    )
            self.stdOutWriter = ThreadedWriter(self.popen.stdout, stdOutList)
            self.stdErrWriter = ThreadedWriter(self.popen.stderr, stdErrList)
            getLogger().info("stdout = " + str(self.popen.stdout))
            getLogger().info("stderr = " + str(self.popen.stderr))
            getLogger().info("stdin  = " + str(self.popen.stdin))
            self.stdOutWriter.start()
            self.stdErrWriter.start()

        except:
            getLogger().error(
                "popen failed for cmd=" + self.getCommandLine() + "\n"
            )
            raise

    def run(self):
        self.runNoWait()
        return self.wait()

    def getStdInFile(self):
        return self.popen.stdin

    def getStdOutFile(self):
        return self.popen.stdout

    def getStdErrFile(self):
        return self.popen.stderr

    def isRunning(self):
        return (self.popen.poll() == -1)

    def getExitStatus(self):
        pollVal = self.popen.poll()
        if (pollVal == -1):
            return None
        return pollVal

    def getStdOutLineList(self):
        return self.stdOutWriter.getLineList()

    def getStdOutString(self):
        return str.join("", (self.stdOutWriter.getLineList()))

    def getStdErrLineList(self):
        return self.stdErrWriter.getLineList()

    def getStdErrString(self):
        return str.join("", (self.stdErrWriter.getLineList()))