diff --git a/completion.py b/completion.py index 9a09f5b10..26ca52746 100644 --- a/completion.py +++ b/completion.py @@ -6,6 +6,9 @@ import git from potodo import potodo +import translators + + @cache def branches_from_devguide(devguide_dir: Path) -> list[str]: p = devguide_dir.joinpath('include/release-cycle.json') @@ -15,20 +18,20 @@ def branches_from_devguide(devguide_dir: Path) -> list[str]: ] -def get_completion(clones_dir: str, repo: str) -> float: +def get_completion(clones_dir: str, repo: str) -> tuple[float, int]: clone_path = Path(clones_dir, repo) for branch in branches_from_devguide(Path(clones_dir, 'devguide')) + ['master']: try: - git.Repo.clone_from( - f'https://github.com/{repo}.git', clone_path, depth=1, branch=branch - ) + git.Repo.clone_from(f'https://github.com/{repo}.git', clone_path, branch=branch) except git.GitCommandError: print(f'failed to clone {repo} {branch}') + translators_number = 0 continue else: + translators_number = translators.get_number(clone_path) break with TemporaryDirectory() as tmpdir: completion = potodo.merge_and_scan_path( clone_path, pot_path=Path(clones_dir, 'cpython/Doc/build/gettext'), merge_path=Path(tmpdir), hide_reserved=False, api_url='' ).completion - return completion + return completion, translators_number diff --git a/generate.py b/generate.py index 629a02921..c38bffde2 100644 --- a/generate.py +++ b/generate.py @@ -32,11 +32,11 @@ subprocess.run(['make', '-C', Path(clones_dir, 'cpython/Doc'), 'gettext'], check=True) for language, repo in repositories.get_languages_and_repos(devguide_dir): if repo: - completion_number = get_completion(clones_dir, repo) + completion_number, translators_number = get_completion(clones_dir, repo) visitors_number = visitors.get_number_of_visitors(language) else: completion_number, visitors_number = 0.0, 0 - completion_progress.append((language, repo, completion_number, visitors_number)) + completion_progress.append((language, repo, completion_number, translators_number, visitors_number)) print(completion_progress[-1]) template = Template( @@ -53,11 +53,12 @@ language visitors + translators completion -{% for language, repo, completion, visitors in completion_progress | sort(attribute=2) | reverse %} +{% for language, repo, completion, translators, visitors in completion_progress | sort(attribute=2) | reverse %} {% if repo %} @@ -70,9 +71,11 @@ {{ '{:,}'.format(visitors) }} + {{ '{:,}'.format(translators) }} {% else %} {{ language }} 0 + 0 {% endif %}
{{ completion | round(2) }}%
diff --git a/translators.py b/translators.py new file mode 100644 index 000000000..dd3db133f --- /dev/null +++ b/translators.py @@ -0,0 +1,30 @@ +from collections.abc import Generator +from pathlib import Path + +from git import Repo +from polib import pofile + + +def get_number(path: Path) -> int: + from_headers = len(set(yield_from_headers(path))) + from_git_history = get_number_from_git_history(path) + return max(from_headers, from_git_history) + +def get_number_from_git_history(path: Path) -> int: + return len(Repo(path).git.shortlog('-s', 'HEAD').splitlines()) + +def yield_from_headers(path: Path) -> Generator[str, None, None]: + for file in path.rglob('*.po'): + try: + header = pofile(file).header.splitlines() + except IOError: + continue + if 'Translators:' not in header: + continue + for translator_record in header[header.index('Translators:') + 1:]: + try: + translator, _year = translator_record.split(', ') + except ValueError: + yield translator_record + else: + yield translator