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

View File

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

View File

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