| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import subprocess |
| 7 |
import re, os |
| 8 |
|
| 9 |
def _eintr_retry_call(func, *args): |
| 10 |
while True: |
| 11 |
try: |
| 12 |
return func(*args) |
| 13 |
except OSError as e: |
| 14 |
if e.errno == errno.EINTR: |
| 15 |
continue |
| 16 |
raise |
| 17 |
|
| 18 |
def cmdForFile(f): |
| 19 |
|
| 20 |
|
| 21 |
args = 'ctags -n -u --fields=+K -f -' |
| 22 |
|
| 23 |
opt_I_file = os.getenv('SEASCOPE_CTAGS_OPT_I_FILE') |
| 24 |
if opt_I_file and os.path.isfile(opt_I_file): |
| 25 |
args += ' -I ' + opt_I_file |
| 26 |
|
| 27 |
suffix_cmd_map = [] |
| 28 |
custom_map = os.getenv('SEASCOPE_CTAGS_SUFFIX_CMD_MAP') |
| 29 |
if custom_map: |
| 30 |
custom_map = eval(custom_map) |
| 31 |
suffix_cmd_map += custom_map |
| 32 |
suffix_cmd_map.append( ['', args] ) |
| 33 |
for (suffix, cmd) in suffix_cmd_map: |
| 34 |
if f.endswith(suffix): |
| 35 |
return cmd |
| 36 |
return None |
| 37 |
|
| 38 |
def ct_query(filename): |
| 39 |
args = cmdForFile(filename) |
| 40 |
args = args.split() |
| 41 |
args.append(filename) |
| 42 |
try: |
| 43 |
proc = subprocess.Popen(args, stdout=subprocess.PIPE) |
| 44 |
(out_data, err_data) = _eintr_retry_call(proc.communicate) |
| 45 |
err_data = err_data.decode() if err_data else '' |
| 46 |
out_data = out_data.decode().split('\n') |
| 47 |
except Exception as e: |
| 48 |
out_data = [ |
| 49 |
'Failed to run ctags cmd\tignore\t0;\t ', |
| 50 |
'cmd: %s\tignore\t0;\t ' % ' '.join(args), |
| 51 |
'error: %s\tignore\t0;\t ' % str(e), |
| 52 |
'ctags not installed ?\tignore\t0;\t ', |
| 53 |
] |
| 54 |
res = [] |
| 55 |
for line in out_data: |
| 56 |
if (line == ''): |
| 57 |
break |
| 58 |
line = line.split('\t') |
| 59 |
num = line[2].split(';', 1)[0] |
| 60 |
line = [line[0], num, line[3]] |
| 61 |
res.append(line) |
| 62 |
return res |
| 63 |
|
| 64 |
is_OrderedDict_available = False |
| 65 |
try: |
| 66 |
|
| 67 |
from collections import OrderedDict |
| 68 |
is_OrderedDict_available = True |
| 69 |
except: |
| 70 |
pass |
| 71 |
|
| 72 |
def emptyOrderedDict(): |
| 73 |
if is_OrderedDict_available: |
| 74 |
return OrderedDict({}) |
| 75 |
return {} |
| 76 |
|
| 77 |
class CtagsTreeBuilder: |
| 78 |
def __init__(self): |
| 79 |
self.symTree = emptyOrderedDict() |
| 80 |
|
| 81 |
def cmdForFile(self, f): |
| 82 |
|
| 83 |
|
| 84 |
args = 'ctags -n -u --fields=+K-f-t -f -' |
| 85 |
|
| 86 |
opt_I_file = os.getenv('SEASCOPE_CTAGS_OPT_I_FILE') |
| 87 |
if opt_I_file and os.path.isfile(opt_I_file): |
| 88 |
args += ' -I ' + opt_I_file |
| 89 |
|
| 90 |
suffix_cmd_map = [] |
| 91 |
custom_map = os.getenv('SEASCOPE_CTAGS_SUFFIX_CMD_MAP') |
| 92 |
if custom_map: |
| 93 |
custom_map = eval(custom_map) |
| 94 |
suffix_cmd_map += custom_map |
| 95 |
suffix_cmd_map.append( ['', args] ) |
| 96 |
for (suffix, cmd) in suffix_cmd_map: |
| 97 |
if f.endswith(suffix): |
| 98 |
return cmd |
| 99 |
return None |
| 100 |
|
| 101 |
def runCtags(self, f): |
| 102 |
args = self.cmdForFile(f) |
| 103 |
args = args.split() |
| 104 |
args.append(f) |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
proc = subprocess.Popen(args, stdout=subprocess.PIPE) |
| 109 |
(out_data, err_data) = proc.communicate() |
| 110 |
out_data = out_data.decode() |
| 111 |
return out_data |
| 112 |
|
| 113 |
def parseCtagsOutput(self, data): |
| 114 |
data = re.split(r'\r?\n', data) |
| 115 |
res = [] |
| 116 |
for line in data: |
| 117 |
if line == '': |
| 118 |
continue |
| 119 |
try: |
| 120 |
line = line.split('\t', 4) |
| 121 |
res.append(line) |
| 122 |
except: |
| 123 |
print('bad line:', line) |
| 124 |
return res |
| 125 |
|
| 126 |
|
| 127 |
def addToSymLayout(self, sc): |
| 128 |
t = self.symTree |
| 129 |
if sc and sc != '': |
| 130 |
for s in re.split(r'::|\.', sc): |
| 131 |
if s not in t: |
| 132 |
t[s] = emptyOrderedDict() |
| 133 |
t = t[s] |
| 134 |
|
| 135 |
def addToSymTree(self, sc, line): |
| 136 |
t = self.symTree |
| 137 |
if sc and sc != '': |
| 138 |
for s in re.split(r'::|\.', sc): |
| 139 |
assert s in t |
| 140 |
t = t[s] |
| 141 |
|
| 142 |
cline = [line[0], line[2].split(';')[0], line[3]] |
| 143 |
if line[0] in t: |
| 144 |
|
| 145 |
x = t[line[0]] |
| 146 |
if '+' not in x: |
| 147 |
x['+'] = cline |
| 148 |
return |
| 149 |
if '*' not in t: |
| 150 |
t['*'] = [] |
| 151 |
t['*'].append(cline) |
| 152 |
|
| 153 |
|
| 154 |
def buildTree(self, data): |
| 155 |
type_list = [ 'namespace', 'class', 'interface', 'struct', 'union', 'enum', 'function' ] |
| 156 |
|
| 157 |
for line in data: |
| 158 |
if len(line) == 4: |
| 159 |
continue |
| 160 |
try: |
| 161 |
sd = dict([ x.split(':', 1) for x in line[4].split('\t')]) |
| 162 |
except: |
| 163 |
print('bad line', line) |
| 164 |
continue |
| 165 |
line[4] = sd |
| 166 |
count = 0 |
| 167 |
for t in type_list: |
| 168 |
if t in sd: |
| 169 |
self.addToSymLayout(sd[t]) |
| 170 |
count = count + 1 |
| 171 |
if count != 1: |
| 172 |
print('******** count == 1 *********') |
| 173 |
print(data) |
| 174 |
print(line) |
| 175 |
|
| 176 |
|
| 177 |
if len(self.symTree) == 0: |
| 178 |
return (data, False) |
| 179 |
|
| 180 |
for line in data: |
| 181 |
if len(line) == 4: |
| 182 |
self.addToSymTree(None, line) |
| 183 |
continue |
| 184 |
sd = line[4] |
| 185 |
count = 0 |
| 186 |
for t in type_list: |
| 187 |
if t in sd: |
| 188 |
self.addToSymTree(sd[t], line) |
| 189 |
count = count + 1 |
| 190 |
if count != 1: |
| 191 |
print('******** count == 1 *********') |
| 192 |
print(data) |
| 193 |
print(line) |
| 194 |
|
| 195 |
|
| 196 |
return (self.symTree, True) |
| 197 |
|
| 198 |
def doQuery(self, filename): |
| 199 |
try: |
| 200 |
output = self.runCtags(filename) |
| 201 |
output = self.parseCtagsOutput(output) |
| 202 |
output = self.buildTree(output) |
| 203 |
except Exception as e: |
| 204 |
print(str(e)) |
| 205 |
output = [None, False] |
| 206 |
return output |
| 207 |
|
| 208 |
|
| 209 |
def ct_tree_query(filename): |
| 210 |
ct = CtagsTreeBuilder() |
| 211 |
output = ct.doQuery(filename) |
| 212 |
return output |
| 213 |
|
| 214 |
if __name__ == '__main__': |
| 215 |
import optparse |
| 216 |
import sys |
| 217 |
depth = 0 |
| 218 |
def recursePrint(t): |
| 219 |
global depth |
| 220 |
for k, v in list(t.items()): |
| 221 |
if k == '*': |
| 222 |
for line in v: |
| 223 |
print('%s%s' % (' ' * depth, line)) |
| 224 |
continue |
| 225 |
if k == '+': |
| 226 |
continue |
| 227 |
|
| 228 |
if '+' in v: |
| 229 |
k = v['+'] |
| 230 |
print('%s%s' % (' ' * depth, k)) |
| 231 |
|
| 232 |
depth = depth + 4 |
| 233 |
recursePrint(v) |
| 234 |
depth = depth - 4 |
| 235 |
|
| 236 |
op = optparse.OptionParser() |
| 237 |
(options, args) = op.parse_args() |
| 238 |
if len(args) != 1: |
| 239 |
print('Please specify a file') |
| 240 |
sys.exit(-1) |
| 241 |
|
| 242 |
(output, isTree) = ct_tree_query(args[0]) |
| 243 |
if isTree: |
| 244 |
recursePrint(output) |
| 245 |
else: |
| 246 |
for line in output: |
| 247 |
print(line) |
| 248 |
|