|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (c) 2020-2022 The Bitcoin Core developers |
| 4 | +# Distributed under the MIT software license, see the accompanying |
| 5 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 6 | +# |
| 7 | +# Check for circular dependencies |
| 8 | + |
| 9 | +import glob |
| 10 | +import os |
| 11 | +import re |
| 12 | +import subprocess |
| 13 | +import sys |
| 14 | + |
| 15 | +EXPECTED_CIRCULAR_DEPENDENCIES = ( |
| 16 | + "chainparamsbase -> util/system -> chainparamsbase", |
| 17 | + "node/blockstorage -> validation -> node/blockstorage", |
| 18 | + "index/blockfilterindex -> node/blockstorage -> validation -> index/blockfilterindex", |
| 19 | + "index/base -> validation -> index/blockfilterindex -> index/base", |
| 20 | + "index/coinstatsindex -> node/coinstats -> index/coinstatsindex", |
| 21 | + "policy/fees -> txmempool -> policy/fees", |
| 22 | + "qt/addresstablemodel -> qt/walletmodel -> qt/addresstablemodel", |
| 23 | + "qt/recentrequeststablemodel -> qt/walletmodel -> qt/recentrequeststablemodel", |
| 24 | + "qt/sendcoinsdialog -> qt/walletmodel -> qt/sendcoinsdialog", |
| 25 | + "qt/transactiontablemodel -> qt/walletmodel -> qt/transactiontablemodel", |
| 26 | + "wallet/fees -> wallet/wallet -> wallet/fees", |
| 27 | + "wallet/wallet -> wallet/walletdb -> wallet/wallet", |
| 28 | + "node/coinstats -> validation -> node/coinstats", |
| 29 | +) |
| 30 | + |
| 31 | +CODE_DIR = "src" |
| 32 | + |
| 33 | + |
| 34 | +def main(): |
| 35 | + circular_dependencies = [] |
| 36 | + exit_code = 0 |
| 37 | + os.chdir( |
| 38 | + CODE_DIR |
| 39 | + ) # We change dir before globbing since glob.glob's root_dir option is only available in Python 3.10 |
| 40 | + |
| 41 | + # Using glob.glob since subprocess.run's globbing won't work without shell=True |
| 42 | + files = [] |
| 43 | + for path in ["*", "*/*", "*/*/*"]: |
| 44 | + for extension in ["h", "cpp"]: |
| 45 | + files.extend(glob.glob(f"{path}.{extension}")) |
| 46 | + |
| 47 | + command = ["python3", "../contrib/devtools/circular-dependencies.py", *files] |
| 48 | + dependencies_output = subprocess.run( |
| 49 | + command, |
| 50 | + stdout=subprocess.PIPE, |
| 51 | + universal_newlines=True, |
| 52 | + ) |
| 53 | + |
| 54 | + for dependency_str in dependencies_output.stdout.rstrip().split("\n"): |
| 55 | + circular_dependencies.append( |
| 56 | + re.sub("^Circular dependency: ", "", dependency_str) |
| 57 | + ) |
| 58 | + |
| 59 | + # Check for an unexpected dependencies |
| 60 | + for dependency in circular_dependencies: |
| 61 | + if dependency not in EXPECTED_CIRCULAR_DEPENDENCIES: |
| 62 | + exit_code = 1 |
| 63 | + print( |
| 64 | + f'A new circular dependency in the form of "{dependency}" appears to have been introduced.\n', |
| 65 | + file=sys.stderr, |
| 66 | + ) |
| 67 | + |
| 68 | + # Check for missing expected dependencies |
| 69 | + for expected_dependency in EXPECTED_CIRCULAR_DEPENDENCIES: |
| 70 | + if expected_dependency not in circular_dependencies: |
| 71 | + exit_code = 1 |
| 72 | + print( |
| 73 | + f'Good job! The circular dependency "{expected_dependency}" is no longer present.', |
| 74 | + ) |
| 75 | + print( |
| 76 | + f"Please remove it from EXPECTED_CIRCULAR_DEPENDENCIES in {__file__}", |
| 77 | + ) |
| 78 | + print( |
| 79 | + "to make sure this circular dependency is not accidentally reintroduced.\n", |
| 80 | + ) |
| 81 | + |
| 82 | + sys.exit(exit_code) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + main() |
0 commit comments