1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
# Copyright 2023 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import enum
import os
import print_histogram_names
class WellKnownAllowlistPath(enum.Enum):
"""Enum representing the known production allowlist paths to validate.
This is used to collect the paths to production allowlist and make them
accessible to the presubmit checks between this directory and the external
presubmit checks. Plus a common methods to access relevant information.
Currently there is only one allowlist, but it seems to make sense to oraganize
it like this to expose those access methods rather then having multiple
constants or duplicated code among users of this check.
"""
ANDROID_WEBVIEW = os.path.join('android_webview', 'java', 'src', 'org',
'chromium', 'android_webview', 'metrics',
'HistogramsAllowlist.java')
def relative_path(self):
"""Returns the path of the allowlist file relative to src/."""
return self.value
def filename(self):
return os.path.basename(self.value)
def get_histograms_allowlist_content(allowlist_path):
histogramNames = []
with open(allowlist_path) as file:
shouldParse = False
for line in file:
if line.strip() == '// histograms_allowlist_check START_PARSING':
shouldParse = True
continue
if line.strip() == '// histograms_allowlist_check END_PARSING':
break
if shouldParse:
# Remove white space, quotes and commas from the entries.
histogramNames.append(line.strip().replace('"', '').replace(',', ''))
return histogramNames
def check_histograms_allowlist(output_api, allowlist_path, histograms_files):
"""Checks that all histograms in the allowlist are defined within histograms.
Args:
output_api: The output api type, generally provided by the PRESUBMIT system.
allowlist_path: The path to the allowlist file to validate histograms
against.
histograms_files: The list of histograms files to look for allowlisted
histograms in.
"""
all_histograms = print_histogram_names.get_names(histograms_files)
histograms_allowlist = get_histograms_allowlist_content(allowlist_path)
errors = []
for histogram in histograms_allowlist:
if histogram not in all_histograms:
errors.append(f'{allowlist_path} contains unknown histogram '
f'<{histogram}>')
if not errors:
return []
results = [
output_api.PresubmitError(
f'All histograms in {allowlist_path} must be valid.',
errors,
)
]
return results
|