/usr/share/dune/cmake/scripts/extract_cmake_data.py is in libdune-common-dev 2.5.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | #!/usr/bin/env python
""" This script will parse a cmake module and extract some
rst documentation from it. This might not be as elegant as
writing a Sphinx domain or using a custom extension with
cmake related directives, but it provides a straightforward
working way.
This is used by dune-common to generate the build system documentation.
Users do not want to use this!!!
"""
from __future__ import print_function
import argparse
import os
import re
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('-b', '--builddir', help='The directory where to place the produced output', required=True)
parser.add_argument('-m', '--module', help='The module to parse', required=True)
return vars(parser.parse_args())
def write_line(f, line):
if len(line) > 2:
f.write(line[2:])
else:
f.write('\n')
def read_module(args=get_args()):
modname = os.path.splitext(os.path.basename(args['module']))[0]
modpath = os.path.join(args['builddir'], 'modules')
if not os.path.exists(modpath):
os.makedirs(modpath)
modfile = os.path.join(modpath, modname + '.rst')
with open(args['module'], 'r') as i:
# mod = open(modfile, 'w')
# # Write the first block into the module rst file
# mod.write(".. _" + modname + ":\n\n")
# mod.write(modname + "\n")
# mod.write("="*len(modname) + "\n\n")
# listHeader = False
o = None
for l in i:
if not l.startswith('#'):
return
if l.startswith('# .. cmake_function'):
if o:
o.close()
cmdpath = os.path.join(args['builddir'], 'commands')
if not os.path.exists(cmdpath):
os.makedirs(cmdpath)
try:
cmd = re.findall(r'# .. cmake_function:: (.*)', l)[0]
except IndexError as e:
print("CMake doc syntax error in {}: cannot parse function on line {}".format(args['module'], l))
raise e
cmdfile = os.path.join(cmdpath, cmd + ".rst")
# if not listHeader:
# mod.write("\nThis module defines the following functions or macros:\n\n")
# listHeader = True
# mod.write("* :ref:`{}`\n".format(cmd))
o = open(cmdfile, 'w')
o.write(".. _" + cmd + ":\n\n")
o.write(cmd + "\n")
o.write("="*len(cmd) + "\n\n")
write_line(o, l)
elif l.startswith('# .. cmake_variable'):
if o:
o.close()
varpath = os.path.join(args['builddir'], 'variables')
if not os.path.exists(varpath):
os.makedirs(varpath)
try:
var = re.findall(r'# .. cmake_variable:: (.*)', l)[0]
except IndexError as e:
print("CMake doc syntax error in {}: cannot parse variable on line".format(args['module'], l))
raise e
varfile = os.path.join(varpath, var + ".rst")
o = open(varfile, 'w')
o.write(".. _" + var + ":\n\n")
o.write(var + "\n")
o.write("="*len(var) + "\n\n")
write_line(o, l)
elif l.startswith('# .. cmake_module'):
if o:
o.close()
modpath = os.path.join(args['builddir'], 'modules')
if not os.path.exists(modpath):
os.makedirs(modpath)
modfile = os.path.join(modpath, modname + ".rst")
o = open(modfile, 'w')
o.write(".. _" + modname + ":\n\n")
o.write(modname + "\n")
o.write("="*len(modname) + "\n\n")
write_line(o, l)
else:
if o:
write_line(o, l)
# Parse the given arguments
read_module()
|