diff options
author | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-02-07 11:21:23 -0500 |
---|---|---|
committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-02-07 11:21:23 -0500 |
commit | 39020cf5ae3030bc15035925a0c72eb44eea67b7 (patch) | |
tree | fb82b339c1e5c14334f8d9839f8d836b120fbb08 /common.py | |
parent | d524da9bd072ad1be4ac0d633e3e783094ddc2d7 (diff) |
Added gen_exps.py script.
Diffstat (limited to 'common.py')
-rw-r--r-- | common.py | 26 |
1 files changed, 24 insertions, 2 deletions
@@ -1,9 +1,14 @@ | |||
1 | import os | ||
2 | import re | ||
3 | import subprocess | ||
1 | import sys | 4 | import sys |
5 | |||
2 | from collections import defaultdict | 6 | from collections import defaultdict |
3 | from textwrap import dedent | 7 | from textwrap import dedent |
4 | 8 | ||
5 | def get_executable(prog, hint, optional=False): | 9 | def 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 | ||
33 | def 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 | |||
28 | def recordtype(typename, field_names, default=0): | 50 | def 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)) |