From: Frank Brehm Date: Wed, 15 Feb 2017 10:12:41 +0000 (+0100) Subject: Adding evaluation of command line arguments X-Git-Tag: 0.8.4~31 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=e504690a6979487ec138fe5fa015503580e4d62f;p=pixelpark%2Fpuppetmaster-webhooks.git Adding evaluation of command line arguments --- diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index 4bafe80..96df6a7 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '0.3.6' +__version__ = '0.4.1' # vim: ts=4 et list diff --git a/lib/webhooks/base_app.py b/lib/webhooks/base_app.py index 870cdab..c3ecad1 100644 --- a/lib/webhooks/base_app.py +++ b/lib/webhooks/base_app.py @@ -16,6 +16,7 @@ import textwrap import datetime import json import smtplib +import argparse from email.message import EmailMessage # Third party modules @@ -48,6 +49,9 @@ class BaseHookApp(object): def __init__(self, appname=None, verbose=0, version=__version__): """Constructor.""" + if not getattr(self, 'description', None): + self.description = "Base gitlab webhook application." + self._appname = None """ @ivar: name of the current running application @@ -82,6 +86,8 @@ class BaseHookApp(object): self.git_ssh_url = None self.do_sudo = True + self.cmdline_args = None + self.error_data = [] self.smtp_server = 'smtp.pixelpark.com' self.smtp_port = 25 @@ -97,6 +103,8 @@ class BaseHookApp(object): self._log_directory = os.sep + os.path.join('var', 'log', 'webhooks') + self.do_arg_parser() + self.read_config() self.init_logging() @@ -188,6 +196,55 @@ class BaseHookApp(object): return res + # ------------------------------------------------------------------------- + def do_arg_parser(self): + + + arg_parser = argparse.ArgumentParser( + description=self.description, + add_help=False, + ) + + arg_parser.add_argument( + "-v", "--verbose", + action="count", + dest='verbose', + help='Increase the verbosity level', + ) + + arg_parser.add_argument( + "-h", "--help", + action='help', + dest='help', + help='Show this help message and exit' + ) + + arg_parser.add_argument( + "--usage", + action='store_true', + dest='usage', + help="Display brief usage message and exit" + ) + + arg_parser.add_argument( + "-V", '--version', + action='version', + version='Version of %(prog)s: {}'.format(self.version), + help="Show program's version number and exit" + ) + + self.cmdline_args = arg_parser.parse_args() + + if self.cmdline_args.usage: + arg_parser.print_usage(sys.stdout) + sys.exit(0) + + if self.cmdline_args.verbose is not None: + if self.cmdline_args.verbose > self.verbose: + self.verbose = self.cmdline_args.verbose + if self.cmdline_args.verbose > self._start_verbose: + self._start_verbose = self.cmdline_args.verbose + # ------------------------------------------------------------------------- def read_config(self): """Reading configuration from different YAML files.""" diff --git a/lib/webhooks/r10k.py b/lib/webhooks/r10k.py index 224f14b..10aeef4 100644 --- a/lib/webhooks/r10k.py +++ b/lib/webhooks/r10k.py @@ -42,6 +42,10 @@ class R10kHookApp(BaseHookApp): """Constructor.""" self.ignore_projects = [] + self.description = textwrap.dedent('''\ + Receives push events as JSON-Data and synchronizes + the local repository and deploys it with r10k. + ''').strip() super(R10kHookApp, self).__init__( appname=appname, verbose=verbose, version=version) diff --git a/r10k_hook-new.py b/r10k_hook-new.py index 8091d8b..b7a3189 100755 --- a/r10k_hook-new.py +++ b/r10k_hook-new.py @@ -20,7 +20,7 @@ from webhooks.r10k import R10kHookApp MY_APPNAME = 'r10k-hook' LOG = logging.getLogger(MY_APPNAME) -app = R10kHookApp(appname=MY_APPNAME, verbose=3) +app = R10kHookApp(appname=MY_APPNAME) if app.verbose > 2: LOG.debug("{c} object:\n{o}".format(c=app.__class__.__name__, o=app))