Commit ab8144da authored by Bruno Santos's avatar Bruno Santos Committed by Commit Bot

Add expectation for internal grd files.

Add entry in translation expectations to list grd files that should not
be processed by translation tools, since they might contain parts not
available publicly.

Bug: 959449
Change-Id: Ic5da08cdb8465b7b7d7922d33b2ed0801a556ecc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1596969
Commit-Queue: Bruno Santos <brunoad@chromium.org>
Reviewed-by: default avatarMustafa Emre Acer <meacer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#657365}
parent 4d413bba
...@@ -75,14 +75,24 @@ def get_translatable_grds(repo_root, all_grd_paths, ...@@ -75,14 +75,24 @@ def get_translatable_grds(repo_root, all_grd_paths,
translation_expectations_path: The path to the translation expectations translation_expectations_path: The path to the translation expectations
file, which specifies which grds to translate and into which languages. file, which specifies which grds to translate and into which languages.
""" """
grd_to_langs, untranslated_grds = _parse_translation_expectations( parsed_expectations = _parse_translation_expectations(
translation_expectations_path) translation_expectations_path)
grd_to_langs, untranslated_grds, internal_grds = parsed_expectations
errors = []
# Make sure that grds in internal_grds aren't processed, since they might
# contain pieces not available publicly.
for internal_grd in internal_grds:
try:
all_grd_paths.remove(internal_grd)
except ValueError:
errors.append(
'%s is listed in translation expectations as an internal file to be '
'ignored, but this grd file does not exist.' % internal_grd)
# Check that every grd that appears translatable is listed in # Check that every grd that appears translatable is listed in
# the translation expectations. # the translation expectations.
grds_with_expectations = set(grd_to_langs.keys()).union(untranslated_grds) grds_with_expectations = set(grd_to_langs.keys()).union(untranslated_grds)
all_grds = {p: GRDFile(os.path.join(repo_root, p)) for p in all_grd_paths} all_grds = {p: GRDFile(os.path.join(repo_root, p)) for p in all_grd_paths}
errors = []
for path, grd in all_grds.iteritems(): for path, grd in all_grds.iteritems():
if grd.appears_translatable: if grd.appears_translatable:
if path not in grds_with_expectations: if path not in grds_with_expectations:
...@@ -169,18 +179,25 @@ def _parse_translation_expectations(path): ...@@ -169,18 +179,25 @@ def _parse_translation_expectations(path):
"languages": ["de", "pt-BR"], "languages": ["de", "pt-BR"],
"files": [ "files": [
"chrome/android/android_chrome_strings.grd", "chrome/android/android_chrome_strings.grd",
],
}, },
"untranslated_grds": { "untranslated_grds": {
"chrome/locale_settings.grd": "Not UI strings; localized separately", "chrome/locale_settings.grd": "Not UI strings; localized separately",
"chrome/locale_settings_mac.grd": "Not UI strings; localized separately", "chrome/locale_settings_mac.grd": "Not UI strings; localized separately",
}, },
"internal_grds": [
"chrome/internal.grd",
],
} }
Returns: Returns:
A tuple (grd_to_langs, untranslated_grds). grd_to_langs maps each grd path A tuple (grd_to_langs, untranslated_grds, internal_grds).
to the list of languages into which that grd should be translated. grd_to_langs maps each grd path to the list of languages into which
untranslated_grds is a list of grds that "appear translatable" but should that grd should be translated. untranslated_grds is a list of grds
not be translated. that "appear translatable" but should not be translated.
internal_grds is a list of grds that are internal only and should
not be read by this helper (since they might contain parts not
available publicly).
""" """
with open(path) as f: with open(path) as f:
file_contents = f.read() file_contents = f.read()
...@@ -196,6 +213,7 @@ def _parse_translation_expectations(path): ...@@ -196,6 +213,7 @@ def _parse_translation_expectations(path):
grd_to_langs = {} grd_to_langs = {}
untranslated_grds = [] untranslated_grds = []
internal_grds = []
for group_name, settings in translations_expectations.iteritems(): for group_name, settings in translations_expectations.iteritems():
if group_name == 'untranslated_grds': if group_name == 'untranslated_grds':
...@@ -203,6 +221,11 @@ def _parse_translation_expectations(path): ...@@ -203,6 +221,11 @@ def _parse_translation_expectations(path):
assert_list_of_strings(untranslated_grds, 'untranslated_grds') assert_list_of_strings(untranslated_grds, 'untranslated_grds')
continue continue
if group_name == 'internal_grds':
internal_grds = settings
assert_list_of_strings(internal_grds, 'internal_grds')
continue
languages = settings['languages'] languages = settings['languages']
files = settings['files'] files = settings['files']
assert_list_of_strings(languages, group_name + '.languages') assert_list_of_strings(languages, group_name + '.languages')
...@@ -210,7 +233,7 @@ def _parse_translation_expectations(path): ...@@ -210,7 +233,7 @@ def _parse_translation_expectations(path):
for grd in files: for grd in files:
grd_to_langs[grd] = languages grd_to_langs[grd] = languages
return grd_to_langs, untranslated_grds return grd_to_langs, untranslated_grds, internal_grds
except Exception: except Exception:
print 'Error: failed to parse', path print 'Error: failed to parse', path
......
...@@ -17,7 +17,7 @@ class TcHelperTest(unittest.TestCase): ...@@ -17,7 +17,7 @@ class TcHelperTest(unittest.TestCase):
def test_get_translatable_grds(self): def test_get_translatable_grds(self):
grds = translation_helper.get_translatable_grds( grds = translation_helper.get_translatable_grds(
testdata_path, ['test.grd', 'not_translated.grd'], testdata_path, ['test.grd', 'not_translated.grd', 'internal.grd'],
os.path.join(testdata_path, 'translation_expectations.pyl')) os.path.join(testdata_path, 'translation_expectations.pyl'))
self.assertEqual(1, len(grds)) self.assertEqual(1, len(grds))
...@@ -43,13 +43,28 @@ class TcHelperTest(unittest.TestCase): ...@@ -43,13 +43,28 @@ class TcHelperTest(unittest.TestCase):
'translation_expectations.pyl') 'translation_expectations.pyl')
with self.assertRaises(Exception) as context: with self.assertRaises(Exception) as context:
translation_helper.get_translatable_grds( translation_helper.get_translatable_grds(
testdata_path, ['test.grd'], TRANSLATION_EXPECTATIONS) testdata_path, ['test.grd', 'internal.grd'], TRANSLATION_EXPECTATIONS)
self.assertEqual( self.assertEqual(
'%s needs to be updated. Please fix these issues:\n' '%s needs to be updated. Please fix these issues:\n'
' - not_translated.grd is listed in the translation expectations, ' ' - not_translated.grd is listed in the translation expectations, '
'but this grd file does not exist.' % TRANSLATION_EXPECTATIONS, 'but this grd file does not exist.' % TRANSLATION_EXPECTATIONS,
context.exception.message) context.exception.message)
# The expectations list an internal file (internal.grd), but the grd list
# doesn't contain it.
def test_missing_internal(self):
TRANSLATION_EXPECTATIONS = os.path.join(testdata_path,
'translation_expectations.pyl')
with self.assertRaises(Exception) as context:
translation_helper.get_translatable_grds(
testdata_path, ['test.grd', 'not_translated.grd'],
TRANSLATION_EXPECTATIONS)
self.assertEqual(
'%s needs to be updated. Please fix these issues:\n'
' - internal.grd is listed in translation expectations as an internal '
'file to be ignored, but this grd file does not exist.' %
TRANSLATION_EXPECTATIONS, context.exception.message)
# The expectations list a translatable file (test.grd), but the grd list # The expectations list a translatable file (test.grd), but the grd list
# doesn't contain it. # doesn't contain it.
def test_missing_translatable(self): def test_missing_translatable(self):
...@@ -57,7 +72,8 @@ class TcHelperTest(unittest.TestCase): ...@@ -57,7 +72,8 @@ class TcHelperTest(unittest.TestCase):
'translation_expectations.pyl') 'translation_expectations.pyl')
with self.assertRaises(Exception) as context: with self.assertRaises(Exception) as context:
translation_helper.get_translatable_grds( translation_helper.get_translatable_grds(
testdata_path, ['not_translated.grd'], TRANSLATION_EXPECTATIONS) testdata_path, ['not_translated.grd', 'internal.grd'],
TRANSLATION_EXPECTATIONS)
self.assertEqual( self.assertEqual(
'%s needs to be updated. Please fix these issues:\n' '%s needs to be updated. Please fix these issues:\n'
' - test.grd is listed in the translation expectations, but this grd ' ' - test.grd is listed in the translation expectations, but this grd '
...@@ -71,7 +87,8 @@ class TcHelperTest(unittest.TestCase): ...@@ -71,7 +87,8 @@ class TcHelperTest(unittest.TestCase):
'translation_expectations.pyl') 'translation_expectations.pyl')
with self.assertRaises(Exception) as context: with self.assertRaises(Exception) as context:
translation_helper.get_translatable_grds( translation_helper.get_translatable_grds(
testdata_path, ['test.grd', 'part.grdp', 'not_translated.grd'], testdata_path,
['test.grd', 'part.grdp', 'not_translated.grd', 'internal.grd'],
TRANSLATION_EXPECTATIONS) TRANSLATION_EXPECTATIONS)
self.assertEqual( self.assertEqual(
'%s needs to be updated. Please fix these issues:\n' '%s needs to be updated. Please fix these issues:\n'
......
<?xml version="1.0" encoding="UTF-8"?>
<grit latest_public_release="1" current_release="1">
<release seq="1">
<!-- Part file below doesn't exist intetionally as it shouldn't be checked. -->
<part file="internal_part.grdp" />
</release>
</grit>
...@@ -25,4 +25,9 @@ ...@@ -25,4 +25,9 @@
"untranslated_grds": { "untranslated_grds": {
"not_translated.grd": "Not translated" "not_translated.grd": "Not translated"
}, },
} # Grd files that should not be processed, since they might contain parts not
\ No newline at end of file # available publicly.
"internal_grds": [
"internal.grd",
],
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment