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 | 5250863 | 2006-09-30 18:29:55 +0000 | [diff] [blame] | 117 | """ |
| 118 | Receives either a path to a local file or a URL. |
| 119 | returns either the path to the local file, or the fetched URL |
| 120 | |
| 121 | unmap_url('/usr/src', 'foo.tar', '/tmp') |
| 122 | = '/usr/src/foo.tar' |
| 123 | unmap_url('/usr/src', 'http://site/file', '/tmp') |
| 124 | = '/tmp/file' |
| 125 | (after retrieving it) |
| 126 | """ |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 127 | if is_url(src): |
| 128 | dest = destdir + '/' + os.path.basename(src) |
| 129 | get_file(src, dest) |
| 130 | return dest |
mbligh | 8264186 | 2006-04-23 06:21:36 +0000 | [diff] [blame] | 131 | else: |
| 132 | return srcdir + '/' + src |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 133 | |
| 134 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 135 | def basename(path): |
| 136 | i = path.rfind('/'); |
| 137 | return path[i+1:] |
| 138 | |
| 139 | |
| 140 | def force_copy(src, dest): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 141 | """Replace dest with a new copy of src, even if it exists""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 142 | if os.path.isfile(dest): |
| 143 | os.remove(dest) |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 144 | if os.path.isdir(dest): |
| 145 | dest = os.path.join(dest, os.path.basename(src)) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 146 | return shutil.copyfile(src, dest) |
| 147 | |
| 148 | |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 149 | def force_link(src, dest): |
| 150 | """Link src to dest, overwriting it if it exists""" |
| 151 | return system("ln -sf %s %s" % (src, dest)) |
| 152 | |
| 153 | |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 154 | def file_contains_pattern(file, pattern): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 155 | """Return true if file contains the specified egrep pattern""" |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 156 | if not os.path.isfile(file): |
| 157 | raise NameError, 'file %s does not exist' % file |
mbligh | 5c1e26a | 2006-10-04 14:46:33 +0000 | [diff] [blame] | 158 | return not system('egrep -q "' + pattern + '" ' + file, ignorestatus = 1) |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 159 | |
| 160 | |
| 161 | def list_grep(list, pattern): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 162 | """True if any item in list matches the specified pattern.""" |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 163 | compiled = re.compile(pattern) |
| 164 | for line in list: |
| 165 | match = compiled.search(line) |
| 166 | if (match): |
| 167 | return 1 |
| 168 | return 0 |
| 169 | |
mbligh | 42b81ca | 2006-09-30 22:10:01 +0000 | [diff] [blame] | 170 | def get_os_vendor(): |
| 171 | """Try to guess what's the os vendor |
| 172 | """ |
| 173 | issue = '/etc/issue' |
| 174 | |
| 175 | if not os.path.isfile(issue): |
| 176 | return 'Unknown' |
| 177 | |
| 178 | if file_contains_pattern(issue, 'Red Hat'): |
| 179 | return 'Red Hat' |
| 180 | elif file_contains_pattern(issue, 'Fedora Core'): |
| 181 | return 'Fedora Core' |
| 182 | elif file_contains_pattern(issue, 'SUSE'): |
| 183 | return 'SUSE' |
| 184 | elif file_contains_pattern(issue, 'Ubuntu'): |
| 185 | return 'Ubuntu' |
| 186 | else: |
| 187 | return 'Unknown' |
| 188 | |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 189 | |
mbligh | f49d5cf | 2006-04-23 02:24:42 +0000 | [diff] [blame] | 190 | def get_vmlinux(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 191 | """Return the full path to vmlinux |
| 192 | |
| 193 | Ahem. This is crap. Pray harder. Bad Martin. |
| 194 | """ |
mbligh | ad849ee | 2006-10-02 17:53:18 +0000 | [diff] [blame] | 195 | vmlinux = '/boot/vmlinux-%s' % system_output('uname -r') |
mbligh | 662a2a2 | 2006-10-01 17:01:14 +0000 | [diff] [blame] | 196 | if os.path.isfile(vmlinux): |
| 197 | return vmlinux |
mbligh | 17dbf05 | 2006-10-12 04:46:10 +0000 | [diff] [blame] | 198 | vmlinux = '/lib/modules/%s/build/vmlinux' % system_output('uname -r') |
| 199 | if os.path.isfile(vmlinux): |
| 200 | return vmlinux |
mbligh | 662a2a2 | 2006-10-01 17:01:14 +0000 | [diff] [blame] | 201 | return None |
mbligh | f49d5cf | 2006-04-23 02:24:42 +0000 | [diff] [blame] | 202 | |
| 203 | |
| 204 | def get_systemmap(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 205 | """Return the full path to System.map |
| 206 | |
| 207 | Ahem. This is crap. Pray harder. Bad Martin. |
| 208 | """ |
mbligh | ad849ee | 2006-10-02 17:53:18 +0000 | [diff] [blame] | 209 | map = '/boot/System.map-%s' % system_output('uname -r') |
mbligh | 662a2a2 | 2006-10-01 17:01:14 +0000 | [diff] [blame] | 210 | if os.path.isfile(map): |
| 211 | return map |
mbligh | 17dbf05 | 2006-10-12 04:46:10 +0000 | [diff] [blame] | 212 | map = '/lib/modules/%s/build/System.map' % system_output('uname -r') |
| 213 | if os.path.isfile(map): |
| 214 | return map |
mbligh | 662a2a2 | 2006-10-01 17:01:14 +0000 | [diff] [blame] | 215 | return None |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 216 | |
| 217 | |
| 218 | def get_modules_dir(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 219 | """Return the modules dir for the running kernel version""" |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 220 | kernel_version = system_output('uname -r') |
| 221 | return '/lib/modules/%s/kernel' % kernel_version |
mbligh | f49d5cf | 2006-04-23 02:24:42 +0000 | [diff] [blame] | 222 | |
| 223 | |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 224 | def get_cpu_arch(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 225 | """Work out which CPU architecture we're running on""" |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 226 | f = open('/proc/cpuinfo', 'r') |
| 227 | cpuinfo = f.readlines() |
| 228 | f.close() |
| 229 | if list_grep(cpuinfo, '^cpu.*(RS64|POWER3|Broadband Engine)'): |
| 230 | return 'power' |
| 231 | elif list_grep(cpuinfo, '^cpu.*POWER4'): |
| 232 | return 'power4' |
| 233 | elif list_grep(cpuinfo, '^cpu.*POWER5'): |
| 234 | return 'power5' |
| 235 | elif list_grep(cpuinfo, '^cpu.*POWER6'): |
| 236 | return 'power6' |
| 237 | elif list_grep(cpuinfo, '^cpu.*PPC970'): |
| 238 | return 'power970' |
| 239 | elif list_grep(cpuinfo, 'Opteron'): |
| 240 | return 'x86_64' |
| 241 | elif list_grep(cpuinfo, 'GenuineIntel') and list_grep(cpuinfo, '48 bits virtual'): |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 242 | return 'x86_64' |
| 243 | else: |
| 244 | return 'i386' |
| 245 | |
| 246 | |
mbligh | 548f29a | 2006-10-17 04:55:12 +0000 | [diff] [blame] | 247 | def get_current_kernel_arch(): |
| 248 | """Get the machine architecture, now just a wrap of 'uname -m'.""" |
| 249 | return os.popen('uname -m').read().rstrip() |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 250 | |
| 251 | |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 252 | def get_file_arch(filename): |
| 253 | # -L means follow symlinks |
| 254 | file_data = system_output('file -L ' + filename) |
| 255 | if file_data.count('80386'): |
| 256 | return 'i386' |
| 257 | return None |
| 258 | |
| 259 | |
mbligh | 548f29a | 2006-10-17 04:55:12 +0000 | [diff] [blame] | 260 | def kernelexpand(kernel, args=None): |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 261 | # 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] | 262 | if kernel.find('/') < 0: # contains no path. |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 263 | autodir = os.environ['AUTODIR'] |
| 264 | kernelexpand = os.path.join(autodir, 'tools/kernelexpand') |
mbligh | 548f29a | 2006-10-17 04:55:12 +0000 | [diff] [blame] | 265 | if args: |
| 266 | kernelexpand += ' ' + args |
mbligh | 5629af0 | 2006-09-15 01:54:23 +0000 | [diff] [blame] | 267 | w, r = os.popen2(kernelexpand + ' ' + kernel) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 268 | |
| 269 | kernel = r.readline().strip() |
| 270 | r.close() |
| 271 | w.close() |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 272 | return kernel.split() |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 273 | |
| 274 | |
| 275 | def count_cpus(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 276 | """number of CPUs in the local machine according to /proc/cpuinfo""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 277 | f = file('/proc/cpuinfo', 'r') |
| 278 | cpus = 0 |
| 279 | for line in f.readlines(): |
| 280 | if line.startswith('processor'): |
| 281 | cpus += 1 |
| 282 | return cpus |
| 283 | |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 284 | def system(cmd, ignorestatus = 0): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 285 | """os.system replacement |
| 286 | |
| 287 | We have our own definition of system here, as the stock os.system doesn't |
| 288 | correctly handle sigpipe |
| 289 | (ie things like "yes | head" will hang because yes doesn't get the SIGPIPE). |
| 290 | |
| 291 | Also the stock os.system didn't raise errors based on exit status, this |
| 292 | version does unless you explicitly specify ignorestatus=1 |
| 293 | """ |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 294 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) |
| 295 | try: |
apw | bc2867d | 2006-04-06 18:21:16 +0000 | [diff] [blame] | 296 | status = os.system(cmd) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 297 | finally: |
| 298 | signal.signal(signal.SIGPIPE, signal.SIG_IGN) |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 299 | |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 300 | if ((status != 0) and not ignorestatus): |
apw | bc2867d | 2006-04-06 18:21:16 +0000 | [diff] [blame] | 301 | raise CmdError(cmd, status) |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 302 | return status |
mbligh | 3d91491 | 2006-04-22 17:37:19 +0000 | [diff] [blame] | 303 | |
| 304 | |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 305 | def system_output(command, ignorestatus = 0): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 306 | """Run command and return its output |
| 307 | |
| 308 | ignorestatus |
| 309 | whether to raise a CmdError if command has a nonzero exit status |
| 310 | """ |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 311 | (result, data) = commands.getstatusoutput(command) |
| 312 | if ((result != 0) and not ignorestatus): |
| 313 | raise CmdError, 'command failed: ' + command |
| 314 | return data |
| 315 | |
| 316 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 317 | def where_art_thy_filehandles(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 318 | """Dump the current list of filehandles""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 319 | os.system("ls -l /proc/%d/fd >> /dev/tty" % os.getpid()) |
| 320 | |
| 321 | |
| 322 | def print_to_tty(string): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 323 | """Output string straight to the tty""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 324 | os.system("echo " + string + " >> /dev/tty") |
| 325 | |
| 326 | |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 327 | def dump_object(object): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 328 | """Dump an object's attributes and methods |
| 329 | |
| 330 | kind of like dir() |
| 331 | """ |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 332 | for item in object.__dict__.iteritems(): |
| 333 | print item |
| 334 | try: |
| 335 | (key,value) = item |
| 336 | dump_object(value) |
| 337 | except: |
| 338 | continue |
| 339 | |
| 340 | |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 341 | def environ(env_key): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 342 | """return the requested environment variable, or '' if unset""" |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 343 | if (os.environ.has_key(env_key)): |
mbligh | d931a58 | 2006-10-01 00:30:12 +0000 | [diff] [blame] | 344 | return os.environ[env_key] |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 345 | else: |
| 346 | return '' |
| 347 | |
| 348 | |
| 349 | def prepend_path(newpath, oldpath): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 350 | """prepend newpath to oldpath""" |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 351 | if (oldpath): |
| 352 | return newpath + ':' + oldpath |
| 353 | else: |
| 354 | return newpath |
| 355 | |
| 356 | |
| 357 | def append_path(oldpath, newpath): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 358 | """append newpath to oldpath""" |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 359 | if (oldpath): |
| 360 | return oldpath + ':' + newpath |
| 361 | else: |
| 362 | return newpath |
| 363 | |
| 364 | |
mbligh | 4e75b0d | 2006-08-29 15:22:44 +0000 | [diff] [blame] | 365 | def avgtime_print(dir): |
| 366 | """ Calculate some benchmarking statistics. |
| 367 | Input is a directory containing a file called 'time'. |
| 368 | File contains one-per-line results of /usr/bin/time. |
| 369 | Output is average Elapsed, User, and System time in seconds, |
| 370 | and average CPU percentage. |
| 371 | """ |
| 372 | f = open(dir + "/time") |
| 373 | user = system = elapsed = cpu = count = 0 |
| 374 | r = re.compile('([\d\.]*)user ([\d\.]*)system (\d*):([\d\.]*)elapsed (\d*)%CPU') |
| 375 | for line in f.readlines(): |
| 376 | try: |
| 377 | s = r.match(line); |
mbligh | e1ee267 | 2006-08-29 16:27:15 +0000 | [diff] [blame] | 378 | user += float(s.group(1)) |
| 379 | system += float(s.group(2)) |
| 380 | elapsed += (float(s.group(3)) * 60) + float(s.group(4)) |
| 381 | cpu += float(s.group(5)) |
mbligh | 4e75b0d | 2006-08-29 15:22:44 +0000 | [diff] [blame] | 382 | count += 1 |
| 383 | except: |
| 384 | raise ValueError("badly formatted times") |
| 385 | |
| 386 | f.close() |
| 387 | return "Elapsed: %0.2fs User: %0.2fs System: %0.2fs CPU: %0.0f%%" % \ |
| 388 | (elapsed/count, user/count, system/count, cpu/count) |
| 389 | |
| 390 | |
mbligh | f06db0f | 2006-09-30 17:08:43 +0000 | [diff] [blame] | 391 | def running_config(): |
| 392 | """ |
| 393 | Return path of config file of the currently running kernel |
| 394 | """ |
| 395 | for config in ('/proc/config.gz', \ |
| 396 | '/boot/config-%s' % system_output('uname -r') ): |
| 397 | if os.path.isfile(config): |
| 398 | return config |
| 399 | return None |
mbligh | 9ec8acc | 2006-10-05 06:52:33 +0000 | [diff] [blame] | 400 | |
| 401 | |
| 402 | def cpu_online_map(): |
| 403 | """ |
| 404 | Check out the available cpu online map |
| 405 | """ |
| 406 | cpus = [] |
| 407 | for line in open('/proc/cpuinfo', 'r').readlines(): |
| 408 | if line.startswith('processor'): |
| 409 | cpus.append(line.split()[2]) # grab cpu number |
| 410 | return cpus |
mbligh | 663e4f6 | 2006-10-11 05:03:40 +0000 | [diff] [blame] | 411 | |
| 412 | |
| 413 | def check_glibc_ver(ver): |
| 414 | glibc_ver = commands.getoutput('ldd --version').splitlines()[0] |
mbligh | cabfdaf | 2006-10-11 14:07:48 +0000 | [diff] [blame] | 415 | glibc_ver = re.search(r'(\d+\.\d+(\.\d+)?)', glibc_ver).group() |
mbligh | ea97ab8 | 2006-10-13 20:18:01 +0000 | [diff] [blame] | 416 | if glibc_ver.split('.') < ver.split('.'): |
| 417 | raise "Glibc is too old (%s). Glibc >= %s is needed." % \ |
| 418 | (glibc_ver, ver) |
mbligh | 663e4f6 | 2006-10-11 05:03:40 +0000 | [diff] [blame] | 419 | |
mbligh | ea97ab8 | 2006-10-13 20:18:01 +0000 | [diff] [blame] | 420 | |
| 421 | def check_python_version(): |
| 422 | py_version = (sys.version).split(' ')[0] |
| 423 | version = py_version.split('.')[0:2] |
| 424 | if [int(x) for x in version] < [2, 4]: |
apw | 1f22fe6 | 2006-11-28 10:03:51 +0000 | [diff] [blame^] | 425 | for new in ('/usr/bin/python2.4', '/usr/local/bin/python2.4'): |
| 426 | if os.path.exists(new): |
| 427 | sys.argv.insert(0, new) |
| 428 | os.execv(sys.argv[0], sys.argv) |
mbligh | ea97ab8 | 2006-10-13 20:18:01 +0000 | [diff] [blame] | 429 | raise "Python 2.4 or newer is needed" |