From 8a1aff2389b554798bea5ab82fc4d8b1336a6ef1 Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Mon, 9 Oct 2023 07:22:24 -0700 Subject: [PATCH] rust: Add eprint help to write to stderr Summary: Adding `file=sys.stderr` is a mouthful. Even more if we try to align on always using LF (and not CRLF). Reviewed By: shayne-fletcher Differential Revision: D50080143 fbshipit-source-id: 360b90d25febd7281b0a308adf16171eb59a1e65 --- prelude/rust/tools/buildscript_run.py | 16 ++++++++++------ prelude/rust/tools/failure_filter_action.py | 11 ++++++----- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/prelude/rust/tools/buildscript_run.py b/prelude/rust/tools/buildscript_run.py index ee10e9621619..69a96f3389d1 100755 --- a/prelude/rust/tools/buildscript_run.py +++ b/prelude/rust/tools/buildscript_run.py @@ -15,12 +15,16 @@ import subprocess import sys from pathlib import Path -from typing import Dict, IO, NamedTuple +from typing import Any, Dict, IO, NamedTuple IS_WINDOWS: bool = os.name == "nt" +def eprint(*args: Any, **kwargs: Any) -> None: + print(*args, end="\n", file=sys.stderr, flush=True, **kwargs) + + def cfg_env(rustc_cfg: Path) -> Dict[str, str]: with rustc_cfg.open(encoding="utf-8") as f: lines = f.readlines() @@ -142,13 +146,13 @@ def ensure_rustc_available( shell=IS_WINDOWS, ) except OSError as ex: - print(f"Failed to run {rustc} because {ex}", file=sys.stderr) + eprint(f"Failed to run {rustc} because {ex}") sys.exit(1) except subprocess.CalledProcessError as ex: - print(f"Command failed with exit code {ex.returncode}", file=sys.stderr) - print(f"Command: {ex.cmd}", file=sys.stderr) + eprint(f"Command failed with exit code {ex.returncode}") + eprint(f"Command: {ex.cmd}") if ex.stdout: - print(f"Stdout: {ex.stdout}", file=sys.stderr) + eprint(f"Stdout: {ex.stdout}") sys.exit(1) @@ -216,7 +220,7 @@ def main() -> None: # noqa: C901 if cargo_rustc_cfg_match: flags += "--cfg={}\n".format(cargo_rustc_cfg_match.group(1)) else: - print(line) + print(line, end="\n") args.outfile.write(flags) diff --git a/prelude/rust/tools/failure_filter_action.py b/prelude/rust/tools/failure_filter_action.py index 6fd2342ee7bd..155c93ad5b18 100755 --- a/prelude/rust/tools/failure_filter_action.py +++ b/prelude/rust/tools/failure_filter_action.py @@ -16,7 +16,11 @@ import os import shutil import sys -from typing import IO, List, NamedTuple, Optional, Tuple +from typing import Any, IO, List, NamedTuple, Optional, Tuple + + +def eprint(*args: Any, **kwargs: Any) -> None: + print(*args, end="\n", file=sys.stderr, flush=True, **kwargs) class Args(NamedTuple): @@ -69,10 +73,7 @@ def main() -> int: # Fall back to real copy if that doesn't work shutil.copy(inp, out) else: - print( - f"Missing required input file {short} ({inp})", - file=sys.stderr, - ) + eprint(f"Missing required input file {short} ({inp})") return build_status["status"] # If all the required files were present, then success regardless of