blob: fa2fdf389e819782d6284c09295402b6bf768a5c [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
jeraldabraham5d4d7352014-03-28 02:49:04 -070026 def runProcess(self, processKey, processCallFormat, message, subprocesses, results):
27 print message
28 process = subprocess.Popen(
29 processCallFormat, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
30 subprocesses[processKey] = process
31 try:
32 stdout, stderr = process.communicate()
33 returnCode = process.returncode
34 results[processKey] = (returnCode, stdout, stderr)
35 except IOError as e:
36 pass
37
38 def startProcess(self, processKey, processCallFormat, message):
39 self.processes[processKey] = mp.Process(
40 target = self.runProcess,
41 args = [processKey, processCallFormat, message, self.subprocesses, self.results])
42 self.processes[processKey].start()
43
44 def killProcess(self, processKey):
45 if processKey not in self.results:
46 self.subprocesses[processKey].terminate()
47
48 def hasProcessCompleted(self, processKey):
49 if processKey in self.results:
50 return True
51 else:
52 return False
53
54 def waitForProcessCompletion(self, processKey, waitTime):
55 self.processes[processKey].join(waitTime)
56
57 def getProcessReturnCode(self, processKey):
58 if processKey in self.results:
59 (returnCode, stdout, stderr) = self.results[processKey]
60 return returnCode
61 else:
62 print "Invalid processKey provided - " + processKey
63 return -1
64
65 def getProcessError(self, processKey):
66 if processKey in self.results:
67 (returnCode, stdout, stderr) = self.results[processKey]
68 return stderr
69 else:
70 return "Error not available for processKey - " + processKey
71
jeraldabraham3a2466e2014-04-09 00:19:45 -070072 def getProcessOutput(self, processKey):
73 if processKey in self.results:
74 (returnCode, stdout, stderr) = self.results[processKey]
75 return stdout
76 else:
77 return "Output not available for processKey - " + processKey
78
jeraldabraham5d4d7352014-03-28 02:49:04 -070079 def startNfd(self):
80 self.startProcess("nfd", ["sudo", "nfd"], "-> Starting NFD")
81
82 def killNfd(self):
83 self.killProcess("nfd")
jeraldabraham3a2466e2014-04-09 00:19:45 -070084 time.sleep(2)