From 05a6ace578760340ff3870030441cba43ff85b12 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 24 Aug 2018 12:51:36 +0200 Subject: [PATCH] Writing cache file --- lib/webhooks/__init__.py | 2 +- lib/webhooks/get_forge_modules.py | 68 +++++++++++++++++++++++++++++-- lib/webhooks/module_info.py | 6 +-- 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index 16503b7..ff793f9 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '0.9.5' +__version__ = '0.9.6' # vim: ts=4 et list diff --git a/lib/webhooks/get_forge_modules.py b/lib/webhooks/get_forge_modules.py index cc7e927..2f08595 100644 --- a/lib/webhooks/get_forge_modules.py +++ b/lib/webhooks/get_forge_modules.py @@ -17,18 +17,23 @@ import pipes import json import re import glob -import warnings import datetime +import fcntl +import pwd +import grp + +from operator import attrgetter, itemgetter # Third party modules import six import yaml -import requests + +from six import reraise # Own modules from . import __version__ -from .common import pp, to_str +from .common import pp, to_str, to_bytes from .base_app import BaseHookError, BaseHookApp @@ -161,6 +166,8 @@ class GetForgeModulesApp(BaseHookApp): if self.verbose > 2: LOG.debug("Found modules:\n{}".format(pp(self.modules))) + self.write_cache_file() + print() dt = datetime.datetime.now().isoformat(' ') msg = "[{}]: Finished.".format(dt) @@ -347,6 +354,61 @@ class GetForgeModulesApp(BaseHookApp): raise GetForgeModulesError( "Did not found environments in {!r}.".format(self.puppet_root_env_dir)) + # ------------------------------------------------------------------------- + def write_cache_file(self): + + output_file = os.path.join(self.data_dir, 'modules-info.yaml') + tmp_file = output_file + '.new' + fd = None + + LOG.debug("Sorting found modules ...") + + data = [] + for full_name in self.modules.keys(): + module_info = self.modules[full_name] + data.append(module_info.to_data()) + data.sort(key=itemgetter('name', 'vendor')) + dump = yaml.dump(data, default_flow_style=False) + + LOG.info("Trying to open {!r} exclusive ...".format(tmp_file)) + + try: + fd = os.open(tmp_file, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o644) + fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) + except OSError as e: + msg = "Error on creating file {f!r}: {e}".format(f=tmp_file, e=e) + if e.errno == errno.EEXIST: + LOG.warn(msg) + return None + else: + error_tuple = sys.exc_info() + reraise(GetForgeModulesError, msg, error_tuple[2]) + + try: + os.write(fd, to_bytes('---\n')) + os.write(fd, to_bytes(dump)) + finally: + os.close(fd) + + if os.path.exists(output_file) and not os.geteuid(): + fstat = os.stat(output_file) + user = fstat.st_uid + try: + user = pwd.getpwuid(fstat.st_uid).pw_name + except KeyError: + user = fstat.st_uid + group = fstat.st_gid + try: + group = grp.getgrgid(fstat.st_gid).gr_name + except KeyError: + group = fstat.st_gid + LOG.debug("Chowning {f!r} to {u}:{g} ...".format( + f=tmp_file, u=user, g=group)) + os.chown(tmp_file, fstat.st_uid, fstat.st_gid) + + LOG.info("Renaming {src!r} => {tgt!r}.".format(src=tmp_file, tgt=output_file)) + os.rename(tmp_file, output_file) + # ============================================================================= if __name__ == "__main__": diff --git a/lib/webhooks/module_info.py b/lib/webhooks/module_info.py index b414aff..339548a 100644 --- a/lib/webhooks/module_info.py +++ b/lib/webhooks/module_info.py @@ -264,9 +264,9 @@ class ModuleInfo(BaseObject): res['forge_source'] = self.forge_source res['repo'] = self.repo - red['local_versions'] = copy.copy(self.local_versions) - red['expected_versions'] = copy.copy(self.local_versions) - red['forge_releases'] = copy.copy(self.forge_releases) + res['local_versions'] = copy.copy(self.local_versions) + res['expected_versions'] = copy.copy(self.expected_versions) + res['forge_releases'] = copy.copy(self.forge_releases) return res -- 2.39.5