diff options
author | erkin <e.altunbas@vyos.io> | 2021-03-30 12:31:43 +0300 |
---|---|---|
committer | erkin <e.altunbas@vyos.io> | 2021-03-30 12:31:43 +0300 |
commit | d0a1bf49e2bcbabba74960027c5e3f75ab27853a (patch) | |
tree | 01ce157eaf90fe6052430886fda64cc258159d52 | |
parent | 5d0a54ec53c18289f50ccb9cd69e0df62c63a2dc (diff) | |
download | vyos-1x-d0a1bf49e2bcbabba74960027c5e3f75ab27853a.tar.gz vyos-1x-d0a1bf49e2bcbabba74960027c5e3f75ab27853a.zip |
T3354: Handle user break and prematurely closed stdin
-rwxr-xr-x | src/helpers/strip-private.py | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/src/helpers/strip-private.py b/src/helpers/strip-private.py index 023298eec..420a039eb 100755 --- a/src/helpers/strip-private.py +++ b/src/helpers/strip-private.py @@ -79,13 +79,18 @@ def strip_lines(rules: tuple) -> None: """ Read stdin line by line and apply the given stripping rules. """ - for line in sys.stdin: - if not args.keep_address: - line = strip_address(line) - for (condition, regexp, subst) in rules: - if condition: - line = regexp.sub(subst, line) - print(line, end='') + try: + for line in sys.stdin: + if not args.keep_address: + line = strip_address(line) + for (condition, regexp, subst) in rules: + if condition: + line = regexp.sub(subst, line) + print(line, end='') + # stdin can be cut for any reason, such as user interrupt or the pager terminating before the text can be read. + # All we can do is gracefully exit. + except (BrokenPipeError, EOFError, KeyboardInterrupt): + sys.exit(1) if __name__ == "__main__": args = parser.parse_args() |