scripts: Print errors to stderr instead of stdout

This commit is contained in:
Florian Bruhin 2020-04-21 09:56:34 +02:00
parent d077737840
commit c799b7ddf6
6 changed files with 30 additions and 23 deletions

View File

@ -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)
--------------------

View File

@ -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:

View File

@ -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!")

View File

@ -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)

View File

@ -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:

View File

@ -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):