/usr/share/pyshared/seascope/view/filecontext/plugins/ctags_view/CtagsManager.py is in seascope 0.8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright (c) 2010 Anil Kumar
# All rights reserved.
#
# License: BSD
import subprocess
import re, os
def _eintr_retry_call(func, *args):
while True:
try:
return func(*args)
except OSError, e:
if e.errno == errno.EINTR:
continue
raise
def cmdForFile(f):
suffix_cmd_map = []
custom_map = os.getenv('SEASCOPE_CTAGS_SUFFIX_CMD_MAP')
if custom_map:
custom_map = eval(custom_map)
suffix_cmd_map += custom_map
#args = 'ctags -n -u --fields=+K -f - --extra=+q'
#args = 'ctags -n -u --fields=+Ki -f -'
args = 'ctags -n -u --fields=+K -f -'
suffix_cmd_map.append( ['', args] )
for (suffix, cmd) in suffix_cmd_map:
if f.endswith(suffix):
return cmd
return None
def ct_query(filename):
args = cmdForFile(filename)
args = args.split()
args.append(filename)
try:
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
(out_data, err_data) = _eintr_retry_call(proc.communicate)
out_data = out_data.split('\n')
except Exception as e:
out_data = [
'Failed to run ctags cmd\tignore\t0;\t ',
'cmd: %s\tignore\t0;\t ' % ' '.join(args),
'error: %s\tignore\t0;\t ' % str(e),
'ctags not installed ?\tignore\t0;\t ',
]
res = []
for line in out_data:
if (line == ''):
break
line = line.split('\t')
num = line[2].split(';', 1)[0]
line = [line[0], num, line[3]]
res.append(line)
return res
is_OrderedDict_available = False
try:
# OrderedDict available only in python >= 2.7
from collections import OrderedDict
is_OrderedDict_available = True
except:
pass
def emptyOrderedDict():
if is_OrderedDict_available:
return OrderedDict({})
return {}
class CtagsTreeBuilder:
def __init__(self):
self.symTree = emptyOrderedDict()
def cmdForFile(self, f):
suffix_cmd_map = []
custom_map = os.getenv('SEASCOPE_CTAGS_SUFFIX_CMD_MAP')
if custom_map:
custom_map = eval(custom_map)
suffix_cmd_map += custom_map
#args = 'ctags -n -u --fields=+K -f - --extra=+q'
#args = 'ctags -n -u --fields=+Ki -f -'
args = 'ctags -n -u --fields=+K-f-t -f -'
suffix_cmd_map.append( ['', args] )
for (suffix, cmd) in suffix_cmd_map:
if f.endswith(suffix):
return cmd
return None
def runCtags(self, f):
args = self.cmdForFile(f)
args = args.split()
args.append(f)
# In python >= 2.7 can use subprocess.check_output
# output = subprocess.check_output(args)
# return output
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
(out_data, err_data) = proc.communicate()
return out_data
def parseCtagsOutput(self, data):
data = re.split('\r?\n', data)
res = []
for line in data:
if line == '':
continue
try:
line = line.split('\t', 4)
res.append(line)
except:
print 'bad line:', line
return res
def addToSymLayout(self, sc):
t = self.symTree
if sc and sc != '':
for s in re.split('::|\.', sc):
if s not in t:
t[s] = emptyOrderedDict()
t = t[s]
def addToSymTree(self, sc, line):
t = self.symTree
if sc and sc != '':
for s in re.split('::|\.', sc):
assert s in t
t = t[s]
cline = [line[0], line[2].split(';')[0], line[3]]
if line[0] in t:
#print line[0], 'in', t
x = t[line[0]]
if '+' not in x:
x['+'] = cline
return
if '*' not in t:
t['*'] = []
t['*'].append(cline)
#print '...', t, line
def buildTree(self, data):
type_list = [ 'namespace', 'class', 'interface', 'struct', 'union', 'enum', 'function' ]
# build layout using 5th field
for line in data:
if len(line) == 4:
continue
try:
sd = dict([ x.split(':', 1) for x in line[4].split('\t')])
except:
print 'bad line', line
continue
line[4] = sd
count = 0
for t in type_list:
if t in sd:
self.addToSymLayout(sd[t])
count = count + 1
if count != 1:
print '******** count == 1 *********'
print data
print line
#assert count == 1
if len(self.symTree) == 0:
return (data, False)
for line in data:
if len(line) == 4:
self.addToSymTree(None, line)
continue
sd = line[4]
count = 0
for t in type_list:
if t in sd:
self.addToSymTree(sd[t], line)
count = count + 1
if count != 1:
print '******** count == 1 *********'
print data
print line
#assert count == 1
return (self.symTree, True)
def doQuery(self, filename):
try:
output = self.runCtags(filename)
output = self.parseCtagsOutput(output)
output = self.buildTree(output)
except Exception as e:
print str(e)
output = [None, False]
return output
def ct_tree_query(filename):
ct = CtagsTreeBuilder()
output = ct.doQuery(filename)
return output
if __name__ == '__main__':
import optparse
import sys
depth = 0
def recursePrint(t):
global depth
for k, v in t.items():
if k == '*':
for line in v:
print '%s%s' % (' ' * depth, line)
continue
if k == '+':
continue
if '+' in v:
k = v['+']
print '%s%s' % (' ' * depth, k)
depth = depth + 4
recursePrint(v)
depth = depth - 4
op = optparse.OptionParser()
(options, args) = op.parse_args()
if len(args) != 1:
print 'Please specify a file'
sys.exit(-1)
(output, isTree) = ct_tree_query(args[0])
if isTree:
recursePrint(output)
else:
for line in output:
print line
|