File: build_docset.py

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (301 lines) | stat: -rw-r--r-- 12,780 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/bin/python3
"""
Generate a dash docset from a doxygen documentation.
"""

# cspell: disable
import json
import os
from pathlib import Path
import re
import shutil
import sqlite3
import sys

import bs4

CFG = dict()
REPO_NAMES = dict()

# Global regexes we don't want to recompile every single time we parse a file
CLASS_FILE_RE = re.compile(r'class([a-zA-Z_][a-zA-Z0-9_]*)_1_1([a-zA-Z_][a-zA-Z0-9_]*)\.html')
CLASS_RE = re.compile('sight: (.+) Class Reference')
STRUCT_FILE_RE = re.compile(r'struct([a-zA-Z_][a-zA-Z0-9_]*)_1_1([a-zA-Z_][a-zA-Z0-9_]*)\.html')
STRUCT_RE = re.compile('sight: (.+) Struct Reference')
NAMESPACE_FILE_RE = re.compile(r'namespace.+\.html')
NAMESPACE_RE = re.compile('sight: ([a-zA-Z_][a-zA-Z0-9_:]*) Namespace Reference')
SRV_RE = re.compile('sight: ([a-zA-Z_][a-zA-Z0-9_]*::(?:[a-zA-Z_][a-zA-Z0-9_]*::)*(S[A-Z0-9][a-zA-Z0-9_]*)) Class Reference')
BAD__SRV_RE = re.compile('sight: ([a-zA-Z_][a-zA-Z0-9_]*::(?:[a-zA-Z_][a-zA-Z0-9_]*::)*([A-Z0-9][a-zA-Z0-9_]*)) Class Reference')
OBJ_RE = re.compile('sight: ([a-zA-Z_][a-zA-Z0-9_]*::(?:[a-zA-Z_][a-zA-Z0-9_]*::)*([A-Z0-9][a-zA-Z0-9_]*)) Class Reference')
IFACE_RE = re.compile('sight: ([a-zA-Z_][a-zA-Z0-9_]*::(?:[a-zA-Z_][a-zA-Z0-9_]*::)*(I[A-Z0-9][a-zA-Z0-9_]*|base)) Class Reference')
EXCEPT_RE = re.compile('sight: ([a-zA-Z_][a-zA-Z0-9_]*::(?:[a-zA-Z_][a-zA-Z0-9_]*::)*([A-Z0-9][a-zA-Z0-9_]*)) Struct Reference')

# Regexes of the files to skip
FILE_SKIP_RE = [
    re.compile('pages.html'),
    re.compile(r'dir_.+\.html'),
    re.compile('.+_source.html')
]

def bootstrap_docset():
    """
    Create the skeleton for the docset, i.e. the directory structure along with the SQLite database. Return the SQLite
    database connection.
    """
    # Create the directory structure.
    Path('./sight.docset/Contents/Resources').mkdir(parents=True, exist_ok=True)

    # Then, create the SQLite database
    db = Path('./sight.docset/Contents/Resources/docSet.dsidx')
    if db.exists():
        os.remove(str(db))
    conn_ = sqlite3.connect(str(db))
    cur = conn_.cursor()
    cur.execute('CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);')
    cur.execute('CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);')
    conn_.commit()
    return conn_

def gather_sources():
    """
    Return a list containing the paths to all interesting HTML files contained at the root of the Doxygen html
    directory. We're not interested in what's in the subdirectories.
    """
    files = list()
    for _, _, dir_files in os.walk('./html/'):
        files += [f for f in dir_files if f.endswith('.html')]
    return files

def parse_related_pages():
    """
    Parse the 'pages.html' doxygen file and generate the list of related pages.
    """
    pages = list()
    html = open(os.path.join('./html', 'pages.html'), encoding="utf8").read()
    soup = bs4.BeautifulSoup(html, "html.parser")
    table = soup.find("table", class_="directory")
    for cell in table.find_all("tr"):
        page_name = cell.td.a.string
        page_link = cell.td.a.get('href')
        pages.append((page_name, "Guide", page_link))
    return pages

