gen-manpages.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2022-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  import os
   6  import re
   7  import subprocess
   8  import sys
   9  import tempfile
  10  import argparse
  11  
  12  BINARIES = [
  13  'bin/bitcoin',
  14  'bin/bitcoind',
  15  'bin/bitcoin-cli',
  16  'bin/bitcoin-tx',
  17  'bin/bitcoin-wallet',
  18  'bin/bitcoin-util',
  19  'bin/bitcoin-qt',
  20  ]
  21  
  22  parser = argparse.ArgumentParser(
  23      formatter_class=argparse.RawDescriptionHelpFormatter,
  24  )
  25  parser.add_argument(
  26      "-s",
  27      "--skip-missing-binaries",
  28      action="store_true",
  29      default=False,
  30      help="skip generation for binaries that are not found in the build path",
  31  )
  32  args = parser.parse_args()
  33  
  34  # Paths to external utilities.
  35  git = os.getenv('GIT', 'git')
  36  help2man = os.getenv('HELP2MAN', 'help2man')
  37  
  38  # If not otherwise specified, get top directory from git.
  39  topdir = os.getenv('TOPDIR')
  40  if not topdir:
  41      r = subprocess.run([git, 'rev-parse', '--show-toplevel'], stdout=subprocess.PIPE, check=True, text=True)
  42      topdir = r.stdout.rstrip()
  43  
  44  # Get input and output directories.
  45  builddir = os.getenv('BUILDDIR', os.path.join(topdir, 'build'))
  46  mandir = os.getenv('MANDIR', os.path.join(topdir, 'doc/man'))
  47  
  48  # Verify that all the required binaries are usable, and extract copyright
  49  # message in a first pass.
  50  versions = []
  51  for relpath in BINARIES:
  52      abspath = os.path.join(builddir, relpath)
  53      # Prevent QT from emitting a localized version string for --version and --help
  54      is_qt = relpath == "bin/bitcoin-qt"
  55      try:
  56          cmd_args = ["--version"]
  57          if is_qt:
  58              cmd_args.append("--lang=en")
  59          r = subprocess.run([abspath, *cmd_args], stdout=subprocess.PIPE, check=True, text=True)
  60      except IOError:
  61          if(args.skip_missing_binaries):
  62              print(f'{abspath} not found or not an executable. Skipping...', file=sys.stderr)
  63              continue
  64          else:
  65              print(f'{abspath} not found or not an executable', file=sys.stderr)
  66              sys.exit(1)
  67      # take first line (which must contain version)
  68      output = r.stdout.splitlines()[0]
  69      # find the version e.g. v30.99.0-ce771726f3e7
  70      search = re.search(r"v[0-9]\S+", output)
  71      assert search
  72      verstr = search.group(0)
  73      # remaining lines are copyright
  74      copyright = r.stdout.split('\n')[1:]
  75      assert copyright[0].startswith('Copyright (C)')
  76  
  77      versions.append((abspath, verstr, copyright, is_qt))
  78  
  79  if not versions:
  80      print(f'No binaries found in {builddir}. Please ensure the binaries are present in {builddir}, or set another build path using the BUILDDIR env variable.')
  81      sys.exit(1)
  82  
  83  if any(verstr.endswith('-dirty') for (_, verstr, _, _) in versions):
  84      print("WARNING: Binaries were built from a dirty tree.")
  85      print('man pages generated from dirty binaries should NOT be committed.')
  86      print('To properly generate man pages, please commit your changes (or discard them), rebuild, then run this script again.')
  87      print()
  88  
  89  with tempfile.NamedTemporaryFile('w', suffix='.h2m') as footer:
  90      # Create copyright footer, and write it to a temporary include file.
  91      # Copyright is the same for all binaries, so just use the first.
  92      footer.write('[COPYRIGHT]\n')
  93      footer.write('\n'.join(versions[0][2]).strip())
  94      # Create SEE ALSO section
  95      footer.write('\n[SEE ALSO]\n')
  96      footer.write(', '.join(s.rpartition('/')[2] + '(1)' for s in BINARIES))
  97      footer.write('\n')
  98      footer.flush()
  99  
 100      # Call the binaries through help2man to produce a manual page for each of them.
 101      for (abspath, verstr, _, is_qt) in versions:
 102          outname = os.path.join(mandir, os.path.basename(abspath) + '.1')
 103          help_option = '--help-option=--help' + (' --lang=en' if is_qt else '')
 104          print(f'Generating {outname}…')
 105          subprocess.run([
 106              help2man,
 107              '-N',
 108              '--version-string=' + verstr,
 109              '--include=' + footer.name,
 110              '-o', outname,
 111              help_option,
 112              abspath,
 113          ], check=True)
 114