Skip to content

Commit

Permalink
Readdress use_counter_feature_enum issue brought in
Browse files Browse the repository at this point in the history
https://codereview.chromium.org/2797043002

Made histogram checker raise exception for duplicated enum values.

BUG=711205

Review-Url: https://codereview.chromium.org/2835733002
Cr-Commit-Position: refs/heads/master@{#469169}
  • Loading branch information
LoonyBean authored and Commit bot committed May 3, 2017
1 parent 6257597 commit 647ac07
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions tools/metrics/histograms/update_histogram_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,37 @@ def message(self):
return self.args[0]


class DuplicatedValue(Exception):
"""Exception raised for duplicated enum values.
Attributes:
first_label: First enum label that shares the duplicated enum value.
second_label: Second enum label that shares the duplicated enum value.
"""
def __init__(self, first_label, second_label):
self.first_label = first_label
self.second_label = second_label


def Log(message):
logging.info(message)


def ReadHistogramValues(filename, start_marker, end_marker, strip_k_prefix):
"""Returns a dictionary of enum values and a pair of labels that have the same
enum values, read from a C++ file.
"""Creates a dictionary of enum values, read from a C++ file.
Args:
filename: The unix-style path (relative to src/) of the file to open.
start_marker: A regex that signifies the start of the enum values.
end_marker: A regex that signifies the end of the enum values.
strip_k_prefix: Set to True if enum values are declared as kFoo and the
'k' should be stripped.
Returns:
A boolean indicating wheather the histograms.xml file would be changed.
Raises:
DuplicatedValue: An error when two enum labels share the same value.
"""
# Read the file as a list of lines
with open(path_util.GetInputFile(filename)) as f:
Expand Down Expand Up @@ -92,10 +109,10 @@ def ReadHistogramValues(filename, start_marker, end_marker, strip_k_prefix):
label = label[1:]
# If two enum labels have the same value
if enum_value in result:
return result, (result[enum_value], label)
raise DuplicatedValue(result[enum_value], label)
result[enum_value] = label
enum_value += 1
return result, None
return result


def CreateEnumItemNode(document, value, label):
Expand Down Expand Up @@ -202,14 +219,14 @@ def CheckPresubmitErrors(histogram_enum_name, update_script_name,
A string with presubmit failure description, or None (if no failures).
"""
Log('Reading histogram enum definition from "{0}".'.format(source_enum_path))
source_enum_values, duplicated_values = ReadHistogramValues(
source_enum_path, start_marker, end_marker, strip_k_prefix)

if duplicated_values:
try:
source_enum_values = ReadHistogramValues(
source_enum_path, start_marker, end_marker, strip_k_prefix)
except DuplicatedValue as 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]))
'duplicated values between (%s) and (%s)' %
(histogram_enum_name, duplicated_values.first_label,
duplicated_values.second_label))

(xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values,
source_enum_path)
Expand Down

0 comments on commit 647ac07

Please sign in to comment.