From c799b7ddf62f476844f9bf5b3538db17fd976f87 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 21 Apr 2020 09:56:34 +0200 Subject: scripts: Print errors to stderr instead of stdout --- doc/changelog.asciidoc | 2 ++ scripts/asciidoc2html.py | 11 ++++++----- scripts/dev/build_release.py | 4 ++-- scripts/dictcli.py | 2 +- scripts/mkvenv.py | 23 +++++++++++------------ scripts/utils.py | 11 ++++++++--- 6 files changed, 30 insertions(+), 23 deletions(-) diff --git a/doc/changelog.asciidoc b/doc/changelog.asciidoc index 3406c02c4..bbb166eeb 100644 --- a/doc/changelog.asciidoc +++ b/doc/changelog.asciidoc @@ -62,6 +62,8 @@ Fixed when using `:spawn --userscript`. - `:version` and `--version` now don't crash if there's an (invalid) `/etc/os-release` file which has non-comment lines without a `=` character. +- Scripts in `scripts/` now report errors to `stderr` correctly, instead of + using `stdout`. v1.10.2 (2020-04-17) -------------------- diff --git a/scripts/asciidoc2html.py b/scripts/asciidoc2html.py index ceac1ff41..d43531ca3 100755 --- a/scripts/asciidoc2html.py +++ b/scripts/asciidoc2html.py @@ -263,8 +263,9 @@ class AsciiDoc: subprocess.run(cmdline, check=True, env=env) except (subprocess.CalledProcessError, OSError) as e: self._failed = True - utils.print_col(str(e), 'red') - print("Keeping modified sources in {}.".format(self._homedir)) + utils.print_error(str(e)) + print("Keeping modified sources in {}.".format(self._homedir), + file=sys.stderr) sys.exit(1) @@ -291,9 +292,9 @@ def run(**kwargs): try: asciidoc.prepare() except FileNotFoundError: - utils.print_col("Could not find asciidoc! Please install it, or use " - "the --asciidoc argument to point this script to the " - "correct python/asciidoc.py location!", 'red') + utils.print_error("Could not find asciidoc! Please install it, or use " + "the --asciidoc argument to point this script to " + "the correct python/asciidoc.py location!") sys.exit(1) try: diff --git a/scripts/dev/build_release.py b/scripts/dev/build_release.py index 5167c60aa..68befff65 100755 --- a/scripts/dev/build_release.py +++ b/scripts/dev/build_release.py @@ -423,8 +423,8 @@ def github_upload(artifacts, tag): with open(filename, 'rb') as f: release.upload_asset(mimetype, basename, f, description) except github3.exceptions.ConnectionError as e: - utils.print_col('Failed to upload: {}'.format(e), 'red') - print("Press Enter to retry...") + utils.print_error('Failed to upload: {}'.format(e)) + print("Press Enter to retry...", file=sys.stderr) input() print("Retrying!") diff --git a/scripts/dictcli.py b/scripts/dictcli.py index 2f1f369b0..ebe4e285c 100755 --- a/scripts/dictcli.py +++ b/scripts/dictcli.py @@ -225,7 +225,7 @@ def install(languages): "system-wide. If your qutebrowser uses a newer Qt version " "via a virtualenv, make sure you start this script with " "the virtualenv's Python.".format(e)) - scriptutils.print_col(msg, 'red') + scriptutils.print_error(msg) sys.exit(1) diff --git a/scripts/mkvenv.py b/scripts/mkvenv.py index 3d2079435..04cf0e8c0 100644 --- a/scripts/mkvenv.py +++ b/scripts/mkvenv.py @@ -101,7 +101,7 @@ def run_venv(venv_dir: pathlib.Path, executable, *args: str) -> None: subprocess.run([str(venv_dir / subdir / executable)] + [str(arg) for arg in args], check=True) except subprocess.CalledProcessError as e: - utils.print_col("Subprocess failed, exiting", 'red') + utils.print_error("Subprocess failed, exiting") sys.exit(e.returncode) @@ -124,9 +124,9 @@ def show_tox_error(pyqt_type: str) -> None: raise AssertionError print() - utils.print_col('tox -e {} is deprecated. ' - 'Please use "python3 scripts/mkvenv.py{}" instead.' - .format(env, args), 'red') + utils.print_error('tox -e {} is deprecated. ' + 'Please use "python3 scripts/mkvenv.py{}" instead.' + .format(env, args)) print() @@ -143,9 +143,8 @@ def delete_old_venv(venv_dir: pathlib.Path) -> None: ] if not any(m.exists() for m in markers): - utils.print_col('{} does not look like a virtualenv, ' - 'cowardly refusing to remove it.'.format(venv_dir), - 'red') + utils.print_error('{} does not look like a virtualenv, ' + 'cowardly refusing to remove it.'.format(venv_dir)) sys.exit(1) utils.print_col('$ rm -r {}'.format(venv_dir), 'blue') @@ -160,7 +159,7 @@ def create_venv(venv_dir: pathlib.Path, use_virtualenv: bool = False) -> None: subprocess.run([sys.executable, '-m', 'virtualenv', venv_dir], check=True) except subprocess.CalledProcessError as e: - utils.print_col("virtualenv failed, exiting", 'red') + utils.print_error("virtualenv failed, exiting") sys.exit(e.returncode) else: utils.print_col('$ python3 -m venv {}'.format(venv_dir), 'blue') @@ -269,12 +268,12 @@ def main() -> None: sys.exit(1) elif (args.pyqt_version != 'auto' and args.pyqt_type not in ['binary', 'source']): - utils.print_col('The --pyqt-version option is only available when ' - 'installing PyQt from binary or source', 'red') + utils.print_error('The --pyqt-version option is only available when ' + 'installing PyQt from binary or source') sys.exit(1) elif args.pyqt_wheels_dir != 'wheels' and args.pyqt_type != 'wheels': - utils.print_col('The --pyqt-wheels-dir option is only available when ' - 'installing PyQt from wheels', 'red') + utils.print_error('The --pyqt-wheels-dir option is only available ' + 'when installing PyQt from wheels') sys.exit(1) if not args.keep: diff --git a/scripts/utils.py b/scripts/utils.py index 0d405c8a6..bdf3f96fc 100644 --- a/scripts/utils.py +++ b/scripts/utils.py @@ -21,6 +21,7 @@ import os import os.path +import sys # Import side-effects are an evil thing, but here it's okay so scripts using @@ -58,14 +59,18 @@ def _esc(code): return '\033[{}m'.format(code) -def print_col(text, color): +def print_col(text, color, file=sys.stdout): """Print a colorized text.""" if use_color: fg = _esc(fg_colors[color.lower()]) reset = _esc(fg_colors['reset']) - print(''.join([fg, text, reset])) + print(''.join([fg, text, reset]), file=file) else: - print(text) + print(text, file=file) + + +def print_error(text): + print_col(text, 'red', file=sys.stderr) def print_title(text): -- cgit v1.2.3-54-g00ecf