Skip to content

Commit

Permalink
Allow running the yacfg scripts directly; use absolute imports, add s…
Browse files Browse the repository at this point in the history
…hebang, 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.
  • Loading branch information
jiridanek committed Apr 10, 2024
1 parent bb004ac commit 5f97a2b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
31 changes: 16 additions & 15 deletions src/yacfg/cli/yacfg_cli.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -148,3 +145,7 @@ def error(msg: str, ecode: int = 2) -> None:
def main():
app = CommandLineApp()
app.run()


if __name__ == '__main__':
main()
17 changes: 14 additions & 3 deletions src/yacfg_batch/yacfg_batch_cli.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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()

Expand Down

0 comments on commit 5f97a2b

Please sign in to comment.