cli: add checks for importing modules
This commit is contained in:
parent
8019389ccb
commit
d890599c7c
4 changed files with 163 additions and 29 deletions
|
@ -2,6 +2,7 @@ import os
|
|||
from pathlib import Path
|
||||
import sys
|
||||
from subprocess import check_call, run, PIPE
|
||||
import importlib
|
||||
import traceback
|
||||
|
||||
from . import LazyLogger
|
||||
|
@ -9,9 +10,9 @@ from . import LazyLogger
|
|||
log = LazyLogger('HPI cli')
|
||||
|
||||
class Modes:
|
||||
HELLO = 'hello'
|
||||
CONFIG = 'config'
|
||||
DOCTOR = 'doctor'
|
||||
CONFIG = 'config'
|
||||
DOCTOR = 'doctor'
|
||||
MODULES = 'modules'
|
||||
|
||||
|
||||
def run_mypy(pkg):
|
||||
|
@ -39,28 +40,45 @@ def run_mypy(pkg):
|
|||
return mres
|
||||
|
||||
|
||||
def eprint(x: str):
|
||||
print(x, file=sys.stderr)
|
||||
|
||||
def indent(x: str) -> str:
|
||||
return ''.join(' ' + l for l in x.splitlines(keepends=True))
|
||||
|
||||
def info(x: str):
|
||||
eprint('✅ ' + x)
|
||||
|
||||
def error(x: str):
|
||||
eprint('❌ ' + x)
|
||||
|
||||
def warning(x: str):
|
||||
eprint('❗ ' + x) # todo yellow?
|
||||
|
||||
def tb(e):
|
||||
tb = ''.join(traceback.format_exception(Exception, e, e.__traceback__))
|
||||
sys.stderr.write(indent(tb))
|
||||
|
||||
|
||||
class color:
|
||||
BLACK = '\033[30m'
|
||||
RED = '\033[31m'
|
||||
GREEN = '\033[32m'
|
||||
YELLOW = '\033[33m'
|
||||
BLUE = '\033[34m'
|
||||
MAGENTA = '\033[35m'
|
||||
CYAN = '\033[36m'
|
||||
WHITE = '\033[37m'
|
||||
UNDERLINE = '\033[4m'
|
||||
RESET = '\033[0m'
|
||||
|
||||
|
||||
def config_check(args):
|
||||
def eprint(x: str):
|
||||
print(x, file=sys.stderr)
|
||||
|
||||
def indent(x: str) -> str:
|
||||
return ''.join(' ' + l for l in x.splitlines(keepends=True))
|
||||
|
||||
def info(x: str):
|
||||
eprint('✅ ' + x)
|
||||
|
||||
def error(x: str):
|
||||
eprint('❌ ' + x)
|
||||
|
||||
def warning(x: str):
|
||||
eprint('❗ ' + x) # todo yellow?
|
||||
|
||||
try:
|
||||
import my.config as cfg
|
||||
except Exception as e:
|
||||
error("failed to import the config")
|
||||
tb = ''.join(traceback.format_exception(Exception, e, e.__traceback__))
|
||||
sys.stderr.write(indent(tb))
|
||||
tb(e)
|
||||
sys.exit(1)
|
||||
|
||||
info(f"config file: {cfg.__file__}")
|
||||
|
@ -80,12 +98,35 @@ def config_check(args):
|
|||
sys.stderr.write(indent(mres.stdout.decode('utf8')))
|
||||
|
||||
|
||||
def modules_check(args):
|
||||
verbose = args.verbose
|
||||
|
||||
from .util import get_modules
|
||||
for m in get_modules():
|
||||
try:
|
||||
importlib.import_module(m)
|
||||
except Exception as e:
|
||||
# todo more specific command?
|
||||
vw = '' if verbose else '; pass --verbose to print more information'
|
||||
warning(f'{color.RED}FAIL{color.RESET}: {m:<30} loading failed{vw}')
|
||||
if verbose:
|
||||
tb(e)
|
||||
|
||||
else:
|
||||
info(f'{color.GREEN}OK{color.RESET} : {m:<30}')
|
||||
|
||||
|
||||
def list_modules(args):
|
||||
# todo with docs/etc?
|
||||
from .util import get_modules
|
||||
for m in get_modules():
|
||||
print(f'- {m}')
|
||||
|
||||
|
||||
# todo check that it finds private modules too?
|
||||
def doctor(args):
|
||||
config_check(args)
|
||||
|
||||
|
||||
def hello(args):
|
||||
print('Hello')
|
||||
modules_check(args)
|
||||
|
||||
|
||||
def parser():
|
||||
|
@ -96,10 +137,8 @@ Tool for HPI.
|
|||
Work in progress, will be used for config management, troubleshooting & introspection
|
||||
''')
|
||||
sp = p.add_subparsers(dest='mode')
|
||||
hp = sp.add_parser(Modes.HELLO , help='TODO just a stub, remove later')
|
||||
hp.set_defaults(func=hello)
|
||||
|
||||
dp = sp.add_parser(Modes.DOCTOR, help='Run various checks')
|
||||
dp.add_argument('--verbose', action='store_true', help='Print more diagnosic infomration')
|
||||
dp.set_defaults(func=doctor)
|
||||
|
||||
cp = sp.add_parser(Modes.CONFIG, help='Work with configuration')
|
||||
|
@ -108,6 +147,9 @@ Work in progress, will be used for config management, troubleshooting & introspe
|
|||
# ccp = scp.add_parser('check', help='Check config')
|
||||
# ccp.set_defaults(func=config_check)
|
||||
|
||||
mp = sp.add_parser(Modes.MODULES, help='List available modules')
|
||||
mp.set_defaults(func=list_modules)
|
||||
|
||||
return p
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue