diff options
Diffstat (limited to 'common.py')
| -rw-r--r-- | common.py | 25 |
1 files changed, 23 insertions, 2 deletions
| @@ -2,6 +2,29 @@ import sys | |||
| 2 | from collections import defaultdict | 2 | from collections import defaultdict |
| 3 | from textwrap import dedent | 3 | from textwrap import dedent |
| 4 | 4 | ||
| 5 | def get_executable(prog, hint, optional=False): | ||
| 6 | import os | ||
| 7 | def is_exe(fpath): | ||
| 8 | return os.path.isfile(fpath) and os.access(fpath, os.X_OK) | ||
| 9 | |||
| 10 | fpath, fname = os.path.split(prog) | ||
| 11 | if fpath: | ||
| 12 | if is_exe(prog): | ||
| 13 | return prog | ||
| 14 | else: | ||
| 15 | for path in os.environ["PATH"].split(os.pathsep): | ||
| 16 | exe_file = os.path.join(path, prog) | ||
| 17 | if is_exe(exe_file): | ||
| 18 | return exe_file | ||
| 19 | |||
| 20 | if not optional: | ||
| 21 | sys.stderr.write("Cannot find executable '%s' in PATH. This is a part " | ||
| 22 | "of '%s' which should be added to PATH to run." % | ||
| 23 | (prog, hint)) | ||
| 24 | sys.exit(1) | ||
| 25 | else: | ||
| 26 | return None | ||
| 27 | |||
| 5 | def recordtype(typename, field_names, default=0): | 28 | def recordtype(typename, field_names, default=0): |
| 6 | ''' Mutable namedtuple. Recipe from George Sakkis of MIT.''' | 29 | ''' Mutable namedtuple. Recipe from George Sakkis of MIT.''' |
| 7 | field_names = tuple(map(str, field_names)) | 30 | field_names = tuple(map(str, field_names)) |
| @@ -87,5 +110,3 @@ def load_params(fname): | |||
| 87 | raise IOError("Invalid param file: %s\n%s" % (fname, e)) | 110 | raise IOError("Invalid param file: %s\n%s" % (fname, e)) |
| 88 | 111 | ||
| 89 | return params | 112 | return params |
| 90 | |||
| 91 | |||
