From: Frank Brehm Date: Wed, 2 Jan 2019 16:42:12 +0000 (+0100) Subject: Changing behaviour on paths in lib/webhooks/base_app.py and lib/webhooks/get_forge_mo... X-Git-Tag: 1.6.4^2~86 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=bcdfdd391f6d6d4264e76d6dbb89797ec4e40126;p=pixelpark%2Fpuppetmaster-webhooks.git Changing behaviour on paths in lib/webhooks/base_app.py and lib/webhooks/get_forge_modules.py --- diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index 0b085c6..e3f7327 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '1.4.3' +__version__ = '1.4.4' # vim: ts=4 et list diff --git a/lib/webhooks/base_app.py b/lib/webhooks/base_app.py index ba23769..1c7c8f8 100644 --- a/lib/webhooks/base_app.py +++ b/lib/webhooks/base_app.py @@ -1041,17 +1041,17 @@ class BaseHookApp(BaseApplication): LOG.debug("Checking availibility of data directory {!r} ...".format(self.data_dir)) - if not os.path.exists(self.data_dir): + if not self.data_dir.exists(): raise BaseHookError( - _("Data directory {!r} does not exists.").format(self.data_dir)) + _("Data directory {!r} does not exists.").format(str(self.data_dir))) - if not os.path.isdir(self.data_dir): + if not self.data_dir.is_dir(): raise BaseHookError( - _("Path to data directory {!r} is not a directory.").format(self.data_dir)) + _("Path to data directory {!r} is not a directory.").format(str(self.data_dir))) - if not os.access(self.data_dir, os.W_OK): + if not os.access(str(self.data_dir), os.W_OK): raise BaseHookError( - _("Data directory {!r} is not writeable.").format(self.data_dir)) + _("Data directory {!r} is not writeable.").format(str(self.data_dir))) return True diff --git a/lib/webhooks/get_forge_modules.py b/lib/webhooks/get_forge_modules.py index 1494a6d..efda0b8 100644 --- a/lib/webhooks/get_forge_modules.py +++ b/lib/webhooks/get_forge_modules.py @@ -268,11 +268,14 @@ class GetForgeModulesApp(BaseHookApp): LOG.info(_("Reading metadata files of environment {!r} ...").format(env)) - pattern = os.path.join(self.puppet_root_env_dir, env, 'modules', '*') - if self.verbose > 2: - LOG.debug("Globbing pattern for module directories: {!r}".format(pattern)) + #pattern = os.path.join(self.puppet_root_env_dir, env, 'modules', '*') + #if self.verbose > 2: + # LOG.debug("Globbing pattern for module directories: {!r}".format(str(pattern))) + + modules_dir = self.puppet_root_env_dir / env / 'modules' - for module_dir in glob.glob(pattern): + #for module_dir in glob.glob(str(pattern)): + for module_dir in modules_dir.glob('*'): module_info = self.get_meta_module_info(module_dir, env) if module_info: if self.verbose > 1: @@ -287,39 +290,39 @@ class GetForgeModulesApp(BaseHookApp): def get_meta_module_info(self, module_dir, env): if self.verbose > 1: - LOG.debug("Get module information from {!r}.".format(module_dir)) + LOG.debug("Get module information from {!r}.".format(str(module_dir))) - if not os.path.exists(module_dir): - LOG.warn(_("Directory {!r} does not exists.").format(module_dir)) + if not module_dir.exists(): + LOG.warn(_("Directory {!r} does not exists.").format(str(module_dir))) return None - if not os.path.isdir(module_dir): - LOG.warn(_("Path {!r} is not a directory.").format(module_dir)) + if not module_dir.is_dir(): + LOG.warn(_("Path {!r} is not a directory.").format(str(module_dir))) return None - metadata_file = os.path.join(module_dir, 'metadata.json') - if not os.path.exists(metadata_file): - LOG.warn(_("Metadatafile {!r} does not exists.").format(metadata_file)) + metadata_file = module_dir / 'metadata.json' + if not metadata_file.exists(): + LOG.warn(_("Metadatafile {!r} does not exists.").format(str(metadata_file))) return None - if not os.path.isfile(metadata_file): - LOG.warn(_("Metadatafile {!r} is not a regular file.").format(metadata_file)) + if not metadata_file.is_file(): + LOG.warn(_("Metadatafile {!r} is not a regular file.").format(str(metadata_file))) return None - if not os.access(metadata_file, os.R_OK): - LOG.warn(_("No read access to metadatafile {!r}.").format(metadata_file)) + if not os.access(str(metadata_file), os.R_OK): + LOG.warn(_("No read access to metadatafile {!r}.").format(str(metadata_file))) return None if self.verbose > 2: - LOG.debug("Reading and evaluating {!r}.".format(metadata_file)) + LOG.debug("Reading and evaluating {!r}.".format(str(metadata_file))) json_data = None try: - with open(metadata_file, 'r', **self.open_args) as fh: + with open(str(metadata_file), 'r', **self.open_args) as fh: json_data = json.load(fh) except ValueError as e: LOG.warn(_("Could not interprete {f!r} as a regular JSON file: {e}").format( - f=metadata_file, e=e)) + f=str(metadata_file), e=e)) return None if not json_data: - LOG.warn(_("Did not found any data in {!r}.").format(metadata_file)) + LOG.warn(_("Did not found any data in {!r}.").format(str(metadata_file))) return None return ModuleInfo.init_from_json( @@ -353,14 +356,16 @@ class GetForgeModulesApp(BaseHookApp): # ------------------------------------------------------------------------- def init_puppet_environments(self): - LOG.debug("Collecting all Puppet environments in {!r}.".format(self.puppet_root_env_dir)) - pattern = os.path.join(self.puppet_root_env_dir, '*') + LOG.debug("Collecting all Puppet environments in {!r}.".format( + str(self.puppet_root_env_dir))) + pattern = os.path.join(str(self.puppet_root_env_dir), '*') dirs = glob.glob(pattern) self.environments = [] - for path in dirs: - if os.path.isdir(path): - env = os.path.basename(path) + for rpath in dirs: + path = pathlib.Path(rpath) + if path.is_dir(): + env = path.name self.environments.append(env) self.environments.sort(key=str.lower) @@ -374,7 +379,7 @@ class GetForgeModulesApp(BaseHookApp): def write_cache_file(self): output_file = self.cachefile - tmp_file = output_file + '.new' + tmp_file = pathlib.Path(str(output_file) + '.new') fd = None LOG.debug("Sorting found modules ...") @@ -393,7 +398,7 @@ class GetForgeModulesApp(BaseHookApp): return try: - fd = os.open(tmp_file, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o644) + fd = os.open(str(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) @@ -410,8 +415,8 @@ class GetForgeModulesApp(BaseHookApp): finally: os.close(fd) - if os.path.exists(output_file) and not os.geteuid(): - fstat = os.stat(output_file) + if output_file.exists() and not os.geteuid(): + fstat = output_file.stat() user = fstat.st_uid try: user = pwd.getpwuid(fstat.st_uid).pw_name @@ -424,10 +429,11 @@ class GetForgeModulesApp(BaseHookApp): 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) + os.chown(str(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) + LOG.info(_("Renaming {src!r} => {tgt!r}.").format( + src=str(tmp_file), tgt=(output_file))) + tmp_file.rename(output_file) # ============================================================================= diff --git a/requirements.txt b/requirements.txt index bdeb9b4..62f1daa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,5 +9,5 @@ dnspython flake8 docker-py pathlib -git+https://git.pixelpark.com/frabrehm/python_fb_tools.git@develop +git+https://git.pixelpark.com/frabrehm/python_fb_tools.git@test Babel