1
0
Fork 0

Rework a bit make_readme.py and test_make_readme.py

* Use pathlib.Path

* Use difflib to show actual diffs between new and old readmes
This commit is contained in:
Félix Piédallu 2024-03-13 14:48:52 +01:00
parent 01b683b11f
commit 7fe984b0d2
2 changed files with 45 additions and 39 deletions

View file

@ -2,7 +2,6 @@
import argparse import argparse
import json import json
import os
from pathlib import Path from pathlib import Path
from copy import deepcopy from copy import deepcopy
@ -11,6 +10,9 @@ from typing import Dict, Optional, List, Tuple
import toml import toml
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
README_GEN_DIR = Path(__file__).resolve().parent
APPS_REPO_ROOT = README_GEN_DIR.parent.parent
def value_for_lang(values: Dict, lang: str): def value_for_lang(values: Dict, lang: str):
if not isinstance(values, dict): if not isinstance(values, dict):
@ -27,20 +29,18 @@ def generate_READMEs(app_path: Path):
if not app_path.exists(): if not app_path.exists():
raise Exception("App path provided doesn't exists ?!") raise Exception("App path provided doesn't exists ?!")
if os.path.exists(app_path / "manifest.json"): if (app_path / "manifest.json").exists():
manifest = json.load(open(app_path / "manifest.json")) manifest = json.load(open(app_path / "manifest.json"))
else: else:
manifest = toml.load(open(app_path / "manifest.toml")) manifest = toml.load(open(app_path / "manifest.toml"))
upstream = manifest.get("upstream", {}) upstream = manifest.get("upstream", {})
catalog = toml.load( catalog = toml.load((APPS_REPO_ROOT / "apps.toml").open(encoding="utf-8"))
open(Path(os.path.abspath(__file__)).parent.parent.parent / "apps.toml")
)
from_catalog = catalog.get(manifest["id"], {}) from_catalog = catalog.get(manifest["id"], {})
antifeatures_list = toml.load( antifeatures_list = toml.load(
open(Path(os.path.abspath(__file__)).parent.parent.parent / "antifeatures.toml") (APPS_REPO_ROOT / "antifeatures.toml").open(encoding="utf-8")
) )
if not upstream and not (app_path / "doc" / "DISCLAIMER.md").exists(): if not upstream and not (app_path / "doc" / "DISCLAIMER.md").exists():
@ -49,17 +49,20 @@ def generate_READMEs(app_path: Path):
) )
return return
env = Environment(loader=FileSystemLoader(Path(__file__).parent / "templates")) env = Environment(loader=FileSystemLoader(README_GEN_DIR / "templates"))
screenshots: List[str] screenshots: List[str] = []
screenshots = []
if (app_path / "doc" / "screenshots").exists(): screenshots_dir = app_path / "doc" / "screenshots"
# only pick files (no folder) on the root of 'screenshots' if screenshots_dir.exists():
for entry in os.scandir(os.path.join(app_path, "doc", "screenshots")): for entry in screenshots_dir.iterdir():
if os.DirEntry.is_file(entry): # only pick files (no folder) on the root of 'screenshots'
# ignore '.gitkeep' or any file whose name begins with a dot if not entry.is_file():
if not entry.name.startswith("."): continue
screenshots.append(os.path.relpath(entry.path, app_path)) # ignore '.gitkeep' or any file whose name begins with a dot
if entry.name.startswith("."):
continue
screenshots.append(str(entry.relative_to(app_path)))
# parse available README template and generate a list in the form of: # parse available README template and generate a list in the form of:
# > [("en", ""), ("fr", "_fr"), ...] # > [("en", ""), ("fr", "_fr"), ...]
@ -133,7 +136,7 @@ if __name__ == "__main__":
description="Automatically (re)generate README for apps" description="Automatically (re)generate README for apps"
) )
parser.add_argument( parser.add_argument(
"app_path", help="Path to the app to generate/update READMEs for" "app_path", type=Path, help="Path to the app to generate/update READMEs for"
) )
args = parser.parse_args() args = parser.parse_args()

47
readme_generator/tests/test_make_readme.py Normal file → Executable file
View file

@ -1,36 +1,39 @@
import os #!/usr/bin/env python3
import difflib
import tempfile import tempfile
import subprocess import subprocess
from pathlib import Path from pathlib import Path
CWD = Path(os.path.split(os.path.realpath(__file__))[0]) CWD = Path(__file__).resolve().parent
COMMIT_ID = "8f788213b363a46a5b6faa8f844d86d4adac9446"
TEST_APP_NAME = "gotosocial_ynh"
TEST_APP_REPO = "https://github.com/yunohost-apps/gotosocial_ynh"
TEST_APP_COMMIT_ID = "8f788213b363a46a5b6faa8f844d86d4adac9446"
def diff_files(file_a: Path, file_b: Path) -> bool:
lines_a = file_a.open(encoding="utf-8").readlines()
lines_b = file_b.open(encoding="utf-8").readlines()
diffs = list(difflib.unified_diff(lines_a, lines_b, fromfile='README.before.md', tofile='README.after.md'))
print("".join(diffs))
return len(diffs) == 0
def test_running_make_readme(): def test_running_make_readme():
with tempfile.TemporaryDirectory() as name: with tempfile.TemporaryDirectory() as tempdir:
name = Path(name) tempdir = Path(tempdir)
DIRECTORY = name / "gotosocial_ynh" DIRECTORY = tempdir / TEST_APP_NAME
subprocess.check_call( subprocess.check_call(["git", "clone", "-q", TEST_APP_REPO, DIRECTORY])
[ subprocess.check_call(["git", "checkout", "-q", TEST_APP_COMMIT_ID], cwd=DIRECTORY)
"git",
"clone",
"https://github.com/yunohost-apps/gotosocial_ynh",
DIRECTORY,
"-q",
]
)
subprocess.check_call(["git", "checkout", COMMIT_ID, "-q"], cwd=DIRECTORY)
print(CWD) # Now run test...
subprocess.check_call([CWD / "../make_readme.py", DIRECTORY]) subprocess.check_call([CWD.parent / "make_readme.py", DIRECTORY])
assert open(CWD / "README.md").read() == open(DIRECTORY / "README.md").read() assert diff_files(CWD / "README.md", DIRECTORY / "README.md")
assert ( assert diff_files(CWD / "README_fr.md", DIRECTORY / "README_fr.md")
open(CWD / "README_fr.md").read() == open(DIRECTORY / "README_fr.md").read()
)
if __name__ == "__main__": if __name__ == "__main__":