diff --git a/pythonforandroid/logger.py b/pythonforandroid/logger.py index 009b32e86f..b0b7cbea11 100644 --- a/pythonforandroid/logger.py +++ b/pythonforandroid/logger.py @@ -61,27 +61,35 @@ def format(self, record): class colorama_shim(object): - def __init__(self): + def __init__(self, real): self._dict = defaultdict(str) + self._real = real + self._enabled = False def __getattr__(self, key): - return self._dict[key] + return getattr(self._real, key) if self._enabled else self._dict[key] -Null_Style = Null_Fore = colorama_shim() + def enable(self, enable): + self._enabled = enable -if stdout.isatty(): - Out_Style = Colo_Style - Out_Fore = Colo_Fore -else: - Out_Style = Null_Style - Out_Fore = Null_Fore +Out_Style = colorama_shim(Colo_Style) +Out_Fore = colorama_shim(Colo_Fore) +Err_Style = colorama_shim(Colo_Style) +Err_Fore = colorama_shim(Colo_Fore) -if stderr.isatty(): - Err_Style = Colo_Style - Err_Fore = Colo_Fore -else: - Err_Style = Null_Style - Err_Fore = Null_Fore + +def setup_color(color): + enable_out = (False if color == 'never' else + True if color == 'always' else + stdout.isatty()) + Out_Style.enable(enable_out) + Out_Fore.enable(enable_out) + + enable_err = (False if color == 'never' else + True if color == 'always' else + stderr.isatty()) + Err_Style.enable(enable_err) + Err_Fore.enable(enable_err) def info_main(*args): diff --git a/pythonforandroid/toolchain.py b/pythonforandroid/toolchain.py index 8f8579fca9..028a92a8e8 100755 --- a/pythonforandroid/toolchain.py +++ b/pythonforandroid/toolchain.py @@ -28,10 +28,9 @@ CompiledComponentsPythonRecipe, BootstrapNDKRecipe, NDKRecipe) from pythonforandroid.archs import (ArchARM, ArchARMv7_a, Archx86) -from pythonforandroid.logger import (logger, info, warning, debug, +from pythonforandroid.logger import (logger, info, warning, setup_color, Out_Style, Out_Fore, Err_Style, Err_Fore, - info_notify, info_main, shprint, - Null_Fore, Null_Style) + info_notify, info_main, shprint) from pythonforandroid.util import current_directory, ensure_dir from pythonforandroid.bootstrap import Bootstrap from pythonforandroid.distribution import Distribution, pretty_log_dists @@ -43,17 +42,6 @@ sys.path.insert(0, join(toolchain_dir, "tools", "external")) -info(''.join( - [Err_Style.BRIGHT, Err_Fore.RED, - 'This python-for-android revamp is an experimental alpha release!', - Err_Style.RESET_ALL])) -info(''.join( - [Err_Fore.RED, - ('It should work (mostly), but you may experience ' - 'missing features or bugs.'), - Err_Style.RESET_ALL])) - - def add_boolean_option(parser, names, no_names=None, default=True, dest=None, description=None): group = parser.add_argument_group(description=description) @@ -204,6 +192,9 @@ def __init__(self): parser.add_argument( '--debug', dest='debug', action='store_true', help='Display debug output and all build info') + parser.add_argument( + '--color', dest='color', choices=['always', 'never', 'auto'], + help='Enable or disable color output (default enabled on tty)') parser.add_argument( '--sdk-dir', '--sdk_dir', dest='sdk_dir', default='', help='The filepath where the Android SDK is installed') @@ -282,6 +273,18 @@ def __init__(self): args, unknown = parser.parse_known_args(sys.argv[1:]) self.dist_args = args + setup_color(args.color) + + info(''.join( + [Err_Style.BRIGHT, Err_Fore.RED, + 'This python-for-android revamp is an experimental alpha release!', + Err_Style.RESET_ALL])) + info(''.join( + [Err_Fore.RED, + ('It should work (mostly), but you may experience ' + 'missing features or bugs.'), + Err_Style.RESET_ALL])) + # strip version from requirements, and put them in environ requirements = [] for requirement in split_argument_list(args.requirements): @@ -357,19 +360,8 @@ def recipes(self, args): "--compact", action="store_true", default=False, help="Produce a compact list suitable for scripting") - add_boolean_option( - parser, ["color"], - default=True, - description='Whether the output should be colored:') - args = parser.parse_args(args) - Fore = Out_Fore - Style = Out_Style - if not args.color: - Fore = Null_Fore - Style = Null_Style - ctx = self.ctx if args.compact: print(" ".join(set(Recipe.list_recipes(ctx)))) @@ -380,18 +372,18 @@ def recipes(self, args): print('{Fore.BLUE}{Style.BRIGHT}{recipe.name:<12} ' '{Style.RESET_ALL}{Fore.LIGHTBLUE_EX}' '{version:<8}{Style.RESET_ALL}'.format( - recipe=recipe, Fore=Fore, Style=Style, + recipe=recipe, Fore=Out_Fore, Style=Out_Style, version=version)) print(' {Fore.GREEN}depends: {recipe.depends}' - '{Fore.RESET}'.format(recipe=recipe, Fore=Fore)) + '{Fore.RESET}'.format(recipe=recipe, Fore=Out_Fore)) if recipe.conflicts: print(' {Fore.RED}conflicts: {recipe.conflicts}' '{Fore.RESET}' - .format(recipe=recipe, Fore=Fore)) + .format(recipe=recipe, Fore=Out_Fore)) if recipe.opt_depends: print(' {Fore.YELLOW}optional depends: ' '{recipe.opt_depends}{Fore.RESET}' - .format(recipe=recipe, Fore=Fore)) + .format(recipe=recipe, Fore=Out_Fore)) def bootstraps(self, args): '''List all the bootstraps available to build with.'''