Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

WIP DNM goneri #24

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 26 additions & 33 deletions gouttelette/cmd/refresh_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +21,6 @@
get_module_added_ins,
get_module_from_config,
python_type,
get_generator,
camel_to_snake,
ignore_description,
)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -1222,18 +1222,16 @@ 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


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())
Expand Down Expand Up @@ -1283,54 +1281,49 @@ 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")

generator_coll = re.sub("(.*)_code_generator", r"\1", generator["name"])
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",
type=str,
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()
func = "generate_" + generator_coll + "(args)"
eval(func)

info = VersionInfo(generator["name"])
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,
}[gouttelette_config["generator"]]
func(args)
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"
Expand All @@ -1341,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"
)
)

Expand Down
29 changes: 8 additions & 21 deletions gouttelette/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -90,7 +77,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):
Expand Down