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