Skip to content
Snippets Groups Projects
post_gen_project.py 8.5 KiB
Newer Older
Philipp S. Sommer's avatar
Philipp S. Sommer committed
import git
import os
import subprocess as spr
import glob


{% if cookiecutter.__include_deployment == "no" %}
os.remove("templates/{{cookiecutter.template_base_name}}/deploymentconfig.yaml")
os.remove("templates/{{cookiecutter.template_base_name}}/deployment.yaml")
os.remove("templates/{{cookiecutter.template_base_name}}/helpers/_builtin_resources.tpl")
os.remove("templates/{{cookiecutter.template_base_name}}/helpers/_pod_template.tpl")
os.remove("templates/{{cookiecutter.template_base_name}}/helpers/_builtin_affinity.tpl")
{% if cookiecutter.__include_build_config == "no" %}
os.remove("templates/{{cookiecutter.template_base_name}}/imagestream.yaml")
{% endif %}
{% else %}
{% if cookiecutter.__deployment_type != "Deployment" %}
os.remove("templates/{{cookiecutter.template_base_name}}/deployment.yaml")
{% endif %}
{% if cookiecutter.__deployment_type != "DeploymentConfig" %}
os.remove("templates/{{cookiecutter.template_base_name}}/deploymentconfig.yaml")
os.remove("templates/{{cookiecutter.template_base_name}}/imagestream.yaml")
{% endif %}
{% endif %}

{% if cookiecutter.__include_build_config == "no" %}
os.remove("templates/{{cookiecutter.template_base_name}}/buildconfig.yaml")
Philipp S. Sommer's avatar
Philipp S. Sommer committed
os.remove(
    "templates/{{cookiecutter.template_base_name}}/helpers/_"
Philipp S. Sommer's avatar
Philipp S. Sommer committed
    "builtin_build_secrets.tpl"
)
Philipp S. Sommer's avatar
Philipp S. Sommer committed
os.remove(
    "templates/{{cookiecutter.template_base_name}}/helpers/_"
    "builtin_build_args.tpl"
)
    "templates/{{cookiecutter.template_base_name}}/helpers/_"
    "builtin_build_resources.tpl"
)
os.remove("templates/{{cookiecutter.template_base_name}}/build-secret.yaml")
os.remove("templates/k8sgitlab-secret.yaml")
{% elif cookiecutter.__include_k8sgitlab_secret == "no" %}
os.remove("templates/k8sgitlab-secret.yaml")
{% endif %}

{% if cookiecutter.__include_service == "no" %}
os.remove("templates/{{cookiecutter.template_base_name}}/service.yaml")
{% endif %}

{% if cookiecutter.__include_route == "no" %}
os.remove("templates/{{cookiecutter.template_base_name}}/route.yaml")
{% endif %}

{% if cookiecutter.__include_volume == "no" %}
os.remove(
    "templates/{{cookiecutter.template_base_name}}/persistentvolumeclaim.yaml"
)
os.remove(
    "templates/{{cookiecutter.template_base_name}}/persistentvolumeclaims.yaml"
)
os.remove(
    "templates/{{cookiecutter.template_base_name}}/helpers/_builtin_volumes.tpl"
)
{% elif cookiecutter.__include_volume == "one" %}
os.remove(
    "templates/{{cookiecutter.template_base_name}}/persistentvolumeclaims.yaml"
)
os.remove(
    "templates/{{cookiecutter.template_base_name}}/helpers/_builtin_volumes.tpl"
)
{% elif cookiecutter.__include_volume == "many" %}
os.remove(
    "templates/{{cookiecutter.template_base_name}}/persistentvolumeclaim.yaml"
)
{% endif %}

{% if cookiecutter.__include_secret == "no" %}
os.remove("templates/{{cookiecutter.template_base_name}}/secret.yaml")
os.remove("templates/{{cookiecutter.template_base_name}}/helpers/_builtin_secrets.tpl")
{% endif %}

{% if cookiecutter.__include_configmap == "no" %}
os.remove("templates/{{cookiecutter.template_base_name}}/configmap.yaml")
os.remove("templates/{{cookiecutter.template_base_name}}/helpers/_builtin_configmap.tpl")
{% if cookiecutter.use_reuse == "yes" %}
Philipp S. Sommer's avatar
Philipp S. Sommer committed

spr.check_call(
    [
        "reuse",
        "annotate",
        "--year",
        "{{ cookiecutter.copyright_year }}",
        "--license",
        "{{ cookiecutter.documentation_license }}",
        "--copyright",
        "{{ cookiecutter.copyright_holder }}",
        "--recursive",
        "README.md",
    ]
)

spr.check_call(
    [
        "reuse",
        "annotate",
        "--year",
        "{{ cookiecutter.copyright_year }}",
        "--license",
        "{{ cookiecutter.supplementary_files_license }}",
        "--copyright",
        "{{ cookiecutter.copyright_holder }}",
        "--recursive",
        ".gitignore",
        ".pre-commit-config.yaml",
        "Chart.yaml",
        "values.yaml",
        "linter_values.yaml",
        ".gitlab-ci.yml",
    ]
)

