Add some colors in the terminal.

This commit is contained in:
Matthieu Gautier 2020-02-25 15:48:20 +01:00
parent f504cf54e1
commit 40b5013279
3 changed files with 39 additions and 23 deletions

View File

@ -4,7 +4,7 @@ from collections import OrderedDict
from .buildenv import *
from .platforms import PlatformInfo
from .utils import remove_duplicates, StopBuild
from .utils import remove_duplicates, StopBuild, colorize
from .dependencies import Dependency
from .packages import PACKAGE_NAME_MAPPERS
from ._global import (
@ -21,7 +21,7 @@ class Builder:
target_platform = option('target_platform')
platform = PlatformInfo.get_platform(target_platform, self._targets)
if neutralEnv('distname') not in platform.compatible_hosts:
print(('ERROR: The target platform {} cannot be build on host {}.\n'
print((colorize('ERROR')+': The target platform {} cannot be build on host {}.\n'
'Select another target platform or change your host system.'
).format(platform.name, neutralEnv('distname')))
self.targetDefs = platform.add_targets(option('target'), self._targets)
@ -97,7 +97,7 @@ class Builder:
def prepare_sources(self):
if option('skip_source_prepare'):
print("SKIP")
print(colorize("SKIP"))
return
sourceDefs = remove_duplicates(tDef for tDef in target_steps() if tDef[0]=='source')
@ -149,7 +149,7 @@ class Builder:
packages_to_have = remove_duplicates(packages_to_have)
if option('assume_packages_installed'):
print("SKIP, Assume package installed")
print(colorize("SKIP") + ", Assume package installed")
return
distname = neutralEnv('distname')
@ -170,23 +170,23 @@ class Builder:
try:
subprocess.check_call(command, shell=True)
except subprocess.CalledProcessError:
print("NEEDED")
print(colorize("NEEDED"))
packages_to_install.append(package)
else:
print("SKIP")
print(colorize("SKIP"))
if packages_to_install:
command = package_installer.format(" ".join(packages_to_install))
print(command)
subprocess.check_call(command, shell=True)
else:
print("SKIP, No package to install.")
print(colorize("SKIP")+ ", No package to install.")
def run(self):
try:
print("[INSTALL PACKAGES]")
if option('dont_install_packages'):
print("SKIP")
print(colorize("SKIP"))
else:
self.install_packages()
self.finalize_target_steps()
@ -203,7 +203,7 @@ class Builder:
for platform in PlatformInfo.all_running_platforms.values():
platform.clean_intermediate_directories()
else:
print("SKIP")
print(colorize("SKIP"))
except StopBuild as e:
print(e)
sys.exit("Stopping build due to errors")

View File

@ -3,7 +3,7 @@ import os
import shutil
import time
from kiwixbuild.utils import pj, Context, SkipCommand, WarningMessage, extract_archive, Defaultdict, StopBuild, run_command
from kiwixbuild.utils import pj, Context, SkipCommand, WarningMessage, extract_archive, Defaultdict, StopBuild, run_command, colorize
from kiwixbuild.versions import main_project_versions, base_deps_versions
from kiwixbuild._global import neutralEnv, option
@ -76,14 +76,14 @@ class Source:
ret = function(*args, context=context)
context._finalise()
duration = time.time() - start_time
print("OK ({:.1f}s)".format(duration))
print(colorize("OK"), "({:.1f}s)".format(duration))
return ret
except WarningMessage as e:
print(e)
except SkipCommand:
print("SKIP")
except SkipCommand as e:
print(e)
except subprocess.CalledProcessError:
print("ERROR")
print(colorize("ERROR"))
try:
with open(log, 'r') as f:
print(f.read())
@ -91,7 +91,7 @@ class Source:
pass
raise StopBuild()
except:
print("ERROR")
print(colorize("ERROR"))
raise
@ -245,14 +245,14 @@ class Builder:
ret = function(*args, context=context)
context._finalise()
duration = time.time() - start_time
print("OK ({:.1f}s)".format(duration))
print(colorize("OK"), "({:.1f}s)".format(duration))
return ret
except SkipCommand:
print("SKIP")
except SkipCommand as e:
print(e)
except WarningMessage as e:
print(e)
except subprocess.CalledProcessError:
print("ERROR")
print(colorize("ERROR"))
try:
with open(log, 'r') as f:
print(f.read())
@ -260,7 +260,7 @@ class Builder:
pass
raise StopBuild()
except:
print("ERROR")
print(colorize("ERROR"))
raise
def build(self):

View File

@ -15,6 +15,16 @@ from kiwixbuild._global import neutralEnv, option
pj = os.path.join
COLORS = {
'OK': '\033[92m',
'WARNING': '\033[93m',
'NEEDED': '\033[93m',
'SKIP': '\033[34m',
'ERROR': '\033[91m',
'': '\033[0m',
}
REMOTE_PREFIX = 'http://download.kiwix.org/dev/'
@ -66,6 +76,12 @@ def get_sha256(path):
return sha256.hexdigest()
def colorize(text, color=None):
if color is None:
color = text
return "{}{}{}".format(COLORS[color], text, COLORS[''])
def print_progress(progress):
if option('show_progress'):
text = "{}\033[{}D".format(progress, len(progress))
@ -145,13 +161,13 @@ class BaseCommandResult(Exception):
class SkipCommand(BaseCommandResult):
def __str__(self):
if self.msg:
return "SKIP : {}".format(self.msg)
return "SKIP"
return colorize("SKIP") + " : {}".format(self.msg)
return colorize("SKIP")
class WarningMessage(BaseCommandResult):
def __str__(self):
return "WARNING : {}".format(self.msg)
return colorize("WARNING") + " : {}".format(self.msg)
class StopBuild(BaseCommandResult):