Files
cibuildwheel/cibuildwheel/logger.py
T

436 lines
14 KiB
Python
Raw Normal View History

2026-05-28 00:20:08 +02:00
from __future__ import annotations
2020-11-13 18:37:30 +00:00
import codecs
2025-07-15 20:45:53 -07:00
import contextlib
import dataclasses
import functools
import hashlib
import io
2020-08-03 17:38:07 +01:00
import os
import re
2020-11-13 16:30:27 +00:00
import sys
2025-07-15 20:45:53 -07:00
import textwrap
2020-11-13 16:30:27 +00:00
import time
2025-07-15 20:45:53 -07:00
from pathlib import Path
2020-08-03 17:38:07 +01:00
2025-07-15 20:45:53 -07:00
import humanize
2026-03-24 09:20:41 -04:00
from cibuildwheel.ci import CIProvider, detect_ci_provider, filter_ansi_codes
2025-07-15 20:45:53 -07:00
TYPE_CHECKING = False
2025-07-15 20:45:53 -07:00
if TYPE_CHECKING:
2026-05-28 00:20:08 +02:00
from collections.abc import Generator
from typing import IO, AnyStr, Final, Literal
2026-03-24 09:20:41 -04:00
from cibuildwheel.options import Options
2020-11-23 21:00:22 +00:00
2026-05-28 00:20:08 +02:00
FoldPattern = tuple[str, str]
2023-05-08 21:05:24 +01:00
DEFAULT_FOLD_PATTERN: Final[FoldPattern] = ("{name}", "")
FOLD_PATTERNS: Final[dict[str, FoldPattern]] = {
2021-05-03 11:45:43 -04:00
"azure": ("##[group]{name}", "##[endgroup]"),
"travis": ("travis_fold:start:{identifier}\n{name}", "travis_fold:end:{identifier}"),
"github": ("::group::{name}", "::endgroup::{name}"),
2020-08-03 17:38:07 +01:00
}
2023-05-08 21:05:24 +01:00
PLATFORM_IDENTIFIER_DESCRIPTIONS: Final[dict[str, str]] = {
2021-05-03 11:45:43 -04:00
"manylinux_x86_64": "manylinux x86_64",
"manylinux_i686": "manylinux i686",
"manylinux_aarch64": "manylinux aarch64",
"manylinux_ppc64le": "manylinux ppc64le",
"manylinux_s390x": "manylinux s390x",
2024-11-16 01:07:24 +01:00
"manylinux_armv7l": "manylinux armv7l",
2025-04-28 18:05:03 +02:00
"manylinux_riscv64": "manylinux riscv64",
"musllinux_x86_64": "musllinux x86_64",
"musllinux_i686": "musllinux i686",
"musllinux_aarch64": "musllinux aarch64",
"musllinux_ppc64le": "musllinux ppc64le",
2024-10-01 16:33:57 +02:00
"musllinux_s390x": "musllinux s390x",
"musllinux_armv7l": "musllinux armv7l",
2025-04-28 18:05:03 +02:00
"musllinux_riscv64": "musllinux riscv64",
2021-05-03 11:45:43 -04:00
"win32": "Windows 32bit",
"win_amd64": "Windows 64bit",
"win_arm64": "Windows on ARM 64bit",
2021-05-03 11:45:43 -04:00
"macosx_x86_64": "macOS x86_64",
"macosx_universal2": "macOS Universal 2 - x86_64 and arm64",
"macosx_arm64": "macOS arm64 - Apple Silicon",
2024-05-28 05:31:36 -07:00
"pyodide_wasm32": "Pyodide",
"android_arm64_v8a": "Android arm64_v8a",
"android_x86_64": "Android x86_64",
"ios_arm64_iphoneos": "iOS Device (ARM64)",
"ios_arm64_iphonesimulator": "iOS Simulator (ARM64)",
"ios_x86_64_iphonesimulator": "iOS Simulator (x86_64)",
2020-08-03 17:38:07 +01:00
}
class Colors:
def __init__(self, *, enabled: bool) -> None:
self.red = "\033[31m" if enabled else ""
self.green = "\033[32m" if enabled else ""
self.yellow = "\033[33m" if enabled else ""
self.blue = "\033[34m" if enabled else ""
self.cyan = "\033[36m" if enabled else ""
self.bright_red = "\033[91m" if enabled else ""
self.bright_green = "\033[92m" if enabled else ""
self.white = "\033[37m\033[97m" if enabled else ""
self.gray = "\033[38;5;244m" if enabled else ""
self.bg_grey = "\033[48;5;235m" if enabled else ""
self.bold = "\033[1m" if enabled else ""
self.faint = "\033[2m" if enabled else ""
self.end = "\033[0m" if enabled else ""
class Symbols:
def __init__(self, *, unicode: bool) -> None:
self.done = "✓" if unicode else "done"
self.error = "✕" if unicode else "failed"
2025-07-15 20:45:53 -07:00
@dataclasses.dataclass(kw_only=True, frozen=True)
class BuildInfo:
identifier: str
filename: Path | None
duration: float
@functools.cached_property
def size(self) -> str | None:
if self.filename is None:
return None
return humanize.naturalsize(self.filename.stat().st_size)
@functools.cached_property
def sha256(self) -> str | None:
if self.filename is None:
return None
with self.filename.open("rb") as f:
digest = hashlib.file_digest(f, "sha256")
return digest.hexdigest()
def __str__(self) -> str:
duration = humanize.naturaldelta(self.duration)
if self.filename:
return f"{self.identifier}: {self.filename.name} {self.size} in {duration}, SHA256={self.sha256}"
return f"{self.identifier}: {duration} (test only)"
2020-08-03 17:38:07 +01:00
class Logger:
2025-06-28 03:18:18 -04:00
fold_mode: Literal["azure", "github", "travis", "disabled"]
2020-11-01 11:34:02 +00:00
colors_enabled: bool
2020-11-13 18:37:30 +00:00
unicode_enabled: bool
active_build_identifier: str | None = None
build_start_time: float | None = None
step_start_time: float | None = None
active_fold_group_name: str | None = None
2025-07-15 20:45:53 -07:00
summary: list[BuildInfo]
2020-11-01 11:34:02 +00:00
def __init__(self) -> None:
2021-05-03 11:45:43 -04:00
if sys.platform == "win32" and hasattr(sys.stdout, "reconfigure"):
# the encoding on Windows can be a 1-byte charmap, but all CIs
# support utf8, so we hardcode that
2021-05-03 11:45:43 -04:00
sys.stdout.reconfigure(encoding="utf8")
2020-11-13 18:37:30 +00:00
self.unicode_enabled = file_supports_unicode(sys.stdout)
2020-11-23 21:00:22 +00:00
ci_provider = detect_ci_provider()
2025-07-15 20:45:53 -07:00
match ci_provider:
case CIProvider.azure_pipelines:
self.fold_mode = "azure"
self.colors_enabled = True
2020-08-03 17:38:07 +01:00
2025-07-15 20:45:53 -07:00
case CIProvider.github_actions:
self.fold_mode = "github"
self.colors_enabled = True
2020-08-03 17:38:07 +01:00
2025-07-15 20:45:53 -07:00
case CIProvider.travis_ci:
self.fold_mode = "travis"
self.colors_enabled = True
2020-08-03 17:38:07 +01:00
2025-07-15 20:45:53 -07:00
case CIProvider.appveyor:
self.fold_mode = "disabled"
self.colors_enabled = True
2020-08-03 17:38:07 +01:00
2025-07-15 20:45:53 -07:00
case _:
self.fold_mode = "disabled"
self.colors_enabled = file_supports_color(sys.stdout)
self.summary = []
2020-08-03 17:38:07 +01:00
def build_start(self, identifier: str) -> None:
self.step_end()
2020-08-03 17:38:07 +01:00
c = self.colors
2020-11-01 12:33:14 +00:00
description = build_description_from_identifier(identifier)
2020-11-01 11:34:02 +00:00
print()
2021-05-03 11:45:43 -04:00
print(f"{c.bold}{c.blue}Building {identifier} wheel{c.end}")
print(f"{description}")
2020-11-01 11:34:02 +00:00
print()
2020-08-03 17:38:07 +01:00
2020-11-01 11:34:02 +00:00
self.build_start_time = time.time()
self.active_build_identifier = identifier
2025-07-15 20:45:53 -07:00
def build_end(self, filename: Path | None) -> None:
2020-11-01 11:34:02 +00:00
assert self.build_start_time is not None
2020-11-01 15:19:42 +00:00
assert self.active_build_identifier is not None
2020-11-01 11:43:28 +00:00
self.step_end()
2020-08-03 17:38:07 +01:00
2020-11-01 15:19:42 +00:00
c = self.colors
2020-11-13 18:37:30 +00:00
s = self.symbols
2020-11-01 15:19:42 +00:00
duration = time.time() - self.build_start_time
2025-07-15 20:45:53 -07:00
duration_str = humanize.naturaldelta(duration, minimum_unit="milliseconds")
2020-11-01 15:19:42 +00:00
2020-11-01 11:34:02 +00:00
print()
2025-07-15 20:45:53 -07:00
print(f"{c.green}{s.done} {c.end}{self.active_build_identifier} finished in {duration_str}")
self.summary.append(
BuildInfo(identifier=self.active_build_identifier, filename=filename, duration=duration)
2021-04-30 17:56:34 -04:00
)
2025-07-15 20:45:53 -07:00
2020-11-01 11:34:02 +00:00
self.build_start_time = None
2020-11-01 15:19:42 +00:00
self.active_build_identifier = None
2020-08-03 17:38:07 +01:00
def step(self, step_description: str) -> None:
2020-11-01 11:43:28 +00:00
self.step_end()
2020-11-01 11:34:02 +00:00
self.step_start_time = time.time()
2020-11-01 21:36:44 +00:00
self._start_fold_group(step_description)
2020-08-03 17:38:07 +01:00
def step_end(self, success: bool = True) -> None:
2020-11-01 11:34:02 +00:00
if self.step_start_time is not None:
2020-11-01 21:36:44 +00:00
self._end_fold_group()
2020-11-01 11:34:02 +00:00
c = self.colors
2020-11-13 18:37:30 +00:00
s = self.symbols
2020-11-01 11:34:02 +00:00
duration = time.time() - self.step_start_time
2025-07-15 20:45:53 -07:00
2020-11-01 21:36:44 +00:00
if success:
2021-05-03 11:45:43 -04:00
print(f"{c.green}{s.done} {c.end}{duration:.2f}s".rjust(78))
2020-11-01 21:36:44 +00:00
else:
2021-05-03 11:45:43 -04:00
print(f"{c.red}{s.error} {c.end}{duration:.2f}s".rjust(78))
2020-11-01 21:36:44 +00:00
2020-11-01 11:34:02 +00:00
self.step_start_time = None
def step_end_with_error(self, error: BaseException | str) -> None:
2020-11-01 21:36:44 +00:00
self.step_end(success=False)
self.error(error)
2023-01-14 09:48:01 +01:00
def quiet(self, message: str) -> None:
c = self.colors
print(f"{c.gray}{message}{c.end}", file=sys.stderr)
2022-08-01 20:23:30 +01:00
def notice(self, message: str) -> None:
if self.fold_mode == "github":
print(f"::notice::cibuildwheel: {message}\n", file=sys.stderr)
2022-08-01 20:23:30 +01:00
else:
c = self.colors
print(f"cibuildwheel: {c.bold}note{c.end}: {message}\n", file=sys.stderr)
2022-08-01 20:23:30 +01:00
def warning(self, message: str) -> None:
2021-05-03 11:45:43 -04:00
if self.fold_mode == "github":
print(f"::warning::cibuildwheel: {message}\n", file=sys.stderr)
else:
c = self.colors
print(f"cibuildwheel: {c.yellow}warning{c.end}: {message}\n", file=sys.stderr)
2021-01-05 19:02:15 +00:00
def error(self, error: BaseException | str) -> None:
2021-05-03 11:45:43 -04:00
if self.fold_mode == "github":
print(f"::error::cibuildwheel: {error}\n", file=sys.stderr)
2020-11-01 21:36:44 +00:00
else:
c = self.colors
print(f"cibuildwheel: {c.bright_red}error{c.end}: {error}\n", file=sys.stderr)
2021-01-05 19:02:15 +00:00
2025-07-15 20:45:53 -07:00
@contextlib.contextmanager
2026-05-28 00:20:08 +02:00
def print_summary(self, *, options: Options) -> Generator[None, None, None]:
2025-07-15 20:45:53 -07:00
start = time.time()
yield
duration = time.time() - start
if summary_path := os.environ.get("GITHUB_STEP_SUMMARY"):
github_summary = self._github_step_summary(duration=duration, options=options)
Path(summary_path).write_text(filter_ansi_codes(github_summary), encoding="utf-8")
n_wheels = len([info for info in self.summary if info.filename])
s = "s" if n_wheels > 1 else ""
2025-07-15 20:45:53 -07:00
duration_str = humanize.naturaldelta(duration)
print()
self._start_fold_group(f"{n_wheels} wheel{s} produced in {duration_str}")
2025-07-15 20:45:53 -07:00
for build_info in self.summary:
print(" ", build_info)
self._end_fold_group()
self.summary = []
@property
def step_active(self) -> bool:
return self.step_start_time is not None
def _start_fold_group(self, name: str) -> None:
2020-11-01 21:36:44 +00:00
self._end_fold_group()
2020-11-01 11:43:28 +00:00
self.active_fold_group_name = name
2020-11-01 16:57:07 +00:00
fold_start_pattern = FOLD_PATTERNS.get(self.fold_mode, DEFAULT_FOLD_PATTERN)[0]
identifier = self._fold_group_identifier(name)
2020-11-01 12:00:59 +00:00
print(fold_start_pattern.format(name=self.active_fold_group_name, identifier=identifier))
print()
sys.stdout.flush()
2020-11-01 11:34:02 +00:00
def _end_fold_group(self) -> None:
2020-11-01 11:43:28 +00:00
if self.active_fold_group_name:
2020-11-01 16:57:07 +00:00
fold_start_pattern = FOLD_PATTERNS.get(self.fold_mode, DEFAULT_FOLD_PATTERN)[1]
identifier = self._fold_group_identifier(self.active_fold_group_name)
2021-04-30 17:56:34 -04:00
print(
fold_start_pattern.format(name=self.active_fold_group_name, identifier=identifier)
)
2020-11-01 12:03:21 +00:00
sys.stdout.flush()
2020-11-01 11:43:28 +00:00
self.active_fold_group_name = None
2020-08-03 17:38:07 +01:00
@staticmethod
def _fold_group_identifier(name: str) -> str:
2021-05-03 11:45:43 -04:00
"""
Travis doesn't like fold groups identifiers that have spaces in. This
method converts them to ascii identifiers
2021-05-03 11:45:43 -04:00
"""
2020-11-13 16:46:58 +00:00
# whitespace to underscores
2021-05-03 11:45:43 -04:00
identifier = re.sub(r"\s+", "_", name)
# remove non-alphanum
2021-05-03 11:45:43 -04:00
identifier = re.sub(r"[^A-Za-z\d_]+", "", identifier)
2020-11-13 16:46:58 +00:00
# trim underscores
2021-05-03 11:45:43 -04:00
identifier = identifier.strip("_")
2020-11-13 16:46:58 +00:00
# lowercase, shorten
return identifier.lower()[:20]
2026-05-28 00:20:08 +02:00
def _github_step_summary(self, duration: float, options: Options) -> str:
2025-07-15 20:45:53 -07:00
"""
Returns the GitHub step summary, in markdown format.
"""
out = io.StringIO()
options_summary = options.summary(
identifiers=[bi.identifier for bi in self.summary], skip_unset=True
)
out.write(
textwrap.dedent("""\
### 🎡 cibuildwheel
<details>
<summary>
Build options
</summary>
```yaml
{options_summary}
```
</details>
""").format(options_summary=options_summary)
)
n_wheels = len([b for b in self.summary if b.filename])
wheel_rows = "\n".join(
"<tr>"
f"<td nowrap>{'<samp>' + b.filename.name + '</samp>' if b.filename else '*Test only*'}</td>"
2025-07-15 20:45:53 -07:00
f"<td nowrap>{b.size or 'N/A'}</td>"
f"<td nowrap><samp>{b.identifier}</samp></td>"
f"<td nowrap>{humanize.naturaldelta(b.duration)}</td>"
f"<td nowrap><samp>{b.sha256 or 'N/A'}</samp></td>"
"</tr>"
for b in self.summary
)
out.write(
textwrap.dedent("""\
<table>
<thead>
<tr>
<th align="left">Wheel</th>
<th align="left">Size</th>
<th align="left">Build identifier</th>
<th align="left">Time</th>
<th align="left">SHA256</th>
</tr>
</thead>
<tbody>
{wheel_rows}
</tbody>
</table>
<div align="right"><sup>{n} wheel{s} created in {duration_str}</sup></div>
""").format(
wheel_rows=wheel_rows,
n=n_wheels,
duration_str=humanize.naturaldelta(duration),
s="s" if n_wheels > 1 else "",
)
)
out.write("\n")
out.write("---")
out.write("\n")
return out.getvalue()
2020-08-03 17:38:07 +01:00
@property
def colors(self) -> Colors:
return Colors(enabled=self.colors_enabled)
2020-11-13 18:37:30 +00:00
@property
def symbols(self) -> Symbols:
return Symbols(unicode=self.unicode_enabled)
2020-08-03 17:38:07 +01:00
def build_description_from_identifier(identifier: str) -> str:
2021-05-03 11:45:43 -04:00
python_identifier, _, platform_identifier = identifier.partition("-")
2020-08-03 17:38:07 +01:00
2021-05-03 11:45:43 -04:00
build_description = ""
2020-08-03 17:38:07 +01:00
python_interpreter = python_identifier[0:2]
version_parts = python_identifier[2:].split("_")
python_version = version_parts[0]
2020-08-03 17:38:07 +01:00
2021-05-03 11:45:43 -04:00
if python_interpreter == "cp":
build_description += "CPython"
elif python_interpreter == "pp":
build_description += "PyPy"
2025-04-29 00:25:17 +02:00
elif python_interpreter == "gp":
build_description += "GraalPy"
2020-08-03 17:38:07 +01:00
else:
2022-09-13 08:14:41 -04:00
msg = f"unknown python {python_interpreter!r}"
2022-09-05 13:11:46 -04:00
raise Exception(msg)
2020-08-03 17:38:07 +01:00
build_description += f" {python_version[0]}.{python_version[1:]} "
if len(version_parts) > 1:
build_description += f"(ABI {version_parts[1]}) "
2020-08-03 17:38:07 +01:00
try:
build_description += PLATFORM_IDENTIFIER_DESCRIPTIONS[platform_identifier]
2020-08-03 17:38:07 +01:00
except KeyError as e:
2022-09-05 13:11:46 -04:00
msg = f"unknown platform {platform_identifier!r}"
raise Exception(msg) from e
2020-08-03 17:38:07 +01:00
return build_description
def file_supports_color(file_obj: IO[AnyStr]) -> bool:
2020-11-01 16:57:07 +00:00
"""
Returns True if the running system's terminal supports color.
"""
plat = sys.platform
2021-05-03 11:45:43 -04:00
supported_platform = plat != "win32" or "ANSICON" in os.environ
2020-11-01 16:57:07 +00:00
is_a_tty = file_is_a_tty(file_obj)
2021-04-30 17:56:34 -04:00
return supported_platform and is_a_tty
2020-11-01 16:57:07 +00:00
def file_is_a_tty(file_obj: IO[AnyStr]) -> bool:
2021-05-03 11:45:43 -04:00
return hasattr(file_obj, "isatty") and file_obj.isatty()
def file_supports_unicode(file_obj: IO[AnyStr]) -> bool:
2021-05-03 11:45:43 -04:00
encoding = getattr(file_obj, "encoding", None)
2020-11-13 18:37:30 +00:00
if not encoding:
return False
codec_info = codecs.lookup(encoding)
2021-05-03 11:45:43 -04:00
return "utf" in codec_info.name
2020-11-13 18:37:30 +00:00
# Global instance of the Logger.
# (there's only one stdout per-process, so a global instance is justified)
log = Logger()