]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Adding setting of timestamp of checking
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 24 Aug 2018 12:14:47 +0000 (14:14 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 24 Aug 2018 12:14:47 +0000 (14:14 +0200)
lib/webhooks/__init__.py
lib/webhooks/base_app.py
lib/webhooks/get_forge_modules.py
lib/webhooks/module_info.py

index ff793f9e18ecba12a814f525c902b923f5cbcde1..17c416e1e4c04ceb11fc1ace9cad63a30d31907e 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '0.9.6'
+__version__ = '0.9.7
 
 # vim: ts=4 et list
index 6f413e3a78ba9533d6b320491aabd5455137dbdd..40e8b2fcce2a45ba3bc83f795d08f38858f7c6f7 100644 (file)
@@ -24,6 +24,7 @@ from email.message import EmailMessage
 
 # Third party modules
 import yaml
+import pytz
 
 # Own modules
 import webhooks
@@ -65,6 +66,7 @@ class BaseHookApp(BaseObject):
     fileserver_dir = os.path.join(puppetlabs_cfg_dir, 'code', 'fileserver')
 
     default_data_dir = os.sep + os.path.join('var', 'lib', 'webhooks')
+    default_tz_name = 'Europe/Berlin'
 
     special_chars_re = re.compile(r'[^a-z0-9_\-]', re.IGNORECASE)
     dev_re = re.compile(r'^dev')
@@ -81,6 +83,7 @@ class BaseHookApp(BaseObject):
 
         self.data_dir = self.default_data_dir
         self._read_stdin = True
+        self.tz_name = self.default_tz_name
 
         super(BaseHookApp, self).__init__(
             appname=appname, verbose=verbose, version=version,
@@ -98,6 +101,7 @@ class BaseHookApp(BaseObject):
         self.full_name = None
         self.git_ssh_url = None
         self.do_sudo = True
+        self.tz = None
 
         self.cmdline_args = None
 
@@ -134,6 +138,8 @@ class BaseHookApp(BaseObject):
             appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
             simulate=self.simulate)
 
+        self.tz = pytz.timezone(self.tz_name)
+
         self.post_init()
 
         return
@@ -206,6 +212,7 @@ class BaseHookApp(BaseObject):
         res['fileserver_dir'] = self.fileserver_dir
         res['default_data_dir'] = self.default_data_dir
         res['read_stdin'] = self.read_stdin
+        res['default_tz_name'] = self.default_tz_name
 
         return res
 
@@ -376,6 +383,11 @@ class BaseHookApp(BaseObject):
             if os.path.isabs(pdir):
                 self.default_parent_dir = pdir
 
+        if 'tz' in config and config['tz']:
+            tz = str(config['tz']).strip()
+            if tz:
+                self.tz_name = tz
+
         if 'data_dir' in config and config['data_dir'] and not self.cmdline_args.data_dir:
             path = config['data_dir']
             if not os.path.isabs(path):
index 2f085950cb11aa9c85fcb7c4c1c27cda3f38b931..91aa7ec59d1f64aef9eb0b09a858e765082cc781 100644 (file)
@@ -211,6 +211,7 @@ class GetForgeModulesApp(BaseHookApp):
         if module_info_forge:
             module_info.merge_in(module_info_forge)
         else:
+            module_info.set_ts_checked()
             module_info.forge_avail = False
 
     # -------------------------------------------------------------------------
index 339548a84c0fd005f3e5cf0399d9bd199801a233..6d17c888e7cc7ebc7168a7bf6cffc492b7b3cc04 100644 (file)
@@ -14,10 +14,12 @@ import logging
 import re
 import copy
 import warnings
+import time
 
 # Third party modules
 import six
 import requests
+import pytz
 
 # Own modules
 from .common import pp, to_str, to_bool, is_sequence
@@ -70,6 +72,7 @@ class ModuleInfo(BaseObject):
         self.forge_releases = None
         self._forge_homepage_url = None
         self._forge_source = None
+        self._ts_checked = None
 
         super(ModuleInfo, self).__init__(
             appname=appname, verbose=verbose, version=version,
@@ -222,6 +225,32 @@ class ModuleInfo(BaseObject):
             return
         self._forge_avail = to_bool(value)
 
+    # -------------------------------------------------------------------------
+    @property
+    def ts_checked(self):
+        """The timestamp, when this module was checked on Puppet forge."""
+        return self._ts_checked
+
+    @ts_checked.setter
+    def ts_checked(self, value):
+        if value is None:
+            self._ts_checked = None
+            return
+        self._ts_checked = float(value)
+
+    # -------------------------------------------------------------------------
+    @property
+    def date_checked(self):
+        """A string representation of the check timestamp, if such one is given."""
+        if self.ts_checked is None:
+            return None
+        dt = datetime.datetime.fromtimestamp(self.ts_checked, tz=pytz.utc)
+        return dt.strftime('%Y-%m-%d %H:%M:%S %Z')
+
+    # -------------------------------------------------------------------------
+    def set_ts_checked(self):
+        self._ts_checked = time.time()
+
     # -------------------------------------------------------------------------
     def __repr__(self):
         return str(self)
@@ -245,6 +274,8 @@ class ModuleInfo(BaseObject):
         res['forge_avail'] = self.forge_avail
         res['forge_homepage_url'] = self.forge_homepage_url
         res['forge_source'] = self.forge_source
+        res['ts_checked'] = self.ts_checked
+        res['date_checked'] = self.date_checked
 
         return res
 
@@ -263,6 +294,7 @@ class ModuleInfo(BaseObject):
         res['forge_homepage_url'] = self.forge_homepage_url
         res['forge_source'] = self.forge_source
         res['repo'] = self.repo
+        res['ts_checked'] = self.ts_checked
 
         res['local_versions'] = copy.copy(self.local_versions)
         res['expected_versions'] = copy.copy(self.expected_versions)
@@ -296,6 +328,9 @@ class ModuleInfo(BaseObject):
         if other.repo and not self.repo:
             self.repo = other.repo
 
+        if other.ts_checked and not self.ts_checked:
+            self.ts_checked = other.ts_checked
+
         for env in other.local_versions.keys():
             if env not in self.local_versions:
                 self.local_versions[env] = other.local_versions[env]
@@ -501,6 +536,8 @@ class ModuleInfo(BaseObject):
                 if 'version' in release and release['version']:
                     module_info.forge_releases.append(release['version'].strip())
 
+        module_info.set_ts_checked()
+
         return module_info
 
 # =============================================================================