VirtualBox

source: vbox/trunk/doc/manual/build_id_to_file_mapping.py@ 99159

Last change on this file since 99159 was 99059, checked in by vboxsync, 21 months ago

manual/Makefile.kmk: Cleaning up the makefile step by step... bugref:10348 bugref:10302

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: build_id_to_file_mapping.py 99059 2023-03-20 02:38:04Z vboxsync $
3
4"""
5Scans the given files (globbed) for topic id and stores records the filename
6in the output file.
7
8This is used by add_file_to_id_only_references.py after converting man_V*.xml
9refentry files to dita to correct links.
10"""
11
12__copyright__ = \
13"""
14Copyright (C) 2023 Oracle and/or its affiliates.
15
16This file is part of VirtualBox base platform packages, as
17available from https://www.virtualbox.org.
18
19This program is free software; you can redistribute it and/or
20modify it under the terms of the GNU General Public License
21as published by the Free Software Foundation, in version 3 of the
22License.
23
24This program is distributed in the hope that it will be useful, but
25WITHOUT ANY WARRANTY; without even the implied warranty of
26MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27General Public License for more details.
28
29You should have received a copy of the GNU General Public License
30along with this program; if not, see <https://www.gnu.org/licenses>.
31
32SPDX-License-Identifier: GPL-3.0-only
33"""
34__version__ = "$Revision: 99059 $"
35
36
37# Standard python imports.
38import glob;
39import os;
40import re;
41import sys;
42
43
44g_oReDita = re.compile(r'<topic[^><]*\sid=("[^">]+"|\'[^\'>]+\')');
45
46def scanDitaFileForIds(dIdToFile, sContent, sFile):
47 """
48 Scans the given content of a .dita-file for topic IDs that can be referenced.
49 """
50 for oMatch in g_oReDita.finditer(sContent):
51 sId = oMatch.group(1)[1:-1];
52 if sId:
53 dIdToFile[sId] = sFile;
54
55g_oReRefentry = re.compile(r'<(refentry\b|refsect[12345]\b|cmdsynopsis\b)[^><]*\sid=("[^">]+"|\'[^\'>]+\')');
56
57def scanDocbookRefentryForIds(dIdToFile, sContent, sFile):
58 """
59 Scans the given content of a Docbook refentry file for topic IDs that can be referenced.
60 """
61 for oMatch in g_oReRefentry.finditer(sContent):
62 sId = oMatch.group(2)[1:-1];
63 if sId:
64 dIdToFile[sId] = sFile;
65
66def isDocbook(sContent):
67 """
68 Check if the file is a Docbook one.
69 """
70 return sContent.find('<refentry ') >= 0 and sContent.find('<refentryinfo>');
71
72def error(sMessage):
73 """ Reports an error. """
74 print('build_id_to_file_mapping.py: error: %s' % sMessage, file = sys.stderr);
75 return 1;
76
77def syntax(sMessage):
78 """ Reports a syntax error. """
79 print('build_id_to_file_mapping.py: syntax error: %s' % sMessage, file = sys.stderr);
80 return 2;
81
82def usage():
83 """ Reports usage. """
84 print('usage: build_id_to_file_mapping.py --output <map.db> file1.dita docbook2.xml wild*card.* [...]');
85 return 0;
86
87def main(asArgs):
88 """
89 C-like main function.
90 """
91 #
92 # Process arguments.
93 #
94 dIdToFile = {};
95 sOutput = None;
96 fEndOfArgs = False;
97 iArg = 1;
98 while iArg < len(asArgs):
99 sArg = asArgs[iArg];
100 if sArg[0] == '-' and not fEndOfArgs:
101 # Options.
102 if sArg == '--':
103 fEndOfArgs = True;
104 elif sArg in ('--help', '-h', '-?'):
105 return usage();
106 elif sArg in ('--version', '-V' ):
107 print(__version__[__version__.find(':') + 2:-2]);
108 elif sArg in ('--output', '-o'):
109 iArg += 1;
110 if iArg >= len(asArgs):
111 return syntax('Expected filename following "--output"!');
112 sOutput = asArgs[iArg];
113 else:
114 return syntax('Unknown option: %s' % (sArg,));
115 else:
116 # Input files.
117 if sArg[0] == '@':
118 with open(sArg[1:], 'r', encoding = 'utf-8') as oFile:
119 asFiles = oFile.read().split();
120 else:
121 asFiles = glob.glob(sArg);
122 if not asFiles:
123 return error('File not found: %s' % (sArg,));
124 for sFile in asFiles:
125 try:
126 with open(sFile, 'r', encoding = 'utf-8') as oFile:
127 sContent = oFile.read();
128 except Exception as oXcpt: # pylint: disable=broad-exception-caught
129 return error('Failed to open and read "%s": %s' % (sFile, oXcpt,));
130 if isDocbook(sContent):
131 scanDocbookRefentryForIds(dIdToFile, sContent, os.path.splitext(os.path.basename(sFile))[0] + '.dita');
132 else:
133 scanDitaFileForIds(dIdToFile, sContent, os.path.basename(sFile));
134 iArg += 1;
135
136 # Dump the dictionary.
137 asDict = sorted(['%s=%s' % (sKey, sValue) for sKey, sValue in dIdToFile.items()]);
138 if sOutput is not None:
139 try:
140 with open(sOutput, 'w', encoding = 'utf-8') as oFile:
141 oFile.write('\n'.join(asDict));
142 except Exception as oXcpt: # pylint: disable=broad-exception-caught
143 return error('Failed to open and write "%s": %s' % (sFile, oXcpt,));
144 else:
145 sys.stdout.write('\n'.join(asDict));
146 return 0;
147
148if __name__ == "__main__":
149 sys.exit(main(sys.argv));
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette