Skip to content

Commit 82bef7c

Browse files
Make logged commands readable when they contain quotes (#2993)
* Make logged commands readable when they contain quotes shlex.quote always uses single quotes, so any argument containing a single quote is echoed as '"'"' soup. That is what the Linux log lines in #1055 look like, because the container's `call` reuses its shell-quoted command string for the log message. Add a display-only helper in util.cmd that leaves unambiguous arguments bare, wraps arguments containing single quotes in double quotes so the quotes survive, and falls back to shlex.quote otherwise. Use it at the two places that print a command; execution still uses shlex.quote. Fixes #1055 * Use PurePosixPath in the path test so it passes on Windows * test: check display format round-trips through shlex.split Assisted-by: ClaudeCode:claude-opus-5-5 --------- Co-authored-by: Henry Schreiner <[email protected]>
1 parent 84f6233 commit 82bef7c

3 files changed

Lines changed: 105 additions & 6 deletions

File tree

‎cibuildwheel/oci_container.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from cibuildwheel.ci import CIProvider, detect_ci_provider
3939
from cibuildwheel.errors import OCIEngineTooOldError
4040
from cibuildwheel.logger import log
41-
from cibuildwheel.util.cmd import call
41+
from cibuildwheel.util.cmd import call, format_command_for_display
4242
from cibuildwheel.util.helpers import FlexibleVersion, parse_key_value_string, strtobool
4343

4444
TYPE_CHECKING = False
@@ -518,7 +518,7 @@ def call(
518518
end_of_message = str(uuid.uuid4())
519519

520520
# log the command we're executing
521-
print(f" + {command}")
521+
print(f" + {format_command_for_display(args)}")
522522

523523
# Write a command to the remote shell. First we change the
524524
# cwd, if that's required. Then, we use the `env` utility to run

‎cibuildwheel/util/cmd.py‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,41 @@
1313

1414
TYPE_CHECKING = False
1515
if TYPE_CHECKING:
16-
from collections.abc import Iterator, Mapping
17-
from typing import Literal
16+
from collections.abc import Iterable, Iterator, Mapping
17+
from typing import Final, Literal
1818

1919
from cibuildwheel.typing import PathOrStr
2020

21+
# characters that no shell treats specially, so they never need quoting
22+
_BARE_CHARS: Final[frozenset[str]] = frozenset(
23+
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@%+=:,./-_"
24+
)
25+
# characters that a shell would still act on inside double quotes
26+
_ACTIVE_IN_DOUBLE_QUOTES: Final[frozenset[str]] = frozenset("`$\\")
27+
28+
29+
def format_command_for_display(args: Iterable[PathOrStr]) -> str:
30+
"""
31+
Render a command as a string that's pleasant to read in a log.
32+
33+
This is for display only. Don't build a command out of it - use
34+
shlex.quote for anything that will actually be run.
35+
36+
shlex.quote always uses single quotes, so an argument that itself contains
37+
a single quote comes out as unreadable '"'"' soup. Where it's unambiguous
38+
we leave the argument bare, or wrap it in double quotes so its single
39+
quotes survive intact, and otherwise fall back to shlex.quote.
40+
"""
41+
return " ".join(_quote_for_display(str(arg)) for arg in args)
42+
43+
44+
def _quote_for_display(arg: str) -> str:
45+
if arg and _BARE_CHARS.issuperset(arg):
46+
return arg
47+
if "'" in arg and _ACTIVE_IN_DOUBLE_QUOTES.isdisjoint(arg):
48+
return '"' + arg.replace('"', '\\"') + '"'
49+
return shlex.quote(arg)
50+
2151

2252
@typing.overload
2353
def call(
@@ -51,7 +81,7 @@ def call(
5181
"""
5282
args_ = [str(arg) for arg in args]
5383
# print the command executing for the logs
54-
print("+ " + " ".join(shlex.quote(a) for a in args_))
84+
print("+ " + format_command_for_display(args_))
5585
# workaround platform behaviour differences outlined
5686
# in https://github.com/python/cpython/issues/52803
5787
path_env = env if env is not None else os.environ

‎unit_test/utils_test.py‎

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import re
2+
import shlex
23
import textwrap
3-
from pathlib import Path, PurePath
4+
from pathlib import Path, PurePath, PurePosixPath
45
from unittest.mock import Mock, call
56

67
import pytest
78

89
from cibuildwheel import errors
910
from cibuildwheel.ci import fix_ansi_codes_for_github_actions
11+
from cibuildwheel.util.cmd import format_command_for_display
1012
from cibuildwheel.util.file import copy_test_sources, remove_on_error
1113
from cibuildwheel.util.helpers import (
1214
FlexibleVersion,
@@ -496,3 +498,70 @@ def test_none_any_wheel(self) -> None:
496498

497499
def test_none_platform_wheel(self) -> None:
498500
assert is_abi3_wheel("foo-1.0-cp310-none-win_amd64.whl") is False
501+
502+
503+
class TestFormatCommandForDisplay:
504+
@pytest.mark.parametrize(
505+
("args", "expected"),
506+
[
507+
# ordinary arguments are left bare
508+
(["python", "-m", "pip", "install", "."], "python -m pip install ."),
509+
# whitespace still has to be delimited
510+
(["echo", "hello world"], "echo 'hello world'"),
511+
# single quotes are shown verbatim inside double quotes, rather
512+
# than being exploded into '"'"' by shlex.quote
513+
(["sh", "-c", "echo 'hi'"], """sh -c "echo 'hi'\""""),
514+
# double quotes alone read fine inside shlex.quote's single quotes
515+
(["sh", "-c", 'echo "hi"'], "sh -c 'echo \"hi\"'"),
516+
# empty arguments must stay visible
517+
(["cmd", ""], "cmd ''"),
518+
# shell metacharacters are quoted so the line can't be misread
519+
(["sh", "-c", "a && b | c > d"], "sh -c 'a && b | c > d'"),
520+
],
521+
)
522+
def test_quoting(self, args: list[str], expected: str) -> None:
523+
assert format_command_for_display(args) == expected
524+
525+
def test_issue_1055(self) -> None:
526+
# https://github.com/pypa/cibuildwheel/issues/1055
527+
command = """python -c "import sys; print('hello')\""""
528+
displayed = format_command_for_display(["sh", "-c", command])
529+
assert displayed == '''sh -c "python -c \\"import sys; print('hello')\\""'''
530+
assert """'"'"'""" not in displayed
531+
532+
def test_expandable_arguments_fall_back_to_shlex(self) -> None:
533+
# double quotes would let the shell expand these, so don't use them
534+
for arg in ("it's $HOME", "it's `pwd`", "it's a back\\slash"):
535+
assert format_command_for_display([arg]) == shlex.quote(arg)
536+
537+
@pytest.mark.parametrize(
538+
"args",
539+
[
540+
["python", "-m", "pip", "install", "."],
541+
["cmd", "", "hello world", "\ttab\nnewline"],
542+
["sh", "-c", "a && b | c > d; e & f"],
543+
["sh", "-c", "echo 'hi'"],
544+
["sh", "-c", 'echo "hi"'],
545+
["sh", "-c", """echo "it's" 'a "quote"'"""],
546+
["it's $HOME", "it's `pwd`", "it's a back\\slash", "$'\\n'"],
547+
["'", '"', "''", '""', "'\"'\"'", "\\", "\\\\\"'"],
548+
["!", "!!", "*.whl", "~", "#comment", "a=b", "{a,b}", "(x)", "[y]"],
549+
['"\'" "\\"" \'$\' "`"', "unicode 'ü' \"✓\""],
550+
# the command from https://github.com/pypa/cibuildwheel/issues/1055
551+
[
552+
"sh",
553+
"-c",
554+
(
555+
"""python -c "import sys,glob; open('requirements.txt', 'w').write("""
556+
"""glob.glob('wheelhouse/MyApp-*cp'+''.join(map(str, sys.version_info[:2]))"""
557+
"""+'-*.whl')[0])\""""
558+
),
559+
],
560+
],
561+
)
562+
def test_round_trips_through_shlex(self, args: list[str]) -> None:
563+
assert shlex.split(format_command_for_display(args)) == args
564+
565+
def test_accepts_paths(self) -> None:
566+
# PurePosixPath, so the expected string doesn't depend on the host OS
567+
assert format_command_for_display(["ls", PurePosixPath("/a b/c")]) == "ls '/a b/c'"

0 commit comments

Comments
 (0)