mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 1 | import os,os.path,shutil,urllib,sys,signal,commands |
apw | bc2867d | 2006-04-06 18:21:16 +0000 | [diff] [blame] | 2 | from error import * |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 3 | import re |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 4 | |
| 5 | def grep(pattern, file): |
| 6 | # This is mainly to fix the return code inversion from grep |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 7 | return not system('grep "' + pattern + '" "' + file + '"') |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | def difflist(list1, list2): |
| 11 | # returns items in list 2 that are not in list 1 |
| 12 | diff = []; |
| 13 | for x in list2: |
| 14 | if x not in list1: |
| 15 | diff.append(x) |
| 16 | return diff |
| 17 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 18 | def cat_file_to_cmd(file, command): |
mbligh | 712cd14 | 2006-04-22 18:57:50 +0000 | [diff] [blame] | 19 | if not os.path.isfile(file): |
| 20 | raise NameError, 'invalid file %s to cat to command %s' % file, command |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 21 | if file.endswith('.bz2'): |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 22 | system('bzcat ' + file + ' | ' + command) |
mbligh | 3d91491 | 2006-04-22 17:37:19 +0000 | [diff] [blame] | 23 | elif (file.endswith('.gz') or file.endswith('.tgz')): |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 24 | system('zcat ' + file + ' | ' + command) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 25 | else: |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 26 | system('cat ' + file + ' | ' + command) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 27 | |
mbligh | 712cd14 | 2006-04-22 18:57:50 +0000 | [diff] [blame] | 28 | |
| 29 | # Extract a tarball to a specified directory name instead of whatever |
| 30 | # the top level of a tarball is - useful for versioned directory names, etc |
| 31 | def extract_tarball_to_dir(tarball, dir): |
| 32 | if os.path.exists(dir): |
| 33 | raise NameError, 'target %s already exists' % dir |
| 34 | pwd = os.getcwd() |
| 35 | os.chdir(os.path.dirname(os.path.abspath(dir))) |
| 36 | newdir = extract_tarball(tarball) |
| 37 | os.rename(newdir, dir) |
| 38 | os.chdir(pwd) |
| 39 | |
| 40 | |
| 41 | # Returns the first found newly created directory by the tarball extraction |
| 42 | def extract_tarball(tarball): |
| 43 | oldlist = os.listdir('.') |
| 44 | cat_file_to_cmd(tarball, 'tar xf -') |
| 45 | newlist = os.listdir('.') |
| 46 | newfiles = difflist(oldlist, newlist) # what is new dir ? |
| 47 | new_dir = None |
| 48 | for newfile in newfiles: |
| 49 | if (os.path.isdir(newfile)): |
| 50 | return newfile |
| 51 | raise NameError, "extracting tarball produced no dir" |
| 52 | |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 53 | |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 54 | def is_url(path): |
| 55 | if (path.startswith('http://')) or (path.startswith('ftp://')): |
| 56 | # should cope with other url types here, but we don't handle them yet |
| 57 | return 1 |
| 58 | return 0 |
| 59 | |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 60 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 61 | def get_file(src, dest): |
mbligh | 3118661 | 2006-04-22 21:55:56 +0000 | [diff] [blame] | 62 | if (src == dest): # no-op here allows clean overrides in tests |
| 63 | return |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 64 | # get a file, either from url or local |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 65 | if (is_url(src)): |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 66 | print 'PWD: ' + os.getcwd() |
| 67 | print 'Fetching \n\t', src, '\n\t->', dest |
| 68 | try: |
| 69 | urllib.urlretrieve(src, dest) |
| 70 | except IOError: |
| 71 | sys.stderr.write("Unable to retrieve %s (to %s)\n" % (src, dest)) |
| 72 | sys.exit(1) |
| 73 | return dest |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 74 | shutil.copyfile(src, dest) |
| 75 | return dest |
| 76 | |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 77 | |
mbligh | 8264186 | 2006-04-23 06:21:36 +0000 | [diff] [blame] | 78 | def unmap_url(srcdir, src, destdir = '.'): |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 79 | if is_url(src): |
| 80 | dest = destdir + '/' + os.path.basename(src) |
| 81 | get_file(src, dest) |
| 82 | return dest |
mbligh | 8264186 | 2006-04-23 06:21:36 +0000 | [diff] [blame] | 83 | else: |
| 84 | return srcdir + '/' + src |
mbligh | ea30c8a | 2006-04-22 22:24:25 +0000 | [diff] [blame] | 85 | |
| 86 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 87 | def basename(path): |
| 88 | i = path.rfind('/'); |
| 89 | return path[i+1:] |
| 90 | |
| 91 | |
| 92 | def force_copy(src, dest): |
| 93 | if os.path.isfile(dest): |
| 94 | os.remove(dest) |
| 95 | return shutil.copyfile(src, dest) |
| 96 | |
| 97 | |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 98 | def file_contains_pattern(file, pattern): |
| 99 | if not os.path.isfile(file): |
| 100 | raise NameError, 'file %s does not exist' % file |
| 101 | return not system('egrep -q ' + pattern + ' ' + file, ignorestatus = 1) |
| 102 | |
| 103 | |
| 104 | def list_grep(list, pattern): |
| 105 | compiled = re.compile(pattern) |
| 106 | for line in list: |
| 107 | match = compiled.search(line) |
| 108 | if (match): |
| 109 | return 1 |
| 110 | return 0 |
| 111 | |
| 112 | |
mbligh | f49d5cf | 2006-04-23 02:24:42 +0000 | [diff] [blame] | 113 | def get_vmlinux(): |
| 114 | # Ahem. This is crap. Pray harder. Bad Martin. |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 115 | vmlinux = '/boot/vmlinux' |
| 116 | if not os.path.isfile(vmlinux): |
| 117 | raise NameError, 'Cannot find vmlinux' |
| 118 | return vmlinux |
mbligh | f49d5cf | 2006-04-23 02:24:42 +0000 | [diff] [blame] | 119 | |
| 120 | |
| 121 | def get_systemmap(): |
| 122 | # Ahem. This is crap. Pray harder. Bad Martin. |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 123 | map = '/boot/System.map' |
| 124 | if not os.path.isfile(map): |
| 125 | raise NameError, 'Cannot find System.map' |
| 126 | return map |
| 127 | |
| 128 | |
| 129 | def get_modules_dir(): |
| 130 | kernel_version = system_output('uname -r') |
| 131 | return '/lib/modules/%s/kernel' % kernel_version |
mbligh | f49d5cf | 2006-04-23 02:24:42 +0000 | [diff] [blame] | 132 | |
| 133 | |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 134 | def get_arch(): |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 135 | # Work out which CPU architecture we're running on |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 136 | f = open('/proc/cpuinfo', 'r') |
| 137 | cpuinfo = f.readlines() |
| 138 | f.close() |
| 139 | if list_grep(cpuinfo, '^cpu.*(RS64|POWER3|Broadband Engine)'): |
| 140 | return 'power' |
| 141 | elif list_grep(cpuinfo, '^cpu.*POWER4'): |
| 142 | return 'power4' |
| 143 | elif list_grep(cpuinfo, '^cpu.*POWER5'): |
| 144 | return 'power5' |
| 145 | elif list_grep(cpuinfo, '^cpu.*POWER6'): |
| 146 | return 'power6' |
| 147 | elif list_grep(cpuinfo, '^cpu.*PPC970'): |
| 148 | return 'power970' |
| 149 | elif list_grep(cpuinfo, 'Opteron'): |
| 150 | return 'x86_64' |
| 151 | elif list_grep(cpuinfo, 'GenuineIntel') and list_grep(cpuinfo, '48 bits virtual'): |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 152 | return 'x86_64' |
| 153 | else: |
| 154 | return 'i386' |
| 155 | |
| 156 | |
mbligh | cdf02a4 | 2006-04-23 02:22:49 +0000 | [diff] [blame] | 157 | def get_target_arch(): |
| 158 | arch = get_arch() |
| 159 | if arch.startswith('power'): |
| 160 | return 'ppc64' |
| 161 | else: |
| 162 | return arch |
| 163 | |
| 164 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 165 | def kernelexpand(kernel): |
| 166 | # if not (kernel.startswith('http://') or kernel.startswith('ftp://') or os.path.isfile(kernel)): |
| 167 | if kernel.find('/'): |
| 168 | w, r = os.popen2('./kernelexpand ' + kernel) |
| 169 | |
| 170 | kernel = r.readline().strip() |
| 171 | r.close() |
| 172 | w.close() |
| 173 | return kernel |
| 174 | |
| 175 | |
| 176 | def count_cpus(): |
| 177 | f = file('/proc/cpuinfo', 'r') |
| 178 | cpus = 0 |
| 179 | for line in f.readlines(): |
| 180 | if line.startswith('processor'): |
| 181 | cpus += 1 |
| 182 | return cpus |
| 183 | |
| 184 | |
apw | bc2867d | 2006-04-06 18:21:16 +0000 | [diff] [blame] | 185 | # We have our own definition of system here, as the stock os.system doesn't |
mbligh | 3d91491 | 2006-04-22 17:37:19 +0000 | [diff] [blame] | 186 | # correctly handle sigpipe |
| 187 | # (ie things like "yes | head" will hang because yes doesn't get the SIGPIPE). |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 188 | # |
| 189 | # Also the stock os.system didn't raise errors based on exit status, this |
| 190 | # version does unless you explicitly specify ignorestatus=1 |
| 191 | def system(cmd, ignorestatus = 0): |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 192 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) |
| 193 | try: |
apw | bc2867d | 2006-04-06 18:21:16 +0000 | [diff] [blame] | 194 | status = os.system(cmd) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 195 | finally: |
| 196 | signal.signal(signal.SIGPIPE, signal.SIG_IGN) |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 197 | |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 198 | if ((status != 0) and not ignorestatus): |
apw | bc2867d | 2006-04-06 18:21:16 +0000 | [diff] [blame] | 199 | raise CmdError(cmd, status) |
mbligh | a975fb6 | 2006-04-22 19:56:25 +0000 | [diff] [blame] | 200 | return status |
mbligh | 3d91491 | 2006-04-22 17:37:19 +0000 | [diff] [blame] | 201 | |
| 202 | |
mbligh | 67b5ece | 2006-04-23 02:53:12 +0000 | [diff] [blame] | 203 | def system_output(command, ignorestatus = 0): |
| 204 | (result, data) = commands.getstatusoutput(command) |
| 205 | if ((result != 0) and not ignorestatus): |
| 206 | raise CmdError, 'command failed: ' + command |
| 207 | return data |
| 208 | |
| 209 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 210 | def where_art_thy_filehandles(): |
| 211 | os.system("ls -l /proc/%d/fd >> /dev/tty" % os.getpid()) |
| 212 | |
| 213 | |
| 214 | def print_to_tty(string): |
| 215 | os.system("echo " + string + " >> /dev/tty") |
| 216 | |
| 217 | |
| 218 | class fd_stack: |
| 219 | # Note that we need to redirect both the sys.stdout type descriptor |
| 220 | # (which print, etc use) and the low level OS numbered descriptor |
| 221 | # which os.system() etc use. |
| 222 | |
| 223 | def __init__(self, fd, filehandle): |
| 224 | self.fd = fd # eg 1 |
| 225 | self.filehandle = filehandle # eg sys.stdout |
| 226 | self.stack = [(fd, filehandle)] |
| 227 | |
| 228 | |
| 229 | def redirect(self, filename): |
| 230 | fdcopy = os.dup(self.fd) |
| 231 | self.stack.append( (fdcopy, self.filehandle) ) |
| 232 | # self.filehandle = file(filename, 'w') |
| 233 | if (os.path.isfile(filename)): |
| 234 | newfd = os.open(filename, os.O_WRONLY) |
| 235 | else: |
| 236 | newfd = os.open(filename, os.O_WRONLY | os.O_CREAT) |
| 237 | os.dup2(newfd, self.fd) |
| 238 | os.close(newfd) |
| 239 | self.filehandle = os.fdopen(self.fd, 'w') |
| 240 | |
| 241 | |
| 242 | def tee_redirect(self, filename): |
| 243 | print_to_tty("tee_redirect to " + filename) |
| 244 | where_art_thy_filehandles() |
| 245 | fdcopy = os.dup(self.fd) |
| 246 | self.stack.append( (fdcopy, self.filehandle) ) |
| 247 | r, w = os.pipe() |
| 248 | pid = os.fork() |
| 249 | if pid: # parent |
| 250 | os.close(r) |
| 251 | os.dup2(w, self.fd) |
| 252 | os.close(w) |
| 253 | else: # child |
| 254 | os.close(w) |
| 255 | os.dup2(r, 0) |
| 256 | os.dup2(2, 1) |
| 257 | os.execlp('tee', 'tee', filename) |
| 258 | self.filehandle = os.fdopen(self.fd, 'w') |
| 259 | where_art_thy_filehandles() |
| 260 | print_to_tty("done tee_redirect to " + filename) |
| 261 | |
| 262 | |
| 263 | def restore(self): |
mbligh | cf31533 | 2006-04-02 19:58:30 +0000 | [diff] [blame] | 264 | # print_to_tty("ENTERING RESTORE %d" % self.fd) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 265 | # where_art_thy_filehandles() |
| 266 | (old_fd, old_filehandle) = self.stack.pop() |
| 267 | # print_to_tty("old_fd %d" % old_fd) |
| 268 | # print_to_tty("self.fd %d" % self.fd) |
| 269 | self.filehandle.close() # seems to close old_fd as well. |
| 270 | # where_art_thy_filehandles() |
| 271 | os.dup2(old_fd, self.fd) |
| 272 | # print_to_tty("CLOSING FD %d" % old_fd) |
| 273 | os.close(old_fd) |
| 274 | # where_art_thy_filehandles() |
| 275 | self.filehandle = old_filehandle |
| 276 | # where_art_thy_filehandles() |
| 277 | # print_to_tty("EXIT RESTORE %d" % self.fd) |