-
Notifications
You must be signed in to change notification settings - Fork 3
/
x86doc2docset.py
executable file
·140 lines (121 loc) · 5.16 KB
/
x86doc2docset.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2018 Jan Holthuis
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
import logging
import os
import re
import sqlite3
import bs4
DBPATH = 'Contents/Resources/docSet.dsidx'
DOCPATH = 'Contents/Resources/Documents'
DOCSET = 'Intel_x86_IA32'
DOCSET_DIR = '%s.docset' % DOCSET
INSTR_PATTERN = re.compile(r'^(?:\./)?(?P<filename>[\w:-]+\.html)$')
SIMPLE_INSTR_PATTERN = re.compile(r'^[A-Z0-9x-]+$')
def parse_index(fp, root=None):
logger = logging.getLogger(__name__)
soup = bs4.BeautifulSoup(fp, 'html.parser')
for i, tag in enumerate(soup.find_all('a', {'href': INSTR_PATTERN})):
name = tag.text.strip()
if not name:
continue
matchobj = INSTR_PATTERN.match(tag.attrs['href'])
if not matchobj:
continue
path = matchobj.group('filename')
if path == 'index.html':
continue
for instr in name.split(':'):
logger.info('Found "%s" (path: %s)', instr, path)
yield (instr, 'instruction', path)
if root and not SIMPLE_INSTR_PATTERN.match(instr):
try:
with open(os.path.join(root, path), mode='r') as f:
yield from ((x, 'instruction', path)
for x in parse_combined(f))
except ValueError:
logger.warning('Did not find table column index for '
'combined instruction "%s"', instr)
def parse_combined(fp):
logger = logging.getLogger(__name__)
soup = bs4.BeautifulSoup(fp, 'html.parser')
table = soup.table
colnames = [t.string for t in table.tr.find_all('th')]
colindex = colnames.index('Instruction')
found = set()
for td in table.select('tr > td:nth-of-type(%d)' % (colindex+1)):
matchobj = re.match(r'[A-Z0-9-]+', td.text)
if not matchobj:
continue
name = matchobj.group(0)
if name not in found:
logger.info('Found part "%s" of combined instruction', name)
found.add(name)
yield name
def update_db(dbpath, data, commit=True):
logger = logging.getLogger(__name__)
logger.info('Connecting to database at: %s', dbpath)
conn = sqlite3.connect(dbpath)
cur = conn.cursor()
try:
cur.execute('DROP TABLE IF EXISTS searchIndex;')
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);')
cur.executemany('INSERT OR IGNORE INTO searchIndex(name, type, path) '
'VALUES (?,?,?)', list(data))
except sqlite3.Error:
logger.error('An error ocurred, rolling back database changes...',
exc_info=logger.isEnabledFor(logging.DEBUG))
conn.rollback()
else:
if commit:
logger.info('Committing database changes...')
conn.commit()
else:
logger.warning('Committing has been disabled, rolling back '
'database changes...')
conn.rollback()
finally:
cur.close()
logger.info('Done.')
def main(args=None):
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--root', default='.',
help='root path of the docset')
parser.add_argument('-n', '--dry-run', action='store_true',
help='do not write changes to the database')
parser.add_argument('-c', '--no-combined', action='store_true',
help='do not parse combined instructions')
p_args = parser.parse_args(args)
docset_root = os.path.join(p_args.root, DOCSET_DIR)
dbpath = os.path.join(docset_root, DBPATH)
indexpath = os.path.join(docset_root, DOCPATH, 'index.html')
logging.basicConfig(level=logging.INFO)
root = os.path.dirname(indexpath) if not p_args.no_combined else None
with open(indexpath, mode='r') as f:
update_db(dbpath, parse_index(f, root=root),
commit=not p_args.dry_run)
if __name__ == '__main__':
main()