diff --git a/pbrx/cmd/main.py b/pbrx/cmd/main.py new file mode 100644 index 0000000..ccc8e03 --- /dev/null +++ b/pbrx/cmd/main.py @@ -0,0 +1,98 @@ +# Copyright 2018 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import argparse +import json +import logging +import logging.config +import os +import sys + +try: + import yaml +except ImportError: + yaml = None + +import pbr.version + +log = logging.getLogger("pbrx") + + +def _read_logging_config_file(filename): + if not os.path.exists(filename): + raise ValueError("Unable to read logging config file at %s", filename) + + ext = os.path.splitext(filename)[1] + if ext in (".yml", ".yaml"): + if not yaml: + raise ValueError( + "PyYAML not installed but a yaml logging config was provided." + " Install PyYAML, or convert the config to JSON." + ) + + return yaml.safe_load(open(filename, "r")) + + elif ext == ".json": + return json.load(open(filename, "r")) + + return filename + + +def setup_logging(log_config, debug): + if log_config: + config = _read_logging_config_file(log_config) + if isinstance(config, dict): + logging.config.dictConfig(config) + else: + logging.config.fileConfig(config) + else: + log.addHandler(logging.StreamHandler()) + log.setLevel(logging.DEBUG if debug else logging.INFO) + + +def main(): + parser = argparse.ArgumentParser( + description="pbrx: Utilities for projects using pbr" + ) + parser.add_argument( + "--version", + action="version", + version=str(pbr.version.VersionInfo("pbrx")), + ) + parser.add_argument( + "--debug", help="Emit debug output", action="store_true" + ) + parser.add_argument( + "--log-config", + help="Path to a logging config file. Takes precedence over --debug", + ) + + subparsers = parser.add_subparsers( + title="commands", description="valid commands", help="additional help" + ) + + args = parser.parse_args() + setup_logging(args.log_config, args.debug) + try: + return args.func(args) + + except Exception as e: + log.exception(str(e)) + return 1 + + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/setup.cfg b/setup.cfg index bcbefbc..0834a33 100644 --- a/setup.cfg +++ b/setup.cfg @@ -18,6 +18,10 @@ classifier = Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 +[entry_points] +console_scripts = + pbrx = pbrx.cmd.main:main + [build_sphinx] all-files = 1 warning-is-error = 1