1 # Copyright 2013 The Chromium Authors
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Presubmit script for changes affecting chrome/app/
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into depot_tools.
13 from xml.dom import minidom
15 def _CheckNoProductNameInGeneratedResources(input_api, output_api):
16 """Check that no PRODUCT_NAME placeholders are found in resources files.
18 These kinds of strings prevent proper localization in some languages. For
19 more information, see the following chromium-dev thread:
20 https://groups.google.com/a/chromium.org/forum/#!msg/chromium-dev/PBs5JfR0Aoc/NOcIHII9u14J
24 filename_filter = lambda x: x.LocalPath().endswith(('.grd', '.grdp'))
26 for f, line_num, line in input_api.RightHandSideLines(filename_filter):
27 if ('PRODUCT_NAME' in line and 'name="IDS_PRODUCT_NAME"' not in line and
28 'name="IDS_SHORT_PRODUCT_NAME"' not in line):
29 problems.append('%s:%d' % (f.LocalPath(), line_num))
32 return [output_api.PresubmitPromptWarning(
33 "Don't use PRODUCT_NAME placeholders in string resources. Instead, "
34 "add separate strings to google_chrome_strings.grd and "
35 "chromium_strings.grd. See http://goo.gl/6614MQ for more information. "
36 "Problems with this check? Contact dubroy@chromium.org.",
40 def _CheckFlagsMessageNotTranslated(input_api, output_api):
41 """Check: all about:flags messages are marked as not requiring translation.
43 This assumes that such messages are only added to generated_resources.grd and
44 that all such messages have names starting with IDS_FLAGS_. The expected mark
45 for not requiring translation is 'translateable="false"'.
49 filename_filter = lambda x: x.LocalPath().endswith("generated_resources.grd")
51 for f, line_num, line in input_api.RightHandSideLines(filename_filter):
52 if "name=\"IDS_FLAGS_" in line and not "translateable=\"false\"" in line:
53 problems.append("Missing translateable=\"false\" in %s:%d"
54 % (f.LocalPath(), line_num))
58 return [output_api.PresubmitError(
59 "If you define a flag name, description or value, mark it as not "
60 "requiring translation by adding the 'translateable' attribute with "
61 "value \"false\". See https://crbug.com/587272 for more context.",
65 def _GetInfoStrings(file_contents):
66 """Retrieves IDS_EDU_LOGIN_INFO_* messages from the file contents
72 A list of tuples, where each element represents a message.
73 element[0] is the 'name' attribute of the message
74 element[1] is the message contents
76 return [(message.getAttribute('name'), message.firstChild.nodeValue)
77 for message in (file_contents.getElementsByTagName('grit-part')[0]
78 .getElementsByTagName('message'))
79 if message.getAttribute('name').startswith('IDS_EDU_LOGIN_INFO_')]
81 strings_file = next((af for af in input_api.change.AffectedFiles()
82 if af.AbsoluteLocalPath() == CHROMEOS_STRINGS_PATH), None)
83 if strings_file is None:
86 old_info_strings = _GetInfoStrings(
87 minidom.parseString('\n'.join(strings_file.OldContents())))
88 new_info_strings = _GetInfoStrings(
89 minidom.parseString('\n'.join(strings_file.NewContents())))
90 if set(old_info_strings) == set(new_info_strings):
93 if input_api.change.issue == 0:
94 # First upload, notify about string changes.
96 output_api.PresubmitNotifyResult(
97 UPDATE_TEXT_VERSION_MESSAGE % "v<GERRIT_CL_NUMBER>"),
98 output_api.PresubmitNotifyResult(
99 UPDATE_INVALIDATION_VERSION_MESSAGE % "iv<GERRIT_CL_NUMBER>"),
102 new_text_version = "v" + str(input_api.change.issue)
103 new_invalidation_version = "iv" + str(input_api.change.issue)
105 text_version_file = next((af for af in input_api.change.AffectedFiles()
106 if af.AbsoluteLocalPath() == TEXT_VERSION_PATH),
109 # Check if text version was updated.
110 if text_version_file is None or new_text_version not in '\n'.join(
111 text_version_file.NewContents()):
113 output_api.PresubmitError(
114 UPDATE_TEXT_VERSION_MESSAGE % new_text_version))
115 # Check if invalidation version was updated.
116 if text_version_file is None or new_invalidation_version not in '\n'.join(
117 text_version_file.NewContents()):
119 output_api.PresubmitNotifyResult(
120 UPDATE_INVALIDATION_VERSION_MESSAGE % new_invalidation_version))
123 def _CommonChecks(input_api, output_api):
124 """Checks common to both upload and commit."""
126 results.extend(_CheckNoProductNameInGeneratedResources(input_api, output_api))
127 results.extend(_CheckFlagsMessageNotTranslated(input_api, output_api))
130 def CheckChangeOnUpload(input_api, output_api):
131 return _CommonChecks(input_api, output_api)
133 def CheckChangeOnCommit(input_api, output_api):
134 return _CommonChecks(input_api, output_api)