xref: /aosp_15_r20/external/angle/build/gn_ast/utils.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker# Lint as: python3
2*8975f5c5SAndroid Build Coastguard Worker# Copyright 2021 The Chromium Authors
3*8975f5c5SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker# found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker
6*8975f5c5SAndroid Build Coastguard Workerimport logging
7*8975f5c5SAndroid Build Coastguard Workerimport os
8*8975f5c5SAndroid Build Coastguard Workerimport pathlib
9*8975f5c5SAndroid Build Coastguard Workerimport subprocess
10*8975f5c5SAndroid Build Coastguard Worker
11*8975f5c5SAndroid Build Coastguard Worker# These paths should be relative to repository root.
12*8975f5c5SAndroid Build Coastguard Worker_BAD_FILES = [
13*8975f5c5SAndroid Build Coastguard Worker    # Malformed BUILD.gn file, remove this entry once it is fixed.
14*8975f5c5SAndroid Build Coastguard Worker    "third_party/swiftshader/tests/VulkanUnitTests/BUILD.gn",
15*8975f5c5SAndroid Build Coastguard Worker]
16*8975f5c5SAndroid Build Coastguard Worker
17*8975f5c5SAndroid Build Coastguard Worker
18*8975f5c5SAndroid Build Coastguard Workerdef is_bad_gn_file(filepath: str, root: pathlib.Path) -> bool:
19*8975f5c5SAndroid Build Coastguard Worker    relpath = os.path.relpath(filepath, root)
20*8975f5c5SAndroid Build Coastguard Worker    for bad_filepath in _BAD_FILES:
21*8975f5c5SAndroid Build Coastguard Worker        if relpath == bad_filepath:
22*8975f5c5SAndroid Build Coastguard Worker            logging.info(f'Skipping {relpath}: found in _BAD_FILES list.')
23*8975f5c5SAndroid Build Coastguard Worker            return True
24*8975f5c5SAndroid Build Coastguard Worker    if not os.access(filepath, os.R_OK | os.W_OK):
25*8975f5c5SAndroid Build Coastguard Worker        logging.info(f'Skipping {relpath}: Cannot read and write to it.')
26*8975f5c5SAndroid Build Coastguard Worker        return True
27*8975f5c5SAndroid Build Coastguard Worker    return False
28*8975f5c5SAndroid Build Coastguard Worker
29*8975f5c5SAndroid Build Coastguard Worker
30*8975f5c5SAndroid Build Coastguard Workerdef is_git_ignored(root: pathlib.Path, filepath: str) -> bool:
31*8975f5c5SAndroid Build Coastguard Worker    # The command git check-ignore exits with 0 if the path is ignored, 1 if it
32*8975f5c5SAndroid Build Coastguard Worker    # is not ignored.
33*8975f5c5SAndroid Build Coastguard Worker    exit_code = subprocess.run(['git', 'check-ignore', '-q', filepath],
34*8975f5c5SAndroid Build Coastguard Worker                               cwd=root).returncode
35*8975f5c5SAndroid Build Coastguard Worker    return exit_code == 0
36