Commit 4a04be60 authored by FulcrandG's avatar FulcrandG
Browse files

Using jinja templates instead of wrappers in builder

parent 6681d496
Loading
Loading
Loading
Loading
+108 −107
Original line number Diff line number Diff line
#!/usr/bin/env python3

# This script is used to build the documentation that hub.go2scale.com will be using for every job added to the hub
# This script is used to build the documentation that
# hub.go2scale.com will be using for every job added to the hub

# Directory skeleton for a job:
# ── jobs
@@ -13,31 +14,30 @@
#             ├── 0.1.0.md
#             └──...

from pathlib import Path
from os import listdir
from yaml import full_load
from jinja2 import Environment, FileSystemLoader
import requests

# Job variables
jobs_dir = "jobs"
mkdocs_dir = "docs"
mkdocs_placeholder_file = "placeholder.md"
job_changelog_dir = "versions"
job_description_file = "README.md"
job_license_file = "LICENSE"
job_metadata_file = "job.yml"
JOBS_DIR = "jobs"
MKDOCS_DIR = "docs"
MKDOCS_PLACEHOLDER_FILE = "placeholder.md"
JOB_CHANGELOG_DIR = "versions"
JOB_DESCRIPTION_FILE = "README.md"
JOB_LICENSE_FILE = "LICENSE"
JOB_METADATA_FILE = "job.yml"

gitlab_api_url = "https://gitlab.com/api/v4/"
go2scale_url = "https://hub.go2scale.io/"
GITLAB_API_URL = "https://gitlab.com/api/v4/"
GO2SCALE_URL = "https://hub.go2scale.io/"

