/ contrib / devtools / gen-manpages.py
gen-manpages.py
 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      try:
54          r = subprocess.run([abspath, "--version"], stdout=subprocess.PIPE, check=True, text=True)
55      except IOError:
56          if(args.skip_missing_binaries):
57              print(f'{abspath} not found or not an executable. Skipping...', file=sys.stderr)
58              continue
59          else:
60              print(f'{abspath} not found or not an executable', file=sys.stderr)
61              sys.exit(1)
62      # take first line (which must contain version)
63      output = r.stdout.splitlines()[0]
64      # find the version e.g. v30.99.0-ce771726f3e7
65      search = re.search(r"v[0-9]\S+", output)
66      assert search
67      verstr = search.group(0)
68      # remaining lines are copyright
69      copyright = r.stdout.split('\n')[1:]
70      assert copyright[0].startswith('Copyright (C)')
71  
72      versions.append((abspath, verstr, copyright))
73  
74  if not versions:
75      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.')
76      sys.exit(1)
77  
78  if any(verstr.endswith('-dirty') for (_, verstr, _) in versions):
79      print("WARNING: Binaries were built from a dirty tree.")
80      print('man pages generated from dirty binaries should NOT be committed.')
81      print('To properly generate man pages, please commit your changes (or discard them), rebuild, then run this script again.')
82      print()
83  
84  with tempfile.NamedTemporaryFile('w', suffix='.h2m') as footer:
85      # Create copyright footer, and write it to a temporary include file.
86      # Copyright is the same for all binaries, so just use the first.
87      footer.write('[COPYRIGHT]\n')
88      footer.write('\n'.join(versions[0][2]).strip())
89      # Create SEE ALSO section
90      footer.write('\n[SEE ALSO]\n')
91      footer.write(', '.join(s.rpartition('/')[2] + '(1)' for s in BINARIES))
92      footer.write('\n')
93      footer.flush()
94  
95      # Call the binaries through help2man to produce a manual page for each of them.
96      for (abspath, verstr, _) in versions:
97          outname = os.path.join(mandir, os.path.basename(abspath) + '.1')
98          print(f'Generating {outname}…')
99          subprocess.run([help2man, '-N', '--version-string=' + verstr, '--include=' + footer.name, '-o', outname, abspath], check=True)