Skip to content

Commit

Permalink
Properly support Python ver startup abort
Browse files Browse the repository at this point in the history
Refactor ksconf.cli and ksconf.__main__ so that ksconf can explain that
you're Python is old and bad in a graceful way that doesn't require me
strip back out all the cool Python 3 stuff we've done so far.

This means that ksconf.__main__ modules should remain Python 2.7
(possibly 2.6, if module.__main__ even worked in that version??) *just*
so we can show a graceful error message if/when somebody tries to run
this on an old version of Python.

TL;DR: Trying to have our cake, and eat it too.
  • Loading branch information
lowell80 committed Mar 26, 2022
1 parent 8f93918 commit 97b56d7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
41 changes: 41 additions & 0 deletions ksconf/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# PYTHON_ARGCOMPLETE_OK
#
# Unlike the rest of ksconf, this file should remain Python 2.7 compatible
# This is just enough to gracefully report our presence in an unsupported
# version of Python, if we happen to get installed there. Try really hard
# to avoid showing Python compiler errors as those are really confusing.

""" KSCONF - Ksconf Splunk CONFig tool
Optionally supports argcomplete for commandline argument (tab) completion.
Install & register with:
pip install argcomplete
activate-global-python-argcomplete (in ~/.bashrc)
"""
from __future__ import absolute_import, unicode_literals

import sys

if sys.version_info < (3, 7): # noqa
from ksconf.consts import EXIT_CODE_BAD_PY_VERSION

# Can't actually import 'consts' because it loads 'enum'; so we cheat
EXIT_CODE_BAD_PY_VERSION = 121
sys.stderr.write(
"ABORT! ksconf cannot run here because it requires Python 3.7 or later!\n\n"
"Check to see if you have multiple version of Python installed.\n"
"If you need Python 2.7 or 3.6 support, consider downgrading to an earlier release.\n"
"Releases 0.9.x support these older Python versions.\n\n"
"If you installed this with pip, try running:\n\n"
" pip install -U 'kintyre-splunk-conf>=0.9,<=0.10'\n")
sys.exit(EXIT_CODE_BAD_PY_VERSION)

from ksconf.cli import cli

if __name__ == '__main__': # pragma: no cover
cli()
21 changes: 2 additions & 19 deletions ksconf/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@
import sys
from collections import defaultdict

# Doh! This probably only works for Python 3.6; earlier version will fail due to f-string use (needs refactoring)
if sys.version_info < (3, 7): # noqa
from ksconf.consts import EXIT_CODE_BAD_PY_VERSION
sys.stderr.write(
"ERROR: ksconf requires Python 3.7+.\n\n"
"If you need Python 2.7/3.6 support, consider downgrading.\n"
"Releases 0.9.x support these older Python versions.\n\n"
"You can try running:\n\n"
" pip install -U 'kintyre-splunk-conf>=0.9,<=0.10'\n")
sys.exit(EXIT_CODE_BAD_PY_VERSION)


import ksconf
import ksconf.util
from ksconf.commands import DescriptionHelpFormatterPreserveLayout, get_all_ksconf_cmds
Expand Down Expand Up @@ -221,8 +209,7 @@ def build_cli_parser(do_formatter=False):

def cli(argv=None, _unittest=False):
# TODO: Rename '_unitest' to something more appropriate, maybe _exit=True?
if check_py is not None:
check_py()
check_py()

parser = build_cli_parser(True)
if not _unittest:
Expand Down Expand Up @@ -266,8 +253,4 @@ def check_py():
if KSCONF_DEBUG not in os.environ:
sys.exit(EXIT_CODE_ENV_BUSTED)
# Okay, now NEVER call this code again.... (helpful for unit-testing & nested calls)
globals()["check_py"] = None


if __name__ == '__main__': # pragma: no cover
cli()
globals()["check_py"] = lambda: None

0 comments on commit 97b56d7

Please sign in to comment.