lint-circular-dependencies.py raw

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