From 2683da0318990f67aa5d749abbeb9f6ca2be10d1 Mon Sep 17 00:00:00 2001 From: abc12133 Date: Wed, 29 Nov 2023 15:23:52 +0800 Subject: [PATCH] compile without rich Signed-off-by: abc12133 --- project_build/builder.py | 22 +++- project_build/builder/common/logger.py | 167 +++++++++++++++---------- 2 files changed, 116 insertions(+), 73 deletions(-) diff --git a/project_build/builder.py b/project_build/builder.py index a97a51ef..79e0f094 100755 --- a/project_build/builder.py +++ b/project_build/builder.py @@ -17,7 +17,12 @@ import sys import argparse import os -from rich.console import Console + +RICH=True +try: + from rich.console import Console +except ImportError: + RICH=False from builder.commands.build import Builder from builder.commands.format import Formatter @@ -155,7 +160,10 @@ class FtBuilder: def prepare(self): if self.args is None: - Console().print("FtBuiler Inner Error: args is None", style="bold red") + if RICH: + Console().print("FtBuiler Inner Error: args is None", style="bold red") + else: + print("FtBuiler Inner Error: args is None") self._setup_logger() @@ -202,13 +210,17 @@ def main() -> int: builder = FtBuilder() builder.parse_args() - console = Console() + if RICH: + console = Console() try: builder.prepare() return 0 if builder.do_subcommand() else 1 - except: - console.print_exception(show_locals=True) + except Exception as error: + if RICH: + console.print_exception(show_locals=True) + else: + print(error) return 1 if __name__ == "__main__": diff --git a/project_build/builder/common/logger.py b/project_build/builder/common/logger.py index 771089c4..f9659063 100755 --- a/project_build/builder/common/logger.py +++ b/project_build/builder/common/logger.py @@ -14,10 +14,15 @@ import sys import logging -from rich.style import Style -from rich.text import Text -from rich.highlighter import Highlighter -from rich.logging import RichHandler + +RICH=True +try: + from rich.style import Style + from rich.text import Text + from rich.highlighter import Highlighter + from rich.logging import RichHandler +except ImportError: + RICH=False def _combine_regex(*regexes: str) -> str: """Combine a number of regexes in to a single regex. @@ -26,54 +31,55 @@ def _combine_regex(*regexes: str) -> str: """ return "|".join(regexes) -class AdvancedHighlighter(Highlighter): - """Highlights the text typically produced from ``__repr__`` methods.""" - - base_style = "repr." - re_highlights = [ - r"(?P<)(?P[-\w.:|]*)(?P[\w\W]*)(?P>)", - # r'(?P\B-[\w_]{1,50})=(?P"?[\w_]+"?)?', - r"(?P[][{}()])", - r'(?P\B-{1,2}[\w_-]{1,50})=(?P"?[\w_+=-]+"?)?', - _combine_regex( - r"(?P[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})", - r"(?P([A-Fa-f0-9]{1,4}::?){7}[A-Fa-f0-9]{1,4})", - r"(?P(?:[0-9A-Fa-f]{1,2}-){7}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{1,2}:){7}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{4}\.){3}[0-9A-Fa-f]{4})", - r"(?P(?:[0-9A-Fa-f]{1,2}-){5}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{1,2}:){5}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})", - r"(?P[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})", - # r"(?P[\w.]*?)\(", - r"\b(?PTrue)\b|\b(?PFalse)\b|\b(?PNone)\b", - r"(?P\.\.\.)", - r"(?P(?(?((\.\.)?|\b[-\w_]+)(/[-\w._+]+)*\/)(?P[-\w._+]*)?", - r"(?b?'''.*?(?(file|https|http|ws|wss)://[-0-9a-zA-Z$_+!`(),.?/;:&=%#]*)", - ), - ] - - def highlight(self, text: Text) -> None: - highlight_words = text.highlight_words - highlight_regex = text.highlight_regex - for re_highlight in self.re_highlights: - highlight_regex(re_highlight, style_prefix=self.base_style) - - # Highlight -Dxxx, -fxxx, -Wxxx - highlight_regex(r"\B(?P-(D|f|W)[\w+_-]{1,50})", style_prefix=self.base_style) - # Highlight "error" / "failed" / "invalid" - highlight_words(["error:", "warning:", "ERROR", "invalid"], style=Style(color="bright_red", bold=True), case_sensitive=True) - highlight_words(["failed:", "failed"], style=Style(color="bright_red", bold=True), case_sensitive=False) - # Highlight "success" / "done" - highlight_words(["successfully", "success", "done."], style=Style(color="bright_green", bold=True), case_sensitive=False) - # Highlight CC / CXX / ASM / AR / SOLINK / LINK / STAMP / COPY - highlight_words([" CC ", " CXX ", " ASM ", " AR ", " SOLINK ", " LINK ", " STAMP ", " COPY "], style=Style(color="yellow", bold=True), case_sensitive=True) - # Highlight GN print - highlight_words(["[GN INFO]"], style=Style(color="blue", bold=True), case_sensitive=False) - highlight_words(["[GN DEBUG]"], style=Style(color="dark_blue", bold=True), case_sensitive=False) - highlight_words(["[GN ERROR]", "[GN WARNING]"], style=Style(color="red", bold=True), case_sensitive=False) - # Highlight ^~~~~~~ & ^------ - highlight_regex(r"\B(\^~+)", style=Style(color="red", bold=True)) - highlight_regex(r"\B(\^-+)", style=Style(color="red", bold=True)) +if RICH: + class AdvancedHighlighter(Highlighter): + """Highlights the text typically produced from ``__repr__`` methods.""" + + base_style = "repr." + re_highlights = [ + r"(?P<)(?P[-\w.:|]*)(?P[\w\W]*)(?P>)", + # r'(?P\B-[\w_]{1,50})=(?P"?[\w_]+"?)?', + r"(?P[][{}()])", + r'(?P\B-{1,2}[\w_-]{1,50})=(?P"?[\w_+=-]+"?)?', + _combine_regex( + r"(?P[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})", + r"(?P([A-Fa-f0-9]{1,4}::?){7}[A-Fa-f0-9]{1,4})", + r"(?P(?:[0-9A-Fa-f]{1,2}-){7}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{1,2}:){7}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{4}\.){3}[0-9A-Fa-f]{4})", + r"(?P(?:[0-9A-Fa-f]{1,2}-){5}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{1,2}:){5}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})", + r"(?P[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})", + # r"(?P[\w.]*?)\(", + r"\b(?PTrue)\b|\b(?PFalse)\b|\b(?PNone)\b", + r"(?P\.\.\.)", + r"(?P(?(?((\.\.)?|\b[-\w_]+)(/[-\w._+]+)*\/)(?P[-\w._+]*)?", + r"(?b?'''.*?(?(file|https|http|ws|wss)://[-0-9a-zA-Z$_+!`(),.?/;:&=%#]*)", + ), + ] + + def highlight(self, text: Text) -> None: + highlight_words = text.highlight_words + highlight_regex = text.highlight_regex + for re_highlight in self.re_highlights: + highlight_regex(re_highlight, style_prefix=self.base_style) + + # Highlight -Dxxx, -fxxx, -Wxxx + highlight_regex(r"\B(?P-(D|f|W)[\w+_-]{1,50})", style_prefix=self.base_style) + # Highlight "error" / "failed" / "invalid" + highlight_words(["error:", "warning:", "ERROR", "invalid"], style=Style(color="bright_red", bold=True), case_sensitive=True) + highlight_words(["failed:", "failed"], style=Style(color="bright_red", bold=True), case_sensitive=False) + # Highlight "success" / "done" + highlight_words(["successfully", "success", "done."], style=Style(color="bright_green", bold=True), case_sensitive=False) + # Highlight CC / CXX / ASM / AR / SOLINK / LINK / STAMP / COPY + highlight_words([" CC ", " CXX ", " ASM ", " AR ", " SOLINK ", " LINK ", " STAMP ", " COPY "], style=Style(color="yellow", bold=True), case_sensitive=True) + # Highlight GN print + highlight_words(["[GN INFO]"], style=Style(color="blue", bold=True), case_sensitive=False) + highlight_words(["[GN DEBUG]"], style=Style(color="dark_blue", bold=True), case_sensitive=False) + highlight_words(["[GN ERROR]", "[GN WARNING]"], style=Style(color="red", bold=True), case_sensitive=False) + # Highlight ^~~~~~~ & ^------ + highlight_regex(r"\B(\^~+)", style=Style(color="red", bold=True)) + highlight_regex(r"\B(\^-+)", style=Style(color="red", bold=True)) class LoggerManager: """ @@ -90,21 +96,22 @@ class LoggerManager: pass def setup_level(self, log_level: str): - log_level = log_level.upper() - - try: - handler = RichHandler(rich_tracebacks=True,highlighter=AdvancedHighlighter()) - logging.basicConfig( - level=log_level, - format="%(message)s", - datefmt="[%X]", - handlers=[handler] - ) - except: - self.get_logger().error("Invalid log level: '%s'", log_level) - sys.exit(1) - - self.log_level = log_level + if RICH: + log_level = log_level.upper() + + try: + handler = RichHandler(rich_tracebacks=True,highlighter=AdvancedHighlighter()) + logging.basicConfig( + level=log_level, + format="%(message)s", + datefmt="[%X]", + handlers=[handler] + ) + except: + self.get_logger().error("Invalid log level: '%s'", log_level) + sys.exit(1) + + self.log_level = log_level def add_file_logger(self, out_path, expire_time=2) -> None: # self.file_logger_handler = loguru_logger.add( @@ -119,4 +126,28 @@ class LoggerManager: def get_logger(self) -> logging.Logger: return logging.getLogger("rich") -logger = LoggerManager().get_logger() +if RICH: + logger = LoggerManager().get_logger() +else: + class logger: + _instance = None + + def __new__(cls): + if cls._instance is None: + cls._instance = super().__new__(cls) + return cls._instance + + def __init__(self): + pass + + def debug(args): + print(args) + + def warning(args): + print(args) + + def info(args): + print(args) + + def error(args): + print(args) -- Gitee