blob: ebc4862bc0e1834fd8f30c53baca4b1be737b303 [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
9import errno
10import subprocess
11import multiprocessing as mp
12
13class process_manager:
14
15 manager = mp.Manager()
16 processes = dict()
17 subprocesses = manager.dict()
18 results = manager.dict()
19
20 def runProcess(self, processKey, processCallFormat, message, subprocesses, results):
21 print message
22 process = subprocess.Popen(
23 processCallFormat, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
24 subprocesses[processKey] = process
25 try:
26 stdout, stderr = process.communicate()
27 returnCode = process.returncode
28 results[processKey] = (returnCode, stdout, stderr)
29 except IOError as e:
30 pass
31
32 def startProcess(self, processKey, processCallFormat, message):
33 self.processes[processKey] = mp.Process(
34 target = self.runProcess,
35 args = [processKey, processCallFormat, message, self.subprocesses, self.results])
36 self.processes[processKey].start()
37
38 def killProcess(self, processKey):
39 if processKey not in self.results:
40 self.subprocesses[processKey].terminate()
41
42 def hasProcessCompleted(self, processKey):
43 if processKey in self.results:
44 return True
45 else:
46 return False
47
48 def waitForProcessCompletion(self, processKey, waitTime):
49 self.processes[processKey].join(waitTime)
50
51 def getProcessReturnCode(self, processKey):
52 if processKey in self.results:
53 (returnCode, stdout, stderr) = self.results[processKey]
54 return returnCode
55 else:
56 print "Invalid processKey provided - " + processKey
57 return -1
58
59 def getProcessError(self, processKey):
60 if processKey in self.results:
61 (returnCode, stdout, stderr) = self.results[processKey]
62 return stderr
63 else:
64 return "Error not available for processKey - " + processKey
65
66 def startNfd(self):
67 self.startProcess("nfd", ["sudo", "nfd"], "-> Starting NFD")
68
69 def killNfd(self):
70 self.killProcess("nfd")