[elbe-devel] [PATCH v2 1/6] commands: check-build: Create base for checkers
Bastian Germann
bage at linutronix.de
Tue Aug 4 17:30:08 CEST 2020
Am 03.08.20 um 18:40 schrieb Olivier Dion:
> Checkers for a build should inherit from 'CheckBase' and register
> themself with 'CheckBase.register(tag)' where 'tag' is the name of the
> command on the command line. As a special case, the command 'all'
> will run all checkers. Checkers should implement the 'run' method to
> do their testing of the build. The 'run' method should return 0 for
> success.
>
> Checkers can assume that their working directory is set to the build
> directory before entering 'run'.
>
> Any uncatch exception will result in a failure. Calling
Typo: uncatched
> 'self.fail(reason)', returning none 0 from 'run' or setting 'self.ret'
> to none 0 will also result in a failure.
2x: none or 0? or both?
>
> Signed-off-by: Olivier Dion <dion at linutronix.de>
With the suggested fixes:
Reviewed-by: Bastian Germann <bage at linutronix.de>
> ---
> elbepack/commands/check-build.py | 97 ++++++++++++++++++++++++++++++++
> 1 file changed, 97 insertions(+)
> create mode 100644 elbepack/commands/check-build.py
>
> diff --git a/elbepack/commands/check-build.py b/elbepack/commands/check-build.py
> new file mode 100644
> index 00000000..2db8faf6
> --- /dev/null
> +++ b/elbepack/commands/check-build.py
> @@ -0,0 +1,97 @@
> +# ELBE - Debian Based Embedded Rootfilesystem Builder
> +# Copyright (c) 2020 Olivier Dion <dion at linutronix.de>
> +#
> +# SPDX-License-Identifier: GPL-3.0-or-later
> +
> +import logging
> +import optparse
> +import os
> +import tempfile
> +import traceback
> +
> +from elbepack.log import elbe_logging
> +from elbepack.treeutils import etree
> +from elbepack.shellhelper import get_command_out, command_out, do, CommandError
> +from elbepack.filesystem import TmpdirFilesystem
> +
> +DEVNULL = open(os.devnull, "w")
> +
> +def run_command(argv):
> +
> + oparser = optparse.OptionParser(usage="usage: %prog check-build <cmd> <build-dir>")
> +
> + (_, args) = oparser.parse_args(argv)
> +
> + if len(args) < 2:
> + oparser.print_help()
> + os.sys.exit(20)
> +
> + os.chdir(args[1])
> +
> + if args[0] == "all":
> + tests = [CheckBase.tests[tag] for tag in CheckBase.tests]
> + elif args[0] in CheckBase.tests:
> + tests = [CheckBase.tests[args[0]]]
> + else:
> + print("Invalid check test %s" % args[0])
> + print("Valid tests are:\n\tall")
> + for tag in CheckBase.tests:
> + print("\t%s" % tag)
> + os.sys.exit(20)
> +
> + total_cnt = 0
> + fail_cnt = 0
> +
> + with elbe_logging({"streams":None}):
> +
> + for test in tests:
> +
> + logging.info("Starting test %s (%s)", test.__name__, test.__doc__)
> + ret = test()()
> +
> + total_cnt += 1
> + if ret:
> + fail_cnt += 1
> + logging.error("FAILED test %s (%s)", test.__name__, test.__doc__)
> +
> + logging.info("Passed %d tests ouf of %d",
> + total_cnt - fail_cnt, total_cnt)
> +
> + os.sys.exit(fail_cnt)
> +
> +class CheckException(Exception):
> + pass
> +
> +# TODO:py3 Remove object inheritance
> +# pylint: disable=useless-object-inheritance
> +class CheckBase(object):
> +
> + tests = dict()
> +
> + def __init__(self):
> + self.ret = 0
> +
> + def __call__(self):
> + try:
> + self.ret = self.run()
> + except CheckException as E:
> + logging.exception(E)
> + self.ret = 1
> + except: # pylint: disable=bare-except
> + logging.error(traceback.format_exc())
> + self.ret = 1
> + return self.ret
> +
> + @classmethod
> + def register(cls, tag):
> + def _register(test):
> + cls.tests[tag] = test
> + return test
> + return _register
> +
> + # pylint: disable=no-self-use
> + def run(self):
> + raise Exception("Check run method not implemented")
> +
> + def fail(self, reason):
> + raise CheckException(reason)
>
More information about the elbe-devel
mailing list