mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 1 | """Convenience functions for use by tests or whomever. |
| 2 | """ |
| 3 | |
mbligh | 8eca3a9 | 2007-02-03 20:59:39 +0000 | [diff] [blame] | 4 | import os,os.path,shutil,urllib,sys,signal,commands,pickle,glob |
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 | c69530b | 2007-03-25 20:20:21 +0000 | [diff] [blame] | 71 | def update_version(srcdir, preserve_srcdir, new_version, install, *args, **dargs): |
| 72 | """ |
| 73 | Make sure srcdir is version new_version |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 74 | |
mbligh | c69530b | 2007-03-25 20:20:21 +0000 | [diff] [blame] | 75 | If not, delete it and install() the new version. |
| 76 | |
| 77 | In the preserve_srcdir case, we just check it's up to date, |
| 78 | and if not, we rerun install, without removing srcdir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 79 | """ |
mbligh | 7290556 | 2006-05-25 01:30:49 +0000 | [diff] [blame] | 80 | versionfile = srcdir + '/.version' |
mbligh | c69530b | 2007-03-25 20:20:21 +0000 | [diff] [blame] | 81 | install_needed = True |
| 82 | |
mbligh | ff88e4e | 2006-05-25 19:34:52 +0000 | [diff] [blame] | 83 | if os.path.exists(srcdir): |
| 84 | if os.path.exists(versionfile): |
| 85 | old_version = pickle.load(open(versionfile, 'r')) |
mbligh | c69530b | 2007-03-25 20:20:21 +0000 | [diff] [blame] | 86 | if (old_version == new_version): |
| 87 | install_needed = False |
| 88 | |
| 89 | if install_needed: |
| 90 | if not preserve_srcdir: |
mbligh | 7290556 | 2006-05-25 01:30:49 +0000 | [diff] [blame] | 91 | system('rm -rf ' + srcdir) |
mbligh | 78e1702 | 2006-09-13 19:58:12 +0000 | [diff] [blame] | 92 | install(*args, **dargs) |
mbligh | 7290556 | 2006-05-25 01:30:49 +0000 | [diff] [blame] | 93 | if os.path.exists(srcdir): |
| 94 | pickle.dump(new_version, open(versionfile, 'w')) |
mbligh | dfe8cc6 | 2007-07-23 18:23:17 +0000 | [diff] [blame] | 95 | |
mbligh | 7290556 | 2006-05-25 01:30:49 +0000 | [diff] [blame] | 96 | |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 97 | def is_url(path): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 98 | """true if path is a url |
| 99 | """ |
| 100 | # 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] | 101 | if (path.startswith('http://')) or (path.startswith('ftp://')): |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 102 | return 1 |
| 103 | return 0 |
| 104 | |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 105 | |
mbligh | 8eca3a9 | 2007-02-03 20:59:39 +0000 | [diff] [blame] | 106 | def get_file(src, dest, permissions = None): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 107 | """get a file, either from url or local""" |
mbligh | 3118661 | 2006-04-22 21:55:56 +0000 | [diff] [blame] | 108 | if (src == dest): # no-op here allows clean overrides in tests |
| 109 | return |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 110 | if (is_url(src)): |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 111 | print 'PWD: ' + os.getcwd() |
| 112 | print 'Fetching \n\t', src, '\n\t->', dest |
| 113 | try: |
| 114 | urllib.urlretrieve(src, dest) |
| 115 | except IOError: |
| 116 | sys.stderr.write("Unable to retrieve %s (to %s)\n" % (src, dest)) |
| 117 | sys.exit(1) |
mbligh | 8eca3a9 | 2007-02-03 20:59:39 +0000 | [diff] [blame] | 118 | else: |
| 119 | shutil.copyfile(src, dest) |
| 120 | if permissions: |
| 121 | os.chmod(permissions) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 122 | return dest |
| 123 | |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 124 | |
mbligh | 8264186 | 2006-04-23 06:21:36 +0000 | [diff] [blame] | 125 | def unmap_url(srcdir, src, destdir = '.'): |
mbligh | 5250863 | 2006-09-30 18:29:55 +0000 | [diff] [blame] | 126 | """ |
| 127 | Receives either a path to a local file or a URL. |
| 128 | returns either the path to the local file, or the fetched URL |
| 129 | |
| 130 | unmap_url('/usr/src', 'foo.tar', '/tmp') |
| 131 | = '/usr/src/foo.tar' |
| 132 | unmap_url('/usr/src', 'http://site/file', '/tmp') |
| 133 | = '/tmp/file' |
| 134 | (after retrieving it) |
| 135 | """ |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 136 | if is_url(src): |
| 137 | dest = destdir + '/' + os.path.basename(src) |
| 138 | get_file(src, dest) |
| 139 | return dest |
mbligh | 8264186 | 2006-04-23 06:21:36 +0000 | [diff] [blame] | 140 | else: |
| 141 | return srcdir + '/' + src |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 142 | |
| 143 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 144 | def basename(path): |
| 145 | i = path.rfind('/'); |
| 146 | return path[i+1:] |
| 147 | |
| 148 | |
| 149 | def force_copy(src, dest): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 150 | """Replace dest with a new copy of src, even if it exists""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 151 | if os.path.isfile(dest): |
| 152 | os.remove(dest) |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 153 | if os.path.isdir(dest): |
| 154 | dest = os.path.join(dest, os.path.basename(src)) |
mbligh | ad0eda2 | 2007-08-30 16:19:54 +0000 | [diff] [blame] | 155 | shutil.copyfile(src, dest) |
| 156 | return dest |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 157 | |
| 158 | |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 159 | def force_link(src, dest): |
| 160 | """Link src to dest, overwriting it if it exists""" |
| 161 | return system("ln -sf %s %s" % (src, dest)) |
| 162 | |
| 163 | |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 164 | def file_contains_pattern(file, pattern): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 165 | """Return true if file contains the specified egrep pattern""" |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 166 | if not os.path.isfile(file): |
| 167 | raise NameError, 'file %s does not exist' % file |
mbligh | 5c1e26a | 2006-10-04 14:46:33 +0000 | [diff] [blame] | 168 | return not system('egrep -q "' + pattern + '" ' + file, ignorestatus = 1) |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 169 | |
| 170 | |
| 171 | def list_grep(list, pattern): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 172 | """True if any item in list matches the specified pattern.""" |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 173 | compiled = re.compile(pattern) |
| 174 | for line in list: |
| 175 | match = compiled.search(line) |
| 176 | if (match): |
| 177 | return 1 |
| 178 | return 0 |
| 179 | |
mbligh | 42b81ca | 2006-09-30 22:10:01 +0000 | [diff] [blame] | 180 | def get_os_vendor(): |
| 181 | """Try to guess what's the os vendor |
| 182 | """ |
| 183 | issue = '/etc/issue' |
| 184 | |
| 185 | if not os.path.isfile(issue): |
| 186 | return 'Unknown' |
| 187 | |
| 188 | if file_contains_pattern(issue, 'Red Hat'): |
| 189 | return 'Red Hat' |
| 190 | elif file_contains_pattern(issue, 'Fedora Core'): |
| 191 | return 'Fedora Core' |
| 192 | elif file_contains_pattern(issue, 'SUSE'): |
| 193 | return 'SUSE' |
| 194 | elif file_contains_pattern(issue, 'Ubuntu'): |
| 195 | return 'Ubuntu' |
apw | 39f9cac | 2007-02-28 15:26:05 +0000 | [diff] [blame] | 196 | elif file_contains_pattern(issue, 'Debian'): |
| 197 | return 'Debian' |
mbligh | 42b81ca | 2006-09-30 22:10:01 +0000 | [diff] [blame] | 198 | else: |
| 199 | return 'Unknown' |
| 200 | |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 201 | |
mbligh | f49d5cf | 2006-04-23 02:24:42 +0000 | [diff] [blame] | 202 | def get_vmlinux(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 203 | """Return the full path to vmlinux |
| 204 | |
| 205 | Ahem. This is crap. Pray harder. Bad Martin. |
| 206 | """ |
mbligh | ad849ee | 2006-10-02 17:53:18 +0000 | [diff] [blame] | 207 | vmlinux = '/boot/vmlinux-%s' % system_output('uname -r') |
mbligh | 662a2a2 | 2006-10-01 17:01:14 +0000 | [diff] [blame] | 208 | if os.path.isfile(vmlinux): |
| 209 | return vmlinux |
mbligh | 17dbf05 | 2006-10-12 04:46:10 +0000 | [diff] [blame] | 210 | vmlinux = '/lib/modules/%s/build/vmlinux' % system_output('uname -r') |
| 211 | if os.path.isfile(vmlinux): |
| 212 | return vmlinux |
mbligh | 662a2a2 | 2006-10-01 17:01:14 +0000 | [diff] [blame] | 213 | return None |
mbligh | f49d5cf | 2006-04-23 02:24:42 +0000 | [diff] [blame] | 214 | |
| 215 | |
| 216 | def get_systemmap(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 217 | """Return the full path to System.map |
| 218 | |
| 219 | Ahem. This is crap. Pray harder. Bad Martin. |
| 220 | """ |
mbligh | ad849ee | 2006-10-02 17:53:18 +0000 | [diff] [blame] | 221 | map = '/boot/System.map-%s' % system_output('uname -r') |
mbligh | 662a2a2 | 2006-10-01 17:01:14 +0000 | [diff] [blame] | 222 | if os.path.isfile(map): |
| 223 | return map |
mbligh | 17dbf05 | 2006-10-12 04:46:10 +0000 | [diff] [blame] | 224 | map = '/lib/modules/%s/build/System.map' % system_output('uname -r') |
| 225 | if os.path.isfile(map): |
| 226 | return map |
mbligh | 662a2a2 | 2006-10-01 17:01:14 +0000 | [diff] [blame] | 227 | return None |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 228 | |
| 229 | |
| 230 | def get_modules_dir(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 231 | """Return the modules dir for the running kernel version""" |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 232 | kernel_version = system_output('uname -r') |
| 233 | return '/lib/modules/%s/kernel' % kernel_version |
mbligh | f49d5cf | 2006-04-23 02:24:42 +0000 | [diff] [blame] | 234 | |
| 235 | |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 236 | def get_cpu_arch(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 237 | """Work out which CPU architecture we're running on""" |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 238 | f = open('/proc/cpuinfo', 'r') |
| 239 | cpuinfo = f.readlines() |
| 240 | f.close() |
| 241 | if list_grep(cpuinfo, '^cpu.*(RS64|POWER3|Broadband Engine)'): |
| 242 | return 'power' |
| 243 | elif list_grep(cpuinfo, '^cpu.*POWER4'): |
| 244 | return 'power4' |
| 245 | elif list_grep(cpuinfo, '^cpu.*POWER5'): |
| 246 | return 'power5' |
| 247 | elif list_grep(cpuinfo, '^cpu.*POWER6'): |
| 248 | return 'power6' |
| 249 | elif list_grep(cpuinfo, '^cpu.*PPC970'): |
| 250 | return 'power970' |
| 251 | elif list_grep(cpuinfo, 'Opteron'): |
| 252 | return 'x86_64' |
| 253 | elif list_grep(cpuinfo, 'GenuineIntel') and list_grep(cpuinfo, '48 bits virtual'): |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 254 | return 'x86_64' |
| 255 | else: |
| 256 | return 'i386' |
| 257 | |
| 258 | |
mbligh | 548f29a | 2006-10-17 04:55:12 +0000 | [diff] [blame] | 259 | def get_current_kernel_arch(): |
| 260 | """Get the machine architecture, now just a wrap of 'uname -m'.""" |
| 261 | return os.popen('uname -m').read().rstrip() |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 262 | |
| 263 | |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 264 | def get_file_arch(filename): |
| 265 | # -L means follow symlinks |
| 266 | file_data = system_output('file -L ' + filename) |
| 267 | if file_data.count('80386'): |
| 268 | return 'i386' |
| 269 | return None |
| 270 | |
| 271 | |
mbligh | 548f29a | 2006-10-17 04:55:12 +0000 | [diff] [blame] | 272 | def kernelexpand(kernel, args=None): |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 273 | # 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] | 274 | if kernel.find('/') < 0: # contains no path. |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 275 | autodir = os.environ['AUTODIR'] |
| 276 | kernelexpand = os.path.join(autodir, 'tools/kernelexpand') |
mbligh | 548f29a | 2006-10-17 04:55:12 +0000 | [diff] [blame] | 277 | if args: |
| 278 | kernelexpand += ' ' + args |
mbligh | 5629af0 | 2006-09-15 01:54:23 +0000 | [diff] [blame] | 279 | w, r = os.popen2(kernelexpand + ' ' + kernel) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 280 | |
| 281 | kernel = r.readline().strip() |
| 282 | r.close() |
| 283 | w.close() |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 284 | return kernel.split() |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 285 | |
| 286 | |
| 287 | def count_cpus(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 288 | """number of CPUs in the local machine according to /proc/cpuinfo""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 289 | f = file('/proc/cpuinfo', 'r') |
| 290 | cpus = 0 |
| 291 | for line in f.readlines(): |
| 292 | if line.startswith('processor'): |
| 293 | cpus += 1 |
| 294 | return cpus |
| 295 | |
mbligh | e7a170f | 2006-12-05 07:48:18 +0000 | [diff] [blame] | 296 | |
| 297 | # Returns total memory in kb |
| 298 | def memtotal(): |
| 299 | memtotal = system_output('grep MemTotal /proc/meminfo') |
| 300 | return int(re.search(r'\d+', memtotal).group(0)) |
| 301 | |
| 302 | |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 303 | def system(cmd, ignorestatus = 0): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 304 | """os.system replacement |
| 305 | |
| 306 | We have our own definition of system here, as the stock os.system doesn't |
| 307 | correctly handle sigpipe |
| 308 | (ie things like "yes | head" will hang because yes doesn't get the SIGPIPE). |
| 309 | |
| 310 | Also the stock os.system didn't raise errors based on exit status, this |
| 311 | version does unless you explicitly specify ignorestatus=1 |
| 312 | """ |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 313 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) |
| 314 | try: |
apw | bc2867d | 2006-04-06 18:21:16 +0000 | [diff] [blame] | 315 | status = os.system(cmd) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 316 | finally: |
| 317 | signal.signal(signal.SIGPIPE, signal.SIG_IGN) |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 318 | |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 319 | if ((status != 0) and not ignorestatus): |
apw | bc2867d | 2006-04-06 18:21:16 +0000 | [diff] [blame] | 320 | raise CmdError(cmd, status) |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 321 | return status |
mbligh | 3d91491 | 2006-04-22 17:37:19 +0000 | [diff] [blame] | 322 | |
| 323 | |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 324 | def system_output(command, ignorestatus = 0): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 325 | """Run command and return its output |
| 326 | |
| 327 | ignorestatus |
| 328 | whether to raise a CmdError if command has a nonzero exit status |
| 329 | """ |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 330 | (result, data) = commands.getstatusoutput(command) |
| 331 | if ((result != 0) and not ignorestatus): |
| 332 | raise CmdError, 'command failed: ' + command |
| 333 | return data |
| 334 | |
| 335 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 336 | def where_art_thy_filehandles(): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 337 | """Dump the current list of filehandles""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 338 | os.system("ls -l /proc/%d/fd >> /dev/tty" % os.getpid()) |
| 339 | |
| 340 | |
| 341 | def print_to_tty(string): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 342 | """Output string straight to the tty""" |
mbligh | e80ff5a | 2007-02-26 02:37:30 +0000 | [diff] [blame] | 343 | open('/dev/tty', 'w').write(string + '\n') |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 344 | |
| 345 | |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 346 | def dump_object(object): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 347 | """Dump an object's attributes and methods |
| 348 | |
| 349 | kind of like dir() |
| 350 | """ |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 351 | for item in object.__dict__.iteritems(): |
| 352 | print item |
| 353 | try: |
| 354 | (key,value) = item |
| 355 | dump_object(value) |
| 356 | except: |
| 357 | continue |
| 358 | |
| 359 | |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 360 | def environ(env_key): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 361 | """return the requested environment variable, or '' if unset""" |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 362 | if (os.environ.has_key(env_key)): |
mbligh | d931a58 | 2006-10-01 00:30:12 +0000 | [diff] [blame] | 363 | return os.environ[env_key] |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 364 | else: |
| 365 | return '' |
| 366 | |
| 367 | |
| 368 | def prepend_path(newpath, oldpath): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 369 | """prepend newpath to oldpath""" |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 370 | if (oldpath): |
| 371 | return newpath + ':' + oldpath |
| 372 | else: |
| 373 | return newpath |
| 374 | |
| 375 | |
| 376 | def append_path(oldpath, newpath): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 377 | """append newpath to oldpath""" |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 378 | if (oldpath): |
| 379 | return oldpath + ':' + newpath |
| 380 | else: |
| 381 | return newpath |
| 382 | |
| 383 | |
mbligh | 4e75b0d | 2006-08-29 15:22:44 +0000 | [diff] [blame] | 384 | def avgtime_print(dir): |
| 385 | """ Calculate some benchmarking statistics. |
| 386 | Input is a directory containing a file called 'time'. |
| 387 | File contains one-per-line results of /usr/bin/time. |
| 388 | Output is average Elapsed, User, and System time in seconds, |
| 389 | and average CPU percentage. |
| 390 | """ |
| 391 | f = open(dir + "/time") |
| 392 | user = system = elapsed = cpu = count = 0 |
| 393 | r = re.compile('([\d\.]*)user ([\d\.]*)system (\d*):([\d\.]*)elapsed (\d*)%CPU') |
| 394 | for line in f.readlines(): |
| 395 | try: |
| 396 | s = r.match(line); |
mbligh | e1ee267 | 2006-08-29 16:27:15 +0000 | [diff] [blame] | 397 | user += float(s.group(1)) |
| 398 | system += float(s.group(2)) |
| 399 | elapsed += (float(s.group(3)) * 60) + float(s.group(4)) |
| 400 | cpu += float(s.group(5)) |
mbligh | 4e75b0d | 2006-08-29 15:22:44 +0000 | [diff] [blame] | 401 | count += 1 |
| 402 | except: |
| 403 | raise ValueError("badly formatted times") |
| 404 | |
| 405 | f.close() |
| 406 | return "Elapsed: %0.2fs User: %0.2fs System: %0.2fs CPU: %0.0f%%" % \ |
| 407 | (elapsed/count, user/count, system/count, cpu/count) |
| 408 | |
| 409 | |
mbligh | f06db0f | 2006-09-30 17:08:43 +0000 | [diff] [blame] | 410 | def running_config(): |
| 411 | """ |
| 412 | Return path of config file of the currently running kernel |
| 413 | """ |
mbligh | a1bef1f | 2007-04-03 17:18:07 +0000 | [diff] [blame] | 414 | version = system_output('uname -r') |
mbligh | f06db0f | 2006-09-30 17:08:43 +0000 | [diff] [blame] | 415 | for config in ('/proc/config.gz', \ |
mbligh | a1bef1f | 2007-04-03 17:18:07 +0000 | [diff] [blame] | 416 | '/boot/config-%s' % version, |
| 417 | '/lib/modules/%s/build/.config' % version): |
mbligh | f06db0f | 2006-09-30 17:08:43 +0000 | [diff] [blame] | 418 | if os.path.isfile(config): |
| 419 | return config |
| 420 | return None |
mbligh | 9ec8acc | 2006-10-05 06:52:33 +0000 | [diff] [blame] | 421 | |
| 422 | |
mbligh | a1bef1f | 2007-04-03 17:18:07 +0000 | [diff] [blame] | 423 | def check_for_kernel_feature(feature): |
| 424 | config = running_config() |
| 425 | |
| 426 | if not config: |
| 427 | raise "Can't find kernel config file" |
| 428 | |
| 429 | if config.endswith('.gz'): |
| 430 | grep = 'zgrep' |
| 431 | else: |
| 432 | grep = 'grep' |
| 433 | grep += ' ^CONFIG_%s= %s' % (feature, config) |
| 434 | |
| 435 | if not system_output(grep, ignorestatus = 1): |
| 436 | raise "Kernel doesn't have a %s feature" % (feature) |
| 437 | |
| 438 | |
mbligh | 9ec8acc | 2006-10-05 06:52:33 +0000 | [diff] [blame] | 439 | def cpu_online_map(): |
| 440 | """ |
| 441 | Check out the available cpu online map |
| 442 | """ |
| 443 | cpus = [] |
| 444 | for line in open('/proc/cpuinfo', 'r').readlines(): |
| 445 | if line.startswith('processor'): |
| 446 | cpus.append(line.split()[2]) # grab cpu number |
| 447 | return cpus |
mbligh | 663e4f6 | 2006-10-11 05:03:40 +0000 | [diff] [blame] | 448 | |
| 449 | |
| 450 | def check_glibc_ver(ver): |
| 451 | glibc_ver = commands.getoutput('ldd --version').splitlines()[0] |
mbligh | cabfdaf | 2006-10-11 14:07:48 +0000 | [diff] [blame] | 452 | glibc_ver = re.search(r'(\d+\.\d+(\.\d+)?)', glibc_ver).group() |
mbligh | ea97ab8 | 2006-10-13 20:18:01 +0000 | [diff] [blame] | 453 | if glibc_ver.split('.') < ver.split('.'): |
mbligh | 0763522 | 2007-07-09 21:29:00 +0000 | [diff] [blame] | 454 | raise TestError("Glibc is too old (%s). Glibc >= %s is needed." % \ |
| 455 | (glibc_ver, ver)) |
| 456 | |
| 457 | def check_kernel_ver(ver): |
| 458 | kernel_ver = system_output('uname -r') |
| 459 | kv_tmp = re.split(r'[-]', kernel_ver)[0:3] |
| 460 | if kv_tmp[0].split('.') < ver.split('.'): |
| 461 | raise TestError("Kernel is too old (%s). Kernel > %s is needed." % \ |
| 462 | (kernel_ver, ver)) |
| 463 | |
mbligh | 9061a27 | 2006-12-28 21:20:51 +0000 | [diff] [blame] | 464 | |
| 465 | def read_one_line(filename): |
| 466 | return open(filename, 'r').readline().strip() |
| 467 | |
| 468 | |
| 469 | def write_one_line(filename, str): |
| 470 | str.rstrip() |
| 471 | open(filename, 'w').write(str.rstrip() + "\n") |
mbligh | 264cd8f | 2007-02-02 23:57:43 +0000 | [diff] [blame] | 472 | |
| 473 | |
| 474 | def human_format(number): |
| 475 | # Convert number to kilo / mega / giga format. |
| 476 | if number < 1024: |
| 477 | return "%d" % number |
| 478 | kilo = float(number) / 1024.0 |
| 479 | if kilo < 1024: |
| 480 | return "%.2fk" % kilo |
| 481 | meg = kilo / 1024.0 |
| 482 | if meg < 1024: |
| 483 | return "%.2fM" % meg |
| 484 | gig = meg / 1024.0 |
| 485 | return "%.2fG" % gig |
| 486 | |
mbligh | 8eca3a9 | 2007-02-03 20:59:39 +0000 | [diff] [blame] | 487 | |
| 488 | def numa_nodes(): |
| 489 | node_paths = glob.glob('/sys/devices/system/node/node*') |
| 490 | nodes = [int(re.sub(r'.*node(\d+)', r'\1', x)) for x in node_paths] |
| 491 | return (sorted(nodes)) |
| 492 | |
| 493 | |
| 494 | def node_size(): |
| 495 | nodes = max(len(numa_nodes()), 1) |
| 496 | return ((memtotal() * 1024) / nodes) |
| 497 | |
mbligh | 32bcff3 | 2007-07-25 16:37:32 +0000 | [diff] [blame] | 498 | |
| 499 | def to_seconds(time_string): |
| 500 | """Converts a string in M+:SS.SS format to S+.SS""" |
| 501 | elts = time_string.split(':') |
| 502 | if len(elts) == 1: |
| 503 | return time_string |
| 504 | return str(int(elts[0]) * 60 + float(elts[1])) |
| 505 | |
| 506 | |
| 507 | def extract_all_time_results(results_string): |
| 508 | """Extract user, system, and elapsed times into a list of tuples""" |
| 509 | pattern = re.compile(r"(.*?)user (.*?)system (.*?)elapsed") |
| 510 | results = [] |
mbligh | 526f2b1 | 2007-08-07 16:06:52 +0000 | [diff] [blame] | 511 | for result in pattern.findall(results_string): |
mbligh | 32bcff3 | 2007-07-25 16:37:32 +0000 | [diff] [blame] | 512 | results.append(tuple([to_seconds(elt) for elt in result])) |
mbligh | 526f2b1 | 2007-08-07 16:06:52 +0000 | [diff] [blame] | 513 | return results |
mbligh | c421164 | 2007-08-02 21:00:51 +0000 | [diff] [blame] | 514 | |
| 515 | |
| 516 | def pickle_load(filename): |
| 517 | return pickle.load(open(filename, 'r')) |
| 518 | |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 519 | |
| 520 | # Return the kernel version and build timestamp. |
| 521 | def running_os_release(): |
| 522 | return os.uname()[2:4] |
| 523 | |
| 524 | |
| 525 | def running_os_ident(): |
| 526 | (version, timestamp) = running_os_release() |
| 527 | return version + '::' + timestamp |
mbligh | b830e28 | 2007-10-02 16:35:03 +0000 | [diff] [blame^] | 528 | |
| 529 | |
| 530 | def rpm_installed(package): |
| 531 | # Test if a package is installed or not |
| 532 | try: |
| 533 | system('rpm -q ' + package) |
| 534 | except: |
| 535 | return False |
| 536 | return True |