From ad5e320d98f0101b5c094270d6f26dae981c1460 Mon Sep 17 00:00:00 2001 From: George Burgess IV Date: Wed, 24 Jun 2020 15:45:15 -0700 Subject: debug_info_test: replace whitelist with allowlist This CL tweaks language to better reflect the intent that's meant to be conveyed, per go/chromium-project-code-inclusion . This change is meant to trivially be a local, functional nop; renaming extensions will be a later CL. BUG=chromium:1099035 TEST=CQ+1 Change-Id: Iaee8b96c54660b4ebb4363a230e768c3da3f885a Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/2265049 Reviewed-by: Manoj Gupta Tested-by: George Burgess Commit-Queue: George Burgess --- debug_info_test/allowlist.py | 69 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 debug_info_test/allowlist.py (limited to 'debug_info_test/allowlist.py') diff --git a/debug_info_test/allowlist.py b/debug_info_test/allowlist.py new file mode 100644 index 00000000..978b2370 --- /dev/null +++ b/debug_info_test/allowlist.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""Allowlist functions.""" + +from __future__ import print_function + +import glob +import os +import re + + +# Matching a string of length m in an NFA of size n is O(mn^2), but the +# performance also depends largely on the implementation. It appears to be fast +# enough according to the tests. +# +# The performance bottleneck of this script is readelf. Unless this becomes +# slower than readelf, don't waste time here. +def is_allowlisted(list_name, pattern): + """Check whether the given pattern is specified in the allowlist. + + Args: + list_name: name of the allowlist. + pattern: the target string. + + Returns: + True if matched otherwise False. + """ + return pattern and allowlists[list_name].match(pattern) + + +def prepare_allowlist(patterns): + """Join and compile the re patterns. + + Args: + patterns: regex patterns. + + Returns: + A compiled re object. + """ + return re.compile('|'.join(patterns)) + + +# FIXME: s/whitelist/allowlist/ in the file extension. +def load_allowlists(dirname): + """Load allowlists under dirname. + + An allowlist ends with .whitelist. + + Args: + dirname: path to the dir. + + Returns: + A dictionary of 'filename' -> allowlist matcher. + """ + wlist = {} + for fn in glob.glob(os.path.join(dirname, '*.whitelist')): + key = os.path.splitext(os.path.basename(fn))[0] + with open(fn, 'r', encoding='utf-8') as f: + patterns = f.read().splitlines() + patterns = [l for l in patterns if l != ''] + patterns = [l for l in patterns if l[0] != '#'] + wlist[key] = prepare_allowlist(patterns) + return wlist + + +allowlists = load_allowlists(os.path.dirname(__file__)) -- cgit v1.2.3 From 0c1730a05945799cf65aced40063c17ffafe737f Mon Sep 17 00:00:00 2001 From: George Burgess IV Date: Wed, 24 Jun 2020 17:00:47 -0700 Subject: debug_info_test: fix up naming This replaces 'whitelist' with 'allowlist' in debug_info_test's file names, as a follow up to the previous NFC CL's cleanups. BUG=chromium:1099035 TEST=CQ+1 Change-Id: I0e99fa230a78251a6a555941db6f4758d0cf9367 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/2265050 Reviewed-by: Tiancong Wang Tested-by: George Burgess Commit-Queue: George Burgess --- debug_info_test/allowlist.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'debug_info_test/allowlist.py') diff --git a/debug_info_test/allowlist.py b/debug_info_test/allowlist.py index 978b2370..9cf42af0 100644 --- a/debug_info_test/allowlist.py +++ b/debug_info_test/allowlist.py @@ -43,11 +43,10 @@ def prepare_allowlist(patterns): return re.compile('|'.join(patterns)) -# FIXME: s/whitelist/allowlist/ in the file extension. def load_allowlists(dirname): """Load allowlists under dirname. - An allowlist ends with .whitelist. + An allowlist ends with .allowlist. Args: dirname: path to the dir. @@ -56,7 +55,7 @@ def load_allowlists(dirname): A dictionary of 'filename' -> allowlist matcher. """ wlist = {} - for fn in glob.glob(os.path.join(dirname, '*.whitelist')): + for fn in glob.glob(os.path.join(dirname, '*.allowlist')): key = os.path.splitext(os.path.basename(fn))[0] with open(fn, 'r', encoding='utf-8') as f: patterns = f.read().splitlines() -- cgit v1.2.3