mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 1 | __author__ = """Copyright Andy Whitcroft, Martin J. Bligh - 2006, 2007""" |
| 2 | |
mbligh | 42ff92f | 2007-11-24 19:13:15 +0000 | [diff] [blame] | 3 | import sys, os, subprocess, traceback |
mbligh | 3177be4 | 2008-01-15 20:36:54 +0000 | [diff] [blame^] | 4 | from common.error import * |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 5 | |
| 6 | |
| 7 | def parallel(tasklist): |
| 8 | """Run an set of predefined subcommands in parallel""" |
| 9 | pids = [] |
| 10 | error = False |
| 11 | for task in tasklist: |
| 12 | task.fork_start() |
| 13 | for task in tasklist: |
| 14 | status = task.fork_waitfor() |
| 15 | if status != 0: |
| 16 | error = True |
| 17 | if error: |
mbligh | 4d6feff | 2008-01-14 16:48:56 +0000 | [diff] [blame] | 18 | raise AutoservError('One or more subcommands failed') |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 19 | |
| 20 | |
mbligh | 84c0ab1 | 2007-10-24 21:28:58 +0000 | [diff] [blame] | 21 | def parallel_simple(function, arglist, log=True): |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 22 | """Each element in the arglist used to create a subcommand object, |
| 23 | where that arg is used both as a subdir name, and a single argument |
| 24 | to pass to "function". |
| 25 | We create a subcommand object for each element in the list, |
| 26 | then execute those subcommand objects in parallel.""" |
mbligh | dd3235b | 2008-01-14 16:44:19 +0000 | [diff] [blame] | 27 | |
| 28 | # Bypass the multithreading if only one machine. |
| 29 | if len (arglist) == 1: |
| 30 | function(arglist[0]) |
| 31 | return |
| 32 | |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 33 | subcommands = [] |
| 34 | for arg in arglist: |
mbligh | 0c9e782 | 2007-07-25 22:47:51 +0000 | [diff] [blame] | 35 | args = [arg] |
mbligh | 84c0ab1 | 2007-10-24 21:28:58 +0000 | [diff] [blame] | 36 | if log: |
| 37 | subdir = str(arg) |
| 38 | else: |
| 39 | subdir = None |
mbligh | 0c9e782 | 2007-07-25 22:47:51 +0000 | [diff] [blame] | 40 | subcommands.append(subcommand(function, args, subdir)) |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 41 | parallel(subcommands) |
| 42 | |
| 43 | |
mbligh | cee25b1 | 2007-08-31 08:53:05 +0000 | [diff] [blame] | 44 | def _where_art_thy_filehandles(): |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 45 | os.system("ls -l /proc/%d/fd >> /dev/tty" % os.getpid()) |
| 46 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 47 | |
mbligh | cee25b1 | 2007-08-31 08:53:05 +0000 | [diff] [blame] | 48 | def _print_to_tty(string): |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 49 | open('/dev/tty', 'w').write(string + '\n') |
| 50 | |
| 51 | |
mbligh | cee25b1 | 2007-08-31 08:53:05 +0000 | [diff] [blame] | 52 | def _redirect_stream(fd, output): |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 53 | newfd = os.open(output, os.O_WRONLY | os.O_CREAT) |
| 54 | os.dup2(newfd, fd) |
| 55 | os.close(newfd) |
| 56 | if fd == 1: |
| 57 | sys.stdout = os.fdopen(fd, 'w') |
| 58 | if fd == 2: |
| 59 | sys.stderr = os.fdopen(fd, 'w') |
| 60 | |
| 61 | |
mbligh | cee25b1 | 2007-08-31 08:53:05 +0000 | [diff] [blame] | 62 | def _redirect_stream_tee(fd, output, tag): |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 63 | """Use the low-level fork & pipe operations here to get a fd, |
| 64 | not a filehandle. This ensures that we get both the |
| 65 | filehandle and fd for stdout/stderr redirected correctly.""" |
| 66 | r, w = os.pipe() |
| 67 | pid = os.fork() |
| 68 | if pid: # Parent |
| 69 | os.dup2(w, fd) |
| 70 | os.close(r) |
| 71 | os.close(w) |
| 72 | if fd == 1: |
| 73 | sys.stdout = os.fdopen(fd, 'w', 1) |
| 74 | if fd == 2: |
| 75 | sys.stderr = os.fdopen(fd, 'w', 1) |
| 76 | return |
| 77 | else: # Child |
| 78 | os.close(w) |
| 79 | log = open(output, 'w') |
| 80 | f = os.fdopen(r, 'r') |
| 81 | for line in iter(f.readline, ''): |
| 82 | # Tee straight to file |
| 83 | log.write(line) |
| 84 | log.flush() |
| 85 | # Prepend stdout with the tag |
| 86 | print tag + ' : ' + line, |
| 87 | sys.stdout.flush() |
| 88 | log.close() |
| 89 | os._exit(0) |
| 90 | |
| 91 | |
| 92 | class subcommand: |
mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 93 | def __init__(self, func, args, subdir = None, stdprint = True): |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 94 | # func(args) - the subcommand to run |
| 95 | # subdir - the subdirectory to log results in |
mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 96 | # stdprint - whether to print results to stdout/stderr |
| 97 | if subdir: |
| 98 | self.subdir = os.path.abspath(subdir) |
| 99 | if os.path.exists(self.subdir): |
| 100 | os.system("rm -rf %s" % self.subdir) |
| 101 | os.mkdir(self.subdir) |
| 102 | self.debug = os.path.join(self.subdir, 'debug') |
mbligh | c526da3 | 2007-08-28 10:07:27 +0000 | [diff] [blame] | 103 | os.mkdir(self.debug) |
mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 104 | self.stdout = os.path.join(self.debug, 'stdout') |
| 105 | self.stderr = os.path.join(self.debug, 'stderr') |
| 106 | else: |
| 107 | self.subdir = None |
| 108 | self.debug = '/dev/null' |
| 109 | self.stdout = '/dev/null' |
| 110 | self.stderr = '/dev/null' |
| 111 | |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 112 | self.func = func |
| 113 | self.args = args |
| 114 | self.lambda_function = lambda: func(*args) |
| 115 | self.pid = None |
mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 116 | self.stdprint = stdprint |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 117 | |
| 118 | |
| 119 | def redirect_output(self): |
mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 120 | if self.stdprint: |
| 121 | if self.subdir: |
| 122 | tag = os.path.basename(self.subdir) |
mbligh | cee25b1 | 2007-08-31 08:53:05 +0000 | [diff] [blame] | 123 | _redirect_stream_tee(1, self.stdout, tag) |
| 124 | _redirect_stream_tee(2, self.stderr, tag) |
mbligh | b491d02 | 2007-08-09 23:04:56 +0000 | [diff] [blame] | 125 | else: |
mbligh | cee25b1 | 2007-08-31 08:53:05 +0000 | [diff] [blame] | 126 | _redirect_stream(1, self.stdout) |
| 127 | _redirect_stream(2, self.stderr) |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 128 | |
| 129 | |
| 130 | def fork_start(self): |
| 131 | sys.stdout.flush() |
| 132 | sys.stderr.flush() |
| 133 | self.pid = os.fork() |
| 134 | |
| 135 | if self.pid: # I am the parent |
| 136 | return |
| 137 | |
| 138 | # We are the child from this point on. Never return. |
mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 139 | if self.subdir: |
| 140 | os.chdir(self.subdir) |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 141 | self.redirect_output() |
| 142 | |
| 143 | try: |
| 144 | self.lambda_function() |
| 145 | |
| 146 | except: |
mbligh | 42ff92f | 2007-11-24 19:13:15 +0000 | [diff] [blame] | 147 | traceback.print_exc() |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 148 | sys.stdout.flush() |
| 149 | sys.stderr.flush() |
| 150 | os._exit(1) |
| 151 | |
| 152 | sys.stdout.flush() |
| 153 | sys.stderr.flush() |
| 154 | os._exit(0) |
| 155 | |
| 156 | |
| 157 | def fork_waitfor(self): |
| 158 | (pid, status) = os.waitpid(self.pid, 0) |
| 159 | |
| 160 | if status != 0: |
| 161 | print "subcommand failed pid %d" % pid |
| 162 | print "%s(%s)" % (self.func, self.args) |
| 163 | print "rc=%d" % status |
| 164 | print |
| 165 | if os.path.exists(self.stderr): |
| 166 | for line in open(self.stderr).readlines(): |
| 167 | print line, |
| 168 | print "\n--------------------------------------------\n" |
| 169 | return status |