From 394be7d5a2e42807df9fe7d97400f52abfad0b42 Mon Sep 17 00:00:00 2001 From: Hu Yueh-Wei Date: Sun, 15 Dec 2024 21:50:55 +0800 Subject: [PATCH 1/7] chore: refine cmd_exec.py --- .gnfiles/build/scripts/cmd_exec.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/.gnfiles/build/scripts/cmd_exec.py b/.gnfiles/build/scripts/cmd_exec.py index d90cace..afa48a8 100644 --- a/.gnfiles/build/scripts/cmd_exec.py +++ b/.gnfiles/build/scripts/cmd_exec.py @@ -110,6 +110,8 @@ def run_cmd_realtime( stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, + encoding="utf-8", + errors="replace", shell=set_shell, bufsize=0, env=env, @@ -132,10 +134,7 @@ def run_cmd_realtime( try: while child.poll() is None: if child.stdout: - try: - line = child.stdout.readline() - except UnicodeDecodeError: - line = child.stdout.readline().encode("gbk").decode("gbk") + line = child.stdout.readline() if line != "": console_log += line @@ -160,8 +159,6 @@ def run_cmd_realtime( ) sys.stdout.flush() - except UnicodeDecodeError: - pass except Exception as e: sys.stdout.write( "{}{} > {}\n".format( From 17697e0e09a47c20fa73eab54f5da1bc7a99a86c Mon Sep 17 00:00:00 2001 From: Hu Yueh-Wei Date: Sun, 15 Dec 2024 22:07:40 +0800 Subject: [PATCH 2/7] chore: refine cmd_exec.py --- .gnfiles/build/scripts/cmd_exec.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/.gnfiles/build/scripts/cmd_exec.py b/.gnfiles/build/scripts/cmd_exec.py index afa48a8..b4475d5 100644 --- a/.gnfiles/build/scripts/cmd_exec.py +++ b/.gnfiles/build/scripts/cmd_exec.py @@ -44,19 +44,21 @@ def get_cmd_output(cmd: str, log_level: int = 0) -> tuple[int, str]: if log_level > 1: print(f"> {cmd}") - pipe = os.popen(cmd + " 2>&1", "r") + process = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + shell=True, + text=True, + encoding="utf-8", + errors="replace", + ) output_text = "" - while 1: - line = pipe.readline() - if not line: - break - output_text += line + # Read all output at once. + output_text, _ = process.communicate() - try: - returncode = pipe.close() - except Exception: - returncode = 1 + returncode = process.returncode if returncode is None: returncode = 0 From 492a96528a97210edf0e3bd2beaada56c68f2087 Mon Sep 17 00:00:00 2001 From: Hu Yueh-Wei Date: Sun, 15 Dec 2024 22:23:00 +0800 Subject: [PATCH 3/7] chore: refine cmd_exec.py --- .gnfiles/build/toolchain/msvc/BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/.gnfiles/build/toolchain/msvc/BUILD.gn b/.gnfiles/build/toolchain/msvc/BUILD.gn index b38d4e1..46a9865 100644 --- a/.gnfiles/build/toolchain/msvc/BUILD.gn +++ b/.gnfiles/build/toolchain/msvc/BUILD.gn @@ -28,6 +28,7 @@ default_libs = [ "oldnames.lib", "userenv.lib", "ntdll.lib", + "Shlwapi.lib", "synchronization.lib", ] From 01585462dd458fc0a765d1af14ae0ba85e40c77d Mon Sep 17 00:00:00 2001 From: Hu Yueh-Wei Date: Sun, 15 Dec 2024 22:46:55 +0800 Subject: [PATCH 4/7] chore: refine cmd_exec.py --- .gnfiles/build/toolchain/msvc/BUILD.gn | 1 - 1 file changed, 1 deletion(-) diff --git a/.gnfiles/build/toolchain/msvc/BUILD.gn b/.gnfiles/build/toolchain/msvc/BUILD.gn index 46a9865..b38d4e1 100644 --- a/.gnfiles/build/toolchain/msvc/BUILD.gn +++ b/.gnfiles/build/toolchain/msvc/BUILD.gn @@ -28,7 +28,6 @@ default_libs = [ "oldnames.lib", "userenv.lib", "ntdll.lib", - "Shlwapi.lib", "synchronization.lib", ] From b0434df96274e96c16922e0e8fb083a2d7059c63 Mon Sep 17 00:00:00 2001 From: Hu Yueh-Wei Date: Mon, 16 Dec 2024 10:50:27 +0800 Subject: [PATCH 5/7] chore: refine cmd_exec.py --- .gnfiles/build/scripts/cmd_exec.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gnfiles/build/scripts/cmd_exec.py b/.gnfiles/build/scripts/cmd_exec.py index b4475d5..c2406fa 100644 --- a/.gnfiles/build/scripts/cmd_exec.py +++ b/.gnfiles/build/scripts/cmd_exec.py @@ -44,6 +44,8 @@ def get_cmd_output(cmd: str, log_level: int = 0) -> tuple[int, str]: if log_level > 1: print(f"> {cmd}") + # Use UTF-8 encoding with the `replace` option to avoid decode errors when + # Python handling the `output_text`. process = subprocess.Popen( cmd, stdout=subprocess.PIPE, @@ -59,7 +61,6 @@ def get_cmd_output(cmd: str, log_level: int = 0) -> tuple[int, str]: output_text, _ = process.communicate() returncode = process.returncode - if returncode is None: returncode = 0 @@ -107,6 +108,8 @@ def run_cmd_realtime( if sys.platform == "win32" and cmd[:3] != "cmd": my_cmd = "cmd /c " + cmd + # Use UTF-8 encoding with the `replace` option to avoid decode errors when + # Python handling the `output_text`. child = subprocess.Popen( my_cmd, stdout=subprocess.PIPE, From 32b7d573d1d3ce6ba020c29035f2fbdf93714254 Mon Sep 17 00:00:00 2001 From: Hu Yueh-Wei Date: Mon, 16 Dec 2024 14:43:38 +0800 Subject: [PATCH 6/7] chore: refine cmd_exec.py --- .gnfiles/build/scripts/cmd_exec.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.gnfiles/build/scripts/cmd_exec.py b/.gnfiles/build/scripts/cmd_exec.py index c2406fa..04c8050 100644 --- a/.gnfiles/build/scripts/cmd_exec.py +++ b/.gnfiles/build/scripts/cmd_exec.py @@ -5,11 +5,12 @@ # Refer to the "LICENSE" file in the root directory for more information. # import sys +import io import os import subprocess -# psutil is not a builtin module, so use it if it exists. +# `psutil` is not a builtin module, so use it if it exists. try: import psutil @@ -17,6 +18,10 @@ except ImportError: has_psutil = False +# Reconfigure stdout to use UTF-8 on Windows. +if sys.platform == "win32": + sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8") + def get_cmd_output(cmd: str, log_level: int = 0) -> tuple[int, str]: """Return (status, output) of executing cmd in a shell.""" From 5f2baabb2830aeb85263f6644ad3ceff43d875cd Mon Sep 17 00:00:00 2001 From: Hu Yueh-Wei Date: Mon, 16 Dec 2024 15:30:28 +0800 Subject: [PATCH 7/7] chore: refine cmd_exec.py --- .gnfiles/build/scripts/cmd_exec.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.gnfiles/build/scripts/cmd_exec.py b/.gnfiles/build/scripts/cmd_exec.py index 04c8050..dc7ca49 100644 --- a/.gnfiles/build/scripts/cmd_exec.py +++ b/.gnfiles/build/scripts/cmd_exec.py @@ -5,7 +5,6 @@ # Refer to the "LICENSE" file in the root directory for more information. # import sys -import io import os import subprocess @@ -18,10 +17,6 @@ except ImportError: has_psutil = False -# Reconfigure stdout to use UTF-8 on Windows. -if sys.platform == "win32": - sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8") - def get_cmd_output(cmd: str, log_level: int = 0) -> tuple[int, str]: """Return (status, output) of executing cmd in a shell."""