diff options
author | Erik Skultety <eskultet@redhat.com> | 2023-03-10 13:57:03 +0100 |
---|---|---|
committer | Erik Skultety <eskultet@redhat.com> | 2023-03-14 15:50:34 +0100 |
commit | f0cc11e3f13e51518fec0ba6857a8fcba03b8e88 (patch) | |
tree | a1f33be0142f039994b8ddf7483481a1afd86780 | |
parent | e1595b52a1adb2878b71c3dad5856fd442e91cac (diff) | |
download | libvirt-ci-f0cc11e3f13e51518fec0ba6857a8fcba03b8e88.zip libvirt-ci-f0cc11e3f13e51518fec0ba6857a8fcba03b8e88.tar.gz libvirt-ci-f0cc11e3f13e51518fec0ba6857a8fcba03b8e88.tar.bz2 |
lcitool: Introduce a __main__ module
The thing with transitioning from setup.py to pyproject.toml build
strategy is that we now must specify a list of all entry points for the
project, IOW whatever is going to become the CLI executable. With
setup.py we got away by having a bin/lcitool script which would get
installed directly, but that's not how setuptools builds work after PEP
517 [1]. The build backend now creates the executables automatically as
wrappers over the entry points that are required to be listed in the
project's build config. There's one more requirement - the entry point
needs to be importable through Python's standard means. Since
bin/lcitool is not part of the lcitool package, hence not easily
importable as a module, we need to have its logic elsewhere. One of the
standard ways of doing that is by creating a __main__.py module which
python also automatically looks for when instructed to run the project
directly from its repo.
[1] https://peps.python.org/pep-0517/
Signed-off-by: Erik Skultety <eskultet@redhat.com>
-rw-r--r-- | lcitool/__main__.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/lcitool/__main__.py b/lcitool/__main__.py new file mode 100644 index 0000000..a045e81 --- /dev/null +++ b/lcitool/__main__.py @@ -0,0 +1,41 @@ +import sys +import logging + +from lcitool.application import Application +from lcitool.commandline import CommandLine +from lcitool.logger import LevelFormatter + + +class LcitoolLogger(logging.Logger): + def debug(self, *args, **kwargs): + super().debug(*args, **kwargs, exc_info=True) + + +def main(): + log_level_formats = { + logging.DEBUG: "[%(levelname)s] %(module)s:%(funcName)s:%(lineno)d: %(message)s", + logging.ERROR: "[%(levelname)s]: %(message)s", + } + + logging.setLoggerClass(LcitoolLogger) + + custom_formatter = LevelFormatter(log_level_formats) + custom_handler = logging.StreamHandler(stream=sys.stderr) + custom_handler.setFormatter(custom_formatter) + + log = logging.getLogger() + log.addHandler(custom_handler) + + args = CommandLine().parse() + + if args.debug: + log.setLevel(logging.DEBUG) + + try: + Application().run(args) + except Exception: + log.exception("An unexpected error occurred") + sys.exit(1) + + +main() |