# TODO: strip the leading repository path from the HTML file to get rid of the ugly full paths ?
def file_repo(f_soup):
    """
    Return the name of the repository that a particular documentation file was generated from, or None if not possible.
    """
    lists = f_soup.find_all('ul')
    if lists:
        file_path = lists[-1].li.get_text()
        for repo in CFG['repositories']:
            candidates = [repo for repo in CFG['repositories'] if file_path.startswith(repo)]
            if candidates:
                res = max(candidates, key=len)
                return REPO_NAMES[res]
    return None

def parse_file(f_):
    """
    Parse a HTML file and return a (potentially empty) list of 3-tuples to add to the SQLite database.
    """
    # Doxygen names the documentation files in a friendly manner, which means we can guess what is inside from the file
    # name, and ignore files that we know we don't care about. This script currently looks for files containing classes
    # or structs.
    new_entries = list()
    # Some files are of no interest to us and can be skipped
    if any(map(lambda regexp: regexp.match(f_), FILE_SKIP_RE)):
        return new_entries
    try:
        html = open(os.path.join('./html', f_), encoding="utf8").read()
        soup = bs4.BeautifulSoup(html, "html.parser")
        inherits_base = soup.find(class_='inherit_header pub_methods_classfwServices_1_1base')
        inherits_object = soup.find(class_='inherit_header pub_methods_classfwData_1_1Object')
        inherits_exception = soup.find(class_='inherit_header pub_methods_classfwCore_1_1Exception')

        item_type_re = {
            "Service": SRV_RE,
            "Object": OBJ_RE,
            "Interface": IFACE_RE,
            "Exception": EXCEPT_RE
        }
        def is_item_type(soup, ty_str):
            """
            Test if the HTML contained in the supplied soup describes and element of the specified type based on the
            doxygen page title. Accepted types are 'Service', 'Object', 'Interface' and 'Exception'. If true, return an
            entry to add to the sqlite DB, else return None.
            """
            title = soup.title.get_text()
            match = item_type_re[ty_str].search(title)
            if match:
                path = match.group(1)
                repo = file_repo(soup)
                if repo is not None:
                    path = path + " ({})".format(repo)
                return (path, ty_str, f_)
            return None

        def is_bad_service(soup):
            """
            Test if the HTML contained in the supplied soup describes a service, with more lenient rules regarding
            the name of the service. If true, print a warning regarding the service name and return an entry to add to
            the sqlite DB, otherwise return None.
            """
            title = soup.title.get_text()
            match = BAD__SRV_RE.search(title)
            if match:
                path = match.group(1)
                srv = match.group(2)
                repo = file_repo(soup)
                if repo is not None:
                    path = path + " ({})".format(repo)
                print("Warning: service {} has non compliant name (no S prefix)".format(srv))
                return (path, "Service", f_)
            return None

        file_type_re = {
            "Class": CLASS_RE,
            "Namespace": NAMESPACE_RE,
            "Struct": STRUCT_RE,
        }
        def is_file_type(soup, ty_str):
            """
            Test if the HTML contained in the supplied soup describes and element of the specified type based on the
            doxygen page title. Accepted types are 'Class', 'Namespace', and 'Struct'. If true, return an
            entry to add to the sqlite DB, else return None.
            """
            title = soup.title.get_text()
            match = file_type_re[ty_str].search(title)
            if match:
                struct_ = match.group(1)
                return (struct_, ty_str, f_)
            return None

        if CLASS_FILE_RE.match(f_):
            # We know the file contains a class, find what kind of class
            class_triple = is_file_type(soup, 'Class')
            if class_triple is None:
                return new_entries
            class_name = class_triple[0]
            if inherits_base:
                # The class inherits base, it can be a service or an interface
                triple = is_item_type(soup, 'Interface')
                if triple is not None:
                    new_entries.append(triple)
                else:
                    # Not an interface, probably a service
                    triple = is_item_type(soup, 'Service')
                    if triple is not None:
                        new_entries.append(triple)
                    else:
                        triple = is_bad_service(soup)
                        if triple is not None:
                            new_entries.append(triple)
                        else:
                            print("Warning: unexepected behaviour for class {} while parsing file {}".format(class_name, f_))
            elif class_name == "fwData::Object":
                # Special case, Object is not an actual data.
                new_entries.append((class_name, "Class", f_))
            elif inherits_object:
                # Not a service and inherits fwData::Object, this class is probably a data.
                triple = is_item_type(soup, 'Object')
                if triple is not None:
                    new_entries.append(triple)
            elif class_name == "fwCore::Exception":
                # Special case for fwCore::Exception
                new_entries.append((class_name, "Exception", f_))
            elif inherits_exception:
                # Inherits an exception type, this is probably an exception
                # TODO: I'm pretty sure this won't catch all exceptions in the codebase
                triple = is_item_type(soup, 'Exception')
                if triple is not None:
                    new_entries.append(triple)
            else:
                # Plain old class
                new_entries.append(class_triple)
        elif STRUCT_FILE_RE.match(f_):
            # We know the file contains a struct, find what kind of struct
            struct_triple = is_file_type(soup, 'Struct')
            if struct_triple is None:
                return new_entries
            new_entries.append(struct_triple)
            if inherits_exception:
                # Inherits an exception type, this is probably an exception
                # TODO: I'm pretty sure this won't catch all exceptions in the codebase
                triple = is_item_type(soup, 'Exception')
                if triple is not None:
                    new_entries.append(triple)
        elif NAMESPACE_FILE_RE.match(f_):
            # We know the file contains a namespace, find what kind of namespace (i.e. Bundle, Library, regular
            # namespace...)
            namespace_triple = is_file_type(soup, 'Namespace')
            if namespace_triple is None:
                return new_entries
            namespace_name = namespace_triple[0]
            if namespace_name in CFG['srclibs']:
                new_entries.append((namespace_name, "Library", f_))
            elif namespace_name in CFG['bundles']:
                # There is no 'Bundle' entry type, unfortunately. Component, Package or Module would be suitable
                # replacements. I chose Package.
                new_entries.append((namespace_name, "Package", f_))
            else:
                new_entries.append(namespace_triple)
    except UnicodeDecodeError:
        print('The file ' + f_ + ' is not valid UTF-8')
    except FileNotFoundError:
        # Occurs for files in the search subdirectory, it's OK, we don't care about those
        pass
    return new_entries

