blob: 49667ae74d4e42a9a9465d34986dbf81abc51c4d [file] [log] [blame]
#!/usr/bin/env python3
#
# SPDX-FileCopyrightText: © 2020 Open Networking Foundation <support@opennetworking.org>
# SPDX-License-Identifier: Apache-2.0
# role/hooks/post_gen_project.py
# Generates platform-specific files for ansible roles
from __future__ import absolute_import
import os
import jinja2
from cookiecutter import generate as ccg
from cookiecutter.environment import StrictEnvironment
import cookiecutter.utils as utils
# Docs for hooks
# https://cookiecutter.readthedocs.io/en/latest/advanced/hooks.html
# Tickets related to what thei above
# https://github.com/cookiecutter/cookiecutter/issues/474
# https://github.com/cookiecutter/cookiecutter/issues/851
# other implementations
# https://github.com/ckan/ckan/blob/master/contrib/cookiecutter/ckan_extension/hooks/post_gen_project.py
# CWD is output dir
PROJECT_DIR = os.path.realpath(os.path.join("..", os.path.curdir))
# Hack, but 'cookiecutter._template' is a relative path
TEMPLATE_DIR = os.path.realpath(
os.path.join(os.path.curdir, "../{{ cookiecutter._template }}")
)
# script is rendered as a template, so this will be filled in with the
# cookiecutter dict, which is why noqa is needed.
CONTEXT = {{cookiecutter | jsonify}} # noqa: F821, E227
def delete_file(filepath):
""" delete generated file from output directory """
os.remove(os.path.join(PROJECT_DIR, filepath))
def generate_platforms():
""" Generate files specific to each platform in platforms dict """
# list all platform tmplated files here
# {% raw %}
platform_files = [
"{{cookiecutter.role_name}}/tasks/{{cookiecutter.platform}}.yml",
"{{cookiecutter.role_name}}/vars/{{cookiecutter.platform}}.yml",
]
# {% endraw %}
# delete the unneded file generated by platform_files earlier
env = StrictEnvironment(context={"cookiecutter": CONTEXT})
for infile in platform_files:
outfile_tmpl = env.from_string(infile)
delete_file(outfile_tmpl.render({"cookiecutter": CONTEXT}))
platforms = list(CONTEXT["platforms"].keys())
# Combine Ubuntu and Debian as they're the same ansible_os_family
if "Ubuntu" in platforms and "Debian" not in platforms:
platforms.remove("Ubuntu")
platforms.append("Debian")
# Iterate over platforms creating files
for platform in platforms:
# assign platform name to cookiecutter.platform
CONTEXT.update({"platform": platform})
# build jinja2 environment
env = StrictEnvironment(
context={"cookiecutter": CONTEXT}, keep_trailing_newline=True,
)
# must be in template dir for generate_file to work
with utils.work_in(TEMPLATE_DIR):
env.loader = jinja2.FileSystemLoader(".")
for infile in platform_files:
ccg.generate_file(
project_dir=PROJECT_DIR,
infile=infile,
context={"cookiecutter": CONTEXT},
env=env,
)
def delete_inactive_licenses():
# get list of licenses written to output
license_dir = os.path.join(os.path.curdir, "LICENSES")
license_files = os.listdir(license_dir)
# delete any files that don't start with the license identifier
for licfile in license_files:
if not licfile.startswith(CONTEXT["license"]):
os.remove(os.path.join(os.path.curdir, "LICENSES", licfile))
if __name__ == "__main__":
generate_platforms()
delete_inactive_licenses()