forked from bootlin/elixir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.py
executable file
·244 lines (223 loc) · 5.56 KB
/
lib.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
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
#!/usr/bin/env 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 subprocess, os
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
def script(*args):
args = (os.path.join(CURRENT_DIR, 'script.sh'),) + args
# subprocess.run was introduced in Python 3.5
# fall back to subprocess.check_output if it's not available
if hasattr(subprocess, 'run'):
p = subprocess.run(args, stdout=subprocess.PIPE)
p = p.stdout
else:
p = subprocess.check_output(args)
return p
# Invoke ./script.sh with the given arguments
# Returns the list of output lines
def scriptLines(*args):
p = script(*args)
p = p.split(b'\n')
del p[-1]
return p
def unescape(bstr):
subs = (
('\1','\n'),
)
for a,b in subs:
a = a.encode()
b = b.encode()
bstr = bstr.replace(a, b)
return bstr
def decode(byte_object):
# decode('ascii') fails on special chars
# FIXME: major hack until we handle everything as bytestrings
try:
return byte_object.decode('utf-8')
except UnicodeDecodeError:
return byte_object.decode('iso-8859-1')
# List of tokens which we don't want to consider as identifiers
# Typically for very frequent variable names and things redefined by #define
# TODO: allow to have per project blacklists
blacklist = (
b'NULL',
b'__',
b'adapter',
b'addr',
b'arg',
b'attr',
b'base',
b'bp',
b'buf',
b'buffer',
b'c',
b'card',
b'char',
b'chip',
b'cmd',
b'codec',
b'const',
b'count',
b'cpu',
b'ctx',
b'data',
b'default',
b'define',
b'desc',
b'dev',
b'driver',
b'else',
b'end',
b'endif',
b'entry',
b'err',
b'error',
b'event',
b'extern',
b'failed',
b'flags',
b'h',
b'host',
b'hw',
b'i',
b'id',
b'idx',
b'if',
b'index',
b'info',
b'inline',
b'int',
b'irq',
b'j',
b'len',
b'length',
b'list',
b'lock',
b'long',
b'mask',
b'mode',
b'msg',
b'n',
b'name',
b'net',
b'next',
b'offset',
b'ops',
b'out',
b'p',
b'page',
b'pdev',
b'port',
b'priv',
b'ptr',
b'q',
b'r',
b'rc',
b'rdev',
b'reg',
b'regs',
b'req',
b'res',
b'result',
b'ret',
b'return',
b'retval',
b'root',
b's',
b'sb',
b'size',
b'sizeof',
b'sk',
b'skb',
b'spec',
b'start',
b'state',
b'static',
b'status',
b'struct',
b't',
b'tmp',
b'tp',
b'type',
b'val',
b'value',
b'vcpu',
b'x'
)
def isIdent(bstr):
if (len(bstr) < 2 or
bstr in blacklist or
bstr.startswith(b'~')):
return False
else:
return True
def autoBytes(arg):
if type(arg) is str:
arg = arg.encode()
elif type(arg) is int:
arg = str(arg).encode()
return arg
def getDataDir():
try:
dir=os.environ['LXR_DATA_DIR']
except KeyError:
print(argv[0] + ': LXR_DATA_DIR needs to be set')
exit(1)
return dir
def currentProject():
return os.path.basename(os.path.dirname(getDataDir()))
# List all families supported by Elixir
families = ['A', 'B', 'C', 'D', 'K', 'M']
def validFamily(family):
return family in families
def getFileFamily(filename):
name, ext = os.path.splitext(filename)
if ext.lower() in ['.c', '.cc', '.cpp', '.c++', '.cxx', '.h', '.s'] :
return 'C' # C file family and ASM
elif ext.lower() in ['.dts', '.dtsi'] :
return 'D' # Devicetree files
elif name.lower()[:7] in ['kconfig'] and not ext.lower() in ['.rst']:
# Some files are named like Kconfig-nommu so we only check the first 7 letters
# We also exclude documentation files that can be named kconfig
return 'K' # Kconfig files
elif name.lower()[:8] in ['makefile'] and not ext.lower() in ['.rst']:
return 'M' # Makefiles
else :
return None
# 1 char values are file families
# 2 chars values with a M are macros families
compatibility_list = {
'C' : ['C', 'K'],
'K' : ['K'],
'D' : ['D', 'CM'],
'M' : ['K']
}
# Check if families are compatible
# First argument can be a list of different families
# Second argument is the key for chossing the right array in the compatibility list
def compatibleFamily(file_family, requested_family):
return any(item in file_family for item in compatibility_list[requested_family])
# Check if a macro is compatible with the requested family
# First argument can be a list of different families
# Second argument is the key for chossing the right array in the compatibility list
def compatibleMacro(macro_family, requested_family):
result = False
for item in macro_family:
item += 'M'
result = result or item in compatibility_list[requested_family]
return result