Commit 14d9a1a8 authored by lukasza's avatar lukasza Committed by Commit bot

Fix presubmit_scheme_histograms.py and presubmit_bad_message_reasons.py

This CL fixes presubmit_scheme_histograms.py and
presubmit_bad_message_reasons.py which after r462591 would always report
presubmit errors (because it started returning a tuple from HistogramNeedsUpdate
which always coerces to True in the broken presubmit scripts).

After r462591 HistogramNeedsUpdate effectively checks all
histogram-enum-related presubmits (both for updating histograms.xml and
for detecting duplicate values).  Therefore the current CL renames this
function to CheckPresubmitErrors and makes it return a presubmit error
(rather than a tuple of (needs_updating, duplicates)).  This refactoring
has a nice side benefit - after this CL, all presubmits going through
CheckPresubmitErrors will check for duplicate values (r462591 only added
this check for UseCounter histograms).

This CL also tweaks content/browser/PRESUBMIT.py so that it applies
*both* to upload-time and commit-time checks (without this change
|git cl presubmit| would not hit the checks related to
content/browser/bad_message.h - they would only be hit when running
|git cl presubmit --upload|).

Manual testing done:
- Make changes in content/browser/bad_message.h,
  chrome/browser/extensions/chrome_content_browser_client_extensions_part.cc,
  third_party/WebKit/Source/code/frame
- Run git cl presubmit and verify that the presubmit
  checks detect 1) duplicates and 2) the need to update histograms.xml

BUG=577772
TEST=See "Manual testing done" above.
TBR=sky@chromium.org, jochen@chromium.org, rdevlin.cronin@chromium.org

