diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2018-11-03 19:29:00 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2018-11-03 19:29:00 -0400 |
commit | 89d529895a21c40d1e3f3913b02540f32ab9a0b9 (patch) | |
tree | dde3b370d68a0049afb1715613ca8982e0d7a4ee /lab | |
parent | e1331826649b26465f090ad9e3100ea0870aefc3 (diff) | |
download | python-coveragepy-git-89d529895a21c40d1e3f3913b02540f32ab9a0b9.tar.gz |
Debug-time environment variables can be set with set_env.py
Diffstat (limited to 'lab')
-rwxr-xr-x | lab/set_env.py | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/lab/set_env.py b/lab/set_env.py new file mode 100755 index 00000000..184649ee --- /dev/null +++ b/lab/set_env.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python3 +# +# Run this like: +# +# $ $(lab/set_env.py) +# + +import functools +import glob +import itertools +import os +import re +import sys + +pstderr = functools.partial(print, file=sys.stderr) + +SETTINGS = [] + +line_pattern = r"\$set_env.py: (\w+) - (.*)" +globs = "*/*.py *.py" + +filenames = itertools.chain.from_iterable(glob.glob(g) for g in globs.split()) +files = 0 +for filename in filenames: + files += 1 + with open(filename) as f: + for line in f: + m = re.search(line_pattern, line) + if m: + SETTINGS.append(m.groups()) +pstderr("Read {} files".format(files)) + + +def read_them(): + values = {} + for name, _ in SETTINGS: + values[name] = os.environ.get(name) + return values + +def show_them(values): + for i, (name, description) in enumerate(SETTINGS, start=1): + value = values[name] + if value is None: + eq = ' ' + value = '' + else: + eq = '=' + value = repr(value) + pstderr("{:2d}: {:>30s} {} {:12s} {}".format(i, name, eq, value, description)) + +def set_by_num(values, n, value): + setting_name = SETTINGS[int(n)-1][0] + values[setting_name] = value + +def get_new_values(values): + show = True + while True: + if show: + show_them(values) + show = False + pstderr("") + pstderr("> ", end='') + sys.stderr.flush() + try: + cmd = input("").strip().split(None, 1) + except EOFError: + pstderr("\n") + break + if not cmd: + continue + if cmd[0] == 'q': + break + if cmd[0] == 'x': + set_by_num(values, cmd[1], None) + else: + try: + nsetting = int(cmd[0]) + except ValueError: + pass + else: + set_by_num(values, nsetting, cmd[1]) + show = True + + return values + +def as_exports(values): + exports = [] + for name, value in values.items(): + if value is None: + exports.append("export -n {}".format(name)) + else: + exports.append("export {}={!r}".format(name, value)) + return "eval " + "; ".join(exports) + +print(as_exports(get_new_values(read_them()))) |