| 1 |
import codecs |
| 2 |
import logging |
| 3 |
import os |
| 4 |
import re |
| 5 |
import shutil |
| 6 |
import sys |
| 7 |
from datetime import datetime, timedelta |
| 8 |
from difflib import unified_diff |
| 9 |
from subprocess import check_call |
| 10 |
from typing import Iterable |
| 11 |
|
| 12 |
from compare_locales.merge import merge_channels |
| 13 |
from compare_locales.paths.configparser import TOMLParser |
| 14 |
from compare_locales.paths.files import ProjectFiles |
| 15 |
from fluent.migrate.repo_client import RepoClient, git |
| 16 |
from fluent.migrate.validator import Validator |
| 17 |
from fluent.syntax import FluentParser, FluentSerializer |
| 18 |
from mach.util import get_state_dir |
| 19 |
from mozpack.path import join, normpath |
| 20 |
|
| 21 |
L10N_SOURCE_NAME = "l10n-source" |
| 22 |
L10N_SOURCE_REPO = "https://github.com/mozilla-l10n/firefox-l10n-source.git" |
| 23 |
|
| 24 |
PULL_AFTER = timedelta(days=2) |
| 25 |
|
| 26 |
|
| 27 |
def inspect_migration(path): |
| 28 |
"""Validate recipe and extract some metadata.""" |
| 29 |
return Validator.validate(path) |
| 30 |
|
| 31 |
|
| 32 |
def prepare_directories(cmd): |
| 33 |
""" |
| 34 |
Ensure object dir exists, |
| 35 |
and that repo dir has a relatively up-to-date clone of l10n-source or gecko-strings. |
| 36 |
|
| 37 |
We run this once per mach invocation, for all tested migrations. |
| 38 |
""" |
| 39 |
obj_dir = join(cmd.topobjdir, "python", "l10n") |
| 40 |
if not os.path.exists(obj_dir): |
| 41 |
os.makedirs(obj_dir) |
| 42 |
|
| 43 |
repo_dir = join(get_state_dir(), L10N_SOURCE_NAME) |
| 44 |
marker = join(repo_dir, ".git", "l10n_pull_marker") |
| 45 |
|
| 46 |
try: |
| 47 |
last_pull = datetime.fromtimestamp(os.stat(marker).st_mtime) |
| 48 |
skip_clone = datetime.now() < last_pull + PULL_AFTER |
| 49 |
except OSError: |
| 50 |
skip_clone = False |
| 51 |
if not skip_clone: |
| 52 |
if os.path.exists(repo_dir): |
| 53 |
check_call(["git", "pull", L10N_SOURCE_REPO], cwd=repo_dir) |
| 54 |
else: |
| 55 |
check_call(["git", "clone", L10N_SOURCE_REPO, repo_dir]) |
| 56 |
with open(marker, "w") as fh: |
| 57 |
fh.flush() |
| 58 |
|
| 59 |
return obj_dir, repo_dir |
| 60 |
|
| 61 |
|
| 62 |
def diff_resources(left_path, right_path): |
| 63 |
parser = FluentParser(with_spans=False) |
| 64 |
serializer = FluentSerializer(with_junk=True) |
| 65 |
lines = [] |
| 66 |
for p in (left_path, right_path): |
| 67 |
with codecs.open(p, encoding="utf-8") as fh: |
| 68 |
res = parser.parse(fh.read()) |
| 69 |
lines.append(serializer.serialize(res).splitlines(True)) |
| 70 |
sys.stdout.writelines( |
| 71 |
chunk for chunk in unified_diff(lines[0], lines[1], left_path, right_path) |
| 72 |
) |
| 73 |
|
| 74 |
|
| 75 |
def test_migration( |
| 76 |
cmd, |
| 77 |
obj_dir: str, |
| 78 |
repo_dir: str, |
| 79 |
to_test: list[str], |
| 80 |
references: Iterable[str], |
| 81 |
): |
| 82 |
"""Test the given recipe. |
| 83 |
|
| 84 |
This creates a workdir by l10n-merging gecko-strings and the m-c source, |
| 85 |
to mimic gecko-strings after the patch to test landed. |
| 86 |
It then runs the recipe with a gecko-strings clone as localization, both |
| 87 |
dry and wet. |
| 88 |
It inspects the generated commits, and shows a diff between the merged |
| 89 |
reference and the generated content. |
| 90 |
The diff is intended to be visually inspected. Some changes might be |
| 91 |
expected, in particular when formatting of the en-US strings is different. |
| 92 |
""" |
| 93 |
rv = 0 |
| 94 |
migration_name = os.path.splitext(os.path.split(to_test)[1])[0] |
| 95 |
work_dir = join(obj_dir, migration_name) |
| 96 |
|
| 97 |
paths = os.path.normpath(to_test).split(os.sep) |
| 98 |
# Migration modules should be in a sub-folder of l10n. |
| 99 |
migration_module = ( |
| 100 |
".".join(paths[paths.index("l10n") + 1 : -1]) + "." + migration_name |
| 101 |
) |
| 102 |
|
| 103 |
if os.path.exists(work_dir): |
| 104 |
shutil.rmtree(work_dir) |
| 105 |
os.makedirs(join(work_dir, "reference")) |
| 106 |
l10n_toml = join(cmd.topsrcdir, cmd.substs["MOZ_BUILD_APP"], "locales", "l10n.toml") |
| 107 |
pc = TOMLParser().parse(l10n_toml, env={"l10n_base": work_dir}) |
| 108 |
pc.set_locales(["reference"]) |
| 109 |
files = ProjectFiles("reference", [pc]) |
| 110 |
ref_root = join(work_dir, "reference") |
| 111 |
for ref in references: |
| 112 |
if ref != normpath(ref): |
| 113 |
cmd.log( |
| 114 |
logging.ERROR, |
| 115 |
"fluent-migration-test", |
| 116 |
{"file": to_test, "ref": ref}, |
| 117 |
'Reference path "{ref}" needs to be normalized for {file}', |
| 118 |
) |
| 119 |
rv = 1 |
| 120 |
continue |
| 121 |
full_ref = join(ref_root, ref) |
| 122 |
m = files.match(full_ref) |
| 123 |
if m is None: |
| 124 |
raise ValueError("Bad reference path: " + ref) |
| 125 |
m_c_path = m[1] |
| 126 |
g_s_path = join(work_dir, L10N_SOURCE_NAME, ref) |
| 127 |
resources = [ |
| 128 |
b"" if not os.path.exists(f) else open(f, "rb").read() |
| 129 |
for f in (g_s_path, m_c_path) |
| 130 |
] |
| 131 |
ref_dir = os.path.dirname(full_ref) |
| 132 |
if not os.path.exists(ref_dir): |
| 133 |
os.makedirs(ref_dir) |
| 134 |
open(full_ref, "wb").write(merge_channels(ref, resources)) |
| 135 |
l10n_root = join(work_dir, "en-US") |
| 136 |
git(work_dir, "clone", repo_dir, l10n_root) |
| 137 |
client = RepoClient(l10n_root) |
| 138 |
old_tip = client.head() |
| 139 |
run_migration = [ |
| 140 |
cmd._virtualenv_manager.python_path, |
| 141 |
"-m", |
| 142 |
"fluent.migrate.tool", |
| 143 |
"--lang", |
| 144 |
"en-US", |
| 145 |
"--reference-dir", |
| 146 |
ref_root, |
| 147 |
"--localization-dir", |
| 148 |
l10n_root, |
| 149 |
"--dry-run", |
| 150 |
migration_module, |
| 151 |
] |
| 152 |
cmd.run_process(run_migration, cwd=work_dir, line_handler=print) |
| 153 |
# drop --dry-run |
| 154 |
run_migration.pop(-2) |
| 155 |
cmd.run_process(run_migration, cwd=work_dir, line_handler=print) |
| 156 |
tip = client.head() |
| 157 |
if old_tip == tip: |
| 158 |
cmd.log( |
| 159 |
logging.WARN, |
| 160 |
"fluent-migration-test", |
| 161 |
{"file": to_test}, |
| 162 |
"No migration applied for {file}", |
| 163 |
) |
| 164 |
return rv |
| 165 |
for ref in references: |
| 166 |
diff_resources(join(ref_root, ref), join(l10n_root, ref)) |
| 167 |
messages = client.log(old_tip, tip) |
| 168 |
bug = re.search("[0-9]{5,}", migration_name) |
| 169 |
# Just check first message for bug number, they're all following the same pattern |
| 170 |
if bug is None or bug.group() not in messages[0]: |
| 171 |
rv = 1 |
| 172 |
cmd.log( |
| 173 |
logging.ERROR, |
| 174 |
"fluent-migration-test", |
| 175 |
{"file": to_test}, |
| 176 |
"Missing or wrong bug number for {file}", |
| 177 |
) |
| 178 |
if any(f"part {n + 1}" not in msg for n, msg in enumerate(messages)): |
| 179 |
rv = 1 |
| 180 |
cmd.log( |
| 181 |
logging.ERROR, |
| 182 |
"fluent-migration-test", |
| 183 |
{"file": to_test}, |
| 184 |
'Commit messages should have "part {{index}}" for {file}', |
| 185 |
) |
| 186 |
return rv |