from security_scripts.kli import env_control config = configparser.ConfigParser() import os rel_path = "defaults.cfg" cfg_sources = [rel_path, # built-in config for fallback os.path.expanduser(env_control()) # env value ] config.read(cfg_sources) profile = config.get("DEFAULT", "profile", fallback="scimma-uiuc-aws-admin") loglevel = config.get("DOWNLOAD", "loglevel",fallback="NORMAL") """Create command line arguments""" parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('--profile','-p',default=profile,help='aws profile to use (default: %(default)s)') parser.add_argument('--loglevel', '-l', help="Level for reporting e.g. NORMAL, VERBOSE, DEBUG (default: %(default)s)", default=loglevel, choices=["NONE", "NORMAL", "DOTS", "WARN", "ERROR", "VERBOSE", "VVERBOSE", "DEBUG"]) parser = parser_builder(None, parser, config, False) args = parser.parse_args() shlog.basicConfig(level=args.loglevel) vault_main(args)
def catcher(): import configparser import sys from datetime import date, timedelta config = configparser.ConfigParser() # proven to work both in package and unpacked form import os script_dir = os.path.dirname(__file__) # <-- absolute dir the script is in rel_path = "cfg/defaults.cfg" abs_file_path = os.path.join(script_dir, rel_path) cfg_sources = [ abs_file_path, # built-in config for fallback os.path.expanduser(env_control()) # env value ] config.read(cfg_sources) loglevel = config.get("DEFAULT", "loglevel", fallback="NORMAL") profile = config.get("DEFAULT", "profile", fallback="scimma-uiuc-aws-admin") """Create command line arguments""" parent_parser = argparse.ArgumentParser( add_help=False, description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parent_parser.add_argument( '--loglevel', '-l', help= "Level for reporting e.g. NORMAL, VERBOSE, DEBUG (default: %(default)s)", default=loglevel, choices=[ "NONE", "NORMAL", "DOTS", "WARN", "ERROR", "VERBOSE", "VVERBOSE", "DEBUG" ]) parent_parser.add_argument( '--profile', '-p', default=profile, help='~/.aws/config profile to use (default: %(default)s)') parser = argparse.ArgumentParser(add_help=False) subparsers = parser.add_subparsers() import importlib import pkgutil import security_scripts # switch to it as working dir original_wd = os.getcwd() os.chdir(security_scripts.__path__[0]) commands_paths = ["information", "controls"] for commands_path in commands_paths: for importer, command_name, _ in pkgutil.iter_modules([commands_path]): full_package_name = "security_scripts.%s.%s" % (commands_path, command_name) module = importlib.import_module(full_package_name) try: subparsers = module.parser_builder(parent_parser, subparsers, config, True) except AttributeError: # no parser builder found in file pass # switch back to original working directory os.chdir(original_wd) # parse args or handle help args = parser.parse_args() if len(sys.argv) == 1: parser_help(parser) sys.exit() if not args.func or not args: # there are no subfunctions parser_help(parser) exit(1) shlog.basicConfig(level=args.loglevel) args.func(args)