mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 1 | """Convenience functions for use by tests or whomever. |
| 2 | """ |
| 3 | |
mbligh | ff88e4e | 2006-05-25 19:34:52 +0000 | [diff] [blame] | 4 | import os,os.path,shutil,urllib,sys,signal,commands,pickle |
apw | bc2867d | 2006-04-06 18:21:16 +0000 | [diff] [blame] | 5 | from error import * |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 6 | import re |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 7 | |
| 8 | def grep(pattern, file): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 9 | """This is mainly to fix the return code inversion from grep""" |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 10 | return not system('grep "' + pattern + '" "' + file + '"') |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 11 | |
| 12 | |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 13 | def difflist(list1, list2): |
| 14 | """returns items in list2 that are not in list1""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 15 | diff = []; |
| 16 | for x in list2: |
| 17 | if x not in list1: |
| 18 | diff.append(x) |
| 19 | return diff |
| 20 | |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 21 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 22 | def cat_file_to_cmd(file, command): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 23 | """equivalent to 'cat file | command' but knows to use zcat or bzcat if appropriate""" |
mbligh | 712cd14 | 2006-04-22 18:57:50 +0000 | [diff] [blame] | 24 | if not os.path.isfile(file): |
| 25 | raise NameError, 'invalid file %s to cat to command %s' % file, command |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 26 | if file.endswith('.bz2'): |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 27 | system('bzcat ' + file + ' | ' + command) |
mbligh | 3d91491 | 2006-04-22 17:37:19 +0000 | [diff] [blame] | 28 | elif (file.endswith('.gz') or file.endswith('.tgz')): |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 29 | system('zcat ' + file + ' | ' + command) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 30 | else: |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 31 | system('cat ' + file + ' | ' + command) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 32 | |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 33 | |
mbligh | 712cd14 | 2006-04-22 18:57:50 +0000 | [diff] [blame] | 34 | def extract_tarball_to_dir(tarball, dir): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 35 | """ |
| 36 | Extract a tarball to a specified directory name instead of whatever |
| 37 | the top level of a tarball is - useful for versioned directory names, etc |
| 38 | """ |
mbligh | 712cd14 | 2006-04-22 18:57:50 +0000 | [diff] [blame] | 39 | if os.path.exists(dir): |
| 40 | raise NameError, 'target %s already exists' % dir |
| 41 | pwd = os.getcwd() |
| 42 | os.chdir(os.path.dirname(os.path.abspath(dir))) |
| 43 | newdir = extract_tarball(tarball) |
| 44 | os.rename(newdir, dir) |
| 45 | os.chdir(pwd) |
| 46 | |
| 47 | |
mbligh | 712cd14 | 2006-04-22 18:57:50 +0000 | [diff] [blame] | 48 | def extract_tarball(tarball): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 49 | """Returns the first found newly created directory by the tarball extraction""" |
mbligh | 712cd14 | 2006-04-22 18:57:50 +0000 | [diff] [blame] | 50 | oldlist = os.listdir('.') |
| 51 | cat_file_to_cmd(tarball, 'tar xf -') |
| 52 | newlist = os.listdir('.') |
| 53 | newfiles = difflist(oldlist, newlist) # what is new dir ? |
| 54 | new_dir = None |
| 55 | for newfile in newfiles: |
| 56 | if (os.path.isdir(newfile)): |
| 57 | return newfile |
| 58 | raise NameError, "extracting tarball produced no dir" |
| 59 | |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 60 | |
mbligh | 7290556 | 2006-05-25 01:30:49 +0000 | [diff] [blame] | 61 | def update_version(srcdir, new_version, install): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 62 | """Make sure srcdir is version new_version |
| 63 | |
| 64 | If not, delete it and install() the new version |
| 65 | """ |
mbligh | 7290556 | 2006-05-25 01:30:49 +0000 | [diff] [blame] | 66 | versionfile = srcdir + '/.version' |
mbligh | ff88e4e | 2006-05-25 19:34:52 +0000 | [diff] [blame] | 67 | if os.path.exists(srcdir): |
| 68 | if os.path.exists(versionfile): |
| 69 | old_version = pickle.load(open(versionfile, 'r')) |
| 70 | if (old_version != new_version): |
| 71 | system('rm -rf ' + srcdir) |
| 72 | else: |
mbligh | 7290556 | 2006-05-25 01:30:49 +0000 | [diff] [blame] | 73 | system('rm -rf ' + srcdir) |
| 74 | if not os.path.exists(srcdir): |
| 75 | install() |
| 76 | if os.path.exists(srcdir): |
| 77 | pickle.dump(new_version, open(versionfile, 'w')) |
| 78 | |
| 79 | |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 80 | def is_url(path): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 81 | """true if path is a url |
| 82 | """ |
| 83 | # should cope with other url types here, but we only handle http and ftp |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 84 | if (path.startswith('http://')) or (path.startswith('ftp://')): |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 85 | return 1 |
| 86 | return 0 |
| 87 | |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 88 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 89 | def get_file(src, dest): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 90 | """get a file, either from url or local""" |
mbligh | 3118661 | 2006-04-22 21:55:56 +0000 | [diff] [blame] | 91 | if (src == dest): # no-op here allows clean overrides in tests |
| 92 | return |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 93 | if (is_url(src)): |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 94 | print 'PWD: ' + os.getcwd() |
| 95 | print 'Fetching \n\t', src, '\n\t->', dest |
| 96 | try: |
| 97 | urllib.urlretrieve(src, dest) |
| 98 | except IOError: |
| 99 | sys.stderr.write("Unable to retrieve %s (to %s)\n" % (src, dest)) |
| 100 | sys.exit(1) |
| 101 | return dest |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 102 | shutil.copyfile(src, dest) |
| 103 | return dest |
| 104 | |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 105 | |
mbligh | 8264186 | 2006-04-23 06:21:36 +0000 | [diff] [blame] | 106 | def unmap_url(srcdir, src, destdir = '.'): |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 107 | if is_url(src): |
| 108 | dest = destdir + '/' + os.path.basename(src) |
| 109 | get_file(src, dest) |
| 110 | return dest |
mbligh | 8264186 | 2006-04-23 06:21:36 +0000 | [diff] [blame] | 111 | else: |
| 112 | return srcdir + '/' + src |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 113 | |
| 114 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 115 | def basename(path): |
| 116 | i = path.rfind('/'); |
| 117 | return path[i+1:] |
| 118 | |
| 119 | |
| 120 | def force_copy(src, dest): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 121 | """Replace dest with a new copy of src, even if it exists""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 122 | if os.path.isfile(dest): |
| 123 | os.remove(dest) |
| 124 | return shutil.copyfile(src, dest) |
| 125 | |
| 126 | |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 127 | def file_contains_pattern(file, pattern): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 128 | """Return true if file contains the specified egrep pattern""" |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 129 | if not os.path.isfile(file): |
| 130 | raise NameError, 'file %s does not exist' % file |
| 131 | return not system('egrep -q ' + pattern + ' ' + file, ignorestatus = 1) |
| 132 | |
| 133 | |
| 134 | def list_grep(list, pattern): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 135 | """True if any item in list matches the specified pattern.""" |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 136 | compiled = re.compile(pattern) |
| 137 | for line in list: |
| 138 | match = compiled.search(line) |
| 139 | if (match): |
| 140 | return 1 |
| 141 | return 0 |
| 142 | |
| 143 | |
mbligh | f49d5cf | 2006-04-23 02:24:42 +0000 | [diff] [blame] | 144 | def get_vmlinux(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 145 | """Return the full path to vmlinux |
| 146 | |
| 147 | Ahem. This is crap. Pray harder. Bad Martin. |
| 148 | """ |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 149 | vmlinux = '/boot/vmlinux' |
| 150 | if not os.path.isfile(vmlinux): |
| 151 | raise NameError, 'Cannot find vmlinux' |
| 152 | return vmlinux |
mbligh | f49d5cf | 2006-04-23 02:24:42 +0000 | [diff] [blame] | 153 | |
| 154 | |
| 155 | def get_systemmap(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 156 | """Return the full path to System.map |
| 157 | |
| 158 | Ahem. This is crap. Pray harder. Bad Martin. |
| 159 | """ |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 160 | map = '/boot/System.map' |
| 161 | if not os.path.isfile(map): |
| 162 | raise NameError, 'Cannot find System.map' |
| 163 | return map |
| 164 | |
| 165 | |
| 166 | def get_modules_dir(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 167 | """Return the modules dir for the running kernel version""" |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 168 | kernel_version = system_output('uname -r') |
| 169 | return '/lib/modules/%s/kernel' % kernel_version |
mbligh | f49d5cf | 2006-04-23 02:24:42 +0000 | [diff] [blame] | 170 | |
| 171 | |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame^] | 172 | def get_cpu_arch(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 173 | """Work out which CPU architecture we're running on""" |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 174 | f = open('/proc/cpuinfo', 'r') |
| 175 | cpuinfo = f.readlines() |
| 176 | f.close() |
| 177 | if list_grep(cpuinfo, '^cpu.*(RS64|POWER3|Broadband Engine)'): |
| 178 | return 'power' |
| 179 | elif list_grep(cpuinfo, '^cpu.*POWER4'): |
| 180 | return 'power4' |
| 181 | elif list_grep(cpuinfo, '^cpu.*POWER5'): |
| 182 | return 'power5' |
| 183 | elif list_grep(cpuinfo, '^cpu.*POWER6'): |
| 184 | return 'power6' |
| 185 | elif list_grep(cpuinfo, '^cpu.*PPC970'): |
| 186 | return 'power970' |
| 187 | elif list_grep(cpuinfo, 'Opteron'): |
| 188 | return 'x86_64' |
| 189 | elif list_grep(cpuinfo, 'GenuineIntel') and list_grep(cpuinfo, '48 bits virtual'): |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 190 | return 'x86_64' |
| 191 | else: |
| 192 | return 'i386' |
| 193 | |
| 194 | |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame^] | 195 | def get_kernel_arch(): |
| 196 | """Work out the current kernel architecture (as a kernel arch)""" |
| 197 | arch = os.popen('uname -m').read().rstrip() |
| 198 | if ((arch == 'i586') or (arch == 'i686')): |
| 199 | return 'i386' |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 200 | else: |
| 201 | return arch |
| 202 | |
| 203 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 204 | def kernelexpand(kernel): |
| 205 | # if not (kernel.startswith('http://') or kernel.startswith('ftp://') or os.path.isfile(kernel)): |
| 206 | if kernel.find('/'): |
| 207 | w, r = os.popen2('./kernelexpand ' + kernel) |
| 208 | |
| 209 | kernel = r.readline().strip() |
| 210 | r.close() |
| 211 | w.close() |
| 212 | return kernel |
| 213 | |
| 214 | |
| 215 | def count_cpus(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 216 | """number of CPUs in the local machine according to /proc/cpuinfo""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 217 | f = file('/proc/cpuinfo', 'r') |
| 218 | cpus = 0 |
| 219 | for line in f.readlines(): |
| 220 | if line.startswith('processor'): |
| 221 | cpus += 1 |
| 222 | return cpus |
| 223 | |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 224 | def system(cmd, ignorestatus = 0): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 225 | """os.system replacement |
| 226 | |
| 227 | We have our own definition of system here, as the stock os.system doesn't |
| 228 | correctly handle sigpipe |
| 229 | (ie things like "yes | head" will hang because yes doesn't get the SIGPIPE). |
| 230 | |
| 231 | Also the stock os.system didn't raise errors based on exit status, this |
| 232 | version does unless you explicitly specify ignorestatus=1 |
| 233 | """ |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 234 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) |
| 235 | try: |
apw | bc2867d | 2006-04-06 18:21:16 +0000 | [diff] [blame] | 236 | status = os.system(cmd) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 237 | finally: |
| 238 | signal.signal(signal.SIGPIPE, signal.SIG_IGN) |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 239 | |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 240 | if ((status != 0) and not ignorestatus): |
apw | bc2867d | 2006-04-06 18:21:16 +0000 | [diff] [blame] | 241 | raise CmdError(cmd, status) |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 242 | return status |
mbligh | 3d91491 | 2006-04-22 17:37:19 +0000 | [diff] [blame] | 243 | |
| 244 | |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 245 | def system_output(command, ignorestatus = 0): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 246 | """Run command and return its output |
| 247 | |
| 248 | ignorestatus |
| 249 | whether to raise a CmdError if command has a nonzero exit status |
| 250 | """ |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 251 | (result, data) = commands.getstatusoutput(command) |
| 252 | if ((result != 0) and not ignorestatus): |
| 253 | raise CmdError, 'command failed: ' + command |
| 254 | return data |
| 255 | |
| 256 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 257 | def where_art_thy_filehandles(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 258 | """Dump the current list of filehandles""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 259 | os.system("ls -l /proc/%d/fd >> /dev/tty" % os.getpid()) |
| 260 | |
| 261 | |
| 262 | def print_to_tty(string): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 263 | """Output string straight to the tty""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 264 | os.system("echo " + string + " >> /dev/tty") |
| 265 | |
| 266 | |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 267 | def dump_object(object): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 268 | """Dump an object's attributes and methods |
| 269 | |
| 270 | kind of like dir() |
| 271 | """ |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 272 | for item in object.__dict__.iteritems(): |
| 273 | print item |
| 274 | try: |
| 275 | (key,value) = item |
| 276 | dump_object(value) |
| 277 | except: |
| 278 | continue |
| 279 | |
| 280 | |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 281 | def environ(env_key): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 282 | """return the requested environment variable, or '' if unset""" |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 283 | if (os.environ.has_key(env_key)): |
| 284 | return os.environ(env_key) |
| 285 | else: |
| 286 | return '' |
| 287 | |
| 288 | |
| 289 | def prepend_path(newpath, oldpath): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 290 | """prepend newpath to oldpath""" |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 291 | if (oldpath): |
| 292 | return newpath + ':' + oldpath |
| 293 | else: |
| 294 | return newpath |
| 295 | |
| 296 | |
| 297 | def append_path(oldpath, newpath): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 298 | """append newpath to oldpath""" |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 299 | if (oldpath): |
| 300 | return oldpath + ':' + newpath |
| 301 | else: |
| 302 | return newpath |
| 303 | |
| 304 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 305 | class fd_stack: |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 306 | """a stack of fd redirects |
| 307 | |
| 308 | Redirects cause existing fd's to be pushed on the stack; restore() |
| 309 | causes the current set of redirects to be popped, restoring the previous |
| 310 | filehandle destinations. |
| 311 | |
| 312 | Note that we need to redirect both the sys.stdout type descriptor |
| 313 | (which print, etc use) and the low level OS numbered descriptor |
| 314 | which os.system() etc use. |
| 315 | """ |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 316 | |
| 317 | def __init__(self, fd, filehandle): |
| 318 | self.fd = fd # eg 1 |
| 319 | self.filehandle = filehandle # eg sys.stdout |
| 320 | self.stack = [(fd, filehandle)] |
| 321 | |
| 322 | |
| 323 | def redirect(self, filename): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 324 | """Redirect output to the specified file |
| 325 | |
| 326 | Overwrites the previous contents, if any. |
| 327 | """ |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 328 | fdcopy = os.dup(self.fd) |
| 329 | self.stack.append( (fdcopy, self.filehandle) ) |
| 330 | # self.filehandle = file(filename, 'w') |
| 331 | if (os.path.isfile(filename)): |
| 332 | newfd = os.open(filename, os.O_WRONLY) |
| 333 | else: |
| 334 | newfd = os.open(filename, os.O_WRONLY | os.O_CREAT) |
| 335 | os.dup2(newfd, self.fd) |
| 336 | os.close(newfd) |
| 337 | self.filehandle = os.fdopen(self.fd, 'w') |
| 338 | |
| 339 | |
| 340 | def tee_redirect(self, filename): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 341 | """Tee output to the specified file |
| 342 | |
| 343 | Overwrites the previous contents, if any. |
| 344 | """ |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 345 | print_to_tty("tee_redirect to " + filename) |
| 346 | where_art_thy_filehandles() |
| 347 | fdcopy = os.dup(self.fd) |
| 348 | self.stack.append( (fdcopy, self.filehandle) ) |
| 349 | r, w = os.pipe() |
| 350 | pid = os.fork() |
| 351 | if pid: # parent |
| 352 | os.close(r) |
| 353 | os.dup2(w, self.fd) |
| 354 | os.close(w) |
| 355 | else: # child |
| 356 | os.close(w) |
| 357 | os.dup2(r, 0) |
| 358 | os.dup2(2, 1) |
| 359 | os.execlp('tee', 'tee', filename) |
| 360 | self.filehandle = os.fdopen(self.fd, 'w') |
| 361 | where_art_thy_filehandles() |
| 362 | print_to_tty("done tee_redirect to " + filename) |
| 363 | |
| 364 | |
| 365 | def restore(self): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 366 | """unredirect one level""" |
mbligh | cf31533 | 2006-04-02 19:58:30 +0000 | [diff] [blame] | 367 | # print_to_tty("ENTERING RESTORE %d" % self.fd) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 368 | # where_art_thy_filehandles() |
| 369 | (old_fd, old_filehandle) = self.stack.pop() |
| 370 | # print_to_tty("old_fd %d" % old_fd) |
| 371 | # print_to_tty("self.fd %d" % self.fd) |
| 372 | self.filehandle.close() # seems to close old_fd as well. |
| 373 | # where_art_thy_filehandles() |
| 374 | os.dup2(old_fd, self.fd) |
| 375 | # print_to_tty("CLOSING FD %d" % old_fd) |
| 376 | os.close(old_fd) |
| 377 | # where_art_thy_filehandles() |
| 378 | self.filehandle = old_filehandle |
| 379 | # where_art_thy_filehandles() |
| 380 | # print_to_tty("EXIT RESTORE %d" % self.fd) |