2021-12-28 16:58:19 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import hashlib
|
|
|
|
import re
|
2022-01-06 16:35:14 +00:00
|
|
|
import yaml
|
2022-01-22 16:25:55 +00:00
|
|
|
import json
|
2022-01-25 15:03:01 +00:00
|
|
|
import filecmp
|
|
|
|
from contextlib import contextmanager
|
2021-12-28 16:58:19 +00:00
|
|
|
|
|
|
|
whoami = os.path.basename(sys.argv[0])
|
|
|
|
BANNER = f'''//
|
|
|
|
// This file is automatically generated by {whoami}.
|
|
|
|
// Edits will be automatically overwritten if the build is
|
|
|
|
// run in maintainer mode.
|
|
|
|
//'''
|
|
|
|
|
|
|
|
|
|
|
|
def warn(*args, **kwargs):
|
|
|
|
print(*args, file=sys.stderr, **kwargs)
|
|
|
|
|
|
|
|
|
2022-01-25 15:03:01 +00:00
|
|
|
@contextmanager
|
|
|
|
def write_file(filename):
|
|
|
|
tmpfile = filename + '.tmp'
|
|
|
|
with open(tmpfile, 'w') as f:
|
|
|
|
yield f
|
|
|
|
if os.path.exists(filename) and filecmp.cmp(filename, tmpfile, False):
|
|
|
|
os.unlink(tmpfile)
|
|
|
|
else:
|
|
|
|
os.rename(tmpfile, filename)
|
|
|
|
|
|
|
|
|
2021-12-28 16:58:19 +00:00
|
|
|
class Main:
|
2022-01-11 16:49:33 +00:00
|
|
|
SOURCES = [
|
|
|
|
whoami,
|
|
|
|
'manual/_ext/qpdf.py',
|
|
|
|
'job.yml',
|
|
|
|
'manual/cli.rst',
|
|
|
|
]
|
2022-01-06 16:35:14 +00:00
|
|
|
DESTS = {
|
|
|
|
'decl': 'libqpdf/qpdf/auto_job_decl.hh',
|
2022-01-06 16:46:40 +00:00
|
|
|
'init': 'libqpdf/qpdf/auto_job_init.hh',
|
2022-01-11 16:49:33 +00:00
|
|
|
'help': 'libqpdf/qpdf/auto_job_help.hh',
|
2022-01-22 16:25:55 +00:00
|
|
|
'schema': 'libqpdf/qpdf/auto_job_schema.hh',
|
2022-01-29 14:08:45 +00:00
|
|
|
'json_decl': 'libqpdf/qpdf/auto_job_json_decl.hh',
|
|
|
|
'json_init': 'libqpdf/qpdf/auto_job_json_init.hh',
|
2022-01-25 20:46:32 +00:00
|
|
|
# Others are added in top
|
2022-01-06 16:35:14 +00:00
|
|
|
}
|
2021-12-28 16:58:19 +00:00
|
|
|
SUMS = 'job.sums'
|
|
|
|
|
|
|
|
def main(self, args=sys.argv[1:], prog=whoami):
|
|
|
|
options = self.parse_args(args, prog)
|
|
|
|
self.top(options)
|
|
|
|
|
|
|
|
def parse_args(self, args, prog):
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
prog=prog,
|
|
|
|
description='Generate files for QPDFJob',
|
|
|
|
)
|
|
|
|
mxg = parser.add_mutually_exclusive_group(required=True)
|
|
|
|
mxg.add_argument('--check',
|
|
|
|
help='update checksums if files are not up to date',
|
|
|
|
action='store_true', default=False)
|
|
|
|
mxg.add_argument('--generate',
|
|
|
|
help='generate files from sources',
|
|
|
|
action='store_true', default=False)
|
|
|
|
return parser.parse_args(args)
|
|
|
|
|
|
|
|
def top(self, options):
|
2022-01-25 20:46:32 +00:00
|
|
|
with open('job.yml', 'r') as f:
|
|
|
|
data = yaml.safe_load(f.read())
|
|
|
|
self.config_decls = {}
|
2022-01-26 18:17:57 +00:00
|
|
|
self.declared_configs = set()
|
2022-01-25 20:46:32 +00:00
|
|
|
for o in data['options']:
|
|
|
|
config = o.get('config', None)
|
|
|
|
if config is not None:
|
|
|
|
self.DESTS[config] = f'include/qpdf/auto_job_{config}.hh'
|
|
|
|
self.config_decls[config] = []
|
|
|
|
|
2022-01-25 15:03:01 +00:00
|
|
|
if self.check_hashes():
|
|
|
|
exit(0)
|
|
|
|
elif options.check:
|
|
|
|
exit(f'{whoami}: auto job inputs have changed')
|
2021-12-28 16:58:19 +00:00
|
|
|
elif options.generate:
|
2022-01-25 20:46:32 +00:00
|
|
|
self.generate(data)
|
2021-12-28 16:58:19 +00:00
|
|
|
else:
|
|
|
|
exit(f'{whoami} unknown mode')
|
|
|
|
|
|
|
|
def get_hashes(self):
|
|
|
|
hashes = {}
|
2022-01-06 16:35:14 +00:00
|
|
|
for i in sorted([*self.SOURCES, *self.DESTS.values()]):
|
2021-12-28 16:58:19 +00:00
|
|
|
m = hashlib.sha256()
|
2022-01-06 16:35:14 +00:00
|
|
|
try:
|
|
|
|
with open(i, 'rb') as f:
|
|
|
|
m.update(f.read())
|
|
|
|
hashes[i] = m.hexdigest()
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2021-12-28 16:58:19 +00:00
|
|
|
return hashes
|
|
|
|
|
2022-01-25 15:03:01 +00:00
|
|
|
def check_hashes(self):
|
2021-12-28 16:58:19 +00:00
|
|
|
hashes = self.get_hashes()
|
|
|
|
match = False
|
|
|
|
try:
|
|
|
|
old_hashes = {}
|
|
|
|
with open(self.SUMS, 'r') as f:
|
|
|
|
for line in f.readlines():
|
|
|
|
m = re.match(r'^(\S+) (\S+)\s*$', line)
|
|
|
|
if m:
|
|
|
|
old_hashes[m.group(1)] = m.group(2)
|
|
|
|
match = old_hashes == hashes
|
|
|
|
except Exception:
|
|
|
|
pass
|
2022-01-25 15:03:01 +00:00
|
|
|
return match
|
2021-12-28 16:58:19 +00:00
|
|
|
|
|
|
|
def update_hashes(self):
|
|
|
|
hashes = self.get_hashes()
|
|
|
|
with open(self.SUMS, 'w') as f:
|
|
|
|
print(f'# Generated by {whoami}', file=f)
|
|
|
|
for k, v in hashes.items():
|
|
|
|
print(f'{k} {v}', file=f)
|
|
|
|
|
2022-01-07 22:01:10 +00:00
|
|
|
def generate_doc(self, df, f):
|
|
|
|
st_top = 0
|
|
|
|
st_topic = 1
|
|
|
|
st_option = 2
|
|
|
|
st_option_help = 3
|
|
|
|
state = st_top
|
|
|
|
|
|
|
|
indent = None
|
|
|
|
topic = None
|
|
|
|
option = None
|
|
|
|
short_text = None
|
|
|
|
long_text = None
|
|
|
|
|
2022-01-11 16:49:33 +00:00
|
|
|
# Generate a bunch of short static functions rather than a big
|
|
|
|
# member function for help. Some compilers have problems with
|
|
|
|
# very large member functions in classes in anonymous
|
|
|
|
# namespaces.
|
|
|
|
|
|
|
|
help_files = 0
|
|
|
|
help_lines = 0
|
|
|
|
|
|
|
|
self.all_topics = set(self.options_without_help)
|
|
|
|
self.referenced_topics = set()
|
2022-01-07 22:01:10 +00:00
|
|
|
|
|
|
|
def set_indent(x):
|
|
|
|
nonlocal indent
|
|
|
|
indent = ' ' * len(x)
|
|
|
|
|
2022-01-11 16:49:33 +00:00
|
|
|
def append_long_text(line, topic):
|
2022-01-07 22:01:10 +00:00
|
|
|
nonlocal indent, long_text
|
|
|
|
if line == '\n':
|
|
|
|
long_text += '\n'
|
|
|
|
elif line.startswith(indent):
|
|
|
|
long_text += line[len(indent):]
|
|
|
|
else:
|
|
|
|
long_text = long_text.strip()
|
2022-01-11 16:49:33 +00:00
|
|
|
if long_text == '':
|
|
|
|
raise Exception(f'missing long text for {topic}')
|
|
|
|
long_text += '\n'
|
2022-01-28 12:48:22 +00:00
|
|
|
if 'help' not in topic:
|
2022-01-27 17:18:47 +00:00
|
|
|
# Help for --help itself has --help=... not
|
|
|
|
# referring to specific options.
|
|
|
|
for i in re.finditer(r'--help=([^\.\s]+)', long_text):
|
|
|
|
self.referenced_topics.add(i.group(1))
|
2022-01-07 22:01:10 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
lineno = 0
|
|
|
|
for line in df.readlines():
|
2022-01-11 16:49:33 +00:00
|
|
|
if help_lines == 0:
|
|
|
|
if help_files > 0:
|
|
|
|
print('}', file=f)
|
|
|
|
help_files += 1
|
|
|
|
help_lines += 1
|
|
|
|
print(f'static void add_help_{help_files}(QPDFArgParser& ap)\n'
|
|
|
|
'{', file=f)
|
2022-01-07 22:01:10 +00:00
|
|
|
lineno += 1
|
|
|
|
if state == st_top:
|
|
|
|
m = re.match(r'^(\s*\.\. )help-topic (\S+): (.*)$', line)
|
|
|
|
if m:
|
|
|
|
set_indent(m.group(1))
|
|
|
|
topic = m.group(2)
|
|
|
|
short_text = m.group(3)
|
|
|
|
long_text = ''
|
|
|
|
state = st_topic
|
|
|
|
continue
|
2022-01-11 16:49:33 +00:00
|
|
|
m = re.match(
|
2022-01-27 13:29:40 +00:00
|
|
|
r'^(\s*\.\. )qpdf:option:: (([^=\[\s]+)([\[= ](.+))?)$',
|
2022-01-11 16:49:33 +00:00
|
|
|
line)
|
2022-01-07 22:01:10 +00:00
|
|
|
if m:
|
|
|
|
if topic is None:
|
|
|
|
raise Exception('option seen before topic')
|
|
|
|
set_indent(m.group(1))
|
|
|
|
option = m.group(3)
|
|
|
|
synopsis = m.group(2)
|
|
|
|
if synopsis.endswith('`'):
|
|
|
|
raise Exception(
|
|
|
|
f'stray ` at end of option line (line {lineno})')
|
|
|
|
if synopsis != option:
|
|
|
|
long_text = synopsis + '\n'
|
|
|
|
else:
|
|
|
|
long_text = ''
|
|
|
|
state = st_option
|
|
|
|
continue
|
|
|
|
elif state == st_topic:
|
2022-01-11 16:49:33 +00:00
|
|
|
if append_long_text(line, topic):
|
|
|
|
self.all_topics.add(topic)
|
|
|
|
print(f'ap.addHelpTopic("{topic}", "{short_text}",'
|
2022-01-07 22:01:10 +00:00
|
|
|
f' R"({long_text})");', file=f)
|
2022-01-11 16:49:33 +00:00
|
|
|
help_lines += 1
|
2022-01-07 22:01:10 +00:00
|
|
|
state = st_top
|
|
|
|
elif state == st_option:
|
|
|
|
if line == '\n' or line.startswith(indent):
|
|
|
|
m = re.match(r'^(\s*\.\. )help: (.*)$', line)
|
|
|
|
if m:
|
|
|
|
set_indent(m.group(1))
|
|
|
|
short_text = m.group(2)
|
|
|
|
state = st_option_help
|
|
|
|
else:
|
2022-01-11 16:49:33 +00:00
|
|
|
raise Exception('option without help text')
|
2022-01-07 22:01:10 +00:00
|
|
|
state = st_top
|
|
|
|
elif state == st_option_help:
|
2022-01-11 16:49:33 +00:00
|
|
|
if append_long_text(line, option):
|
|
|
|
if option in self.options_without_help:
|
|
|
|
self.options_without_help.remove(option)
|
|
|
|
else:
|
|
|
|
raise Exception(
|
|
|
|
f'help for unknown option {option},'
|
|
|
|
f' lineno={lineno}')
|
2022-01-22 16:25:55 +00:00
|
|
|
if option not in self.help_options:
|
|
|
|
self.jdata[option[2:]]['help'] = short_text
|
2022-01-11 16:49:33 +00:00
|
|
|
print(f'ap.addOptionHelp("{option}", "{topic}",'
|
2022-01-07 22:01:10 +00:00
|
|
|
f' "{short_text}", R"({long_text})");', file=f)
|
2022-01-11 16:49:33 +00:00
|
|
|
help_lines += 1
|
2022-01-07 22:01:10 +00:00
|
|
|
state = st_top
|
2022-01-11 16:49:33 +00:00
|
|
|
if help_lines == 20:
|
|
|
|
help_lines = 0
|
|
|
|
print('}', file=f)
|
|
|
|
print('static void add_help(QPDFArgParser& ap)\n{', file=f)
|
|
|
|
for i in range(help_files):
|
|
|
|
print(f' add_help_{i+1}(ap);', file=f)
|
|
|
|
print('ap.addHelpFooter("For detailed help, visit'
|
|
|
|
' the qpdf manual: https://qpdf.readthedocs.io\\n");', file=f)
|
|
|
|
print('}\n', file=f)
|
|
|
|
for i in self.referenced_topics:
|
|
|
|
if i not in self.all_topics:
|
|
|
|
raise Exception(f'help text referenced --help={i}')
|
|
|
|
for i in self.options_without_help:
|
|
|
|
raise Exception(
|
|
|
|
'Options without help: ' +
|
|
|
|
', '.join(self.options_without_help))
|
2022-01-07 22:01:10 +00:00
|
|
|
|
2022-01-25 20:46:32 +00:00
|
|
|
def generate(self, data):
|
2021-12-28 16:58:19 +00:00
|
|
|
warn(f'{whoami}: regenerating auto job files')
|
2022-01-06 16:35:14 +00:00
|
|
|
self.validate(data)
|
2022-01-22 16:25:55 +00:00
|
|
|
# Add the built-in help options to tables that we populate as
|
|
|
|
# we read job.yml since we won't encounter these in job.yml
|
|
|
|
self.help_options = set(
|
2022-01-11 16:49:33 +00:00
|
|
|
['--completion-bash', '--completion-zsh', '--help']
|
|
|
|
)
|
2022-01-22 16:25:55 +00:00
|
|
|
self.options_without_help = set(self.help_options)
|
2022-01-21 14:07:24 +00:00
|
|
|
self.prepare(data)
|
2022-01-25 15:03:01 +00:00
|
|
|
with write_file(self.DESTS['decl']) as f:
|
2022-01-06 18:23:15 +00:00
|
|
|
print(BANNER, file=f)
|
2022-01-21 14:07:24 +00:00
|
|
|
for i in self.decls:
|
|
|
|
print(i, file=f)
|
2022-01-25 15:03:01 +00:00
|
|
|
with write_file(self.DESTS['init']) as f:
|
2022-01-06 18:23:15 +00:00
|
|
|
print(BANNER, file=f)
|
2022-01-21 14:07:24 +00:00
|
|
|
for i in self.init:
|
|
|
|
print(i, file=f)
|
2022-01-25 15:03:01 +00:00
|
|
|
with write_file(self.DESTS['help']) as f:
|
2022-01-11 16:49:33 +00:00
|
|
|
with open('manual/cli.rst', 'r') as df:
|
|
|
|
print(BANNER, file=f)
|
|
|
|
self.generate_doc(df, f)
|
2022-01-22 16:25:55 +00:00
|
|
|
self.generate_schema(data)
|
2022-01-25 15:03:01 +00:00
|
|
|
with write_file(self.DESTS['schema']) as f:
|
2022-01-22 16:25:55 +00:00
|
|
|
print('static constexpr char const* JOB_SCHEMA_DATA = R"(' +
|
|
|
|
json.dumps(self.schema, indent=2, separators=(',', ': ')) +
|
|
|
|
')";', file=f)
|
2022-01-25 20:46:32 +00:00
|
|
|
for k, v in self.config_decls.items():
|
|
|
|
with write_file(self.DESTS[k]) as f:
|
|
|
|
print(BANNER, file=f)
|
|
|
|
for i in v:
|
|
|
|
print(i, file=f)
|
2022-01-29 14:08:45 +00:00
|
|
|
with write_file(self.DESTS['json_decl']) as f:
|
|
|
|
print(BANNER, file=f)
|
|
|
|
for i in self.json_decls:
|
|
|
|
print(i, file=f)
|
|
|
|
with write_file(self.DESTS['json_init']) as f:
|
|
|
|
print(BANNER, file=f)
|
|
|
|
for i in self.json_init:
|
|
|
|
print(i, file=f)
|
2021-12-28 16:58:19 +00:00
|
|
|
|
|
|
|
# Update hashes last to ensure that this will be rerun in the
|
|
|
|
# event of a failure.
|
|
|
|
self.update_hashes()
|
2022-01-06 16:35:14 +00:00
|
|
|
# DON'T ADD CODE TO generate AFTER update_hashes
|
|
|
|
|
2022-01-25 20:46:32 +00:00
|
|
|
def handle_trivial(self, i, identifier, cfg, prefix, kind, v):
|
|
|
|
decl_arg = 1
|
2022-01-25 11:58:15 +00:00
|
|
|
if kind == 'bare':
|
2022-01-25 20:46:32 +00:00
|
|
|
decl_arg = 0
|
2022-01-25 11:58:15 +00:00
|
|
|
self.init.append(f'this->ap.addBare("{i}", '
|
2022-01-25 16:07:53 +00:00
|
|
|
f'[this](){{{cfg}->{identifier}();}});')
|
2022-01-25 11:58:15 +00:00
|
|
|
elif kind == 'optional_parameter':
|
|
|
|
self.init.append(f'this->ap.addOptionalParameter("{i}", '
|
2022-01-25 16:07:53 +00:00
|
|
|
f'[this](char *x){{{cfg}->{identifier}(x);}});')
|
2022-01-25 11:58:15 +00:00
|
|
|
elif kind == 'required_parameter':
|
|
|
|
self.init.append(f'this->ap.addRequiredParameter("{i}", '
|
2022-01-25 16:07:53 +00:00
|
|
|
f'[this](char *x){{{cfg}->{identifier}(x);}}'
|
2022-01-25 11:58:15 +00:00
|
|
|
f', "{v}");')
|
|
|
|
elif kind == 'required_choices':
|
|
|
|
self.init.append(f'this->ap.addChoices("{i}", '
|
2022-01-25 16:07:53 +00:00
|
|
|
f'[this](char *x){{{cfg}->{identifier}(x);}}'
|
2022-01-25 11:58:15 +00:00
|
|
|
f', true, {v}_choices);')
|
|
|
|
elif kind == 'optional_choices':
|
|
|
|
self.init.append(f'this->ap.addChoices("{i}", '
|
2022-01-25 16:07:53 +00:00
|
|
|
f'[this](char *x){{{cfg}->{identifier}(x);}}'
|
2022-01-25 11:58:15 +00:00
|
|
|
f', false, {v}_choices);')
|
2022-01-26 18:17:57 +00:00
|
|
|
|
2022-01-25 20:46:32 +00:00
|
|
|
# Generate declarations for config methods separately by
|
|
|
|
# config object.
|
2022-01-26 22:02:30 +00:00
|
|
|
config_prefix = prefix + 'Config'
|
2022-01-25 20:46:32 +00:00
|
|
|
arg = ''
|
|
|
|
if decl_arg:
|
|
|
|
arg = 'char const* parameter'
|
2022-01-26 22:18:03 +00:00
|
|
|
fn = f'{config_prefix}* {identifier}({arg})'
|
2022-01-26 18:17:57 +00:00
|
|
|
if fn not in self.declared_configs:
|
|
|
|
self.declared_configs.add(fn)
|
|
|
|
self.config_decls[cfg].append(f'QPDF_DLL {fn};')
|
2022-01-25 11:58:15 +00:00
|
|
|
|
|
|
|
def handle_flag(self, i, identifier, kind, v):
|
|
|
|
if kind == 'bare':
|
|
|
|
self.decls.append(f'void {identifier}();')
|
|
|
|
self.init.append(f'this->ap.addBare("{i}", '
|
|
|
|
f'b(&ArgParser::{identifier}));')
|
|
|
|
elif kind == 'optional_parameter':
|
|
|
|
self.decls.append(f'void {identifier}(char *);')
|
|
|
|
self.init.append(f'this->ap.addOptionalParameter("{i}", '
|
|
|
|
f'p(&ArgParser::{identifier}));')
|
|
|
|
elif kind == 'required_parameter':
|
|
|
|
self.decls.append(f'void {identifier}(char *);')
|
|
|
|
self.init.append(f'this->ap.addRequiredParameter("{i}", '
|
|
|
|
f'p(&ArgParser::{identifier})'
|
|
|
|
f', "{v}");')
|
|
|
|
elif kind == 'required_choices':
|
|
|
|
self.decls.append(f'void {identifier}(char *);')
|
|
|
|
self.init.append(f'this->ap.addChoices("{i}", '
|
|
|
|
f'p(&ArgParser::{identifier})'
|
|
|
|
f', true, {v}_choices);')
|
|
|
|
elif kind == 'optional_choices':
|
|
|
|
self.decls.append(f'void {identifier}(char *);')
|
|
|
|
self.init.append(f'this->ap.addChoices("{i}", '
|
|
|
|
f'p(&ArgParser::{identifier})'
|
|
|
|
f', false, {v}_choices);')
|
|
|
|
|
2022-01-21 14:07:24 +00:00
|
|
|
def prepare(self, data):
|
|
|
|
self.decls = []
|
|
|
|
self.init = []
|
2022-01-29 14:08:45 +00:00
|
|
|
self.json_decls = []
|
|
|
|
self.json_init = []
|
2022-01-26 22:31:31 +00:00
|
|
|
self.jdata = {}
|
2022-01-29 23:26:11 +00:00
|
|
|
self.by_table = {}
|
2022-01-22 16:25:55 +00:00
|
|
|
|
2022-01-29 23:26:11 +00:00
|
|
|
def add_jdata(flag, table, details):
|
2022-01-22 16:25:55 +00:00
|
|
|
nonlocal self
|
|
|
|
if table == 'help':
|
|
|
|
self.help_options.add(f'--{flag}')
|
|
|
|
elif flag in self.jdata:
|
2022-01-29 23:26:11 +00:00
|
|
|
self.jdata[flag]['tables'][table] = details
|
2022-01-22 16:25:55 +00:00
|
|
|
else:
|
|
|
|
self.jdata[flag] = {
|
2022-01-29 23:26:11 +00:00
|
|
|
'tables': {table: details},
|
2022-01-22 16:25:55 +00:00
|
|
|
}
|
2022-01-06 16:35:14 +00:00
|
|
|
|
2022-01-21 14:07:24 +00:00
|
|
|
self.init.append('auto b = [this](void (ArgParser::*f)()) {')
|
|
|
|
self.init.append(' return QPDFArgParser::bindBare(f, this);')
|
|
|
|
self.init.append('};')
|
|
|
|
self.init.append('auto p = [this](void (ArgParser::*f)(char *)) {')
|
|
|
|
self.init.append(' return QPDFArgParser::bindParam(f, this);')
|
|
|
|
self.init.append('};')
|
|
|
|
self.init.append('')
|
|
|
|
for k, v in data['choices'].items():
|
2022-01-29 23:26:11 +00:00
|
|
|
s = f'static char const* {k}_choices[] = {{'
|
2022-01-21 14:07:24 +00:00
|
|
|
for i in v:
|
|
|
|
s += f'"{i}", '
|
2022-01-29 23:26:11 +00:00
|
|
|
s += '0};'
|
|
|
|
self.init.append(s)
|
|
|
|
self.json_init.append(s)
|
2022-01-21 14:07:24 +00:00
|
|
|
self.init.append('')
|
2022-01-29 23:26:11 +00:00
|
|
|
self.json_init.append('')
|
2022-01-06 16:35:14 +00:00
|
|
|
|
2022-01-06 18:23:15 +00:00
|
|
|
for o in data['options']:
|
|
|
|
table = o['table']
|
|
|
|
if table in ('main', 'help'):
|
|
|
|
continue
|
2022-01-21 14:07:24 +00:00
|
|
|
i = self.to_identifier(table, 'O', True)
|
|
|
|
self.decls.append(f'static constexpr char const* {i} = "{table}";')
|
|
|
|
self.decls.append('')
|
2022-01-06 18:23:15 +00:00
|
|
|
for o in data['options']:
|
|
|
|
table = o['table']
|
2022-01-25 11:58:15 +00:00
|
|
|
config = o.get('config', None)
|
2022-01-25 20:46:32 +00:00
|
|
|
table_prefix = o.get('prefix', '')
|
|
|
|
arg_prefix = 'arg' + table_prefix
|
2022-01-29 23:26:11 +00:00
|
|
|
config_prefix = o.get('config_prefix', table_prefix)
|
|
|
|
manual = o.get('manual', [])
|
|
|
|
json_prefix = table_prefix or table
|
|
|
|
self.by_table[json_prefix] = {
|
|
|
|
'config': config,
|
|
|
|
'manual': manual,
|
|
|
|
}
|
2022-01-06 19:26:41 +00:00
|
|
|
if table == 'main':
|
2022-01-21 14:07:24 +00:00
|
|
|
self.init.append('this->ap.selectMainOptionTable();')
|
2022-01-06 19:26:41 +00:00
|
|
|
elif table == 'help':
|
2022-01-21 14:07:24 +00:00
|
|
|
self.init.append('this->ap.selectHelpOptionTable();')
|
2022-01-06 19:26:41 +00:00
|
|
|
else:
|
|
|
|
identifier = self.to_identifier(table, 'argEnd', False)
|
2022-01-21 14:07:24 +00:00
|
|
|
self.init.append(f'this->ap.registerOptionTable("{table}",'
|
|
|
|
f' b(&ArgParser::{identifier}));')
|
2022-01-06 19:26:41 +00:00
|
|
|
if o.get('positional', False):
|
2022-01-25 20:46:32 +00:00
|
|
|
self.decls.append(f'void {arg_prefix}Positional(char*);')
|
2022-01-21 14:07:24 +00:00
|
|
|
self.init.append('this->ap.addPositional('
|
2022-01-25 20:46:32 +00:00
|
|
|
f'p(&ArgParser::{arg_prefix}Positional));')
|
2022-01-25 11:58:15 +00:00
|
|
|
flags = {}
|
|
|
|
|
2022-01-06 19:26:41 +00:00
|
|
|
for i in o.get('bare', []):
|
2022-01-25 11:58:15 +00:00
|
|
|
flags[i] = ['bare', None]
|
2022-01-06 19:26:41 +00:00
|
|
|
for i in o.get('optional_parameter', []):
|
2022-01-25 11:58:15 +00:00
|
|
|
flags[i] = ['optional_parameter', None]
|
2022-01-21 14:07:24 +00:00
|
|
|
for i, v in o.get('required_parameter', {}).items():
|
2022-01-25 11:58:15 +00:00
|
|
|
flags[i] = ['required_parameter', v]
|
2022-01-21 14:07:24 +00:00
|
|
|
for i, v in o.get('required_choices', {}).items():
|
2022-01-25 11:58:15 +00:00
|
|
|
flags[i] = ['required_choices', v]
|
2022-01-21 14:07:24 +00:00
|
|
|
for i, v in o.get('optional_choices', {}).items():
|
2022-01-25 11:58:15 +00:00
|
|
|
flags[i] = ['optional_choices', v]
|
|
|
|
self.options_without_help.add(f'--{i}')
|
|
|
|
|
|
|
|
for i, [kind, v] in flags.items():
|
2022-01-21 14:07:24 +00:00
|
|
|
self.options_without_help.add(f'--{i}')
|
2022-01-29 23:26:11 +00:00
|
|
|
add_jdata(i, json_prefix, [kind, v])
|
|
|
|
if config is None or i in manual:
|
2022-01-25 20:46:32 +00:00
|
|
|
identifier = self.to_identifier(i, arg_prefix, False)
|
2022-01-25 11:58:15 +00:00
|
|
|
self.handle_flag(i, identifier, kind, v)
|
|
|
|
else:
|
|
|
|
identifier = self.to_identifier(i, '', False)
|
2022-01-25 20:46:32 +00:00
|
|
|
self.handle_trivial(
|
2022-01-29 23:26:11 +00:00
|
|
|
i, identifier, config, config_prefix, kind, v)
|
2022-01-25 11:58:15 +00:00
|
|
|
|
2022-01-21 14:07:24 +00:00
|
|
|
if table not in ('main', 'help'):
|
|
|
|
identifier = self.to_identifier(table, 'argEnd', False)
|
|
|
|
self.decls.append(f'void {identifier}();')
|
2022-01-22 16:25:55 +00:00
|
|
|
|
2022-01-29 23:26:11 +00:00
|
|
|
def handle_json_trivial(self, key, fdata):
|
|
|
|
config = None
|
|
|
|
for t, [kind, v] in fdata['tables'].items():
|
|
|
|
# We have determined that all tables, if multiple, have
|
|
|
|
# the same config.
|
|
|
|
tdata = self.by_table[t]
|
|
|
|
config = tdata['config']
|
|
|
|
if kind == 'bare':
|
|
|
|
self.json_init.append(
|
|
|
|
f'addBare("{key}", [this]() {{ {config}->{key}(); }});')
|
|
|
|
elif kind == 'optional_parameter' or kind == 'required_parameter':
|
|
|
|
# No optional parameters in json
|
|
|
|
self.json_init.append(
|
|
|
|
f'addParameter("{key}", [this](char const* p)'
|
|
|
|
f' {{ {config}->{key}(p); }});')
|
|
|
|
elif kind == 'optional_choices' or kind == 'required_choices':
|
|
|
|
# No optional choices in json
|
|
|
|
self.json_init.append(
|
|
|
|
f'addChoices("{key}", {v}_choices,'
|
|
|
|
f' [this](char const* p) {{ {config}->{key}(p); }});')
|
|
|
|
|
|
|
|
def handle_json_manual(self, key, path):
|
|
|
|
method = re.sub(r'\.([a-zA-Z0-9])',
|
|
|
|
lambda x: x.group(1).upper(),
|
|
|
|
f'setup{path}.{key}')
|
|
|
|
self.json_decls.append(
|
|
|
|
f'void {method}(std::string const&);')
|
|
|
|
self.json_init.append(
|
|
|
|
f'doSetup("{key}", bindSetup(&Handlers::{method}));')
|
2022-01-22 16:25:55 +00:00
|
|
|
|
2022-01-30 19:54:18 +00:00
|
|
|
def option_to_json_key(self, s):
|
|
|
|
return self.to_identifier(s, '', False)
|
|
|
|
|
|
|
|
def build_schema(self, j, s, flag, path, expected, options_seen):
|
|
|
|
if flag:
|
|
|
|
identifier = self.to_identifier(path, '', False)
|
|
|
|
self.json_decls.append(f'void begin{identifier}(JSON);')
|
|
|
|
self.json_decls.append(f'void end{identifier}();')
|
|
|
|
self.json_init.append(
|
|
|
|
f'beginDict("{flag}",'
|
|
|
|
f' bindJSON(&Handlers::begin{identifier}),'
|
|
|
|
f' bindBare(&Handlers::end{identifier})); // {path}')
|
|
|
|
for k, v in j.items():
|
|
|
|
is_trivial = False
|
|
|
|
if k in expected:
|
|
|
|
is_trivial = True
|
|
|
|
common_config = None
|
|
|
|
for t in expected[k]['tables']:
|
|
|
|
tdata = self.by_table[t]
|
|
|
|
if k in tdata['manual']:
|
|
|
|
is_trivial = False
|
|
|
|
if common_config is None:
|
|
|
|
common_config = tdata['config']
|
|
|
|
elif common_config != tdata['config']:
|
|
|
|
is_trivial = False
|
|
|
|
elif not (k.startswith('_') or isinstance(v, str)):
|
|
|
|
raise Exception(f'json: unknown key {k}')
|
|
|
|
if k.startswith('_'):
|
|
|
|
schema_key = k[1:]
|
|
|
|
else:
|
|
|
|
schema_key = re.sub(r'[^\.]+\.', '', k)
|
|
|
|
schema_key = self.option_to_json_key(schema_key)
|
|
|
|
schema_value = v
|
|
|
|
is_dict = False
|
|
|
|
if k in expected:
|
|
|
|
options_seen.add(re.sub('^_', '', k))
|
|
|
|
if v is None:
|
|
|
|
schema_value = re.sub(
|
|
|
|
r'--(\S+)',
|
|
|
|
lambda x: self.option_to_json_key(x.group(1)),
|
|
|
|
expected[k]['help'])
|
|
|
|
if (isinstance(v, dict)):
|
|
|
|
is_dict = True
|
|
|
|
schema_value = {}
|
|
|
|
self.build_schema(v, schema_value,
|
|
|
|
schema_key, f'{path}.{schema_key}',
|
|
|
|
expected, options_seen)
|
|
|
|
elif (isinstance(v, list)):
|
|
|
|
if len(v) != 1:
|
|
|
|
raise Exception('json contains array with length != 1')
|
|
|
|
if isinstance(v[0], dict):
|
|
|
|
is_dict = True
|
|
|
|
schema_value = [{}]
|
|
|
|
subpath = f'{path}.{schema_key}'
|
|
|
|
identifier = self.to_identifier(subpath, '', False)
|
|
|
|
self.json_decls.append(
|
|
|
|
f'void begin{identifier}Array(JSON);')
|
|
|
|
self.json_decls.append(
|
|
|
|
f'void end{identifier}Array();')
|
|
|
|
self.json_init.append(
|
|
|
|
f'beginArray("{flag}",'
|
|
|
|
f' bindJSON(&Handlers::begin{identifier}Array),'
|
|
|
|
f' bindBare(&Handlers::end{identifier}Array));'
|
|
|
|
f' // {subpath}[]')
|
|
|
|
self.build_schema(v[0], schema_value[0],
|
|
|
|
schema_key, subpath,
|
|
|
|
expected, options_seen)
|
|
|
|
self.json_init.append(
|
|
|
|
f'endContainer(); // {subpath}[]')
|
|
|
|
elif schema_value is None:
|
|
|
|
raise Exception(f'unknown schema value for {k}')
|
|
|
|
s[schema_key] = schema_value
|
|
|
|
if not is_dict:
|
|
|
|
if is_trivial:
|
|
|
|
self.handle_json_trivial(schema_key, expected[k])
|
|
|
|
else:
|
|
|
|
self.handle_json_manual(schema_key, path)
|
|
|
|
if flag:
|
|
|
|
self.json_init.append(f'endContainer(); // {path}')
|
|
|
|
|
2022-01-29 23:26:11 +00:00
|
|
|
def generate_schema(self, data):
|
2022-01-22 16:25:55 +00:00
|
|
|
# Check to make sure that every command-line option is
|
|
|
|
# represented either in data['json'] or data['no-json'].
|
|
|
|
|
|
|
|
# Build a list of options that we expect. If an option appears
|
|
|
|
# once, we just expect to see it once. If it appears in more
|
|
|
|
# than one options table, we need to see a separate version of
|
2022-01-29 23:26:11 +00:00
|
|
|
# it for each option table. It is represented in job.yml
|
|
|
|
# prepended with the table prefix. The table prefix is removed
|
2022-01-22 16:25:55 +00:00
|
|
|
# in the schema.
|
|
|
|
expected = {}
|
|
|
|
for k, v in self.jdata.items():
|
|
|
|
tables = v['tables']
|
|
|
|
if len(tables) == 1:
|
|
|
|
expected[k] = {**v}
|
|
|
|
else:
|
|
|
|
for t in sorted(tables):
|
|
|
|
expected[f'{t}.{k}'] = {**v}
|
|
|
|
options_seen = set(data['no-json'])
|
|
|
|
|
|
|
|
# Walk through the json information building the schema as we
|
|
|
|
# go. This verifies consistency between command-line options
|
|
|
|
# and the json section of the data and builds up a schema by
|
|
|
|
# populating with help information as available.
|
2022-01-30 19:54:18 +00:00
|
|
|
self.schema = {}
|
|
|
|
self.build_schema(data['json'], self.schema, '', '',
|
|
|
|
expected, options_seen)
|
2022-01-22 16:25:55 +00:00
|
|
|
if options_seen != set(expected.keys()):
|
|
|
|
raise Exception('missing from json: ' +
|
|
|
|
str(set(expected.keys()) - options_seen))
|
2022-01-21 14:07:24 +00:00
|
|
|
|
|
|
|
def check_keys(self, what, d, exp):
|
|
|
|
if not isinstance(d, dict):
|
|
|
|
exit(f'{what} is not a dictionary')
|
|
|
|
actual = set(d.keys())
|
|
|
|
extra = actual - exp
|
|
|
|
if extra:
|
|
|
|
exit(f'{what}: unknown keys = {extra}')
|
|
|
|
|
|
|
|
def validate(self, data):
|
2022-01-22 16:25:55 +00:00
|
|
|
self.check_keys('top', data, set(
|
|
|
|
['choices', 'options', 'no-json', 'json']))
|
2022-01-21 14:07:24 +00:00
|
|
|
for o in data['options']:
|
|
|
|
self.check_keys('top', o, set(
|
2022-01-26 22:02:30 +00:00
|
|
|
['table', 'prefix', 'config', 'config_prefix',
|
2022-01-26 18:17:57 +00:00
|
|
|
'manual', 'bare', 'positional',
|
2022-01-21 14:07:24 +00:00
|
|
|
'optional_parameter', 'required_parameter',
|
2022-01-26 18:17:57 +00:00
|
|
|
'required_choices', 'optional_choices']))
|
2022-01-21 14:07:24 +00:00
|
|
|
|
|
|
|
def to_identifier(self, label, prefix, const):
|
|
|
|
identifier = re.sub(r'[^a-zA-Z0-9]', '_', label)
|
|
|
|
if const:
|
|
|
|
identifier = f'{prefix}_{identifier.upper()}'
|
|
|
|
else:
|
2022-01-22 16:25:55 +00:00
|
|
|
if prefix:
|
2022-01-29 23:26:11 +00:00
|
|
|
identifier = f'{prefix}_{identifier}'
|
2022-01-21 14:07:24 +00:00
|
|
|
identifier = re.sub(r'_([a-z])',
|
|
|
|
lambda x: x.group(1).upper(),
|
|
|
|
identifier).replace('_', '')
|
|
|
|
return identifier
|
2022-01-06 16:46:40 +00:00
|
|
|
|
2021-12-28 16:58:19 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
os.chdir(os.path.dirname(os.path.realpath(__file__)))
|
|
|
|
Main().main()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
exit(130)
|