2018-09-21 04:34:55 -04:00
|
|
|
from __future__ import print_function
|
2017-11-07 23:27:57 -05:00
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
_COLOR_CODES = {
|
2018-09-26 02:09:00 -04:00
|
|
|
"white": u'\033[0m',
|
|
|
|
"red": u'\033[31m',
|
|
|
|
"green": u'\033[32m',
|
|
|
|
"orange": u'\033[33m',
|
|
|
|
"blue": u'\033[34m',
|
|
|
|
"purple": u'\033[35m',
|
|
|
|
"W": u'\033[0m',
|
|
|
|
"R": u'\033[31m',
|
|
|
|
"G": u'\033[32m',
|
|
|
|
"O": u'\033[33m',
|
|
|
|
"B": u'\033[34m',
|
|
|
|
"P": u'\033[35m'
|
2017-11-07 23:27:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-21 04:34:55 -04:00
|
|
|
def console_log(data, color="white", end="\n"):
|
2017-11-07 23:27:57 -05:00
|
|
|
"""
|
|
|
|
log data to console.
|
|
|
|
(if not flush console log, Gitlab-CI won't update logs during job execution)
|
|
|
|
|
|
|
|
:param data: data content
|
|
|
|
:param color: color
|
|
|
|
"""
|
|
|
|
if color not in _COLOR_CODES:
|
|
|
|
color = "white"
|
|
|
|
color_codes = _COLOR_CODES[color]
|
2018-12-04 07:46:48 -05:00
|
|
|
if isinstance(data, type(b'')):
|
2018-09-26 02:09:00 -04:00
|
|
|
data = data.decode('utf-8', 'replace')
|
2018-09-21 04:34:55 -04:00
|
|
|
print(color_codes + data, end=end)
|
2017-11-07 23:27:57 -05:00
|
|
|
if color not in ["white", "W"]:
|
|
|
|
# reset color to white for later logs
|
2018-09-26 02:09:00 -04:00
|
|
|
print(_COLOR_CODES["white"] + u"\r")
|
2018-09-21 04:34:55 -04:00
|
|
|
sys.stdout.flush()
|
2018-12-14 11:37:43 -05:00
|
|
|
|
|
|
|
|
2019-11-22 02:49:40 -05:00
|
|
|
__LOADED_MODULES = dict()
|
|
|
|
# we should only load one module once.
|
|
|
|
# if we load one module twice,
|
|
|
|
# python will regard the same object loaded in the first time and second time as different objects.
|
|
|
|
# it will lead to strange errors like `isinstance(object, type_of_this_object)` return False
|
|
|
|
|
|
|
|
|
2018-12-14 11:37:43 -05:00
|
|
|
def load_source(name, path):
|
|
|
|
try:
|
2019-11-22 02:49:40 -05:00
|
|
|
return __LOADED_MODULES[name]
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
from importlib.machinery import SourceFileLoader
|
|
|
|
ret = SourceFileLoader(name, path).load_module()
|
|
|
|
except ImportError:
|
|
|
|
# importlib.machinery doesn't exists in Python 2 so we will use imp (deprecated in Python 3)
|
|
|
|
import imp
|
|
|
|
ret = imp.load_source(name, path)
|
|
|
|
__LOADED_MODULES[name] = ret
|
|
|
|
return ret
|