forked from bootlin/elixir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
executable file
·168 lines (142 loc) · 4.7 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
#!/usr/bin/python3
# This file is part of Elixir, a source code cross-referencer.
#
# Copyright (C) 2017 Mikaël Bouillot
#
# 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
from io import BytesIO
import re
from lib import autoBytes
import os.path
##################################################################################
defTypeR = {
'd': 'define',
'e': 'enum',
'E': 'enumerator',
'f': 'function',
'l': 'label',
'M': 'macro',
'm': 'member',
'p': 'prototype',
's': 'struct',
't': 'typedef',
'u': 'union',
'v': 'variable' }
defTypeD = {v: k for k, v in defTypeR.items()}
##################################################################################
maxId = 999999999
class DefList:
def __init__ (self, data=b''):
self.data = data
def iter (self, dummy=False):
for p in self.data.split (b','):
p = re.search (b'(\d*)(\w)(\d*)', p)
id, type, line = p.groups()
id = int (id)
type = defTypeR [type.decode()]
line = int (line)
yield (id, type, line)
if dummy:
yield (maxId, None, None)
def append (self, id, type, line):
if type not in defTypeD:
return
p = str(id) + defTypeD[type] + str(line)
if self.data != b'':
p = ',' + p
self.data += p.encode()
def pack (self):
return self.data
class PathList:
def __init__ (self, data=b''):
self.data = data
def iter (self, dummy=False):
for p in self.data.split (b'\n'):
if (p == b''): continue
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
self.data = self.data + p + b'\n'
def pack (self):
return self.data
class RefList:
def __init__ (self, data=b''):
self.data = data
def iter (self, dummy=False):
size = len (self.data)
s = BytesIO (self.data)
while s.tell() < size:
line = s.readline()
line = line [:-1]
b,c = line.split (b':')
b = int (b.decode())
c = c.decode()
yield (b, c)
s.close()
if dummy:
yield (maxId, None)
def append (self, id, lines):
p = str(id) + ':' + lines + '\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 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):
if os.path.isdir (dir):
self.dir = dir
else:
raise FileNotFoundError
ro = readonly
self.vars = BsdDB (dir + '/variables.db', ro, lambda x: int (x.decode()) )
self.blob = BsdDB (dir + '/blobs.db', ro, lambda x: int (x.decode()) )
self.hash = BsdDB (dir + '/hashes.db', ro, lambda x: x )
self.file = BsdDB (dir + '/filenames.db', ro, lambda x: x.decode() )
self.vers = BsdDB (dir + '/versions.db', ro, PathList)
self.defs = BsdDB (dir + '/definitions.db', ro, DefList)
self.refs = BsdDB (dir + '/references.db', ro, RefList)