diff options
| author | django-bot <ops@djangoproject.com> | 2022-02-03 20:24:19 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
| commit | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch) | |
| tree | f0506b668a013d0063e5fba3dbf4863b466713ba /scripts | |
| parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/manage_translations.py | 108 |
1 files changed, 71 insertions, 37 deletions
diff --git a/scripts/manage_translations.py b/scripts/manage_translations.py index fd394e10ab..5b82011f20 100644 --- a/scripts/manage_translations.py +++ b/scripts/manage_translations.py @@ -26,7 +26,7 @@ import django from django.conf import settings from django.core.management import call_command -HAVE_JS = ['admin'] +HAVE_JS = ["admin"] def _get_locale_dirs(resources, include_core=True): @@ -35,33 +35,35 @@ def _get_locale_dirs(resources, include_core=True): optionally including the django core catalog. If resources list is not None, filter directories matching resources content. """ - contrib_dir = os.path.join(os.getcwd(), 'django', 'contrib') + contrib_dir = os.path.join(os.getcwd(), "django", "contrib") dirs = [] # Collect all locale directories for contrib_name in os.listdir(contrib_dir): - path = os.path.join(contrib_dir, contrib_name, 'locale') + path = os.path.join(contrib_dir, contrib_name, "locale") if os.path.isdir(path): dirs.append((contrib_name, path)) if contrib_name in HAVE_JS: dirs.append(("%s-js" % contrib_name, path)) if include_core: - dirs.insert(0, ('core', os.path.join(os.getcwd(), 'django', 'conf', 'locale'))) + dirs.insert(0, ("core", os.path.join(os.getcwd(), "django", "conf", "locale"))) # Filter by resources, if any if resources is not None: res_names = [d[0] for d in dirs] dirs = [ld for ld in dirs if ld[0] in resources] if len(resources) > len(dirs): - print("You have specified some unknown resources. " - "Available resource names are: %s" % (', '.join(res_names),)) + print( + "You have specified some unknown resources. " + "Available resource names are: %s" % (", ".join(res_names),) + ) exit(1) return dirs def _tx_resource_for_name(name): - """ Return the Transifex resource name """ - if name == 'core': + """Return the Transifex resource name""" + if name == "core": return "django.core" else: return "django.contrib-%s" % name @@ -71,10 +73,15 @@ def _check_diff(cat_name, base_path): """ Output the approximate number of changed/added strings in the en catalog. """ - po_path = '%(path)s/en/LC_MESSAGES/django%(ext)s.po' % { - 'path': base_path, 'ext': 'js' if cat_name.endswith('-js') else ''} - p = run("git diff -U0 %s | egrep '^[-+]msgid' | wc -l" % po_path, - capture_output=True, shell=True) + po_path = "%(path)s/en/LC_MESSAGES/django%(ext)s.po" % { + "path": base_path, + "ext": "js" if cat_name.endswith("-js") else "", + } + p = run( + "git diff -U0 %s | egrep '^[-+]msgid' | wc -l" % po_path, + capture_output=True, + shell=True, + ) num_changes = int(p.stdout.strip()) print("%d changed/added messages in '%s' catalog." % (num_changes, cat_name)) @@ -90,14 +97,14 @@ def update_catalogs(resources=None, languages=None): print("`update_catalogs` will always process all resources.") contrib_dirs = _get_locale_dirs(None, include_core=False) - os.chdir(os.path.join(os.getcwd(), 'django')) + os.chdir(os.path.join(os.getcwd(), "django")) print("Updating en catalogs for Django and contrib apps...") - call_command('makemessages', locale=['en']) + call_command("makemessages", locale=["en"]) print("Updating en JS catalogs for Django and contrib apps...") - call_command('makemessages', locale=['en'], domain='djangojs') + call_command("makemessages", locale=["en"], domain="djangojs") # Output changed stats - _check_diff('core', os.path.join(os.getcwd(), 'conf', 'locale')) + _check_diff("core", os.path.join(os.getcwd(), "conf", "locale")) for name, dir_ in contrib_dirs: _check_diff(name, dir_) @@ -113,26 +120,26 @@ def lang_stats(resources=None, languages=None): for name, dir_ in locale_dirs: print("\nShowing translations stats for '%s':" % name) - langs = sorted(d for d in os.listdir(dir_) if not d.startswith('_')) + langs = sorted(d for d in os.listdir(dir_) if not d.startswith("_")) for lang in langs: if languages and lang not in languages: continue # TODO: merge first with the latest en catalog - po_path = '{path}/{lang}/LC_MESSAGES/django{ext}.po'.format( - path=dir_, lang=lang, ext='js' if name.endswith('-js') else '' + po_path = "{path}/{lang}/LC_MESSAGES/django{ext}.po".format( + path=dir_, lang=lang, ext="js" if name.endswith("-js") else "" ) p = run( - ['msgfmt', '-vc', '-o', '/dev/null', po_path], + ["msgfmt", "-vc", "-o", "/dev/null", po_path], capture_output=True, - env={'LANG': 'C'}, - encoding='utf-8', + env={"LANG": "C"}, + encoding="utf-8", ) if p.returncode == 0: # msgfmt output stats on stderr - print('%s: %s' % (lang, p.stderr.strip())) + print("%s: %s" % (lang, p.stderr.strip())) else: print( - 'Errors happened when checking %s translation for %s:\n%s' + "Errors happened when checking %s translation for %s:\n%s" % (lang, name, p.stderr) ) @@ -147,23 +154,40 @@ def fetch(resources=None, languages=None): for name, dir_ in locale_dirs: # Transifex pull if languages is None: - run(['tx', 'pull', '-r', _tx_resource_for_name(name), '-a', '-f', '--minimum-perc=5']) - target_langs = sorted(d for d in os.listdir(dir_) if not d.startswith('_') and d != 'en') + run( + [ + "tx", + "pull", + "-r", + _tx_resource_for_name(name), + "-a", + "-f", + "--minimum-perc=5", + ] + ) + target_langs = sorted( + d for d in os.listdir(dir_) if not d.startswith("_") and d != "en" + ) else: for lang in languages: - run(['tx', 'pull', '-r', _tx_resource_for_name(name), '-f', '-l', lang]) + run(["tx", "pull", "-r", _tx_resource_for_name(name), "-f", "-l", lang]) target_langs = languages # msgcat to wrap lines and msgfmt for compilation of .mo file for lang in target_langs: - po_path = '%(path)s/%(lang)s/LC_MESSAGES/django%(ext)s.po' % { - 'path': dir_, 'lang': lang, 'ext': 'js' if name.endswith('-js') else ''} + po_path = "%(path)s/%(lang)s/LC_MESSAGES/django%(ext)s.po" % { + "path": dir_, + "lang": lang, + "ext": "js" if name.endswith("-js") else "", + } if not os.path.exists(po_path): - print("No %(lang)s translation for resource %(name)s" % { - 'lang': lang, 'name': name}) + print( + "No %(lang)s translation for resource %(name)s" + % {"lang": lang, "name": name} + ) continue - run(['msgcat', '--no-location', '-o', po_path, po_path]) - msgfmt = run(['msgfmt', '-c', '-o', '%s.mo' % po_path[:-3], po_path]) + run(["msgcat", "--no-location", "-o", po_path, po_path]) + msgfmt = run(["msgfmt", "-c", "-o", "%s.mo" % po_path[:-3], po_path]) if msgfmt.returncode != 0: errors.append((name, lang)) if errors: @@ -174,12 +198,22 @@ def fetch(resources=None, languages=None): if __name__ == "__main__": - RUNABLE_SCRIPTS = ('update_catalogs', 'lang_stats', 'fetch') + RUNABLE_SCRIPTS = ("update_catalogs", "lang_stats", "fetch") parser = ArgumentParser() - parser.add_argument('cmd', nargs=1, choices=RUNABLE_SCRIPTS) - parser.add_argument("-r", "--resources", action='append', help="limit operation to the specified resources") - parser.add_argument("-l", "--languages", action='append', help="limit operation to the specified languages") + parser.add_argument("cmd", nargs=1, choices=RUNABLE_SCRIPTS) + parser.add_argument( + "-r", + "--resources", + action="append", + help="limit operation to the specified resources", + ) + parser.add_argument( + "-l", + "--languages", + action="append", + help="limit operation to the specified languages", + ) options = parser.parse_args() eval(options.cmd[0])(options.resources, options.languages) |
