From 62a332cc33a70afeea7d379c4fb4a68a0dead201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A9ri=20Le=20Bouder?= Date: Wed, 21 Dec 2022 09:46:16 -0500 Subject: [PATCH 1/3] utils.get_module_from_config(): store the config in gouttelette directory Store the `modules.yaml` in a dedicated `gouttelette` directory from the collection directory. --- gouttelette/utils.py | 2 +- tests/unit/cmd/fixtures/{ => gouttelette}/modules.yaml | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename tests/unit/cmd/fixtures/{ => gouttelette}/modules.yaml (100%) diff --git a/gouttelette/utils.py b/gouttelette/utils.py index 2ffb87f..993f4d0 100644 --- a/gouttelette/utils.py +++ b/gouttelette/utils.py @@ -90,7 +90,7 @@ def indent(text_block: str, indent: int = 0) -> str: def get_module_from_config(module: str, target_dir: Path) -> dict[str, Any]: - module_file = target_dir / "modules.yaml" + module_file = target_dir / "gouttelette" / "modules.yaml" raw_content = module_file.read_text() for i in yaml.safe_load(raw_content): diff --git a/tests/unit/cmd/fixtures/modules.yaml b/tests/unit/cmd/fixtures/gouttelette/modules.yaml similarity index 100% rename from tests/unit/cmd/fixtures/modules.yaml rename to tests/unit/cmd/fixtures/gouttelette/modules.yaml From 5b6082c351110a22a21d19950c083760a89b1120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A9ri=20Le=20Bouder?= Date: Thu, 22 Dec 2022 11:40:30 -0500 Subject: [PATCH 2/3] refresh_modules.py: avoid the eval() call --- gouttelette/cmd/refresh_modules.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/gouttelette/cmd/refresh_modules.py b/gouttelette/cmd/refresh_modules.py index 3ac2c82..46aac79 100644 --- a/gouttelette/cmd/refresh_modules.py +++ b/gouttelette/cmd/refresh_modules.py @@ -1294,7 +1294,6 @@ def main(): if not generator: raise Exception("gouttelette.yaml is missing generator value") - generator_coll = re.sub("(.*)_code_generator", r"\1", generator["name"]) parser = argparse.ArgumentParser( description=f"Build the {generator['name']} modules." ) @@ -1320,8 +1319,12 @@ def main(): help="location where to store the collected schemas (default: ./gouttelette/api_specifications/amazon_cloud)", ) args = parser.parse_args() - func = "generate_" + generator_coll + "(args)" - eval(func) + + func = { + "vmware_rest_code_generator": generate_vmware_rest, + "amazon_cloud_code_generator": generate_amazon_cloud, + }[generator["name"]] + func(args) info = VersionInfo(generator["name"]) dev_md = args.target_dir / "dev.md" From 46666ebdc94eb9da4bae64b6a016ce74d495a644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A9ri=20Le=20Bouder?= Date: Wed, 21 Dec 2022 11:30:54 -0500 Subject: [PATCH 3/3] load templates from gouttelette/templates in the target directory --- gouttelette/cmd/refresh_modules.py | 54 ++++++++++++------------------ gouttelette/utils.py | 27 ++++----------- 2 files changed, 29 insertions(+), 52 deletions(-) diff --git a/gouttelette/cmd/refresh_modules.py b/gouttelette/cmd/refresh_modules.py index 46aac79..38cfdb4 100644 --- a/gouttelette/cmd/refresh_modules.py +++ b/gouttelette/cmd/refresh_modules.py @@ -7,7 +7,6 @@ import pathlib import re import shutil -import pkg_resources from pbr.version import VersionInfo from .content_library_data import content_library_static_ds import yaml @@ -22,7 +21,6 @@ get_module_added_ins, get_module_from_config, python_type, - get_generator, camel_to_snake, ignore_description, ) @@ -195,7 +193,7 @@ def gen_documentation( parameters: List, added_ins: Dict, next_version: str, - target_dir: str, + target_dir: pathlib.Path, ) -> Dict: short_description = description.split(". ")[0] @@ -568,6 +566,7 @@ def renderer(self, target_dir: str, next_version: str): content = jinja2_renderer( self.template_file, + target_dir=target_dir, arguments=indent(arguments, 4), documentation=documentation_to_string, name=self.name, @@ -947,6 +946,7 @@ def renderer(self, target_dir: str, next_version: str): content = jinja2_renderer( self.template_file, + target_dir=target_dir, arguments=indent(arguments, 4), documentation=documentation, list_index=self.list_index(), @@ -1222,10 +1222,6 @@ def generate_amazon_cloud(args: Iterable): with open(runtime_file, "w") as file: yaml.safe_dump(yaml_dict, file, sort_keys=False) - collection_dir = pkg_resources.resource_filename("gouttelette", "data") - print(f"Copying files from {collection_dir}") - shutil.copytree(collection_dir, args.target_dir, dirs_exist_ok=True) - return @@ -1233,7 +1229,9 @@ def generate_vmware_rest(args: Iterable): module_list = [] for json_file in ["vcenter.json", "content.json", "appliance.json"]: print("Generating modules from {}".format(json_file)) - api_spec_file = args.target_dir / "api_specifications" / "7.0.2" / json_file + api_spec_file = ( + args.target_dir / "gouttelette" / "api_specifications" / "7.0.2" / json_file + ) raw_content = api_spec_file.read_text() swagger_file = SwaggerFile(raw_content) resources = swagger_file.init_resources(swagger_file.paths.values()) @@ -1283,27 +1281,18 @@ def generate_vmware_rest(args: Iterable): module_utils_dir = args.target_dir / "plugins" / "module_utils" module_utils_dir.mkdir(exist_ok=True) vmware_rest_dest = module_utils_dir / "vmware_rest.py" - vmware_rest_dest.write_bytes( - pkg_resources.resource_string("gouttelette", "module_utils/vmware_rest.py") - ) return def main(): - generator = get_generator() - if not generator: - raise Exception("gouttelette.yaml is missing generator value") - - parser = argparse.ArgumentParser( - description=f"Build the {generator['name']} modules." - ) + parser = argparse.ArgumentParser(description=f"Build the modules.") parser.add_argument( "--target-dir", dest="target_dir", type=pathlib.Path, - default=pathlib.Path(generator["default_path"]), - help=f"location of the target repository (default: {generator['default_path']})", + default=pathlib.Path("."), + help=f"location of the target repository (default: .)", ) parser.add_argument( "--next-version", @@ -1311,29 +1300,30 @@ def main(): default="TODO", help="the next major version", ) - if generator.get("name") == "amazon_cloud_code_generator": - parser.add_argument( - "--schema-dir", - type=pathlib.Path, - default=pathlib.Path("gouttelette/api_specifications/"), - help="location where to store the collected schemas (default: ./gouttelette/api_specifications/amazon_cloud)", - ) + parser.add_argument( + "--schema-dir", + type=pathlib.Path, + default=pathlib.Path("gouttelette/api_specifications/"), + help="location where to store the collected schemas (default: ./gouttelette/api_specifications/)", + ) args = parser.parse_args() + gouttelette_config = yaml.safe_load( + (args.target_dir / "gouttelette.yml").read_text() + ) + print(gouttelette_config["generator"]) func = { "vmware_rest_code_generator": generate_vmware_rest, "amazon_cloud_code_generator": generate_amazon_cloud, - }[generator["name"]] + }[gouttelette_config["generator"]] func(args) - - info = VersionInfo(generator["name"]) dev_md = args.target_dir / "dev.md" dev_md.write_text( ( "The modules are autogenerated by:\n" "https://github.com/ansible-collections/gouttelette\n" "" - f"version: {info.version_string()}\n" + f"version: TODO\n" ) ) dev_md = args.target_dir / "commit_message" @@ -1344,7 +1334,7 @@ def main(): "The modules are autogenerated by:\n" "https://github.com/ansible-collections/gouttelette\n" "" - f"version: {info.version_string()}\n" + f"version: TODO\n" ) ) diff --git a/gouttelette/utils.py b/gouttelette/utils.py index 993f4d0..d461a64 100644 --- a/gouttelette/utils.py +++ b/gouttelette/utils.py @@ -14,31 +14,18 @@ from functools import lru_cache -def jinja2_renderer(template_file: str, **kwargs: Dict[str, Any]) -> str: +def jinja2_renderer( + template_file: str, target_dir: Path, **kwargs: Dict[str, Any] +) -> str: - template_path = re.sub("(.*)_code_generator", r"\1", get_generator()["name"]) - templateLoader = jinja2.FileSystemLoader("gouttelette") - templateEnv = jinja2.Environment(loader=templateLoader) - template = templateEnv.get_template( - "templates/" + template_path + "/" + template_file + templateLoader = jinja2.FileSystemLoader( + str(target_dir / "gouttelette" / "templates") ) + templateEnv = jinja2.Environment(loader=templateLoader) + template = templateEnv.get_template(template_file) return template.render(kwargs) -def get_generator() -> Dict[str, Any]: - generator = {} - with open("gouttelette.yml", "r") as file: - try: - generator.update({"name": yaml.safe_load(file)["generator"]}) - if "amazon_cloud_code_generator" in generator["name"]: - generator.update({"default_path": "cloud"}) - elif "vmware_rest_code_generator" in generator["name"]: - generator.update({"default_path": "vmware_rest"}) - except yaml.YAMLError as exc: - print(exc) - return generator - - def format_documentation(documentation: Any) -> str: yaml.Dumper.ignore_aliases = lambda *args: True # type: ignore