def populate_db(conn_, services):
    """
    Fill the sqlite database with the supplied list of (name, entry_type, file_path) triples.
    """
    cur = conn_.cursor()
    for triple in services:
        try:
            cur.execute("INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES (?, ?, ?);", triple)
        except Exception:
            print("Error inserting " + str(triple))
    conn_.commit()

def copy_files():
    """
    Copy the doxygen HTML files into the docset destination.
    """
    try:
        shutil.copytree('./html', './sight.docset/Contents/Resources/Documents')
    except shutil.Error as err:
        errors = err.args[0]
        print("Warning: some files were not copied correctly. The generated docset might be incomplete.")
        for src, _, why in errors:
            print("File '" + src + "' was not copied correctly. Reason: " + why)

def main():
    """
    Builds the dash docset.
    """
    global CFG
    global REPO_NAMES
    try:
        CFG = json.loads(open('./projects.json', encoding="utf8").read())
    except (OSError, json.JSONDecodeError) as err:
        print("Error loading configuration file: " + str(err))
        return
    REPO_NAMES = {repo: Path(repo).parent.name if Path(repo).name == "src" else Path(repo).name for repo in CFG['repositories']}
    if CFG is None:
        sys.exit(1)
    conn = bootstrap_docset()
    html_files = gather_sources()
    entries = list()
    for f in html_files:
        f_entries = parse_file(f)
        if f_entries:
            entries += f_entries
    entries += parse_related_pages()
    populate_db(conn, entries)
    copy_files()
    conn.close()

if __name__ == '__main__':
    main()