Review-Url: https://codereview.chromium.org/2841823007
Cr-Commit-Position: refs/heads/master@{#468003}
parent de0f39d2
...@@ -21,7 +21,7 @@ def CheckChangeOnCommit(input_api, output_api): ...@@ -21,7 +21,7 @@ def CheckChangeOnCommit(input_api, output_api):
def _RunHistogramChecks(input_api, output_api, histogram_name): def _RunHistogramChecks(input_api, output_api, histogram_name):
try: try:
# Setup sys.path so that we can call histrogram code # Setup sys.path so that we can call histograms code.
import sys import sys
original_sys_path = sys.path original_sys_path = sys.path
sys.path = sys.path + [input_api.os_path.join( sys.path = sys.path + [input_api.os_path.join(
......
...@@ -8,7 +8,7 @@ match the corresponding bad_message.h file. ...@@ -8,7 +8,7 @@ match the corresponding bad_message.h file.
def _RunHistogramChecks(input_api, output_api, histogram_name): def _RunHistogramChecks(input_api, output_api, histogram_name):
try: try:
# Setup sys.path so that we can call histrogram code # Setup sys.path so that we can call histograms code.
import sys import sys
original_sys_path = sys.path original_sys_path = sys.path
sys.path = sys.path + [input_api.os_path.join( sys.path = sys.path + [input_api.os_path.join(
......
...@@ -8,7 +8,7 @@ match the corresponding bad_message.h file. ...@@ -8,7 +8,7 @@ match the corresponding bad_message.h file.
def _RunHistogramChecks(input_api, output_api, histogram_name): def _RunHistogramChecks(input_api, output_api, histogram_name):
try: try:
# Setup sys.path so that we can call histrogram code # Setup sys.path so that we can call histograms code.
import sys import sys
original_sys_path = sys.path original_sys_path = sys.path
sys.path = sys.path + [input_api.os_path.join( sys.path = sys.path + [input_api.os_path.join(
......
...@@ -6,9 +6,15 @@ ...@@ -6,9 +6,15 @@
match the corresponding bad_message.h file. match the corresponding bad_message.h file.
""" """
def CheckChangeOnCommit(input_api, output_api):
return _CommonChecks(input_api, output_api)
def CheckChangeOnUpload(input_api, output_api):
return _CommonChecks(input_api, output_api)
def _RunHistogramChecks(input_api, output_api, histogram_name): def _RunHistogramChecks(input_api, output_api, histogram_name):
try: try:
# Setup sys.path so that we can call histrogram code # Setup sys.path so that we can call histograms code.
import sys import sys
original_sys_path = sys.path original_sys_path = sys.path
sys.path = sys.path + [input_api.os_path.join( sys.path = sys.path + [input_api.os_path.join(
...@@ -24,5 +30,5 @@ def _RunHistogramChecks(input_api, output_api, histogram_name): ...@@ -24,5 +30,5 @@ def _RunHistogramChecks(input_api, output_api, histogram_name):
finally: finally:
sys.path = original_sys_path sys.path = original_sys_path
def CheckChangeOnUpload(input_api, output_api): def _CommonChecks(input_api, output_api):
return _RunHistogramChecks(input_api, output_api, "BadMessageReasonContent") return _RunHistogramChecks(input_api, output_api, "BadMessageReasonContent")
...@@ -36,7 +36,7 @@ def _RunHistogramValueCheckers(input_api, output_api): ...@@ -36,7 +36,7 @@ def _RunHistogramValueCheckers(input_api, output_api):
def _RunHistogramChecks(input_api, output_api, histogram_name): def _RunHistogramChecks(input_api, output_api, histogram_name):
try: try:
# Setup sys.path so that we can call histrogram code # Setup sys.path so that we can call histograms code.
import sys import sys
original_sys_path = sys.path original_sys_path = sys.path
sys.path = sys.path + [input_api.os_path.join( sys.path = sys.path + [input_api.os_path.join(
......
...@@ -74,24 +74,16 @@ def _RunUmaHistogramChecks(input_api, output_api): ...@@ -74,24 +74,16 @@ def _RunUmaHistogramChecks(input_api, output_api):
start_marker = '^enum Feature : uint32_t {' start_marker = '^enum Feature : uint32_t {'
end_marker = '^kNumberOfFeatures' end_marker = '^kNumberOfFeatures'
should_update_histogram, duplicated_values = update_histogram_enum.HistogramNeedsUpdate( presubmit_error = update_histogram_enum.CheckPresubmitErrors(
histogram_enum_name='FeatureObserver', histogram_enum_name='FeatureObserver',
update_script_name='update_use_counter_feature_enum.py',
source_enum_path=source_path, source_enum_path=source_path,
start_marker=start_marker, start_marker=start_marker,
end_marker=end_marker, end_marker=end_marker,
strip_k_prefix=True) strip_k_prefix=True)
if duplicated_values: if presubmit_error:
return [output_api.PresubmitPromptWarning( return [output_api.PresubmitPromptWarning(presubmit_error,
'UseCounter::Feature has been updated and there exists duplicated ' items=[source_path])]
'values between (%s) and (%s)' % duplicated_values,
items=[source_path])]
if should_update_histogram:
return [output_api.PresubmitPromptWarning(
'UseCounter::Feature has been updated and the UMA mapping needs to '
'be regenerated. Please run update_use_counter_feature_enum.py in '
'src/tools/metrics/histograms/ to update the mapping.',
items=[source_path])]
return [] return []
......
...@@ -26,13 +26,13 @@ def PrecheckBadMessage(input_api, output_api, histogram_name): ...@@ -26,13 +26,13 @@ def PrecheckBadMessage(input_api, output_api, histogram_name):
START_MARKER='^enum (class )?BadMessageReason {' START_MARKER='^enum (class )?BadMessageReason {'
END_MARKER='^BAD_MESSAGE_MAX' END_MARKER='^BAD_MESSAGE_MAX'
if update_histogram_enum.HistogramNeedsUpdate( presubmit_error = update_histogram_enum.CheckPresubmitErrors(
histogram_enum_name=histogram_name, histogram_enum_name=histogram_name,
update_script_name='update_bad_message_reasons.py',
source_enum_path=source_path, source_enum_path=source_path,
start_marker=START_MARKER, start_marker=START_MARKER,
end_marker=END_MARKER): end_marker=END_MARKER)
return [output_api.PresubmitPromptWarning( if presubmit_error:
'bad_messages.h has been updated but histogram.xml does not ' return [output_api.PresubmitPromptWarning(presubmit_error,
'appear to be updated.\nPlease run:\n' items=[source_path])]
' python tools/metrics/histograms/update_bad_message_reasons.py\n')]
return [] return []
...@@ -18,14 +18,13 @@ def PrecheckShouldAllowOpenURLEnums(input_api, output_api): ...@@ -18,14 +18,13 @@ def PrecheckShouldAllowOpenURLEnums(input_api, output_api):
if source_file not in affected_files: if source_file not in affected_files:
return [] return []
if update_histogram_enum.HistogramNeedsUpdate( presubmit_error = update_histogram_enum.CheckPresubmitErrors(
histogram_enum_name='ShouldAllowOpenURLFailureScheme', histogram_enum_name='ShouldAllowOpenURLFailureScheme',
update_script_name='update_should_allow_open_url_histograms.py',
source_enum_path=source_file, source_enum_path=source_file,
start_marker='^enum ShouldAllowOpenURLFailureScheme {', start_marker='^enum ShouldAllowOpenURLFailureScheme {',
end_marker='^SCHEME_LAST'): end_marker='^SCHEME_LAST')
return [output_api.PresubmitPromptWarning( if presubmit_error:
'ShouldAllowOpenURLFailureScheme has been updated but histogram.xml ' return [output_api.PresubmitPromptWarning(presubmit_error,
'does not appear to be updated.\nPlease run:\n' items=[source_file])]
' python tools/metrics/histograms/'
'update_should_allow_open_url_histograms.py\n')]
return [] return []
...@@ -87,12 +87,12 @@ def ReadHistogramValues(filename, start_marker, end_marker, strip_k_prefix): ...@@ -87,12 +87,12 @@ def ReadHistogramValues(filename, start_marker, end_marker, strip_k_prefix):
label = m.group(1) label = m.group(1)
else: else:
continue continue
# If two enum labels have the same value
if enum_value in result:
return result, (result[enum_value], label)
if strip_k_prefix: if strip_k_prefix:
assert label.startswith('k'), "Enum " + label + " should start with 'k'." assert label.startswith('k'), "Enum " + label + " should start with 'k'."
label = label[1:] label = label[1:]
# If two enum labels have the same value
if enum_value in result:
return result, (result[enum_value], label)
result[enum_value] = label result[enum_value] = label
enum_value += 1 enum_value += 1
return result, None return result, None
...@@ -180,30 +180,45 @@ def _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, ...@@ -180,30 +180,45 @@ def _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values,
return (xml, new_xml) return (xml, new_xml)
def HistogramNeedsUpdate(histogram_enum_name, source_enum_path, start_marker, def CheckPresubmitErrors(histogram_enum_name, update_script_name,
source_enum_path, start_marker,
end_marker, strip_k_prefix = False): end_marker, strip_k_prefix = False):
"""Reads a C++ enum from a .h file and does a dry run of updating """Reads a C++ enum from a .h file and checks for presubmit violations:
histograms.xml to match. Returns true if the histograms.xml file would be 1. Failure to update histograms.xml to match
changed. 2. Introduction of duplicate values.
Args: Args:
histogram_enum_name: The name of the XML <enum> attribute to update. histogram_enum_name: The name of the XML <enum> attribute to update.
update_script_name: The name of an update script to run to update the UMA
mappings for the enum.
source_enum_path: A unix-style path, relative to src/, giving source_enum_path: A unix-style path, relative to src/, giving
the C++ header file from which to read the enum. the C++ header file from which to read the enum.
start_marker: A regular expression that matches the start of the C++ enum. start_marker: A regular expression that matches the start of the C++ enum.
end_marker: A regular expression that matches the end of the C++ enum. end_marker: A regular expression that matches the end of the C++ enum.
strip_k_prefix: Set to True if enum values are declared as kFoo and the strip_k_prefix: Set to True if enum values are declared as kFoo and the
'k' should be stripped. 'k' should be stripped.
Returns:
A string with presubmit failure description, or None (if no failures).
""" """
Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) Log('Reading histogram enum definition from "{0}".'.format(source_enum_path))
source_enum_values, duplicated_values = ReadHistogramValues( source_enum_values, duplicated_values = ReadHistogramValues(
source_enum_path, start_marker, end_marker, strip_k_prefix) source_enum_path, start_marker, end_marker, strip_k_prefix)
if duplicated_values: if duplicated_values:
return False, duplicated_values return ('%s enum has been updated and there exist '
'duplicated values between (%s) and (%s)' % (histogram_enum_name,
duplicated_values[0],
duplicated_values[1]))
(xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values,
source_enum_path) source_enum_path)
return xml != new_xml, None if xml != new_xml:
return ('%s enum has been updated and the UMA mapping needs to be '
'regenerated. Please run %s in src/tools/metrics/histograms/ to '
'update the mapping.' % (histogram_enum_name, update_script_name))
return None
def UpdateHistogramFromDict(histogram_enum_name, source_enum_values, def UpdateHistogramFromDict(histogram_enum_name, source_enum_values,
......
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