lint-circular-dependencies.py raw
1 #!/usr/bin/env python3
2 #
3 # Copyright (c) 2020-2022 The Limenka 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 os
10 import re
11 import subprocess
12 import sys
13
14 EXPECTED_CIRCULAR_DEPENDENCIES = (
15 "chainparamsbase -> common/args -> chainparamsbase",
16 "httprpc -> rpc/server -> httprpc",
17 "node/blockstorage -> validation -> node/blockstorage",
18 "node/utxo_snapshot -> validation -> node/utxo_snapshot",
19 "qt/addresstablemodel -> qt/walletmodel -> qt/addresstablemodel",
20 "qt/recentrequeststablemodel -> qt/walletmodel -> qt/recentrequeststablemodel",
21 "qt/sendcoinsdialog -> qt/walletmodel -> qt/sendcoinsdialog",
22 "qt/transactiontablemodel -> qt/walletmodel -> qt/transactiontablemodel",
23 "wallet/wallet -> wallet/walletdb -> wallet/wallet",
24 "kernel/coinstats -> validation -> kernel/coinstats",
25
26 # Temporary, removed in followup https://github.com/limenka/limenka/pull/24230
27 "index/base -> node/context -> net_processing -> index/blockfilterindex -> index/base",
28 )
29 EXPECTED_CIRCULAR_DEPENDENCIES = ()
30
31 CODE_DIR = "src"
32
33
34 def main():
35 circular_dependencies = []
36 exit_code = 0
37
38 os.chdir(CODE_DIR)
39 files = subprocess.check_output(
40 ['git', 'ls-files', '--', '*.h', '*.cpp'],
41 text=True,
42 ).splitlines()
43
44 command = [sys.executable, "../contrib/devtools/circular-dependencies.py", *files]
45 dependencies_output = subprocess.run(
46 command,
47 stdout=subprocess.PIPE,
48 text=True,
49 )
50
51 for dependency_str in dependencies_output.stdout.rstrip().split("\n"):
52 if dependency_str == '': continue
53 circular_dependencies.append(
54 re.sub("^Circular dependency: ", "", dependency_str)
55 )
56
57 # Check for an unexpected dependencies
58 for dependency in circular_dependencies:
59 if dependency not in EXPECTED_CIRCULAR_DEPENDENCIES:
60 exit_code = 1
61 print(
62 f'A new circular dependency in the form of "{dependency}" appears to have been introduced.\n',
63 file=sys.stderr,
64 )
65
66 # Check for missing expected dependencies
67 for expected_dependency in EXPECTED_CIRCULAR_DEPENDENCIES:
68 if expected_dependency not in circular_dependencies:
69 exit_code = 1
70 print(
71 f'Good job! The circular dependency "{expected_dependency}" is no longer present.',
72 )
73 print(
74 f"Please remove it from EXPECTED_CIRCULAR_DEPENDENCIES in {__file__}",
75 )
76 print(
77 "to make sure this circular dependency is not accidentally reintroduced.\n",
78 )
79
80 sys.exit(exit_code)
81
82
83 if __name__ == "__main__":
84 main()
85