jeraldabraham | 5d4d735 | 2014-03-28 02:49:04 -0700 | [diff] [blame] | 1 | #!/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 | |
jeraldabraham | 3a2466e | 2014-04-09 00:19:45 -0700 | [diff] [blame^] | 9 | import time |
jeraldabraham | 5d4d735 | 2014-03-28 02:49:04 -0700 | [diff] [blame] | 10 | import errno |
| 11 | import subprocess |
| 12 | import multiprocessing as mp |
| 13 | |
jeraldabraham | 3a2466e | 2014-04-09 00:19:45 -0700 | [diff] [blame^] | 14 | class ProcessManager: |
jeraldabraham | 5d4d735 | 2014-03-28 02:49:04 -0700 | [diff] [blame] | 15 | |
| 16 | manager = mp.Manager() |
| 17 | processes = dict() |
| 18 | subprocesses = manager.dict() |
| 19 | results = manager.dict() |
| 20 | |
jeraldabraham | 3a2466e | 2014-04-09 00:19:45 -0700 | [diff] [blame^] | 21 | def cleanupProcesses(self): |
| 22 | self.processes.clear() |
| 23 | self.subprocesses.clear() |
| 24 | self.results.clear() |
| 25 | |
jeraldabraham | 5d4d735 | 2014-03-28 02:49:04 -0700 | [diff] [blame] | 26 | 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 | |
jeraldabraham | 3a2466e | 2014-04-09 00:19:45 -0700 | [diff] [blame^] | 72 | 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 | |
jeraldabraham | 5d4d735 | 2014-03-28 02:49:04 -0700 | [diff] [blame] | 79 | def startNfd(self): |
| 80 | self.startProcess("nfd", ["sudo", "nfd"], "-> Starting NFD") |
| 81 | |
| 82 | def killNfd(self): |
| 83 | self.killProcess("nfd") |
jeraldabraham | 3a2466e | 2014-04-09 00:19:45 -0700 | [diff] [blame^] | 84 | time.sleep(2) |