1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: build_id_to_file_mapping.py 99059 2023-03-20 02:38:04Z vboxsync $
|
---|
3 |
|
---|
4 | """
|
---|
5 | Scans the given files (globbed) for topic id and stores records the filename
|
---|
6 | in the output file.
|
---|
7 |
|
---|
8 | This is used by add_file_to_id_only_references.py after converting man_V*.xml
|
---|
9 | refentry files to dita to correct links.
|
---|
10 | """
|
---|
11 |
|
---|
12 | __copyright__ = \
|
---|
13 | """
|
---|
14 | Copyright (C) 2023 Oracle and/or its affiliates.
|
---|
15 |
|
---|
16 | This file is part of VirtualBox base platform packages, as
|
---|
17 | available from https://www.virtualbox.org.
|
---|
18 |
|
---|
19 | This program is free software; you can redistribute it and/or
|
---|
20 | modify it under the terms of the GNU General Public License
|
---|
21 | as published by the Free Software Foundation, in version 3 of the
|
---|
22 | License.
|
---|
23 |
|
---|
24 | This program is distributed in the hope that it will be useful, but
|
---|
25 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
26 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
27 | General Public License for more details.
|
---|
28 |
|
---|
29 | You should have received a copy of the GNU General Public License
|
---|
30 | along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
31 |
|
---|
32 | SPDX-License-Identifier: GPL-3.0-only
|
---|
33 | """
|
---|
34 | __version__ = "$Revision: 99059 $"
|
---|
35 |
|
---|
36 |
|
---|
37 | # Standard python imports.
|
---|
38 | import glob;
|
---|
39 | import os;
|
---|
40 | import re;
|
---|
41 | import sys;
|
---|
42 |
|
---|
43 |
|
---|
44 | g_oReDita = re.compile(r'<topic[^><]*\sid=("[^">]+"|\'[^\'>]+\')');
|
---|
45 |
|
---|
46 | def 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 |
|
---|
55 | g_oReRefentry = re.compile(r'<(refentry\b|refsect[12345]\b|cmdsynopsis\b)[^><]*\sid=("[^">]+"|\'[^\'>]+\')');
|
---|
56 |
|
---|
57 | def 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 |
|
---|
66 | def isDocbook(sContent):
|
---|
67 | """
|
---|
68 | Check if the file is a Docbook one.
|
---|
69 | """
|
---|
70 | return sContent.find('<refentry ') >= 0 and sContent.find('<refentryinfo>');
|
---|
71 |
|
---|
72 | def error(sMessage):
|
---|
73 | """ Reports an error. """
|
---|
74 | print('build_id_to_file_mapping.py: error: %s' % sMessage, file = sys.stderr);
|
---|
75 | return 1;
|
---|
76 |
|
---|
77 | def 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 |
|
---|
82 | def 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 |
|
---|
87 | def 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 |
|
---|
148 | if __name__ == "__main__":
|
---|
149 | sys.exit(main(sys.argv));
|
---|