forked from bootlin/elixir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
executable file
·206 lines (175 loc) · 6.46 KB
/
data.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
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
#!/usr/bin/env python3
# This file is part of Elixir, a source code cross-referencer.
#
# Copyright (C) 2017--2020 Mikaël Bouillot <mikael.bouillot@bootlin.com>
# and contributors
#
# Elixir is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Elixir is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Elixir. If not, see <http://www.gnu.org/licenses/>.
import bsddb3
import re
from lib import autoBytes
import os
import os.path
import errno
deflist_regex = re.compile(b'(\d*)(\w)(\d*)(\w),?')
deflist_macro_regex = re.compile('\dM\d+(\w)')
##################################################################################
defTypeR = {
'c': 'config',
'd': 'define',
'e': 'enum',
'E': 'enumerator',
'f': 'function',
'l': 'label',
'M': 'macro',
'm': 'member',
'p': 'prototype',
's': 'struct',
't': 'typedef',
'u': 'union',
'v': 'variable',
'x': 'externvar'}
defTypeD = {v: k for k, v in defTypeR.items()}
##################################################################################
maxId = 999999999
class DefList:
'''Stores associations between a blob ID, a type (e.g., "function"),
a line number and a file family.
Also stores in which families the ident exists for faster tests.'''
def __init__(self, data=b'#'):
self.data, self.families = data.split(b'#')
def iter(self, dummy=False):
# Get all element in a list of sublists and sort them
entries = deflist_regex.findall(self.data)
entries.sort(key=lambda x:int(x[0]))
for id, type, line, family in entries:
id = int(id)
type = defTypeR [type.decode()]
line = int(line)
family = family.decode()
yield id, type, line, family
if dummy:
yield maxId, None, None, None
def append(self, id, type, line, family):
if type not in defTypeD:
return
p = str(id) + defTypeD[type] + str(line) + family
if self.data != b'':
p = ',' + p
self.data += p.encode()
self.add_family(family)
def pack(self):
return self.data + b'#' + self.families
def add_family(self, family):
family = family.encode()
if not family in self.families.split(b','):
if self.families != b'':
family = b',' + family
self.families += family
def get_families(self):
return self.families.decode().split(',')
def get_macros(self):
return deflist_macro_regex.findall(self.data.decode()) or ''
class PathList:
'''Stores associations between a blob ID and a file path.
Inserted by update.py sorted by blob ID.'''
def __init__(self, data=b''):
self.data = data
def iter(self, dummy=False):
for p in self.data.split(b'\n')[:-1]:
id, path = p.split(b' ',maxsplit=1)
id = int(id)
path = path.decode()
yield id, path
if dummy:
yield maxId, None
def append(self, id, path):
p = str(id).encode() + b' ' + path + b'\n'
self.data += p
def pack(self):
return self.data
class RefList:
'''Stores a mapping from blob ID to list of lines
and the corresponding family.'''
def __init__(self, data=b''):
self.data = data
def iter(self, dummy=False):
# Split all elements in a list of sublists and sort them
entries = [x.split(b':') for x in self.data.split(b'\n')[:-1]]
entries.sort(key=lambda x:int(x[0]))
for b, c, d in entries:
b = int(b.decode())
c = c.decode()
d = d.decode()
yield b, c, d
if dummy:
yield maxId, None, None
def append(self, id, lines, family):
p = str(id) + ':' + lines + ':' + family + '\n'
self.data += p.encode()
def pack(self):
return self.data
class BsdDB:
def __init__(self, filename, readonly, contentType):
self.filename = filename
self.db = bsddb3.db.DB()
if readonly:
self.db.open(filename, flags=bsddb3.db.DB_RDONLY)
else:
self.db.open(filename,
flags=bsddb3.db.DB_CREATE,
mode=0o644,
dbtype=bsddb3.db.DB_BTREE)
self.ctype = contentType
def exists(self, key):
key = autoBytes(key)
return self.db.exists(key)
def get(self, key):
key = autoBytes(key)
p = self.db.get(key)
p = self.ctype(p)
return p
def get_keys(self):
return self.db.keys()
def put(self, key, val, sync=False):
key = autoBytes(key)
val = autoBytes(val)
if type(val) is not bytes:
val = val.pack()
self.db.put(key, val)
if sync:
self.db.sync()
class DB:
def __init__(self, dir, readonly=True, dtscomp=False):
if os.path.isdir(dir):
self.dir = dir
else:
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), dir)
ro = readonly
self.vars = BsdDB(dir + '/variables.db', ro, lambda x: int(x.decode()) )
# Key-value store of basic information
self.blob = BsdDB(dir + '/blobs.db', ro, lambda x: int(x.decode()) )
# Map hash to sequential integer serial number
self.hash = BsdDB(dir + '/hashes.db', ro, lambda x: x )
# Map serial number back to hash
self.file = BsdDB(dir + '/filenames.db', ro, lambda x: x.decode() )
# Map serial number to filename
self.vers = BsdDB(dir + '/versions.db', ro, PathList)
self.defs = BsdDB(dir + '/definitions.db', ro, DefList)
self.refs = BsdDB(dir + '/references.db', ro, RefList)
self.docs = BsdDB(dir + '/doccomments.db', ro, RefList)
if dtscomp:
self.comps = BsdDB(dir + '/compatibledts.db', ro, RefList)
self.comps_docs = BsdDB(dir + '/compatibledts_docs.db', ro, RefList)
# Use a RefList in case there are multiple doc comments for an identifier