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