From 5f97a2bcda51891fa6d46d71918aae45ad3beb5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jiri=20Dan=C4=9Bk?= Date: Wed, 10 Apr 2024 17:44:20 +0200 Subject: [PATCH] Allow running the yacfg scripts directly; use absolute imports, add shebang, and fixup `sys.path` if needed Shebang has been added at the beginning of yacfg_batch_cli.py and yacfg_cli.py to allow script execution. The import statements have been updated to avoid 'ImportError'. Unnecessary sys.path entries are removed to guard against import misdirection. Also, a script entry point check has been added at the end of yacfg_cli.py. --- src/yacfg/cli/yacfg_cli.py | 31 +++++++++++++++--------------- src/yacfg_batch/yacfg_batch_cli.py | 17 +++++++++++++--- 2 files changed, 30 insertions(+), 18 deletions(-) mode change 100644 => 100755 src/yacfg_batch/yacfg_batch_cli.py diff --git a/src/yacfg/cli/yacfg_cli.py b/src/yacfg/cli/yacfg_cli.py index 0e57790e..fbbd663c 100755 --- a/src/yacfg/cli/yacfg_cli.py +++ b/src/yacfg/cli/yacfg_cli.py @@ -1,19 +1,16 @@ +#! /usr/bin/env -S python3 -sP + import logging import os import sys -from .. import NAME, __version__, logger_settings +from yacfg import NAME, __version__, logger_settings from yacfg.cli.cli_arguments import boolize, parse_key_value_list, parser -from ..config_data import RenderOptions -from ..exceptions import GenerationError, ProfileError, TemplateError -from ..output import ( - export_tuning_variables, - new_profile, - new_profile_rendered, - new_template, -) -from ..query import list_profiles, list_templates -from ..yacfg import generate +from yacfg.config_data import RenderOptions +from yacfg.exceptions import GenerationError, ProfileError, TemplateError +from yacfg.output import export_tuning_variables, new_profile, new_profile_rendered, new_template +from yacfg.query import list_profiles, list_templates +from yacfg.yacfg import generate logger_settings.config_console_logger() @@ -107,10 +104,10 @@ def run(self, args=None): self.error(str(exc), 0) if ( - options.new_profile - or options.new_profile_static - or options.export_tuning - or options.new_template + options.new_profile + or options.new_profile_static + or options.export_tuning + or options.new_template ): sys.exit(0) @@ -148,3 +145,7 @@ def error(msg: str, ecode: int = 2) -> None: def main(): app = CommandLineApp() app.run() + + +if __name__ == '__main__': + main() diff --git a/src/yacfg_batch/yacfg_batch_cli.py b/src/yacfg_batch/yacfg_batch_cli.py old mode 100644 new mode 100755 index 41b8159c..c220769b --- a/src/yacfg_batch/yacfg_batch_cli.py +++ b/src/yacfg_batch/yacfg_batch_cli.py @@ -1,13 +1,24 @@ +#! /usr/bin/env -S python3 -sP + from __future__ import print_function import logging +import pathlib import sys +# `from yacfg_batch import ...` may target both src/yacfg/yacfg_batch/__init__.py +# and src/yacfg/yacfg_batch/yacfg_batch.py (which is on sys.path when running +# this file directly). Remove the undesirable sys.path entry to prevent `ImportError`s. +try: + sys.path.remove(str(pathlib.Path(__file__).resolve().parent)) +except ValueError: + pass + from yacfg import NAME, logger_settings -from . import __version__ -from .cli_arguments import parser -from .yacfg_batch import generate +from yacfg_batch import __version__ +from yacfg_batch.cli_arguments import parser +from yacfg_batch.yacfg_batch import generate logger_settings.config_console_logger()