blob: 507396f2f9bbe18475cdf623f46ab3f5b55e2974 [file] [log] [blame]
mblighc86b0b42006-07-28 17:35:28 +00001"""Convenience functions for use by tests or whomever.
2"""
3
mbligh8eca3a92007-02-03 20:59:39 +00004import os,os.path,shutil,urllib,sys,signal,commands,pickle,glob
apwbc2867d2006-04-06 18:21:16 +00005from error import *
mbligh4e75b0d2006-08-29 15:22:44 +00006import re,string
mblighf4c35322006-03-13 01:01:10 +00007
8def grep(pattern, file):
mbligh7bdbfbd2006-09-30 16:47:01 +00009 """
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
mblighf4c35322006-03-13 01:01:10 +000018
19
mblighc86b0b42006-07-28 17:35:28 +000020def difflist(list1, list2):
21 """returns items in list2 that are not in list1"""
mblighf4c35322006-03-13 01:01:10 +000022 diff = [];
23 for x in list2:
24 if x not in list1:
25 diff.append(x)
26 return diff
27
mblighc86b0b42006-07-28 17:35:28 +000028
mbligh7bdbfbd2006-09-30 16:47:01 +000029def 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 """
mbligh712cd142006-04-22 18:57:50 +000034 if not os.path.isfile(file):
35 raise NameError, 'invalid file %s to cat to command %s' % file, command
mblighf4c35322006-03-13 01:01:10 +000036 if file.endswith('.bz2'):
mbligh7bdbfbd2006-09-30 16:47:01 +000037 return system('bzcat ' + file + ' | ' + command, ignorestatus)
mbligh3d914912006-04-22 17:37:19 +000038 elif (file.endswith('.gz') or file.endswith('.tgz')):
mbligh7bdbfbd2006-09-30 16:47:01 +000039 return system('zcat ' + file + ' | ' + command, ignorestatus)
mblighf4c35322006-03-13 01:01:10 +000040 else:
mbligh7bdbfbd2006-09-30 16:47:01 +000041 return system('cat ' + file + ' | ' + command, ignorestatus)
mblighf4c35322006-03-13 01:01:10 +000042
mblighc86b0b42006-07-28 17:35:28 +000043
mbligh712cd142006-04-22 18:57:50 +000044def extract_tarball_to_dir(tarball, dir):
mblighc86b0b42006-07-28 17:35:28 +000045 """
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 """
mbligh712cd142006-04-22 18:57:50 +000049 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
mbligh712cd142006-04-22 18:57:50 +000058def extract_tarball(tarball):
mblighc86b0b42006-07-28 17:35:28 +000059 """Returns the first found newly created directory by the tarball extraction"""
mbligh712cd142006-04-22 18:57:50 +000060 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
mblighcdf02a42006-04-23 02:22:49 +000070
mblighc69530b2007-03-25 20:20:21 +000071def update_version(srcdir, preserve_srcdir, new_version, install, *args, **dargs):
72 """
73 Make sure srcdir is version new_version
mblighc86b0b42006-07-28 17:35:28 +000074
mblighc69530b2007-03-25 20:20:21 +000075 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
mblighc86b0b42006-07-28 17:35:28 +000079 """
mbligh72905562006-05-25 01:30:49 +000080 versionfile = srcdir + '/.version'
mblighc69530b2007-03-25 20:20:21 +000081 install_needed = True
82
mblighff88e4e2006-05-25 19:34:52 +000083 if os.path.exists(srcdir):
84 if os.path.exists(versionfile):
85 old_version = pickle.load(open(versionfile, 'r'))
mblighc69530b2007-03-25 20:20:21 +000086 if (old_version == new_version):
87 install_needed = False
88
89 if install_needed:
90 if not preserve_srcdir:
mbligh72905562006-05-25 01:30:49 +000091 system('rm -rf ' + srcdir)
mbligh78e17022006-09-13 19:58:12 +000092 install(*args, **dargs)
mbligh72905562006-05-25 01:30:49 +000093 if os.path.exists(srcdir):
94 pickle.dump(new_version, open(versionfile, 'w'))
mblighdfe8cc62007-07-23 18:23:17 +000095
mbligh72905562006-05-25 01:30:49 +000096
mblighea30c8a2006-04-22 22:24:25 +000097def is_url(path):
mblighc86b0b42006-07-28 17:35:28 +000098 """true if path is a url
99 """
100 # should cope with other url types here, but we only handle http and ftp
mblighea30c8a2006-04-22 22:24:25 +0000101 if (path.startswith('http://')) or (path.startswith('ftp://')):
mblighea30c8a2006-04-22 22:24:25 +0000102 return 1
103 return 0
104
mblighcdf02a42006-04-23 02:22:49 +0000105
mbligh8eca3a92007-02-03 20:59:39 +0000106def get_file(src, dest, permissions = None):
mblighc86b0b42006-07-28 17:35:28 +0000107 """get a file, either from url or local"""
mbligh31186612006-04-22 21:55:56 +0000108 if (src == dest): # no-op here allows clean overrides in tests
109 return
mblighea30c8a2006-04-22 22:24:25 +0000110 if (is_url(src)):
mblighf4c35322006-03-13 01:01:10 +0000111 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)
mbligh8eca3a92007-02-03 20:59:39 +0000118 else:
119 shutil.copyfile(src, dest)
120 if permissions:
121 os.chmod(permissions)
mblighf4c35322006-03-13 01:01:10 +0000122 return dest
123
mblighea30c8a2006-04-22 22:24:25 +0000124
mbligh82641862006-04-23 06:21:36 +0000125def unmap_url(srcdir, src, destdir = '.'):
mbligh52508632006-09-30 18:29:55 +0000126 """
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 """
mblighea30c8a2006-04-22 22:24:25 +0000136 if is_url(src):
137 dest = destdir + '/' + os.path.basename(src)
138 get_file(src, dest)
139 return dest
mbligh82641862006-04-23 06:21:36 +0000140 else:
141 return srcdir + '/' + src
mblighea30c8a2006-04-22 22:24:25 +0000142
143
mblighf4c35322006-03-13 01:01:10 +0000144def basename(path):
145 i = path.rfind('/');
146 return path[i+1:]
147
148
149def force_copy(src, dest):
mblighc86b0b42006-07-28 17:35:28 +0000150 """Replace dest with a new copy of src, even if it exists"""
mblighf4c35322006-03-13 01:01:10 +0000151 if os.path.isfile(dest):
152 os.remove(dest)
mbligh1e8858e2006-11-24 22:18:35 +0000153 if os.path.isdir(dest):
154 dest = os.path.join(dest, os.path.basename(src))
mblighad0eda22007-08-30 16:19:54 +0000155 shutil.copyfile(src, dest)
156 return dest
mblighf4c35322006-03-13 01:01:10 +0000157
158
mblighfdbcaec2006-10-01 23:28:57 +0000159def 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
mblighcdf02a42006-04-23 02:22:49 +0000164def file_contains_pattern(file, pattern):
mblighc86b0b42006-07-28 17:35:28 +0000165 """Return true if file contains the specified egrep pattern"""
mblighcdf02a42006-04-23 02:22:49 +0000166 if not os.path.isfile(file):
167 raise NameError, 'file %s does not exist' % file
mbligh5c1e26a2006-10-04 14:46:33 +0000168 return not system('egrep -q "' + pattern + '" ' + file, ignorestatus = 1)
mblighcdf02a42006-04-23 02:22:49 +0000169
170
171def list_grep(list, pattern):
mblighc86b0b42006-07-28 17:35:28 +0000172 """True if any item in list matches the specified pattern."""
mblighcdf02a42006-04-23 02:22:49 +0000173 compiled = re.compile(pattern)
174 for line in list:
175 match = compiled.search(line)
176 if (match):
177 return 1
178 return 0
179
mbligh42b81ca2006-09-30 22:10:01 +0000180def 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'
apw39f9cac2007-02-28 15:26:05 +0000196 elif file_contains_pattern(issue, 'Debian'):
197 return 'Debian'
mbligh42b81ca2006-09-30 22:10:01 +0000198 else:
199 return 'Unknown'
200
mblighcdf02a42006-04-23 02:22:49 +0000201
mblighf49d5cf2006-04-23 02:24:42 +0000202def get_vmlinux():
mblighc86b0b42006-07-28 17:35:28 +0000203 """Return the full path to vmlinux
204
205 Ahem. This is crap. Pray harder. Bad Martin.
206 """
mblighad849ee2006-10-02 17:53:18 +0000207 vmlinux = '/boot/vmlinux-%s' % system_output('uname -r')
mbligh662a2a22006-10-01 17:01:14 +0000208 if os.path.isfile(vmlinux):
209 return vmlinux
mbligh17dbf052006-10-12 04:46:10 +0000210 vmlinux = '/lib/modules/%s/build/vmlinux' % system_output('uname -r')
211 if os.path.isfile(vmlinux):
212 return vmlinux
mbligh662a2a22006-10-01 17:01:14 +0000213 return None
mblighf49d5cf2006-04-23 02:24:42 +0000214
215
216def get_systemmap():
mblighc86b0b42006-07-28 17:35:28 +0000217 """Return the full path to System.map
218
219 Ahem. This is crap. Pray harder. Bad Martin.
220 """
mblighad849ee2006-10-02 17:53:18 +0000221 map = '/boot/System.map-%s' % system_output('uname -r')
mbligh662a2a22006-10-01 17:01:14 +0000222 if os.path.isfile(map):
223 return map
mbligh17dbf052006-10-12 04:46:10 +0000224 map = '/lib/modules/%s/build/System.map' % system_output('uname -r')
225 if os.path.isfile(map):
226 return map
mbligh662a2a22006-10-01 17:01:14 +0000227 return None
mbligh67b5ece2006-04-23 02:53:12 +0000228
229
230def get_modules_dir():
mblighc86b0b42006-07-28 17:35:28 +0000231 """Return the modules dir for the running kernel version"""
mbligh67b5ece2006-04-23 02:53:12 +0000232 kernel_version = system_output('uname -r')
233 return '/lib/modules/%s/kernel' % kernel_version
mblighf49d5cf2006-04-23 02:24:42 +0000234
235
mbligh5970cf02006-08-06 15:39:22 +0000236def get_cpu_arch():
mblighc86b0b42006-07-28 17:35:28 +0000237 """Work out which CPU architecture we're running on"""
mblighcdf02a42006-04-23 02:22:49 +0000238 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'):
mblighf4c35322006-03-13 01:01:10 +0000254 return 'x86_64'
255 else:
256 return 'i386'
257
258
mbligh548f29a2006-10-17 04:55:12 +0000259def get_current_kernel_arch():
260 """Get the machine architecture, now just a wrap of 'uname -m'."""
261 return os.popen('uname -m').read().rstrip()
mblighcdf02a42006-04-23 02:22:49 +0000262
263
mblighfdbcaec2006-10-01 23:28:57 +0000264def 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
mbligh548f29a2006-10-17 04:55:12 +0000272def kernelexpand(kernel, args=None):
mblighf4c35322006-03-13 01:01:10 +0000273 # if not (kernel.startswith('http://') or kernel.startswith('ftp://') or os.path.isfile(kernel)):
mbligh5629af02006-09-15 01:54:23 +0000274 if kernel.find('/') < 0: # contains no path.
mbligh534015f2006-09-15 03:28:56 +0000275 autodir = os.environ['AUTODIR']
276 kernelexpand = os.path.join(autodir, 'tools/kernelexpand')
mbligh548f29a2006-10-17 04:55:12 +0000277 if args:
278 kernelexpand += ' ' + args
mbligh5629af02006-09-15 01:54:23 +0000279 w, r = os.popen2(kernelexpand + ' ' + kernel)
mblighf4c35322006-03-13 01:01:10 +0000280
281 kernel = r.readline().strip()
282 r.close()
283 w.close()
mbligh534015f2006-09-15 03:28:56 +0000284 return kernel.split()
mblighf4c35322006-03-13 01:01:10 +0000285
286
287def count_cpus():
mblighc86b0b42006-07-28 17:35:28 +0000288 """number of CPUs in the local machine according to /proc/cpuinfo"""
mblighf4c35322006-03-13 01:01:10 +0000289 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
mblighe7a170f2006-12-05 07:48:18 +0000296
297# Returns total memory in kb
298def memtotal():
299 memtotal = system_output('grep MemTotal /proc/meminfo')
300 return int(re.search(r'\d+', memtotal).group(0))
301
302
mbligha975fb62006-04-22 19:56:25 +0000303def system(cmd, ignorestatus = 0):
mblighc86b0b42006-07-28 17:35:28 +0000304 """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 """
mblighf4c35322006-03-13 01:01:10 +0000313 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
314 try:
apwbc2867d2006-04-06 18:21:16 +0000315 status = os.system(cmd)
mblighf4c35322006-03-13 01:01:10 +0000316 finally:
317 signal.signal(signal.SIGPIPE, signal.SIG_IGN)
mbligha975fb62006-04-22 19:56:25 +0000318
mbligh67b5ece2006-04-23 02:53:12 +0000319 if ((status != 0) and not ignorestatus):
apwbc2867d2006-04-06 18:21:16 +0000320 raise CmdError(cmd, status)
mbligha975fb62006-04-22 19:56:25 +0000321 return status
mbligh3d914912006-04-22 17:37:19 +0000322
323
mbligh67b5ece2006-04-23 02:53:12 +0000324def system_output(command, ignorestatus = 0):
mblighc86b0b42006-07-28 17:35:28 +0000325 """Run command and return its output
326
327 ignorestatus
328 whether to raise a CmdError if command has a nonzero exit status
329 """
mbligh67b5ece2006-04-23 02:53:12 +0000330 (result, data) = commands.getstatusoutput(command)
331 if ((result != 0) and not ignorestatus):
332 raise CmdError, 'command failed: ' + command
333 return data
334
335
mblighf4c35322006-03-13 01:01:10 +0000336def where_art_thy_filehandles():
mblighc86b0b42006-07-28 17:35:28 +0000337 """Dump the current list of filehandles"""
mblighf4c35322006-03-13 01:01:10 +0000338 os.system("ls -l /proc/%d/fd >> /dev/tty" % os.getpid())
339
340
341def print_to_tty(string):
mblighc86b0b42006-07-28 17:35:28 +0000342 """Output string straight to the tty"""
mblighe80ff5a2007-02-26 02:37:30 +0000343 open('/dev/tty', 'w').write(string + '\n')
mblighf4c35322006-03-13 01:01:10 +0000344
345
mblighb8a14e32006-05-06 00:17:35 +0000346def dump_object(object):
mblighc86b0b42006-07-28 17:35:28 +0000347 """Dump an object's attributes and methods
348
349 kind of like dir()
350 """
mblighb8a14e32006-05-06 00:17:35 +0000351 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
mbligh4b089662006-06-14 22:34:58 +0000360def environ(env_key):
mblighc86b0b42006-07-28 17:35:28 +0000361 """return the requested environment variable, or '' if unset"""
mbligh4b089662006-06-14 22:34:58 +0000362 if (os.environ.has_key(env_key)):
mblighd931a582006-10-01 00:30:12 +0000363 return os.environ[env_key]
mbligh4b089662006-06-14 22:34:58 +0000364 else:
365 return ''
366
367
368def prepend_path(newpath, oldpath):
mblighc86b0b42006-07-28 17:35:28 +0000369 """prepend newpath to oldpath"""
mbligh4b089662006-06-14 22:34:58 +0000370 if (oldpath):
371 return newpath + ':' + oldpath
372 else:
373 return newpath
374
375
376def append_path(oldpath, newpath):
mblighc86b0b42006-07-28 17:35:28 +0000377 """append newpath to oldpath"""
mbligh4b089662006-06-14 22:34:58 +0000378 if (oldpath):
379 return oldpath + ':' + newpath
380 else:
381 return newpath
382
383
mbligh4e75b0d2006-08-29 15:22:44 +0000384def 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);
mblighe1ee2672006-08-29 16:27:15 +0000397 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))
mbligh4e75b0d2006-08-29 15:22:44 +0000401 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
mblighf06db0f2006-09-30 17:08:43 +0000410def running_config():
411 """
412 Return path of config file of the currently running kernel
413 """
mbligha1bef1f2007-04-03 17:18:07 +0000414 version = system_output('uname -r')
mblighf06db0f2006-09-30 17:08:43 +0000415 for config in ('/proc/config.gz', \
mbligha1bef1f2007-04-03 17:18:07 +0000416 '/boot/config-%s' % version,
417 '/lib/modules/%s/build/.config' % version):
mblighf06db0f2006-09-30 17:08:43 +0000418 if os.path.isfile(config):
419 return config
420 return None
mbligh9ec8acc2006-10-05 06:52:33 +0000421
422
mbligha1bef1f2007-04-03 17:18:07 +0000423def 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
mbligh9ec8acc2006-10-05 06:52:33 +0000439def 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
mbligh663e4f62006-10-11 05:03:40 +0000448
449
450def check_glibc_ver(ver):
451 glibc_ver = commands.getoutput('ldd --version').splitlines()[0]
mblighcabfdaf2006-10-11 14:07:48 +0000452 glibc_ver = re.search(r'(\d+\.\d+(\.\d+)?)', glibc_ver).group()
mblighea97ab82006-10-13 20:18:01 +0000453 if glibc_ver.split('.') < ver.split('.'):
mbligh07635222007-07-09 21:29:00 +0000454 raise TestError("Glibc is too old (%s). Glibc >= %s is needed." % \
455 (glibc_ver, ver))
456
457def 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
mbligh9061a272006-12-28 21:20:51 +0000464
465def read_one_line(filename):
466 return open(filename, 'r').readline().strip()
467
468
469def write_one_line(filename, str):
470 str.rstrip()
471 open(filename, 'w').write(str.rstrip() + "\n")
mbligh264cd8f2007-02-02 23:57:43 +0000472
473
474def 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
mbligh8eca3a92007-02-03 20:59:39 +0000487
488def 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
494def node_size():
495 nodes = max(len(numa_nodes()), 1)
496 return ((memtotal() * 1024) / nodes)
497
mbligh32bcff32007-07-25 16:37:32 +0000498
499def 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
507def 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 = []
mbligh526f2b12007-08-07 16:06:52 +0000511 for result in pattern.findall(results_string):
mbligh32bcff32007-07-25 16:37:32 +0000512 results.append(tuple([to_seconds(elt) for elt in result]))
mbligh526f2b12007-08-07 16:06:52 +0000513 return results
mblighc4211642007-08-02 21:00:51 +0000514
515
516def pickle_load(filename):
517 return pickle.load(open(filename, 'r'))
518
mbligh237bed32007-09-05 13:05:57 +0000519
520# Return the kernel version and build timestamp.
521def running_os_release():
522 return os.uname()[2:4]
523
524
525def running_os_ident():
526 (version, timestamp) = running_os_release()
527 return version + '::' + timestamp
mblighb830e282007-10-02 16:35:03 +0000528
529
530def 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