VirtualBox

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

Last change on this file since 99038 was 99038, checked in by vboxsync, 20 months ago

manual: Got rid of the correct_reference_targets Makefile-file rule, the new tactic is to scan the checked in .dita-files and .xml-files, store the id->filename mappings in file, and apply them as part of the refentry->dita conversion recipe for each individual Docbook-file. Use pattern rules for copying files to the staging area. bugref:10302

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: build_id_to_file_mapping.py 99038 2023-03-18 05:03:58Z 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: 99038 $"
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 asFiles = glob.glob(sArg);
118 if not asFiles:
119 return error('File not found: %s' % (sArg,));
120 for sFile in asFiles:
121 try:
122 with open(sFile, 'r', encoding = 'utf-8') as oFile:
123 sContent = oFile.read();
124 except Exception as oXcpt: # pylint: disable=broad-exception-caught
125 return error('Failed to open and read "%s": %s' % (sFile, oXcpt,));
126 if isDocbook(sContent):
127 scanDocbookRefentryForIds(dIdToFile, sContent, os.path.splitext(os.path.basename(sFile))[0] + '.dita');
128 else:
129 scanDitaFileForIds(dIdToFile, sContent, os.path.basename(sFile));
130 iArg += 1;
131
132 # Dump the dictionary.
133 asDict = sorted(['%s=%s' % (sKey, sValue) for sKey, sValue in dIdToFile.items()]);
134 if sOutput is not None:
135 try:
136 with open(sOutput, 'w', encoding = 'utf-8') as oFile:
137 oFile.write('\n'.join(asDict));
138 except Exception as oXcpt: # pylint: disable=broad-exception-caught
139 return error('Failed to open and write "%s": %s' % (sFile, oXcpt,));
140 else:
141 sys.stdout.write('\n'.join(asDict));
142 return 0;
143
144if __name__ == "__main__":
145 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