check-doc.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2015-present The Bitcoin Core developers
   3  # Distributed under the MIT software license, see the accompanying
   4  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
   5  
   6  '''
   7  This checks if all command line args are documented.
   8  Return value is 0 to indicate no error.
   9  '''
  10  
  11  from subprocess import check_output
  12  import re
  13  
  14  FOLDER_GREP = 'src'
  15  FOLDER_TEST = 'src/test/'
  16  REGEX_ARG = r'\b(?:GetArg|GetArgs|GetBoolArg|GetIntArg|GetPathArg|IsArgSet|get_net)\("(-[^"]+)"'
  17  REGEX_DOC = r'AddArg\("(-[^"=]+?)(?:=|")'
  18  CMD_ROOT_DIR = '$(git rev-parse --show-toplevel)/{}'.format(FOLDER_GREP)
  19  CMD_GREP_ARGS = r"git grep --perl-regexp '{}' -- {} ':(exclude){}'".format(REGEX_ARG, CMD_ROOT_DIR, FOLDER_TEST)
  20  CMD_GREP_WALLET_ARGS = r"git grep --function-context 'void WalletInit::AddWalletOptions' -- {} | grep AddArg".format(CMD_ROOT_DIR)
  21  CMD_GREP_WALLET_HIDDEN_ARGS = r"git grep --function-context 'void DummyWalletInit::AddWalletOptions' -- {}".format(CMD_ROOT_DIR)
  22  CMD_GREP_DOCS = r"git grep --perl-regexp '{}' {}".format(REGEX_DOC, CMD_ROOT_DIR)
  23  # list unsupported, deprecated and duplicate args as they need no documentation
  24  SET_DOC_OPTIONAL = set(['-h', '-?', '-dbcrashratio', '-forcecompactdb', '-ipcconnect', '-ipcfd'])
  25  
  26  
  27  def lint_missing_argument_documentation():
  28      used = check_output(CMD_GREP_ARGS, shell=True, text=True).strip()
  29      docd = check_output(CMD_GREP_DOCS, shell=True, text=True).strip()
  30  
  31      args_used = set(re.findall(re.compile(REGEX_ARG), used))
  32      args_docd = set(re.findall(re.compile(REGEX_DOC), docd)).union(SET_DOC_OPTIONAL)
  33      args_need_doc = args_used.difference(args_docd)
  34      args_unknown = args_docd.difference(args_used)
  35  
  36      print("Args used        : {}".format(len(args_used)))
  37      print("Args documented  : {}".format(len(args_docd)))
  38      print("Args undocumented: {}".format(len(args_need_doc)))
  39      print(args_need_doc)
  40      print("Args unknown     : {}".format(len(args_unknown)))
  41      print(args_unknown)
  42  
  43      assert 0 == len(args_need_doc), "Please document the following arguments: {}".format(args_need_doc)
  44  
  45  
  46  def lint_missing_hidden_wallet_args():
  47      wallet_args = check_output(CMD_GREP_WALLET_ARGS, shell=True, text=True).strip()
  48      wallet_hidden_args = check_output(CMD_GREP_WALLET_HIDDEN_ARGS, shell=True, text=True).strip()
  49  
  50      wallet_args = set(re.findall(re.compile(REGEX_DOC), wallet_args))
  51      wallet_hidden_args = set(re.findall(re.compile(r'    "([^"=]+)'), wallet_hidden_args))
  52  
  53      hidden_missing = wallet_args.difference(wallet_hidden_args)
  54      if hidden_missing:
  55          assert 0, "Please add {} to the hidden args in DummyWalletInit::AddWalletOptions".format(hidden_missing)
  56  
  57  
  58  def main():
  59      lint_missing_argument_documentation()
  60      lint_missing_hidden_wallet_args()
  61  
  62  
  63  if __name__ == "__main__":
  64      main()
  65