blob: e58c984c2b4b7c6adb427a94ae4c4c794c95329a [file] [log] [blame]
mblighf4c35322006-03-13 01:01:10 +00001import os,os.path,shutil,urllib,sys,signal
2
3def grep(pattern, file):
4# This is mainly to fix the return code inversion from grep
5 return not os.system('grep "' + pattern + '" "' + file + '"')
6
7
8def difflist(list1, list2):
9# returns items in list 2 that are not in list 1
10 diff = [];
11 for x in list2:
12 if x not in list1:
13 diff.append(x)
14 return diff
15
16
17def cat_file_to_cmd(file, command):
18 if file.endswith('.bz2'):
19 os.system('bzcat ' + file + ' | ' + command)
20 elif file.endswith('.gz'):
21 os.system('zcat ' + file + ' | ' + command)
22 else:
23 os.system('cat ' + file + ' | ' + command)
24
25
26def get_file(src, dest):
27 # get a file, either from url or local
28 if (src.startswith('http://')) or (src.startswith('ftp://')):
29 print 'PWD: ' + os.getcwd()
30 print 'Fetching \n\t', src, '\n\t->', dest
31 try:
32 urllib.urlretrieve(src, dest)
33 except IOError:
34 sys.stderr.write("Unable to retrieve %s (to %s)\n" % (src, dest))
35 sys.exit(1)
36 return dest
37 # elif os.path.isfile(src):
38 shutil.copyfile(src, dest)
39 return dest
40
41def basename(path):
42 i = path.rfind('/');
43 return path[i+1:]
44
45
46def force_copy(src, dest):
47 if os.path.isfile(dest):
48 os.remove(dest)
49 return shutil.copyfile(src, dest)
50
51
52def get_target_arch():
53# Work out which CPU architecture we're running on
54 if not os.system("egrep '^cpu.*(RS64|POWER(3|4|5)|PPC970|Broadband Engine)' /proc/cpuinfo"):
55 return 'ppc64'
56 elif not os.system("grep -q 'Opteron' /proc/cpuinfo"):
57 # THIS IS WRONG, needs Intel too
58 return 'x86_64'
59 else:
60 return 'i386'
61
62
63def kernelexpand(kernel):
64 # if not (kernel.startswith('http://') or kernel.startswith('ftp://') or os.path.isfile(kernel)):
65 if kernel.find('/'):
66 w, r = os.popen2('./kernelexpand ' + kernel)
67
68 kernel = r.readline().strip()
69 r.close()
70 w.close()
71 return kernel
72
73
74def count_cpus():
75 f = file('/proc/cpuinfo', 'r')
76 cpus = 0
77 for line in f.readlines():
78 if line.startswith('processor'):
79 cpus += 1
80 return cpus
81
82
83# We have our own definiton of system here, as the stock os.system doesn't
84# correctly handle sigpipe (ie things like "yes | head" will hang because
85# yes doesn't get the SIGPIPE).
86def system(cmd):
87 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
88 try:
89 os.system(cmd)
90 finally:
91 signal.signal(signal.SIGPIPE, signal.SIG_IGN)
92
93
94def where_art_thy_filehandles():
95 os.system("ls -l /proc/%d/fd >> /dev/tty" % os.getpid())
96
97
98def print_to_tty(string):
99 os.system("echo " + string + " >> /dev/tty")
100
101
102class fd_stack:
103 # Note that we need to redirect both the sys.stdout type descriptor
104 # (which print, etc use) and the low level OS numbered descriptor
105 # which os.system() etc use.
106
107 def __init__(self, fd, filehandle):
108 self.fd = fd # eg 1
109 self.filehandle = filehandle # eg sys.stdout
110 self.stack = [(fd, filehandle)]
111
112
113 def redirect(self, filename):
114 fdcopy = os.dup(self.fd)
115 self.stack.append( (fdcopy, self.filehandle) )
116 # self.filehandle = file(filename, 'w')
117 if (os.path.isfile(filename)):
118 newfd = os.open(filename, os.O_WRONLY)
119 else:
120 newfd = os.open(filename, os.O_WRONLY | os.O_CREAT)
121 os.dup2(newfd, self.fd)
122 os.close(newfd)
123 self.filehandle = os.fdopen(self.fd, 'w')
124
125
126 def tee_redirect(self, filename):
127 print_to_tty("tee_redirect to " + filename)
128 where_art_thy_filehandles()
129 fdcopy = os.dup(self.fd)
130 self.stack.append( (fdcopy, self.filehandle) )
131 r, w = os.pipe()
132 pid = os.fork()
133 if pid: # parent
134 os.close(r)
135 os.dup2(w, self.fd)
136 os.close(w)
137 else: # child
138 os.close(w)
139 os.dup2(r, 0)
140 os.dup2(2, 1)
141 os.execlp('tee', 'tee', filename)
142 self.filehandle = os.fdopen(self.fd, 'w')
143 where_art_thy_filehandles()
144 print_to_tty("done tee_redirect to " + filename)
145
146
147 def restore(self):
148 print_to_tty("ENTERING RESTORE %d" % self.fd)
149 # where_art_thy_filehandles()
150 (old_fd, old_filehandle) = self.stack.pop()
151 # print_to_tty("old_fd %d" % old_fd)
152 # print_to_tty("self.fd %d" % self.fd)
153 self.filehandle.close() # seems to close old_fd as well.
154 # where_art_thy_filehandles()
155 os.dup2(old_fd, self.fd)
156 # print_to_tty("CLOSING FD %d" % old_fd)
157 os.close(old_fd)
158 # where_art_thy_filehandles()
159 self.filehandle = old_filehandle
160 # where_art_thy_filehandles()
161 # print_to_tty("EXIT RESTORE %d" % self.fd)