aboutsummaryrefslogtreecommitdiffstats
path: root/common.py
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2013-02-07 11:21:23 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2013-02-07 11:21:23 -0500
commit39020cf5ae3030bc15035925a0c72eb44eea67b7 (patch)
treefb82b339c1e5c14334f8d9839f8d836b120fbb08 /common.py
parentd524da9bd072ad1be4ac0d633e3e783094ddc2d7 (diff)
Added gen_exps.py script.
Diffstat (limited to 'common.py')
-rw-r--r--common.py26
1 files changed, 24 insertions, 2 deletions
diff --git a/common.py b/common.py
index 0990cfe..ad3c418 100644
--- a/common.py
+++ b/common.py
@@ -1,9 +1,14 @@
1import os
2import re
3import subprocess
1import sys 4import sys
5
2from collections import defaultdict 6from collections import defaultdict
3from textwrap import dedent 7from textwrap import dedent
4 8
5def get_executable(prog, hint, optional=False): 9def get_executable(prog, hint, optional=False):
6 import os 10 '''Search for @prog in system PATH. Print @hint if no binary is found.'''
11
7 def is_exe(fpath): 12 def is_exe(fpath):
8 return os.path.isfile(fpath) and os.access(fpath, os.X_OK) 13 return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
9 14
@@ -19,12 +24,29 @@ def get_executable(prog, hint, optional=False):
19 24
20 if not optional: 25 if not optional:
21 sys.stderr.write("Cannot find executable '%s' in PATH. This is a part " 26 sys.stderr.write("Cannot find executable '%s' in PATH. This is a part "
22 "of '%s' which should be added to PATH to run." % 27 "of '%s' which should be added to PATH to run.\n" %
23 (prog, hint)) 28 (prog, hint))
24 sys.exit(1) 29 sys.exit(1)
25 else: 30 else:
26 return None 31 return None
27 32
33def get_config_option(option):
34 '''Search for @option in installed kernel config (if present).
35 Raise an IOError if the kernel config isn't found in /boot/.'''
36 uname = subprocess.check_output(["uname", "-r"])[-1]
37 fname = "/boot/config-%s" % uname
38
39 if os.path.exists(fname):
40 config_regex = "^CONFIG_{}=(?P<val>.*)$".format(option)
41 match = re.search(config_regex, open(fname, 'r').read())
42 if not match:
43 return None
44 else:
45 return match.group("val")
46
47 else:
48 raise IOError("No config file exists!")
49
28def recordtype(typename, field_names, default=0): 50def recordtype(typename, field_names, default=0):
29 ''' Mutable namedtuple. Recipe from George Sakkis of MIT.''' 51 ''' Mutable namedtuple. Recipe from George Sakkis of MIT.'''
30 field_names = tuple(map(str, field_names)) 52 field_names = tuple(map(str, field_names))