|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# JMS PUT COPYRIGHTS HERE |
| 4 | + |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import argparse |
| 8 | + |
| 9 | +def find_help_files(root, verbose=False): |
| 10 | + """Search for help-*.txt files, skipping .git and 3rd-party directories.""" |
| 11 | + help_files = [] |
| 12 | + skip_dirs = ['.git', '3rd-party'] |
| 13 | + for root_dir, dirs, files in os.walk(root): |
| 14 | + for sd in skip_dirs: |
| 15 | + if sd in dirs: |
| 16 | + dirs.remove(sd) |
| 17 | + |
| 18 | + for file in files: |
| 19 | + if file.startswith("help-") and file.endswith(".txt"): |
| 20 | + full_path = os.path.join(root_dir, file) |
| 21 | + help_files.append(full_path) |
| 22 | + if verbose: |
| 23 | + print(f"Found: {full_path}") |
| 24 | + return help_files |
| 25 | + |
| 26 | +def parse_ini_files(file_paths, verbose=False): |
| 27 | + """Parse INI-style files, returning a dictionary with filenames as keys.""" |
| 28 | + data = {} |
| 29 | + for file_path in file_paths: |
| 30 | + sections = {} |
| 31 | + current_section = None |
| 32 | + with open(file_path) as file: |
| 33 | + for line in file: |
| 34 | + line = line.strip() |
| 35 | + if line.startswith('#') or not line: |
| 36 | + continue |
| 37 | + if line.startswith('[') and line.endswith(']'): |
| 38 | + current_section = line[1:-1] |
| 39 | + sections[current_section] = list() |
| 40 | + elif current_section is not None: |
| 41 | + sections[current_section].append(line) |
| 42 | + |
| 43 | + data[os.path.basename(file_path)] = sections |
| 44 | + |
| 45 | + if verbose: |
| 46 | + print(f"Parsed: {file_path} ({len(sections)} sections found)") |
| 47 | + |
| 48 | + return data |
| 49 | + |
| 50 | +def generate_c_code(parsed_data): |
| 51 | + """Generate C code with an array of filenames and their corresponding INI sections.""" |
| 52 | + c_code = f"""// THIS FILE IS GENERATED AUTOMATICALLY! EDITS WILL BE LOST! |
| 53 | +// This file generated by {sys.argv[0]} |
| 54 | +
|
| 55 | +""" |
| 56 | + # Can't have embedded {} in f strings; make this a separate |
| 57 | + # addition to c_code. |
| 58 | + c_code += """#include <stdio.h> |
| 59 | +#include <string.h> |
| 60 | + |
| 61 | +typedef struct { |
| 62 | + const char *section; |
| 63 | + const char *content; |
| 64 | +} ini_entry; |
| 65 | + |
| 66 | +typedef struct { |
| 67 | + const char *filename; |
| 68 | + ini_entry *entries; |
| 69 | +} file_entry; |
| 70 | +
|
| 71 | +""" |
| 72 | + |
| 73 | + ini_arrays = [] |
| 74 | + file_entries = [] |
| 75 | + |
| 76 | + for idx, (filename, sections) in enumerate(parsed_data.items()): |
| 77 | + var_name = filename.replace('-', '_').replace('.', '_') |
| 78 | + |
| 79 | + ini_entries = [] |
| 80 | + for section, content_list in sections.items(): |
| 81 | + content = '\n'.join(content_list) |
| 82 | + c_content = content.replace('"','\\"').replace("\n", '\\n"\n"') |
| 83 | + ini_entries.append(f' {{ "{section}", "{c_content}" }}') |
| 84 | + ini_entries.append(f' {{ NULL, NULL }}') |
| 85 | + |
| 86 | + ini_array_name = f"ini_entries_{idx}" |
| 87 | + ini_arrays.append(f"static ini_entry {ini_array_name}[] = {{\n" + ",\n".join(ini_entries) + "\n};\n") |
| 88 | + file_entries.append(f' {{ "{filename}", {ini_array_name} }}') |
| 89 | + file_entries.append(f' {{ NULL, NULL }}') |
| 90 | + |
| 91 | + c_code += "\n".join(ini_arrays) + "\n" |
| 92 | + c_code += "static file_entry help_files[] = {\n" + ",\n".join(file_entries) + "\n};\n" |
| 93 | + |
| 94 | + c_code += """ |
| 95 | +
|
| 96 | +const char *opal_show_help_get_content(const char *filename, const char* topic) |
| 97 | +{ |
| 98 | + file_entry *fe; |
| 99 | + ini_entry *ie; |
| 100 | +
|
| 101 | + for (int i = 0; help_files[i].filename != NULL; ++i) { |
| 102 | + fe = &(help_files[i]); |
| 103 | + if (strcmp(fe->filename, filename) == 0) { |
| 104 | + for (int j = 0; fe->entries[j].section != NULL; ++j) { |
| 105 | + ie = &(fe->entries[j]); |
| 106 | + if (strcmp(ie->section, topic) == 0) { |
| 107 | + return ie->content; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +
|
| 113 | + return NULL; |
| 114 | +} |
| 115 | +""" |
| 116 | + |
| 117 | + return c_code |
| 118 | + |
| 119 | +#------------------------------- |
| 120 | + |
| 121 | +def main(): |
| 122 | + parser = argparse.ArgumentParser(description="Generate C code from help text INI files.") |
| 123 | + parser.add_argument("--root", |
| 124 | + required=True, |
| 125 | + help="Root directory to search for help-*.txt files") |
| 126 | + parser.add_argument("--out", |
| 127 | + required=True, |
| 128 | + help="Output C file") |
| 129 | + parser.add_argument("--verbose", |
| 130 | + action="store_true", |
| 131 | + help="Enable verbose output") |
| 132 | + args = parser.parse_args() |
| 133 | + |
| 134 | + if args.verbose: |
| 135 | + print(f"Searching in: {args.root}") |
| 136 | + |
| 137 | + file_paths = find_help_files(args.root, args.verbose) |
| 138 | + parsed_data = parse_ini_files(file_paths, args.verbose) |
| 139 | + c_code = generate_c_code(parsed_data) |
| 140 | + |
| 141 | + if os.path.exists(args.out): |
| 142 | + with open(args.out) as f: |
| 143 | + existing_content = f.read() |
| 144 | + |
| 145 | + if existing_content == c_code: |
| 146 | + if args.verbose: |
| 147 | + print(f"Help string content has not changed; not re-writing {args.out}") |
| 148 | + exit(0) |
| 149 | + |
| 150 | + with open(args.out, "w") as f: |
| 151 | + f.write(c_code) |
| 152 | + |
| 153 | + if args.verbose: |
| 154 | + print(f"Generated C code written to {args.out}") |
| 155 | + |
| 156 | +if __name__ == "__main__": |
| 157 | + main() |
0 commit comments