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