1
0
Fork 0
ynh-apps_tools/README-generator/make_readme.py

68 lines
2.1 KiB
Python
Raw Normal View History

#! /usr/bin/env python3
2021-05-10 17:23:40 +02:00
2021-04-28 20:11:42 +02:00
import argparse
2021-04-23 20:01:40 +02:00
import json
import os
from pathlib import Path
2021-04-23 20:01:40 +02:00
2021-04-28 20:11:42 +02:00
from jinja2 import Environment, FileSystemLoader
2021-04-23 20:01:40 +02:00
def generate_READMEs(app_path: str):
app_path = Path(app_path)
2021-04-23 20:01:40 +02:00
if not app_path.exists():
raise Exception("App path provided doesn't exists ?!")
2021-04-23 20:01:40 +02:00
manifest = json.load(open(app_path / "manifest.json"))
2021-05-21 20:12:00 +02:00
upstream = manifest.get("upstream", {})
if not upstream and not (app_path / "doc" / "DISCLAIMER.md").exists():
print(
"There's no 'upstream' key in the manifest, and doc/DISCLAIMER.md doesn't exists - therefore assuming that we shall not auto-update the README.md for this app yet."
)
2021-05-21 20:12:00 +02:00
return
env = Environment(loader=FileSystemLoader(Path(__file__).parent / "templates"))
2021-04-23 20:01:40 +02:00
for lang, lang_suffix in [("en", ""), ("fr", "_fr")]:
2021-04-23 20:01:40 +02:00
template = env.get_template(f"README{lang_suffix}.md.j2")
2021-04-23 20:01:40 +02:00
if (app_path / "doc" / "screenshots").exists():
screenshots = os.listdir(os.path.join(app_path, "doc", "screenshots"))
if ".gitkeep" in screenshots:
screenshots.remove(".gitkeep")
else:
screenshots = []
2021-04-23 20:01:40 +02:00
if (app_path / "doc" / f"DISCLAIMER{lang_suffix}.md").exists():
disclaimer = (app_path / "doc" / f"DISCLAIMER{lang_suffix}.md").read_text()
# Fallback to english if maintainer too lazy to translate the disclaimer idk
elif (app_path / "doc" / "DISCLAIMER.md").exists():
disclaimer = (app_path / "doc" / "DISCLAIMER.md").read_text()
else:
disclaimer = None
2021-04-23 20:01:40 +02:00
out = template.render(
lang=lang,
upstream=upstream,
screenshots=screenshots,
disclaimer=disclaimer,
manifest=manifest,
)
(app_path / f"README{lang_suffix}.md").write_text(out)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Automatically (re)generate README for apps"
)
parser.add_argument(
"app_path", help="Path to the app to generate/update READMEs for"
)
args = parser.parse_args()
generate_READMEs(args.app_path)