Zack Williams | ad45bf0 | 2020-03-04 21:37:20 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # SPDX-FileCopyrightText: © 2020 Open Networking Foundation <support@opennetworking.org> |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | |
| 6 | # role/hooks/post_gen_project.py |
| 7 | # Generates platform-specific files for ansible roles |
| 8 | |
| 9 | from __future__ import absolute_import |
| 10 | import os |
| 11 | import jinja2 |
| 12 | |
| 13 | from cookiecutter import generate as ccg |
| 14 | from cookiecutter.environment import StrictEnvironment |
| 15 | import cookiecutter.utils as utils |
| 16 | |
| 17 | # Docs for hooks |
| 18 | # https://cookiecutter.readthedocs.io/en/latest/advanced/hooks.html |
| 19 | |
| 20 | # Tickets related to what thei above |
| 21 | # https://github.com/cookiecutter/cookiecutter/issues/474 |
| 22 | # https://github.com/cookiecutter/cookiecutter/issues/851 |
| 23 | |
| 24 | # other implementations |
| 25 | # https://github.com/ckan/ckan/blob/master/contrib/cookiecutter/ckan_extension/hooks/post_gen_project.py |
| 26 | |
| 27 | # CWD is output dir |
| 28 | PROJECT_DIR = os.path.realpath(os.path.join("..", os.path.curdir)) |
| 29 | |
| 30 | # Hack, but 'cookiecutter._template' is a relative path |
| 31 | TEMPLATE_DIR = os.path.realpath( |
| 32 | os.path.join(os.path.curdir, "../{{ cookiecutter._template }}") |
| 33 | ) |
| 34 | |
| 35 | # script is rendered as a template, so this will be filled in with the |
| 36 | # cookiecutter dict, which is why noqa is needed. |
| 37 | CONTEXT = {{cookiecutter | jsonify}} # noqa: F821, E227 |
| 38 | |
| 39 | |
| 40 | def delete_file(filepath): |
| 41 | """ delete generated file from output directory """ |
| 42 | os.remove(os.path.join(PROJECT_DIR, filepath)) |
| 43 | |
| 44 | |
| 45 | def generate_platforms(): |
| 46 | """ Generate files specific to each platform in platforms dict """ |
| 47 | |
| 48 | # list all platform tmplated files here |
| 49 | # {% raw %} |
| 50 | platform_files = [ |
| 51 | "{{cookiecutter.role_name}}/tasks/{{cookiecutter.platform}}.yml", |
| 52 | "{{cookiecutter.role_name}}/vars/{{cookiecutter.platform}}.yml", |
| 53 | ] |
| 54 | # {% endraw %} |
| 55 | |
| 56 | # delete the unneded file generated by platform_files earlier |
| 57 | env = StrictEnvironment(context={"cookiecutter": CONTEXT}) |
| 58 | for infile in platform_files: |
| 59 | outfile_tmpl = env.from_string(infile) |
| 60 | delete_file(outfile_tmpl.render({"cookiecutter": CONTEXT})) |
| 61 | |
| 62 | platforms = list(CONTEXT["platforms"].keys()) |
| 63 | |
| 64 | # Combine Ubuntu and Debian as they're the same ansible_os_family |
| 65 | if "Ubuntu" in platforms and "Debian" not in platforms: |
| 66 | platforms.remove("Ubuntu") |
| 67 | platforms.append("Debian") |
| 68 | |
| 69 | # Iterate over platforms creating files |
| 70 | for platform in platforms: |
| 71 | |
| 72 | # assign platform name to cookiecutter.platform |
| 73 | CONTEXT.update({"platform": platform}) |
| 74 | |
| 75 | # build jinja2 environment |
| 76 | env = StrictEnvironment( |
| 77 | context={"cookiecutter": CONTEXT}, keep_trailing_newline=True, |
| 78 | ) |
| 79 | |
| 80 | # must be in template dir for generate_file to work |
| 81 | with utils.work_in(TEMPLATE_DIR): |
| 82 | env.loader = jinja2.FileSystemLoader(".") |
| 83 | |
| 84 | for infile in platform_files: |
| 85 | ccg.generate_file( |
| 86 | project_dir=PROJECT_DIR, |
| 87 | infile=infile, |
| 88 | context={"cookiecutter": CONTEXT}, |
| 89 | env=env, |
| 90 | ) |
| 91 | |
| 92 | |
| 93 | def delete_inactive_licenses(): |
| 94 | |
| 95 | # get list of licenses written to output |
| 96 | license_dir = os.path.join(os.path.curdir, "LICENSES") |
| 97 | license_files = os.listdir(license_dir) |
| 98 | |
| 99 | # delete any files that don't start with the license identifier |
| 100 | for licfile in license_files: |
| 101 | if not licfile.startswith(CONTEXT["license"]): |
| 102 | os.remove(os.path.join(os.path.curdir, "LICENSES", licfile)) |
| 103 | |
| 104 | |
| 105 | if __name__ == "__main__": |
| 106 | |
| 107 | generate_platforms() |
| 108 | delete_inactive_licenses() |