# Templates variables
builder_dir = "builder"
template_dir = "templates"
template_index = "index.md.j2"
template_doc = "job_documentation.md.j2"
template_placeholder = "placeholder.md.j2"
index_file = "index.md"
BUILDER_DIR = "builder"
TEMPLATE_DIR = "templates"
TMEPLATE_INDEX = "index.md.j2"
TEMPLATE_DOC = "job_documentation.md.j2"
TEMPLATE_PLACEHOLDER = "placeholder.md.j2"
INDEX_FILE = "index.md"
index = {
    "static_tests": [],
    "build": [],
@@ -46,38 +46,38 @@ index = {
    "deployment": []
}

def get_conf(job_path, job_name):
def get_conf(job_path):
    # Load yaml file
  with open(job_path + "/" + job_metadata_file) as file:
    return full_load(file)
    with open(job_path + "/" + JOB_METADATA_FILE) as conf_file:
        return full_load(conf_file)

def get_description(job_path, job_name):
def get_description(job_path):
    # Concatenate description to final file
  with open(job_path + "/" + job_description_file) as file:
    return file.read()
    with open(job_path + "/" + JOB_DESCRIPTION_FILE) as readme_file:
        return readme_file.read()

def get_changelogs(job_path, job_name):
    latest = {
    "version": listdir(job_path + "/" + job_changelog_dir)[0][0:-3],
    "url": gitlab_api_url + job_name + ".yml"
      "version": listdir(job_path + "/" + JOB_CHANGELOG_DIR)[0][0:-3],
      "url": GO2SCALE_URL + job_name + ".yml"
    }

    changelogs = []
  for version in listdir(job_path + "/" + job_changelog_dir):
    with open(job_path + "/" + job_changelog_dir + "/" + version) as file:
    for version in listdir(job_path + "/" + JOB_CHANGELOG_DIR):
        with open(job_path + "/" + JOB_CHANGELOG_DIR + "/" + version) as changelog_file:
            changelogs.append({
                "version": version[0:-3],
        "url": gitlab_api_url + version[0:-3] + "/" + job_name + ".yml",
        "changelog": file.readlines()
                "url": GO2SCALE_URL + version[0:-3] + "/" + job_name + ".yml",
                "changelog": changelog_file.readlines()
            })
    return (latest, changelogs)

def get_license(job_path, job_name):
  with open(job_path + "/" + job_license_file) as file:
    return file.readlines()
def get_license(job_path):
    with open(job_path + "/" + JOB_LICENSE_FILE) as license_file:
        return license_file.readlines()

def get_user(job_path, job, code_owner):
  url = gitlab_api_url + "users?username=" + code_owner
def get_user(code_owner):
    url = GITLAB_API_URL + "users?username=" + code_owner

    response = requests.request("GET", url)

@@ -86,28 +86,28 @@ def get_user(job_path, job, code_owner):
    return None

def create_job_doc(job):
  job_path = jobs_dir + "/" + job
    job_path = JOBS_DIR + "/" + job

    # Getting conf for indexing
  conf = get_conf(job_path, job)
    conf = get_conf(job_path)
    code_owner = conf.get("code-owner")
    index[conf["default_stage"]].append(conf)

  mkdocs_file_path = mkdocs_dir + "/" + jobs_dir + "/" + conf["default_stage"] + "/" + job + ".md"
    mkdocs_file_path = MKDOCS_DIR + "/" + JOBS_DIR + "/" + conf["default_stage"] + "/" + job + ".md"

    # Get variables for jinja
  description = get_description(job_path, job)
    description = get_description(job_path)
    latest, changelogs = get_changelogs(job_path, job)
  license = get_license(job_path, job)
  user = get_user(job_path, job, conf["code-owner"])
    license_content = get_license(job_path)
    user = get_user(conf["code-owner"])

    # Write final file
  with open(mkdocs_file_path, 'w+') as file:
    env = Environment(loader=FileSystemLoader(builder_dir + "/" + template_dir))
    template = env.get_template(template_doc)
    file.write(template.render(
    with open(mkdocs_file_path, 'w+') as doc_file:
        env = Environment(loader=FileSystemLoader(BUILDER_DIR + "/" + TEMPLATE_DIR))
        template = env.get_template(TEMPLATE_DOC)
        doc_file.write(template.render(
            readme = description,
      license = license,
            license = license_content,
            latest = latest,
            changelogs = changelogs,
            gitlab_image = user["avatar_url"],
@@ -118,17 +118,18 @@ def create_job_doc(job):

def add_placeholder():
    # Verify that there is a .md file for every stage, or mkdocs will break
  for stage in index.keys():
    if len(listdir(mkdocs_dir + "/" + jobs_dir + "/" + stage)) == 1:
    for stage_key, _ in index.items():
        placeholder_path = MKDOCS_DIR + "/" + JOBS_DIR + "/" + stage_key
        if len(listdir(placeholder_path)) == 1:
            # There is only the .pages file, so mkdocs will break
      with open(mkdocs_dir + "/" + jobs_dir + "/" + stage + "/" + mkdocs_placeholder_file, "w+") as file:
        env = Environment(loader=FileSystemLoader(builder_dir + "/" + template_dir))
        template = env.get_template(template_placeholder)
        file.write(template.render())
            with open(placeholder_path + "/" + MKDOCS_PLACEHOLDER_FILE, "w+") as file_handle:
                env = Environment(loader=FileSystemLoader(BUILDER_DIR + "/" + TEMPLATE_DIR))
                template = env.get_template(TEMPLATE_PLACEHOLDER)
                file_handle.write(template.render())

if __name__ == "__main__":
    # Iterate over every directories in jobs directory to create their job.md for the documentation
  jobs = listdir(jobs_dir)
    jobs = listdir(JOBS_DIR)

    for job in jobs:
        create_job_doc(job)
@@ -137,9 +138,9 @@ if __name__ == "__main__":
    add_placeholder()

    # Using jinja2 with a template to create the index
  env = Environment(loader=FileSystemLoader(builder_dir + "/" + template_dir))
  template = env.get_template(template_index)
    env = Environment(loader=FileSystemLoader(BUILDER_DIR + "/" + TEMPLATE_DIR))
    template = env.get_template(TMEPLATE_INDEX)
    index_content = template.render(index=index)

  with open(mkdocs_dir + "/" + jobs_dir + "/" + index_file, "w+") as file:
    file.write(index_content)
    with open(MKDOCS_DIR + "/" + JOBS_DIR + "/" + INDEX_FILE, "w+") as index_file:
        index_file.write(index_content)