Files
cibuildwheel/cibuildwheel/logger.py
T

264 lines
8.5 KiB
Python
Raw Normal View History

2020-11-13 18:37:30 +00:00
import codecs
2020-08-03 17:38:07 +01:00
import os
import re
2020-11-13 16:30:27 +00:00
import sys
import time
2021-01-06 13:50:58 -05:00
from typing import IO, AnyStr, Optional, Union
2020-08-03 17:38:07 +01:00
from cibuildwheel.typing import Final
2020-11-23 21:00:22 +00:00
from cibuildwheel.util import CIProvider, detect_ci_provider
DEFAULT_FOLD_PATTERN: Final = ("{name}", "")
FOLD_PATTERNS: Final = {
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
}
PLATFORM_IDENTIFIER_DESCRIPTIONS: Final = {
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",
"musllinux_x86_64": "musllinux x86_64",
"musllinux_i686": "musllinux i686",
"musllinux_aarch64": "musllinux aarch64",
"musllinux_ppc64le": "musllinux ppc64le",
"musllinux_s390x": "manylinux s390x",
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",
2020-08-03 17:38:07 +01:00
}
class Logger:
2020-11-01 11:34:02 +00:00
fold_mode: str
colors_enabled: bool
2020-11-13 18:37:30 +00:00
unicode_enabled: bool
2020-11-01 11:34:02 +00:00
active_build_identifier: Optional[str] = None
2020-11-01 11:43:28 +00:00
build_start_time: Optional[float] = None
step_start_time: Optional[float] = None
active_fold_group_name: Optional[str] = None
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()
if ci_provider == CIProvider.azure_pipelines:
2021-05-03 11:45:43 -04:00
self.fold_mode = "azure"
2020-08-03 17:38:07 +01:00
self.colors_enabled = True
2020-11-23 21:00:22 +00:00
elif ci_provider == CIProvider.github_actions:
2021-05-03 11:45:43 -04:00
self.fold_mode = "github"
2020-08-03 17:38:07 +01:00
self.colors_enabled = True
2020-11-23 21:00:22 +00:00
elif ci_provider == CIProvider.travis_ci:
2021-05-03 11:45:43 -04:00
self.fold_mode = "travis"
2020-08-03 17:38:07 +01:00
self.colors_enabled = True
2020-11-23 21:00:22 +00:00
elif ci_provider == CIProvider.appveyor:
2021-05-03 11:45:43 -04:00
self.fold_mode = "disabled"
2020-08-03 17:38:07 +01:00
self.colors_enabled = True
else:
2021-05-03 11:45:43 -04:00
self.fold_mode = "disabled"
2020-11-01 16:57:07 +00:00
self.colors_enabled = file_supports_color(sys.stdout)
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
def build_end(self) -> 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
2020-11-01 11:34:02 +00:00
print()
2021-04-30 17:56:34 -04:00
print(
2021-05-03 11:45:43 -04:00
f"{c.green}{s.done} {c.end}{self.active_build_identifier} finished in {duration:.2f}s"
2021-04-30 17:56:34 -04: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
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
2021-01-05 13:50:21 +00:00
def step_end_with_error(self, error: Union[BaseException, str]) -> None:
2020-11-01 21:36:44 +00:00
self.step_end(success=False)
self.error(error)
def warning(self, message: str) -> None:
2021-05-03 11:45:43 -04:00
if self.fold_mode == "github":
print(f"::warning::{message}\n", file=sys.stderr)
else:
c = self.colors
2021-05-03 11:45:43 -04:00
print(f"{c.yellow}Warning{c.end}: {message}\n", file=sys.stderr)
2021-01-05 19:02:15 +00:00
def error(self, error: Union[BaseException, str]) -> None:
2021-05-03 11:45:43 -04:00
if self.fold_mode == "github":
print(f"::error::{error}\n", file=sys.stderr)
2020-11-01 21:36:44 +00:00
else:
c = self.colors
2021-05-03 11:45:43 -04:00
print(f"{c.bright_red}Error{c.end}: {error}\n", file=sys.stderr)
2021-01-05 19:02:15 +00:00
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]
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]
python_version = python_identifier[2:]
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"
2020-08-03 17:38:07 +01:00
else:
2021-05-03 11:45:43 -04:00
raise Exception("unknown python")
2020-08-03 17:38:07 +01:00
build_description += f" {python_version[0]}.{python_version[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:
2021-05-03 11:45:43 -04:00
raise Exception("unknown platform") from e
2020-08-03 17:38:07 +01:00
return build_description
2020-11-13 18:37:30 +00:00
class Colors:
def __init__(self, *, enabled: bool) -> None:
2021-05-03 11:45:43 -04:00
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 ""
2020-08-03 17:38:07 +01:00
2021-05-03 11:45:43 -04:00
self.bg_grey = "\033[48;5;235m" if enabled else ""
2020-08-03 17:38:07 +01:00
2021-05-03 11:45:43 -04:00
self.bold = "\033[1m" if enabled else ""
self.faint = "\033[2m" if enabled else ""
2020-08-03 17:38:07 +01:00
2021-05-03 11:45:43 -04:00
self.end = "\033[0m" if enabled else ""
2020-08-03 17:38:07 +01:00
2020-11-13 18:37:30 +00:00
class Symbols:
def __init__(self, *, unicode: bool) -> None:
2021-05-03 11:45:43 -04:00
self.done = "✓" if unicode else "done"
self.error = "✕" if unicode else "failed"
2020-11-01 16:57:07 +00:00
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()