Files
cibuildwheel/cibuildwheel/ci.py
T

77 lines
2.0 KiB
Python
Raw Normal View History

2025-01-27 20:35:56 +01:00
import os
import re
from enum import Enum
2026-03-24 09:20:41 -04:00
from cibuildwheel.util.helpers import strtobool
2025-01-27 20:35:56 +01:00
2025-07-15 20:45:53 -07:00
ANSI_CODE_REGEX = re.compile(r"(\033\[[0-9;]*m)")
2025-01-27 20:35:56 +01:00
class CIProvider(Enum):
2025-05-11 10:16:54 +01:00
# official support
2025-01-27 20:35:56 +01:00
travis_ci = "travis"
circle_ci = "circle_ci"
azure_pipelines = "azure_pipelines"
github_actions = "github_actions"
gitlab = "gitlab"
2025-05-11 10:16:54 +01:00
# unofficial support
appveyor = "appveyor"
2025-01-27 20:35:56 +01:00
other = "other"
def detect_ci_provider() -> CIProvider | None:
if "TRAVIS" in os.environ:
return CIProvider.travis_ci
elif "APPVEYOR" in os.environ:
return CIProvider.appveyor
elif "CIRCLECI" in os.environ:
return CIProvider.circle_ci
elif "AZURE_HTTP_USER_AGENT" in os.environ:
return CIProvider.azure_pipelines
elif "GITHUB_ACTIONS" in os.environ:
return CIProvider.github_actions
elif "GITLAB_CI" in os.environ:
return CIProvider.gitlab
elif strtobool(os.environ.get("CI", "false")):
return CIProvider.other
else:
return None
def fix_ansi_codes_for_github_actions(text: str) -> str:
"""
Github Actions forgets the current ANSI style on every new line. This
function repeats the current ANSI style on every new line.
"""
2025-07-15 20:45:53 -07:00
2025-01-27 20:35:56 +01:00
ansi_codes: list[str] = []
output = ""
for line in text.splitlines(keepends=True):
# add the current ANSI codes to the beginning of the line
output += "".join(ansi_codes) + line
# split the line at each ANSI code
2025-07-15 20:45:53 -07:00
parts = ANSI_CODE_REGEX.split(line)
2025-01-27 20:35:56 +01:00
# if there are any ANSI codes, save them
if len(parts) > 1:
# iterate over the ANSI codes in this line
for code in parts[1::2]:
if code == "\033[0m":
# reset the list of ANSI codes when the clear code is found
ansi_codes = []
else:
ansi_codes.append(code)
return output
2025-07-15 20:45:53 -07:00
def filter_ansi_codes(text: str, /) -> str:
"""
Remove ANSI codes from text.
"""
return ANSI_CODE_REGEX.sub("", text)