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:
parent
01b683b11f
commit
7fe984b0d2
2 changed files with 45 additions and 39 deletions
|
@ -2,7 +2,6 @@
|
|||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from copy import deepcopy
|
||||
|
||||
|
@ -11,6 +10,9 @@ from typing import Dict, Optional, List, Tuple
|
|||
import toml
|
||||
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):
|
||||
if not isinstance(values, dict):
|
||||
|
@ -27,20 +29,18 @@ def generate_READMEs(app_path: Path):
|
|||
if not app_path.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"))
|
||||
else:
|
||||
manifest = toml.load(open(app_path / "manifest.toml"))
|
||||
|
||||
upstream = manifest.get("upstream", {})
|
||||
|
||||
catalog = toml.load(
|
||||
open(Path(os.path.abspath(__file__)).parent.parent.parent / "apps.toml")
|
||||
)
|
||||
catalog = toml.load((APPS_REPO_ROOT / "apps.toml").open(encoding="utf-8"))
|
||||
from_catalog = catalog.get(manifest["id"], {})
|
||||
|
||||
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():
|
||||
|
@ -49,17 +49,20 @@ def generate_READMEs(app_path: Path):
|
|||
)
|
||||
return
|
||||
|
||||
env = Environment(loader=FileSystemLoader(Path(__file__).parent / "templates"))
|
||||
env = Environment(loader=FileSystemLoader(README_GEN_DIR / "templates"))
|
||||
|
||||
screenshots: List[str]
|
||||
screenshots = []
|
||||
if (app_path / "doc" / "screenshots").exists():
|
||||
# only pick files (no folder) on the root of 'screenshots'
|
||||
for entry in os.scandir(os.path.join(app_path, "doc", "screenshots")):
|
||||
if os.DirEntry.is_file(entry):
|
||||
# ignore '.gitkeep' or any file whose name begins with a dot
|
||||
if not entry.name.startswith("."):
|
||||
screenshots.append(os.path.relpath(entry.path, app_path))
|
||||
screenshots: List[str] = []
|
||||
|
||||
screenshots_dir = app_path / "doc" / "screenshots"
|
||||
if screenshots_dir.exists():
|
||||
for entry in screenshots_dir.iterdir():
|
||||
# only pick files (no folder) on the root of 'screenshots'
|
||||
if not entry.is_file():
|
||||
continue
|
||||
# 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:
|
||||
# > [("en", ""), ("fr", "_fr"), ...]
|
||||
|
@ -133,7 +136,7 @@ if __name__ == "__main__":
|
|||
description="Automatically (re)generate README for apps"
|
||||
)
|
||||
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()
|
||||
|
|
47
readme_generator/tests/test_make_readme.py
Normal file → Executable file
47
readme_generator/tests/test_make_readme.py
Normal file → Executable file
|
@ -1,36 +1,39 @@
|
|||
import os
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import difflib
|
||||
import tempfile
|
||||
import subprocess
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
CWD = Path(os.path.split(os.path.realpath(__file__))[0])
|
||||
COMMIT_ID = "8f788213b363a46a5b6faa8f844d86d4adac9446"
|
||||
CWD = Path(__file__).resolve().parent
|
||||
|
||||
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():
|
||||
with tempfile.TemporaryDirectory() as name:
|
||||
name = Path(name)
|
||||
DIRECTORY = name / "gotosocial_ynh"
|
||||
with tempfile.TemporaryDirectory() as tempdir:
|
||||
tempdir = Path(tempdir)
|
||||
DIRECTORY = tempdir / TEST_APP_NAME
|
||||
|
||||
subprocess.check_call(
|
||||
[
|
||||
"git",
|
||||
"clone",
|
||||
"https://github.com/yunohost-apps/gotosocial_ynh",
|
||||
DIRECTORY,
|
||||
"-q",
|
||||
]
|
||||
)
|
||||
subprocess.check_call(["git", "checkout", COMMIT_ID, "-q"], cwd=DIRECTORY)
|
||||
subprocess.check_call(["git", "clone", "-q", TEST_APP_REPO, DIRECTORY])
|
||||
subprocess.check_call(["git", "checkout", "-q", TEST_APP_COMMIT_ID], cwd=DIRECTORY)
|
||||
|
||||
print(CWD)
|
||||
subprocess.check_call([CWD / "../make_readme.py", DIRECTORY])
|
||||
# Now run test...
|
||||
subprocess.check_call([CWD.parent / "make_readme.py", DIRECTORY])
|
||||
|
||||
assert open(CWD / "README.md").read() == open(DIRECTORY / "README.md").read()
|
||||
assert (
|
||||
open(CWD / "README_fr.md").read() == open(DIRECTORY / "README_fr.md").read()
|
||||
)
|
||||
assert diff_files(CWD / "README.md", DIRECTORY / "README.md")
|
||||
assert diff_files(CWD / "README_fr.md", DIRECTORY / "README_fr.md")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Reference in a new issue