]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Adding evaluation of command line arguments
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 15 Feb 2017 10:12:41 +0000 (11:12 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 15 Feb 2017 10:12:41 +0000 (11:12 +0100)
lib/webhooks/__init__.py
lib/webhooks/base_app.py
lib/webhooks/r10k.py
r10k_hook-new.py

index 4bafe803e150073dc47c6d43071725de97093810..96df6a76a1df32c5480aaed9572d0216c6286f05 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '0.3.6'
+__version__ = '0.4.1'
 
 # vim: ts=4 et list
index 870cdabc0b6f5e231efafc4c455e5e87a585e117..c3ecad1274c93f28605f20a43dac8dea61957655 100644 (file)
@@ -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."""
index 224f14b0c8f5042cc5dece9061f790ca40111c84..10aeef46950b9dcab17e7f482992e0676f45cc32 100644 (file)
@@ -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)
index 8091d8b37475b4b557cb987d224496bfeddf4c07..b7a318987a0df17eceaa2952787f4d0b1f568658 100755 (executable)
@@ -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))