spr.check_call(
    [
        "reuse",
        "annotate",
        "--year",
        "{{ cookiecutter.copyright_year }}",
        "--license",
        "{{ cookiecutter.supplementary_files_license }}",
        "--copyright",
        "{{ cookiecutter.copyright_holder }}",
        "--recursive",
        "--style",
        "python",
        ".helmignore",
    ]
)

spr.check_call(
    [
        "reuse",
        "annotate",
        "--year",
        "{{ cookiecutter.copyright_year }}",
        "--license",
        "{{ cookiecutter.templates_license }}",
        "--copyright",
        "{{ cookiecutter.copyright_holder }}",
        "--recursive",
        "--force-dot-license",
        "templates/NOTES.txt",
    ] + glob.glob("templates/{{cookiecutter.template_base_name}}/*/*.tpl")
Philipp S. Sommer's avatar
Philipp S. Sommer committed
)

spr.check_call(
    [
        "reuse",
        "annotate",
        "--year",
        "{{ cookiecutter.copyright_year }}",
        "--license",
        "{{ cookiecutter.templates_license }}",
        "--copyright",
        "{{ cookiecutter.copyright_holder }}",
        "--recursive",
        "templates",
    ]
)

spr.check_call(
    [
        "reuse",
        "annotate",
        "--year",
        "{{ cookiecutter.copyright_year }}",
        "--license",
        "Unlicense",
        "--copyright",
        "{{ cookiecutter.copyright_holder }}",
        "scripts/print_version.py",
    ]
)

spr.check_call(["reuse", "download", "--all"])

{% endif %}

print("Fixing files for project at {{cookiecutter.project_slug}}")
repo = git.Repo.init(".", mkdir=False)
{% if cookiecutter.git_remote_protocoll == "ssh" %}
repo.create_remote(
    "origin",
    "git@{{ cookiecutter.gitlab_host }}:{{ cookiecutter.gitlab_username }}/{{ cookiecutter.project_slug }}.git",
)
{% else %}
repo.create_remote(
    "origin",
    "https://{{ cookiecutter.gitlab_host }}/{{ cookiecutter.gitlab_username }}/{{ cookiecutter.project_slug }}.git",
)
{% endif %}
repo.git.add(".")


spr.check_call(["pre-commit", "install"])

# make a silent run first to fix the files
spr.call(["pre-commit", "run"], stdout=spr.DEVNULL)

# add the files again after pre-commit hook has been run
repo.git.add(".")

spr.check_call(["pre-commit", "run"])

for dirpath, dirnames, filenames in os.walk("."):
    if "__pycache__" in dirnames:
        shutil.rmtree(os.path.join(dirpath, "__pycache__"))

print("""
===============================================================================
                          Contratulations!
===============================================================================
You just created a new helm chart for deployments in openshift.

{% if cookiecutter.use_reuse == "yes" -%}
Philipp S. Sommer's avatar
Philipp S. Sommer committed
+-------------------------------------------------------------+
| IMPORTANT NOTE:                                             |
|                                                             |
|    If you used cruft to create this package, please now run |
|    the following commands to stage the .cruft.json file and |
|    assign it the correct license:                           |
|                                                             |
+-------------------------------------------------------------+

1. Set the correct license information:

Philipp S. Sommer's avatar
Philipp S. Sommer committed
   $ reuse --root {{ cookiecutter.project_slug }} annotate --year {{ cookiecutter.copyright_year }} --license {{ cookiecutter.supplementary_files_license }} --copyright "{{ cookiecutter.copyright_holder }}" {{ cookiecutter.project_slug }}/.cruft.json
Philipp S. Sommer's avatar
Philipp S. Sommer committed

2. Stage the file:

   $ git -C {{ cookiecutter.project_slug }} add .cruft.json .cruft.json.license

3. Make your first commit via

   $ git -C {{ cookiecutter.project_slug }} commit -m "Initial commit"

4. Make sure that the repository at https://{{ cookiecutter.gitlab_host }}/{{ cookiecutter.gitlab_username }}/{{ cookiecutter.project_slug }}
   exists and bring your project on the web with

   $ git -C {{ cookiecutter.project_slug }} push origin main
{% else %}
+-------------------------------------------------------------+
| IMPORTANT NOTE:                                             |
|                                                             |
|    You can now push everything to gitlab!                   |
|                                                             |
+-------------------------------------------------------------+

1. Make your first commit via

   $ git -C {{ cookiecutter.project_slug }} commit -m "Initial commit"

2. Make sure that the repository at https://{{ cookiecutter.gitlab_host }}/{{ cookiecutter.gitlab_username }}/{{ cookiecutter.project_slug }}
   exists and bring your project on the web with

   $ git -C {{ cookiecutter.project_slug }} push origin main
{% endif -%}
""")