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 | a3c97d6 | 2014-04-14 01:29:45 -0700 | [diff] [blame] | 26 | def runProcess(self, |
| 27 | processKey, |
| 28 | processCallFormat, |
| 29 | message, |
| 30 | subprocesses, |
| 31 | results, |
| 32 | inputFile, |
| 33 | outputFile): |
jeraldabraham | 5d4d735 | 2014-03-28 02:49:04 -0700 | [diff] [blame] | 34 | print message |
jeraldabraham | a3c97d6 | 2014-04-14 01:29:45 -0700 | [diff] [blame] | 35 | 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") |
jeraldabraham | 5d4d735 | 2014-03-28 02:49:04 -0700 | [diff] [blame] | 41 | process = subprocess.Popen( |
jeraldabraham | a3c97d6 | 2014-04-14 01:29:45 -0700 | [diff] [blame] | 42 | processCallFormat, |
| 43 | stdin=stdin, |
| 44 | stdout=stdout, |
| 45 | stderr=subprocess.PIPE) |
jeraldabraham | 5d4d735 | 2014-03-28 02:49:04 -0700 | [diff] [blame] | 46 | 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: |
jeraldabraham | a3c97d6 | 2014-04-14 01:29:45 -0700 | [diff] [blame] | 52 | print e |
jeraldabraham | 5d4d735 | 2014-03-28 02:49:04 -0700 | [diff] [blame] | 53 | pass |
| 54 | |
jeraldabraham | a3c97d6 | 2014-04-14 01:29:45 -0700 | [diff] [blame] | 55 | def startProcess(self, |
| 56 | processKey, |
| 57 | processCallFormat, |
| 58 | message, |
| 59 | inputFile=None, |
| 60 | outputFile=None): |
jeraldabraham | 5d4d735 | 2014-03-28 02:49:04 -0700 | [diff] [blame] | 61 | self.processes[processKey] = mp.Process( |
jeraldabraham | a3c97d6 | 2014-04-14 01:29:45 -0700 | [diff] [blame] | 62 | target=self.runProcess, |
| 63 | args=[processKey, |
| 64 | processCallFormat, |
| 65 | message, |
| 66 | self.subprocesses, |
| 67 | self.results, |
| 68 | inputFile, |
| 69 | outputFile]) |
jeraldabraham | 5d4d735 | 2014-03-28 02:49:04 -0700 | [diff] [blame] | 70 | self.processes[processKey].start() |
| 71 | |
| 72 | def killProcess(self, processKey): |
jeraldabraham | b2a2655 | 2014-04-16 21:08:09 -0700 | [diff] [blame] | 73 | if processKey not in self.results and processKey in self.subprocesses: |
jeraldabraham | 5d4d735 | 2014-03-28 02:49:04 -0700 | [diff] [blame] | 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 | |
jeraldabraham | 3a2466e | 2014-04-09 00:19:45 -0700 | [diff] [blame] | 100 | 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 | |
jeraldabraham | 5d4d735 | 2014-03-28 02:49:04 -0700 | [diff] [blame] | 107 | def startNfd(self): |
| 108 | self.startProcess("nfd", ["sudo", "nfd"], "-> Starting NFD") |
| 109 | |
| 110 | def killNfd(self): |
| 111 | self.killProcess("nfd") |
jeraldabraham | 3a2466e | 2014-04-09 00:19:45 -0700 | [diff] [blame] | 112 | time.sleep(2) |