Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refine cmd_exec.py #17

Merged
merged 7 commits into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions .gnfiles/build/scripts/cmd_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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

Expand Down Expand Up @@ -44,20 +44,23 @@ 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")
# 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,
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

try:
returncode = pipe.close()
except Exception:
returncode = 1
# Read all output at once.
output_text, _ = process.communicate()

returncode = process.returncode
if returncode is None:
returncode = 0

Expand Down Expand Up @@ -105,11 +108,15 @@ 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,
stderr=subprocess.STDOUT,
text=True,
encoding="utf-8",
errors="replace",
shell=set_shell,
bufsize=0,
env=env,
Expand All @@ -132,10 +139,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
Expand All @@ -160,8 +164,6 @@ def run_cmd_realtime(
)

sys.stdout.flush()
except UnicodeDecodeError:
pass
except Exception as e:
sys.stdout.write(
"{}{} > {}\n".format(
Expand Down
Loading