diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2021-06-19 12:04:54 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2021-06-19 12:04:54 +0000 |
commit | 4ac7ca7c4f6d13380afa6b0d52313f0f18765038 (patch) | |
tree | 73936aba47fe1dc71e9cc05af9747036e935608c /debug_info_test/allowlist.py | |
parent | b75f321fc8978b92ce3db6886ccb966768f0c7a8 (diff) | |
parent | 4e4201457e5f51a132101c611c79ccff9f713c8b (diff) | |
download | toolchain-utils-4ac7ca7c4f6d13380afa6b0d52313f0f18765038.tar.gz |
Snap for 7474514 from 4e4201457e5f51a132101c611c79ccff9f713c8b to mainline-media-releaseandroid-mainline-12.0.0_r89android-mainline-12.0.0_r74android-mainline-12.0.0_r62android-mainline-12.0.0_r46android-mainline-12.0.0_r29android-mainline-12.0.0_r12android-mainline-12.0.0_r119android-mainline-12.0.0_r104android12-mainline-media-release
Change-Id: I2fe70a8ca191c683886391da7cc959d8659fac4c
Diffstat (limited to 'debug_info_test/allowlist.py')
-rw-r--r-- | debug_info_test/allowlist.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/debug_info_test/allowlist.py b/debug_info_test/allowlist.py new file mode 100644 index 00000000..9cf42af0 --- /dev/null +++ b/debug_info_test/allowlist.py @@ -0,0 +1,68 @@ +# -*- 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)) + + +def load_allowlists(dirname): + """Load allowlists under dirname. + + An allowlist ends with .allowlist. + + Args: + dirname: path to the dir. + + Returns: + A dictionary of 'filename' -> allowlist matcher. + """ + wlist = {} + 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() + 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__)) |