blob: 204d61d296c60074aeeb54e08dbca562ce57f9e1 [file] [log] [blame]
jeraldabraham5d4d7352014-03-28 02:49:04 -07001#!/usr/bin/python2
2# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
3#
4# Copyright (C) 2014 University of Arizona
5# Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
6# See COPYING for copyright and distribution information.
7#
8
jeraldabraham3a2466e2014-04-09 00:19:45 -07009import time
jeraldabraham5d4d7352014-03-28 02:49:04 -070010import errno
11import subprocess
12import multiprocessing as mp
13
jeraldabraham3a2466e2014-04-09 00:19:45 -070014class ProcessManager:
jeraldabraham5d4d7352014-03-28 02:49:04 -070015
16 manager = mp.Manager()
17 processes = dict()
18 subprocesses = manager.dict()
19 results = manager.dict()
20
jeraldabraham3a2466e2014-04-09 00:19:45 -070021 def cleanupProcesses(self):
22 self.processes.clear()
23 self.subprocesses.clear()
24 self.results.clear()
25
jeraldabrahama3c97d62014-04-14 01:29:45 -070026 def runProcess(self,
27 processKey,
28 processCallFormat,
29 message,
30 subprocesses,
31 results,
32 inputFile,
33 outputFile):
jeraldabraham5d4d7352014-03-28 02:49:04 -070034 print message
jeraldabrahama3c97d62014-04-14 01:29:45 -070035 stdin = None
36 if inputFile is not None:
37 stdin = open(inputFile, "r")
38 stdout = subprocess.PIPE
39 if outputFile is not None:
40 stdout = open(outputFile, "w")
jeraldabraham5d4d7352014-03-28 02:49:04 -070041 process = subprocess.Popen(
jeraldabrahama3c97d62014-04-14 01:29:45 -070042 processCallFormat,
43 stdin=stdin,
44 stdout=stdout,
45 stderr=subprocess.PIPE)
jeraldabraham5d4d7352014-03-28 02:49:04 -070046 subprocesses[processKey] = process
47 try:
48 stdout, stderr = process.communicate()
49 returnCode = process.returncode
50 results[processKey] = (returnCode, stdout, stderr)
51 except IOError as e:
jeraldabrahama3c97d62014-04-14 01:29:45 -070052 print e
jeraldabraham5d4d7352014-03-28 02:49:04 -070053 pass
54
jeraldabrahama3c97d62014-04-14 01:29:45 -070055 def startProcess(self,
56 processKey,
57 processCallFormat,
58 message,
59 inputFile=None,
60 outputFile=None):
jeraldabraham5d4d7352014-03-28 02:49:04 -070061 self.processes[processKey] = mp.Process(
jeraldabrahama3c97d62014-04-14 01:29:45 -070062 target=self.runProcess,
63 args=[processKey,
64 processCallFormat,
65 message,
66 self.subprocesses,
67 self.results,
68 inputFile,
69 outputFile])
jeraldabraham5d4d7352014-03-28 02:49:04 -070070 self.processes[processKey].start()
71
72 def killProcess(self, processKey):
73 if processKey not in self.results:
74 self.subprocesses[processKey].terminate()
75
76 def hasProcessCompleted(self, processKey):
77 if processKey in self.results:
78 return True
79 else:
80 return False
81
82 def waitForProcessCompletion(self, processKey, waitTime):
83 self.processes[processKey].join(waitTime)
84
85 def getProcessReturnCode(self, processKey):
86 if processKey in self.results:
87 (returnCode, stdout, stderr) = self.results[processKey]
88 return returnCode
89 else:
90 print "Invalid processKey provided - " + processKey
91 return -1
92
93 def getProcessError(self, processKey):
94 if processKey in self.results:
95 (returnCode, stdout, stderr) = self.results[processKey]
96 return stderr
97 else:
98 return "Error not available for processKey - " + processKey
99
jeraldabraham3a2466e2014-04-09 00:19:45 -0700100 def getProcessOutput(self, processKey):
101 if processKey in self.results:
102 (returnCode, stdout, stderr) = self.results[processKey]
103 return stdout
104 else:
105 return "Output not available for processKey - " + processKey
106
jeraldabraham5d4d7352014-03-28 02:49:04 -0700107 def startNfd(self):
108 self.startProcess("nfd", ["sudo", "nfd"], "-> Starting NFD")
109
110 def killNfd(self):
111 self.killProcess("nfd")
jeraldabraham3a2466e2014-04-09 00:19:45 -0700112 time.sleep(2)