Skip to content

Commit

Permalink
main: multiple changes ...
Browse files Browse the repository at this point in the history
* strip acbs print-out colors when NO_COLOR environment variable is set
* print full build summary even if build failed
  • Loading branch information
liushuyu committed Dec 16, 2023
1 parent 6bec7f9 commit a855891
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
18 changes: 11 additions & 7 deletions acbs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import time
import traceback
import itertools
from pathlib import Path
from typing import List, Tuple

Expand All @@ -20,7 +21,7 @@
from acbs.find import check_package_groups, find_package
from acbs.parser import get_deps_graph, get_tree_by_name, arch, check_buildability
from acbs.pm import install_from_repo
from acbs.utils import (ACBSLogFormatter, full_line_banner, guess_subdir,
from acbs.utils import (ACBSLogFormatter, ACBSLogPlainFormatter, full_line_banner, guess_subdir,
has_stamp, invoke_autobuild, make_build_dir,
print_build_timings, print_package_names, write_checksums,
generate_checksums, is_spec_legacy, check_artifact)
Expand Down Expand Up @@ -80,7 +81,10 @@ def __install_logger(self, str_verbosity=logging.INFO,
logger.setLevel(0) # Set to lowest to bypass the initial filter
str_handler = logging.StreamHandler()
str_handler.setLevel(str_verbosity)
str_handler.setFormatter(ACBSLogFormatter())
if os.environ.get('NO_COLOR'):
str_handler.setFormatter(ACBSLogPlainFormatter())
else:
str_handler.setFormatter(ACBSLogFormatter())
logger.addHandler(str_handler)
log_file_handler = logging.handlers.RotatingFileHandler(
os.path.join(self.log_dir, 'acbs-build.log'), mode='a', maxBytes=2e5, backupCount=3)
Expand Down Expand Up @@ -125,7 +129,7 @@ def build(self) -> None:
except Exception as ex:
logging.exception(ex)
self.save_checkpoint(build_timings, packages)
print_build_timings(build_timings)
print_build_timings(build_timings, [])

def save_checkpoint(self, build_timings, packages):
logging.info('ACBS is trying to save your build status...')
Expand Down Expand Up @@ -208,7 +212,7 @@ def resolve_deps(self, packages, stage2: bool):

def build_sequential(self, build_timings, packages):
# build process
for task in packages:
for idx, task in enumerate(packages):
self.package_cursor += 1
logging.info(
f'Building {task.name} ({self.package_cursor}/{len(packages)})...')
Expand Down Expand Up @@ -249,16 +253,16 @@ def build_sequential(self, build_timings, packages):
logging.info('Installing dependencies from repository...')
install_from_repo(task.installables)
start = time.monotonic()
task_name = f'{task.name} ({task.bin_arch} @ {task.epoch + ":" if task.epoch else ""}{task.version}-{task.rel})'
try:
invoke_autobuild(task, build_dir, self.stage2)
check_artifact(task.name, build_dir)
except Exception:
# early printing of build summary before exploding
if build_timings:
print_build_timings(build_timings)
print_build_timings(build_timings, packages[idx:])
raise RuntimeError(
f'Error when building {task.name}.\nBuild folder: {build_dir}')
task_name = f'{task.name} ({task.bin_arch} @ {task.epoch + ":" if task.epoch else ""}{task.version}-{task.rel})'
f'Build directory of the failed package:\n\n{build_dir}')
build_timings.append((task_name, time.monotonic() - start))

def acbs_except_hdr(self, type_, value, tb):
Expand Down
36 changes: 33 additions & 3 deletions acbs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,27 @@ def format_column(data: Sequence[Tuple[str, ...]]) -> str:
return output


def print_build_timings(timings: List[Tuple[str, float]]):
def print_build_timings(timings: List[Tuple[str, float]], failed_packages: List[str]):
"""
Print the build statistics
:param timings: List of timing data
"""
formatted_timings: List[Tuple[str, str]] = []
print(full_line_banner('', '='))
for timing in timings:
formatted_timings.append((timing[0], human_time(timing[1])))
print(full_line_banner('Build Summary'))
print(format_column(formatted_timings))
print('\t\tACBS Build {}', 'Successful' if not failed_packages else 'Failed')
print(full_line_banner('', '='))
if failed_packages:
print("Failed package:")
print(failed_packages[0])
if timings:
print("Package(s) built:")
print(format_column(formatted_timings))
if len(failed_packages) > 2:
print("Package(s) not built due to previous build failure:")
print(failed_packages[1:])


def is_spec_legacy(spec: str) -> bool:
Expand Down Expand Up @@ -329,3 +339,23 @@ def format(self, record):
logging.INFO, logging.DEBUG):
record.msg = f'[{lvl_map[record.levelname]}]: \033[1m{record.msg}\033[0m'
return super(ACBSLogFormatter, self).format(record)


class ACBSLogPlainFormatter(logging.Formatter):
"""
ABBS-like format logger formatter class
... but with no color codes
"""

def format(self, record):
lvl_map = {
'WARNING': f'WARN',
'INFO': f'INFO',
'DEBUG': f'DEBUG',
'ERROR': f'ERROR',
'CRITICAL': f'CRIT'
}
if record.levelno in (logging.WARNING, logging.ERROR, logging.CRITICAL,
logging.INFO, logging.DEBUG):
record.msg = f'[{lvl_map[record.levelname]}]: {record.msg}'
return super(ACBSLogFormatter, self).format(record)

0 comments on commit a855891

Please sign in to comment.