From: Frank Brehm Date: Tue, 19 Mar 2019 09:19:49 +0000 (+0100) Subject: Moving some methods from class GetForgeModulesApp to class BaseHookApp X-Git-Tag: 1.6.4^2~19 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=11dc2f881727e16e3d7086d051804f9de9f32472;p=pixelpark%2Fpuppetmaster-webhooks.git Moving some methods from class GetForgeModulesApp to class BaseHookApp --- diff --git a/lib/webhooks/base_app.py b/lib/webhooks/base_app.py index 88a012c..2c267f8 100644 --- a/lib/webhooks/base_app.py +++ b/lib/webhooks/base_app.py @@ -50,6 +50,8 @@ from .module_list import ModuleInfoDict from .puppetfile import PuppetfileError, Puppetfile +from .module_meta_info import ModuleMetadata + from .forge.mod_dict import ForgeModuleDict from .xlate import __module_dir__ as __xlate_module_dir__ @@ -1315,6 +1317,87 @@ class BaseHookApp(BaseApplication): raise BaseHookError( _("Did not found environments in {!r}.").format(self.puppet_root_env_dir)) + # ------------------------------------------------------------------------- + def collect_local_modules(self): + + for env in self.environments: + LOG.debug("Evaluating environment {!r} ..".format(env)) + pfile_modules = self.read_puppetfile(env) + if pfile_modules: + for module_info in pfile_modules.values(): + self.env_modules[env].append(module_info) + self.read_metadata_files(env) + if self.verbose > 2: + LOG.debug("ModuleInfoDict:\n{}".format(pp(self.env_modules[env].as_dict()))) + + # ------------------------------------------------------------------------- + def read_metadata_files(self, env): + + LOG.info(_("Reading metadata files of environment {!r} ...").format(env)) + + modules_dir = self.puppet_root_env_dir / env / 'modules' + + for module_dir in modules_dir.glob('*'): + module_info = self.get_meta_module_info(module_dir, env) + if module_info: + if self.verbose > 1: + LOG.debug("Got infos about module {!r}".format(module_info.full_name)) + elif self.verbose > 2: + LOG.debug("Got module info:\n{}".format(pp(module_info.as_dict()))) + self.env_modules[env].merge(module_info) + else: + LOG.warn(_("Got no module info from directory {!r}.").format(module_dir)) + + # ------------------------------------------------------------------------- + def get_meta_module_info(self, module_dir, env): + + if self.verbose > 1: + LOG.debug("Get module information from {!r}.".format(str(module_dir))) + + if not module_dir.exists(): + LOG.warn(_("Directory {!r} does not exists.").format(str(module_dir))) + return None + + if not module_dir.is_dir(): + LOG.warn(_("Path {!r} is not a directory.").format(str(module_dir))) + return None + + 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 metadata_file.is_file(): + LOG.warn(_("Metadatafile {!r} is not a regular file.").format(str(metadata_file))) + return None + 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(str(metadata_file))) + + json_data = None + try: + 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=str(metadata_file), e=e)) + return None + if not json_data: + LOG.warn(_("Did not found any data in {!r}.").format(str(metadata_file))) + return None + + metadata = ModuleMetadata.from_json_data( + json_data, appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) + if self.verbose > 3: + LOG.debug("ModuleMetadata:\n{}".format(pp(metadata.as_dict()))) + if self.verbose > 2: + LOG.debug("ModuleMetadata: got a {c} object:\n{s}".format( + c=metadata.__class__.__name__, s=metadata)) + + return ModuleInfo.init_from_json( + json_data, env, appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) + # ============================================================================= if __name__ == "__main__": diff --git a/lib/webhooks/get_forge_modules.py b/lib/webhooks/get_forge_modules.py index 934976d..38de3e5 100644 --- a/lib/webhooks/get_forge_modules.py +++ b/lib/webhooks/get_forge_modules.py @@ -189,8 +189,7 @@ class GetForgeModulesApp(BaseHookApp): self.collect_local_modules() if self.do_forge: self.get_forge_information() - - self.forge_modules.write_file() + self.forge_modules.write_file() print() d = datetime.datetime.now(LOCALTZ) @@ -198,19 +197,6 @@ class GetForgeModulesApp(BaseHookApp): msg = _("[{}]: Finished.").format(dt) self.print_out(msg) - # ------------------------------------------------------------------------- - def collect_local_modules(self): - - for env in self.environments: - LOG.debug("Evaluating environment {!r} ..".format(env)) - pfile_modules = self.read_puppetfile(env) - if pfile_modules: - for module_info in pfile_modules.values(): - self.env_modules[env].append(module_info) - self.read_metadata_files(env) - if self.verbose > 2: - LOG.debug("ModuleInfoDict:\n{}".format(pp(self.env_modules[env].as_dict()))) - # ------------------------------------------------------------------------- def get_forge_information(self): @@ -260,74 +246,6 @@ class GetForgeModulesApp(BaseHookApp): return module_info - # ------------------------------------------------------------------------- - def read_metadata_files(self, env): - - LOG.info(_("Reading metadata files of environment {!r} ...").format(env)) - - modules_dir = self.puppet_root_env_dir / env / 'modules' - - for module_dir in modules_dir.glob('*'): - module_info = self.get_meta_module_info(module_dir, env) - if module_info: - if self.verbose > 1: - LOG.debug("Got infos about module {!r}".format(module_info.full_name)) - elif self.verbose > 2: - LOG.debug("Got module info:\n{}".format(pp(module_info.as_dict()))) - self.env_modules[env].merge(module_info) - else: - LOG.warn(_("Got no module info from directory {!r}.").format(module_dir)) - - # ------------------------------------------------------------------------- - def get_meta_module_info(self, module_dir, env): - - if self.verbose > 1: - LOG.debug("Get module information from {!r}.".format(str(module_dir))) - - if not module_dir.exists(): - LOG.warn(_("Directory {!r} does not exists.").format(str(module_dir))) - return None - - if not module_dir.is_dir(): - LOG.warn(_("Path {!r} is not a directory.").format(str(module_dir))) - return None - - 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 metadata_file.is_file(): - LOG.warn(_("Metadatafile {!r} is not a regular file.").format(str(metadata_file))) - return None - 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(str(metadata_file))) - - json_data = None - try: - 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=str(metadata_file), e=e)) - return None - if not json_data: - LOG.warn(_("Did not found any data in {!r}.").format(str(metadata_file))) - return None - - metadata = ModuleMetadata.from_json_data( - json_data, appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) - if self.verbose > 3: - LOG.debug("ModuleMetadata:\n{}".format(pp(metadata.as_dict()))) - if self.verbose > 2: - LOG.debug("ModuleMetadata: got a {c} object:\n{s}".format( - c=metadata.__class__.__name__, s=metadata)) - - return ModuleInfo.init_from_json( - json_data, env, appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) - # =============================================================================