-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadme_builder.py
executable file
·90 lines (76 loc) · 2.75 KB
/
readme_builder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from jinja2 import Environment, FileSystemLoader, ChoiceLoader, select_autoescape
import yaml
import os
from pathlib import Path
subtemplates = [
"description",
"documentation",
"quick_start",
"pull_containers",
"copy_packages",
"tags",
"funding"
]
template_paths = []
# Use explicit user setting *or* default repo directory for templates.
# These get called first, allowing repos to override action templates.
TEMPLATE_DIR = os.environ.get("TEMPLATE_DIRECTORY", False)
if TEMPLATE_DIR and len(TEMPLATE_DIR) > 0:
template_paths.append(FileSystemLoader(TEMPLATE_DIR))
else:
WORKSPACE_DIR = os.environ.get("GITHUB_WORKSPACE", False)
if WORKSPACE_DIR:
workspace_template_path = Path(WORKSPACE_DIR, 'templates')
if workspace_template_path.exists():
template_paths.append(FileSystemLoader(str(workspace_template_path)))
# Add the action templates as the final templates to check.
SCRIPT_DIR=os.path.dirname(os.path.realpath(__file__))
template_paths.append(FileSystemLoader(f"{SCRIPT_DIR}/templates/"))
env = Environment(
loader=ChoiceLoader(template_paths),
autoescape=select_autoescape()
)
def get_key_value(obj, key):
if isinstance(obj, dict):
if key in obj:
return obj[key]
for k, v in obj.items():
test_value = get_key_value(v, key)
if test_value:
return test_value
if isinstance(obj, list):
for v in obj:
test_value = get_key_value(v, key)
if test_value:
return test_value
platform = "linux/amd64,linux/arm64,linux/arm/v7"
variants = ["full", "slim", "alpine"]
if os.environ.get('BUILDER_WORKFLOW_PATH', False):
with open(os.environ['BUILDER_WORKFLOW_PATH'], "r") as stream:
try:
build_spec = yaml.safe_load(stream)
platform_guess = get_key_value(build_spec, "platform")
if platform_guess:
platform = platform_guess
strategy = get_key_value(build_spec, "strategy")
if strategy and 'matrix' in strategy:
if 'target_base' in strategy['matrix']:
variants = strategy['matrix']['target_base']
except yaml.YAMLError as exc:
print(exc)
variables = {
"package": os.environ["PACKAGE"],
"project_name": os.environ["PROJECT_NAME"],
"package_versions": os.environ["PACKAGE_VERSIONS"].split(),
"python_versions": os.environ["PYTHON_VERSIONS"].split(),
"organization": os.environ["ORGANIZATION"],
"repository": os.environ["REPOSITORY"],
"repository_short": os.environ["REPOSITORY_SHORT"],
"platforms": platform.split(','),
"variants": variants
}
for template_var in subtemplates:
subtemplate = env.get_template(f"{template_var}.md")
variables[template_var] = subtemplate.render(**variables)
readme_template = env.get_template("README.md")
print(readme_template.render(**variables))