|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# generate_complist() |
| 3 | +# Compares the entries in the component list (components.adoc) with the |
| 4 | +# available man pages and adds tables for missing components. |
| 5 | +# generate_links() |
| 6 | +# Generates a copy of components.adoc with added links to the man pages for the components. |
| 7 | + |
| 8 | +import os |
| 9 | +import re |
| 10 | +import sys |
| 11 | + |
| 12 | +man1_path = '../docs/man/man1' |
| 13 | +man1_files = {f for f in os.listdir(man1_path) if os.path.isfile(os.path.join(man1_path, f))} |
| 14 | +man9_path = '../docs/man/man9' |
| 15 | +man9_files = {f for f in os.listdir(man9_path) if os.path.isfile(os.path.join(man9_path, f))} |
| 16 | +man_files = man1_files.union(man9_files) |
| 17 | +complist_doc = set() |
| 18 | +miss_in_man = set() |
| 19 | +# complist_path = '../docs/src/hal/components.adoc' |
| 20 | + |
| 21 | +def generate_complist(complist_path): |
| 22 | + file1 = open(complist_path, 'r') |
| 23 | + for line in file1: |
| 24 | + if line[0] == '|' and line[1] != '=': |
| 25 | + splitted = line.split('|') |
| 26 | + if splitted[1].strip() != '': |
| 27 | + if 'link:' in splitted[1]: |
| 28 | + link = re.search('(?<=link:).*(?=\\[)', splitted[1]).group() |
| 29 | + comp_man = re.search("[a-zA-Z0-9-_\\.]+(?=\\.html)", link).group() |
| 30 | + if os.path.isfile(os.path.join('../docs/html/hal',link)): |
| 31 | + complist_doc.add(comp_man) |
| 32 | + else: |
| 33 | + print('gen_complist: Broken link:', link, file=sys.stderr) |
| 34 | + miss_in_man.add(comp_man.split(".")[0]) |
| 35 | + else: |
| 36 | + print("gen_complist: Component \"" + splitted[1].strip() + "\" without link") |
| 37 | + miss_in_man.add(splitted[1]) |
| 38 | + file1.close() |
| 39 | + miss_in_list = man_files.difference(complist_doc) |
| 40 | + |
| 41 | + gen_filename = '../docs/src/hal/components_gen.adoc' |
| 42 | + file2 = open(gen_filename, 'w') |
| 43 | + if len(miss_in_list) > 0: |
| 44 | + file2.write('=== Not categorized (auto generated from man pages)\n') |
| 45 | + file2.write('[{tab_options}]\n|=======================\n') |
| 46 | + for i in sorted(miss_in_list): |
| 47 | + file2.write('| ' + i + ' |||\n') |
| 48 | + file2.write('|=======================\n') |
| 49 | + if len(miss_in_man) > 0: |
| 50 | + file2.write('\n=== Without man page or broken link (auto generated from component list)\n') |
| 51 | + file2.write('[{tab_options}]\n|=======================\n') |
| 52 | + for i in sorted(miss_in_man): |
| 53 | + file2.write('| ' + i + ' |||\n') |
| 54 | + file2.write('|=======================\n') |
| 55 | + file2.close() |
| 56 | + |
| 57 | + generate_links(gen_filename, False, True) |
| 58 | + print('gen_complist: Added {} uncategorized and {} potentially obsolete entrie(s) to hal component list'.format(len(miss_in_list), len(miss_in_man))) |
| 59 | + |
| 60 | + |
| 61 | +def generate_links(filename, create_backup=True, add_descr=False): |
| 62 | + file = open(filename, 'r') |
| 63 | + file_links = [] |
| 64 | + links_added = 0 |
| 65 | + for line in file: |
| 66 | + if line[0] == '|' and line[1] != '=': |
| 67 | + splitted = line.split('|') |
| 68 | + |
| 69 | + if 'link:' in splitted[1]: |
| 70 | + link = re.search('(?<=link:).*(?=\[)', splitted[1]).group() |
| 71 | + if not os.path.isfile(os.path.join('../docs/html/hal',link)): |
| 72 | + print('gen_complist: Broken link:', link) |
| 73 | + else: |
| 74 | + comp_man = splitted[1].strip(' ') |
| 75 | + if comp_man in man_files: |
| 76 | + comp, man = comp_man.split(".") |
| 77 | + line = line.replace(comp_man, 'link:../man/man'+man+'/'+comp_man+'.html['+comp+']', 1) |
| 78 | + links_added += 1 |
| 79 | + if add_descr: |
| 80 | + splitted = line.split('|') |
| 81 | + splitted[2] = extract_descr('../docs/man/man'+man+'/'+comp_man)\ |
| 82 | + .replace(comp + ' ', ' ',1).strip('\n -') |
| 83 | + line = '|'.join(splitted) |
| 84 | + file_links.append(line) |
| 85 | + file.close() |
| 86 | + |
| 87 | + if links_added: |
| 88 | + if create_backup: |
| 89 | + os.rename(filename, filename+'~') |
| 90 | + file = open(filename, 'w') |
| 91 | + for line in file_links: |
| 92 | + file.write(line) |
| 93 | + file.close() |
| 94 | + print('gen_complist: Added {} link(s) to {}'.format(links_added, filename)) |
| 95 | + |
| 96 | + |
| 97 | +def extract_descr(filename): |
| 98 | + file = open(filename, 'r') |
| 99 | + descr = '' |
| 100 | + in_descr = False |
| 101 | + |
| 102 | + for line in file: |
| 103 | + if '.SH NAME' in line or '.SH "NAME' in line: |
| 104 | + in_descr = True |
| 105 | + elif '.SH' in line: |
| 106 | + break |
| 107 | + elif in_descr: |
| 108 | + descr += line |
| 109 | + file.close() |
| 110 | + return re.sub(r'\\fB|\\fR|\\fI|\\', '', descr) |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + |
| 114 | + if len(sys.argv) > 1: |
| 115 | + if 'links' in sys.argv: |
| 116 | + generate_links(sys.argv[1]) |
| 117 | + else: |
| 118 | + generate_complist(sys.argv[1]) |
0 commit comments