From: Frank Brehm Date: Fri, 24 Aug 2018 12:14:47 +0000 (+0200) Subject: Adding setting of timestamp of checking X-Git-Tag: 0.9.8^2~2 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=1db96f779f34d2adb43c2d1a153b94d02eaa8ad3;p=pixelpark%2Fpuppetmaster-webhooks.git Adding setting of timestamp of checking --- diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index ff793f9..17c416e 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '0.9.6' +__version__ = '0.9.7 # vim: ts=4 et list diff --git a/lib/webhooks/base_app.py b/lib/webhooks/base_app.py index 6f413e3..40e8b2f 100644 --- a/lib/webhooks/base_app.py +++ b/lib/webhooks/base_app.py @@ -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): diff --git a/lib/webhooks/get_forge_modules.py b/lib/webhooks/get_forge_modules.py index 2f08595..91aa7ec 100644 --- a/lib/webhooks/get_forge_modules.py +++ b/lib/webhooks/get_forge_modules.py @@ -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 # ------------------------------------------------------------------------- diff --git a/lib/webhooks/module_info.py b/lib/webhooks/module_info.py index 339548a..6d17c88 100644 --- a/lib/webhooks/module_info.py +++ b/lib/webhooks/module_info.py @@ -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 # =============================================================================