|
| 1 | +# /// script |
| 2 | +# requires-python = ">=3.11" |
| 3 | +# dependencies = [ |
| 4 | +# "gitpython", |
| 5 | +# "potodo", |
| 6 | +# "jinja2", |
| 7 | +# "requests", |
| 8 | +# "docutils", |
| 9 | +# "sphinx", |
| 10 | +# "python-docs-theme", |
| 11 | +# "dacite", |
| 12 | +# ] |
| 13 | +# /// |
| 14 | +import concurrent.futures |
| 15 | +import itertools |
| 16 | +import logging |
| 17 | +import subprocess |
| 18 | +from collections.abc import Iterator, Sequence |
| 19 | +from datetime import datetime, timezone |
| 20 | +from json import loads |
| 21 | +from pathlib import Path |
| 22 | +from tempfile import TemporaryDirectory |
| 23 | + |
| 24 | +import dacite |
| 25 | +from git import Repo |
| 26 | +from jinja2 import Template |
| 27 | + |
| 28 | +import build_warnings |
| 29 | +from completion import branches_from_devguide |
| 30 | +from generate import LanguageProjectData |
| 31 | +from repositories import Language |
| 32 | + |
| 33 | +generation_time = datetime.now(timezone.utc) |
| 34 | + |
| 35 | + |
| 36 | +def get_projects_metadata( |
| 37 | + completion_progress: Sequence[LanguageProjectData], |
| 38 | +) -> Iterator[int]: |
| 39 | + with TemporaryDirectory() as clones_dir: |
| 40 | + Repo.clone_from( |
| 41 | + 'https://github.com/python/devguide.git', |
| 42 | + devguide_dir := Path(clones_dir, 'devguide'), |
| 43 | + depth=1, |
| 44 | + ) |
| 45 | + latest_branch = branches_from_devguide(devguide_dir)[0] |
| 46 | + Repo.clone_from( |
| 47 | + 'https://github.com/python/cpython.git', |
| 48 | + cpython_dir := Path(clones_dir, 'cpython'), |
| 49 | + depth=1, |
| 50 | + branch=latest_branch, |
| 51 | + ) |
| 52 | + subprocess.run(['make', '-C', cpython_dir / 'Doc', 'venv'], check=True) |
| 53 | + subprocess.run(['make', '-C', cpython_dir / 'Doc', 'gettext'], check=True) |
| 54 | + with concurrent.futures.ProcessPoolExecutor() as executor: |
| 55 | + return executor.map( |
| 56 | + get_metadata, |
| 57 | + *zip(*map(get_language_repo_and_completion, completion_progress)), |
| 58 | + itertools.repeat(clones_dir), |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +def get_metadata( |
| 63 | + language: Language, repo: str | None, completion: float, clones_dir: str |
| 64 | +) -> int: |
| 65 | + return ( |
| 66 | + repo and completion and build_warnings.number(clones_dir, repo, language.code) |
| 67 | + ) or 0 |
| 68 | + |
| 69 | + |
| 70 | +def get_language_repo_and_completion( |
| 71 | + project: LanguageProjectData, |
| 72 | +) -> tuple[Language, str | None, float]: |
| 73 | + return project.language, project.repository, project.completion |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + logging.basicConfig(level=logging.INFO) |
| 78 | + logging.info(f'starting at {generation_time}') |
| 79 | + template = Template(Path('metadata.html.jinja').read_text()) |
| 80 | + |
| 81 | + completion_progress = [ |
| 82 | + dacite.from_dict(LanguageProjectData, project) |
| 83 | + for project in loads(Path('index.json').read_text()) |
| 84 | + ] |
| 85 | + |
| 86 | + output = template.render( |
| 87 | + metadata=zip(completion_progress, get_projects_metadata(completion_progress)), |
| 88 | + generation_time=generation_time, |
| 89 | + duration=(datetime.now(timezone.utc) - generation_time).seconds, |
| 90 | + ) |
| 91 | + |
| 92 | + Path('metadata.html').write_text(output) |
0 commit comments