diff options
Diffstat (limited to 'methods.py')
-rw-r--r-- | methods.py | 148 |
1 files changed, 82 insertions, 66 deletions
diff --git a/methods.py b/methods.py index 01b127ea30..0c29632f10 100644 --- a/methods.py +++ b/methods.py @@ -5,6 +5,7 @@ import glob import subprocess from collections import OrderedDict from collections.abc import Mapping +from enum import Enum from typing import Iterator from pathlib import Path from os.path import normpath, basename @@ -15,6 +16,10 @@ base_folder_only = os.path.basename(os.path.normpath(base_folder_path)) # Listing all the folders we have converted # for SCU in scu_builders.py _scu_folders = set() +# Colors are disabled in non-TTY environments such as pipes. This means +# that if output is redirected to a file, it won't contain color codes. +# Colors are always enabled on continuous integration. +_colorize = bool(sys.stdout.isatty() or os.environ.get("CI")) def set_scu_folders(scu_folders): @@ -22,13 +27,57 @@ def set_scu_folders(scu_folders): _scu_folders = scu_folders +class ANSI(Enum): + """ + Enum class for adding ansi colorcodes directly into strings. + Automatically converts values to strings representing their + internal value, or an empty string in a non-colorized scope. + """ + + RESET = "\x1b[0m" + + BOLD = "\x1b[1m" + ITALIC = "\x1b[3m" + UNDERLINE = "\x1b[4m" + STRIKETHROUGH = "\x1b[9m" + REGULAR = "\x1b[22;23;24;29m" + + BLACK = "\x1b[30m" + RED = "\x1b[31m" + GREEN = "\x1b[32m" + YELLOW = "\x1b[33m" + BLUE = "\x1b[34m" + MAGENTA = "\x1b[35m" + CYAN = "\x1b[36m" + WHITE = "\x1b[37m" + + PURPLE = "\x1b[38;5;93m" + PINK = "\x1b[38;5;206m" + ORANGE = "\x1b[38;5;214m" + GRAY = "\x1b[38;5;244m" + + def __str__(self) -> str: + global _colorize + return str(self.value) if _colorize else "" + + +def print_warning(*values: object) -> None: + """Prints a warning message with formatting.""" + print(f"{ANSI.YELLOW}{ANSI.BOLD}WARNING:{ANSI.REGULAR}", *values, ANSI.RESET, file=sys.stderr) + + +def print_error(*values: object) -> None: + """Prints an error message with formatting.""" + print(f"{ANSI.RED}{ANSI.BOLD}ERROR:{ANSI.REGULAR}", *values, ANSI.RESET, file=sys.stderr) + + def add_source_files_orig(self, sources, files, allow_gen=False): # Convert string to list of absolute paths (including expanding wildcard) if isinstance(files, (str, bytes)): # Keep SCons project-absolute path as they are (no wildcard support) if files.startswith("#"): if "*" in files: - print("ERROR: Wildcards can't be expanded in SCons project-absolute path: '{}'".format(files)) + print_error("Wildcards can't be expanded in SCons project-absolute path: '{}'".format(files)) return files = [files] else: @@ -44,7 +93,7 @@ def add_source_files_orig(self, sources, files, allow_gen=False): for path in files: obj = self.Object(path) if obj in sources: - print('WARNING: Object "{}" already included in environment sources.'.format(obj)) + print_warning('Object "{}" already included in environment sources.'.format(obj)) continue sources.append(obj) @@ -517,7 +566,7 @@ def module_check_dependencies(self, module): missing_deps.append(dep) if missing_deps != []: - print( + print_warning( "Disabling '{}' module as the following dependencies are not satisfied: {}".format( module, ", ".join(missing_deps) ) @@ -576,9 +625,7 @@ def use_windows_spawn_fix(self, platform=None): _, err = proc.communicate() rv = proc.wait() if rv: - print("=====") - print(err) - print("=====") + print_error(err) return rv def mySpawn(sh, escape, cmd, args, env): @@ -601,65 +648,34 @@ def use_windows_spawn_fix(self, platform=None): self["SPAWN"] = mySpawn -def no_verbose(sys, env): - colors = {} - - # Colors are disabled in non-TTY environments such as pipes. This means - # that if output is redirected to a file, it will not contain color codes - if sys.stdout.isatty(): - colors["blue"] = "\033[0;94m" - colors["bold_blue"] = "\033[1;94m" - colors["reset"] = "\033[0m" - else: - colors["blue"] = "" - colors["bold_blue"] = "" - colors["reset"] = "" +def no_verbose(env): + colors = [ANSI.BLUE, ANSI.BOLD, ANSI.REGULAR, ANSI.RESET] # There is a space before "..." to ensure that source file names can be # Ctrl + clicked in the VS Code terminal. - compile_source_message = "{}Compiling {}$SOURCE{} ...{}".format( - colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"] - ) - java_compile_source_message = "{}Compiling {}$SOURCE{} ...{}".format( - colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"] - ) - compile_shared_source_message = "{}Compiling shared {}$SOURCE{} ...{}".format( - colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"] - ) - link_program_message = "{}Linking Program {}$TARGET{} ...{}".format( - colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"] - ) - link_library_message = "{}Linking Static Library {}$TARGET{} ...{}".format( - colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"] - ) - ranlib_library_message = "{}Ranlib Library {}$TARGET{} ...{}".format( - colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"] - ) - link_shared_library_message = "{}Linking Shared Library {}$TARGET{} ...{}".format( - colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"] - ) - java_library_message = "{}Creating Java Archive {}$TARGET{} ...{}".format( - colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"] - ) - compiled_resource_message = "{}Creating Compiled Resource {}$TARGET{} ...{}".format( - colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"] - ) - generated_file_message = "{}Generating {}$TARGET{} ...{}".format( - colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"] - ) - - env.Append(CXXCOMSTR=[compile_source_message]) - env.Append(CCCOMSTR=[compile_source_message]) - env.Append(SHCCCOMSTR=[compile_shared_source_message]) - env.Append(SHCXXCOMSTR=[compile_shared_source_message]) - env.Append(ARCOMSTR=[link_library_message]) - env.Append(RANLIBCOMSTR=[ranlib_library_message]) - env.Append(SHLINKCOMSTR=[link_shared_library_message]) - env.Append(LINKCOMSTR=[link_program_message]) - env.Append(JARCOMSTR=[java_library_message]) - env.Append(JAVACCOMSTR=[java_compile_source_message]) - env.Append(RCCOMSTR=[compiled_resource_message]) - env.Append(GENCOMSTR=[generated_file_message]) + compile_source_message = "{}Compiling {}$SOURCE{} ...{}".format(*colors) + java_compile_source_message = "{}Compiling {}$SOURCE{} ...{}".format(*colors) + compile_shared_source_message = "{}Compiling shared {}$SOURCE{} ...{}".format(*colors) + link_program_message = "{}Linking Program {}$TARGET{} ...{}".format(*colors) + link_library_message = "{}Linking Static Library {}$TARGET{} ...{}".format(*colors) + ranlib_library_message = "{}Ranlib Library {}$TARGET{} ...{}".format(*colors) + link_shared_library_message = "{}Linking Shared Library {}$TARGET{} ...{}".format(*colors) + java_library_message = "{}Creating Java Archive {}$TARGET{} ...{}".format(*colors) + compiled_resource_message = "{}Creating Compiled Resource {}$TARGET{} ...{}".format(*colors) + generated_file_message = "{}Generating {}$TARGET{} ...{}".format(*colors) + + env["CXXCOMSTR"] = compile_source_message + env["CCCOMSTR"] = compile_source_message + env["SHCCCOMSTR"] = compile_shared_source_message + env["SHCXXCOMSTR"] = compile_shared_source_message + env["ARCOMSTR"] = link_library_message + env["RANLIBCOMSTR"] = ranlib_library_message + env["SHLINKCOMSTR"] = link_shared_library_message + env["LINKCOMSTR"] = link_program_message + env["JARCOMSTR"] = java_library_message + env["JAVACCOMSTR"] = java_compile_source_message + env["RCCOMSTR"] = compiled_resource_message + env["GENCOMSTR"] = generated_file_message def detect_visual_c_compiler_version(tools_env): @@ -790,7 +806,7 @@ def generate_cpp_hint_file(filename): with open(filename, "w", encoding="utf-8", newline="\n") as fd: fd.write("#define GDCLASS(m_class, m_inherits)\n") except OSError: - print("Could not write cpp.hint file.") + print_warning("Could not write cpp.hint file.") def glob_recursive(pattern, node="."): @@ -881,7 +897,7 @@ def detect_darwin_sdk_path(platform, env): if sdk_path: env[var_name] = sdk_path except (subprocess.CalledProcessError, OSError): - print("Failed to find SDK path while running xcrun --sdk {} --show-sdk-path.".format(sdk_name)) + print_error("Failed to find SDK path while running xcrun --sdk {} --show-sdk-path.".format(sdk_name)) raise @@ -891,7 +907,7 @@ def is_vanilla_clang(env): try: version = subprocess.check_output([env.subst(env["CXX"]), "--version"]).strip().decode("utf-8") except (subprocess.CalledProcessError, OSError): - print("Couldn't parse CXX environment variable to infer compiler version.") + print_warning("Couldn't parse CXX environment variable to infer compiler version.") return False return not version.startswith("Apple") @@ -928,7 +944,7 @@ def get_compiler_version(env): .decode("utf-8") ) except (subprocess.CalledProcessError, OSError): - print("Couldn't parse CXX environment variable to infer compiler version.") + print_warning("Couldn't parse CXX environment variable to infer compiler version.") return ret else: # TODO: Implement for MSVC |