From: Frank Brehm Date: Thu, 6 Sep 2018 12:51:27 +0000 (+0200) Subject: Adding new command line parameter --no-forge to get-forge-modules X-Git-Tag: 0.11.4^2 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=1290df9376750f332980a9ffa65f089bf6e3e7a5;p=pixelpark%2Fpuppetmaster-webhooks.git Adding new command line parameter --no-forge to get-forge-modules --- diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index 1fc3a9f..abf9521 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '0.11.3' +__version__ = '0.11.4' # vim: ts=4 et list diff --git a/lib/webhooks/base_app.py b/lib/webhooks/base_app.py index 552648d..2da9c3a 100644 --- a/lib/webhooks/base_app.py +++ b/lib/webhooks/base_app.py @@ -293,42 +293,58 @@ class BaseHookApp(BaseObject): add_help=False, ) - arg_parser.add_argument( + self._init_arg_parser(arg_parser) + + self._perform_args(arg_parser) + + # ------------------------------------------------------------------------- + def init_arg_parser(self, arg_parser): + + pass + + # ------------------------------------------------------------------------- + def _init_arg_parser(self, arg_parser): + + self.init_arg_parser(arg_parser) + + general_group = arg_parser.add_argument_group('General options') + + general_group.add_argument( "-N", "--no-error-mail", action='store_true', dest='no_error_mail', help="Don't send error messages in case of some exceptions.", ) - arg_parser.add_argument( + general_group.add_argument( "-D", '--data', '--data-dir', metavar='DIR', dest='data_dir', help="Data directory, default: {!r}.".format(self.data_dir), ) - arg_parser.add_argument( + general_group.add_argument( "-v", "--verbose", action="count", dest='verbose', help='Increase the verbosity level', ) - arg_parser.add_argument( + general_group.add_argument( '-s', '--simulate', '--test', action='store_true', dest='simulate', help="Simulation mode, nothing is really done.", ) - arg_parser.add_argument( + general_group.add_argument( "-C", '--cgi', action='store_true', dest='cgi', help='Enforces behaviour as called as a CGI script.', ) - arg_parser.add_argument( + general_group.add_argument( "-h", "--help", action='help', dest='help', help='Show this help message and exit' ) - arg_parser.add_argument( + general_group.add_argument( "--usage", action='store_true', dest='usage', help="Display brief usage message and exit" ) - arg_parser.add_argument( + general_group.add_argument( "-V", '--version', action='version', version='Version of %(prog)s: {}'.format(self.version), help="Show program's version number and exit" @@ -339,6 +355,9 @@ class BaseHookApp(BaseObject): help="An optional query string like on HTTP GET requests." ) + # ------------------------------------------------------------------------- + def _perform_args(self, arg_parser): + self.cmdline_args = arg_parser.parse_args() if self.cmdline_args.usage: @@ -376,6 +395,13 @@ class BaseHookApp(BaseObject): except ValueError as e: LOG.error(str(e)) + self.perform_args(arg_parser) + + # ------------------------------------------------------------------------- + def perform_args(self, arg_parser): + + pass + # ------------------------------------------------------------------------- def _get_query(self): diff --git a/lib/webhooks/get_forge_modules.py b/lib/webhooks/get_forge_modules.py index 7824b99..0a021d0 100644 --- a/lib/webhooks/get_forge_modules.py +++ b/lib/webhooks/get_forge_modules.py @@ -33,7 +33,7 @@ from six import reraise # Own modules from . import __version__ -from .common import pp, to_bytes +from .common import pp, to_bytes, to_bool from .base_app import BaseHookError, BaseHookApp @@ -81,6 +81,7 @@ class GetForgeModulesApp(BaseHookApp): self.forge_uri = self.default_forge_uri self.http_timeout = self.default_http_timeout self.modules = None + self_do_forge = True self.description = textwrap.dedent('''\ Collects information about all used Puppet Modules from {url} @@ -94,6 +95,31 @@ class GetForgeModulesApp(BaseHookApp): self.modules = ModuleInfoDict( appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) + # ----------------------------------------------------------- + @property + def do_forge(self): + """Flag for simulation mode.""" + return getattr(self, '_do_forge', True) + + @do_forge.setter + def do_forge(self, value): + self._do_forge = to_bool(value) + + # ------------------------------------------------------------------------- + def init_arg_parser(self, arg_parser): + + arg_parser.add_argument( + '--no-forge', action='store_true', dest='no_forge', + help="Don't get module information from Puppet forge.", + ) + + # ------------------------------------------------------------------------- + def perform_args(self, arg_parser): + + if self.cmdline_args.no_forge: + sys.stderr.write("\nDon't get module information from Puppet forge.\n\n") + self.do_forge = False + # ------------------------------------------------------------------------- def post_init(self): @@ -118,6 +144,7 @@ class GetForgeModulesApp(BaseHookApp): res['max_http_timeout'] = self.max_http_timeout res['default_puppet_root_env_dir'] = self.default_puppet_root_env_dir res['open_args'] = self.open_args + res['do_forge'] = self.do_forge return res @@ -170,7 +197,8 @@ class GetForgeModulesApp(BaseHookApp): six.reraise(*sys.exc_info()) self.init_puppet_environments() self.collect_local_modules() - self.get_forge_information() + if self.do_forge: + self.get_forge_information() if self.verbose > 2: LOG.debug("Found modules:\n{}".format(pp(self.modules.as_list())))