| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import os, re |
| 7 |
import copy |
| 8 |
import subprocess |
| 9 |
|
| 10 |
def NOT_IMPLEMENTED(n, f): |
| 11 |
msg = '%s: %s: Not implemeted' % (n, f) |
| 12 |
print(msg) |
| 13 |
assert false |
| 14 |
|
| 15 |
from . import CtagsCache |
| 16 |
|
| 17 |
is_seascope_debug = os.getenv('SEASCOPE_DEBUG') |
| 18 |
|
| 19 |
cmd_table_master = [ |
| 20 |
[ 'REF', ['&References', 'Ctrl+0'], ['References to' ] ], |
| 21 |
[ 'DEF', ['&Definitions', 'Ctrl+1'], ['Definition of' ] ], |
| 22 |
[ '<--', ['&Called Functions', 'Ctrl+2'], ['Functions called by' ] ], |
| 23 |
[ '-->', ['C&alling Functions', 'Ctrl+3'], ['Functions calling' ] ], |
| 24 |
[ 'TXT', ['Find &Text', 'Ctrl+4'], ['Find text' ] ], |
| 25 |
[ 'GREP', ['Find &Egrep', 'Ctrl+5'], ['Find egrep pattern' ] ], |
| 26 |
[ 'FIL', ['Find &File', 'Ctrl+7'], ['Find files' ] ], |
| 27 |
[ 'INC', ['&Include/Import', 'Ctrl+8'], ['Find include/import' ] ], |
| 28 |
[ '---', [None, ], None ], |
| 29 |
[ 'QDEF', ['&Quick Definition', 'Ctrl+]'], None ], |
| 30 |
[ 'CTREE', ['Call Tr&ee', 'Ctrl+\\'], ['Call tree' ] ], |
| 31 |
[ '---', [None, ], None ], |
| 32 |
[ 'CLGRAPH', ['Class &Graph', 'Ctrl+:'], ['Class graph' ] ], |
| 33 |
[ 'CLGRAPHD', ['Class Graph Dir', 'Ctrl+;'], ['Class graph dir' ] ], |
| 34 |
[ 'FFGRAPH', ['File Func Graph', 'Ctrl+^'], ['File Func graph dir' ] ], |
| 35 |
[ '---', [None, ], None ], |
| 36 |
[ 'UPD', ['Re&build Database', None ], None ], |
| 37 |
] |
| 38 |
class PluginFeatureBase: |
| 39 |
def __init__(self): |
| 40 |
self.clgraph_query_args = [ |
| 41 |
['CLGRAPH', 'D', 'Derived classes' ], |
| 42 |
['CLGRAPH', 'B', 'Base classes' ], |
| 43 |
] |
| 44 |
self.clgraph_query_args = [ |
| 45 |
['CLGRAPH', 'D', 'Derived classes' ], |
| 46 |
['CLGRAPH', 'B', 'Base classes' ], |
| 47 |
] |
| 48 |
self.ffgraph_query_args = [ |
| 49 |
['FFGRAPH', 'F', 'File functions graph'], |
| 50 |
['FFGRAPH_E', 'F+E', 'File functions + external graph'], |
| 51 |
['FFGRAPH_D', 'D', 'Directory functions graph'], |
| 52 |
['FFGRAPH_DE', 'D+E', 'Directory functions + external graph'] |
| 53 |
] |
| 54 |
|
| 55 |
def setup(self): |
| 56 |
feat_cmds = [ d[0] for d in self.feat_desc ] |
| 57 |
ct = copy.deepcopy(cmd_table_master) |
| 58 |
self.cmd_table = [ t for t in ct if t[0] in feat_cmds or t[0] == '---' ] |
| 59 |
|
| 60 |
self.menu_cmd_list = [ [c[0]] + c[1] for c in self.cmd_table ] |
| 61 |
|
| 62 |
self.cmd_str2id = {} |
| 63 |
self.cmd_str2qstr = {} |
| 64 |
self.cmd_qstr2str = {} |
| 65 |
for c in self.feat_desc: |
| 66 |
self.cmd_str2id[c[0]] = c[1] |
| 67 |
for c in self.cmd_table: |
| 68 |
if c[2] != None: |
| 69 |
self.cmd_str2qstr[c[0]] = c[2][0] |
| 70 |
self.cmd_qstr2str[c[2][0]] = c[0] |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
self.cmd_qstrlist = [ c[2][0] for c in self.cmd_table if c[2] ] |
| 76 |
|
| 77 |
class ProjectBase: |
| 78 |
prj = None |
| 79 |
qry = None |
| 80 |
|
| 81 |
def __init__(self): |
| 82 |
self.feat = None |
| 83 |
|
| 84 |
def prj_close(self): |
| 85 |
if (self.conf != None): |
| 86 |
self.conf.proj_close() |
| 87 |
self.conf = None |
| 88 |
|
| 89 |
def prj_dir(self): |
| 90 |
return self.conf.c_dir |
| 91 |
def prj_name(self): |
| 92 |
return self.conf.get_proj_name() |
| 93 |
def prj_src_files(self): |
| 94 |
return self.conf.get_proj_src_files() |
| 95 |
|
| 96 |
def prj_is_open(self): |
| 97 |
return self.conf != None |
| 98 |
def prj_is_ready(self): |
| 99 |
return self.conf.is_ready() |
| 100 |
|
| 101 |
def prj_conf(self): |
| 102 |
return self.conf.get_proj_conf() |
| 103 |
|
| 104 |
def prj_update_conf(self, proj_args): |
| 105 |
self.conf.proj_update(proj_args) |
| 106 |
|
| 107 |
def prj_show_settings(self, proj_args): |
| 108 |
NOT_IMPLEMENTED(__name__, __func__) |
| 109 |
def prj_settings(self, proj_args): |
| 110 |
NOT_IMPLEMENTED(__name__, __func__) |
| 111 |
|
| 112 |
def prj_feature_setup(self): |
| 113 |
self.feat.setup() |
| 114 |
|
| 115 |
def prj_query(self, rquery): |
| 116 |
return self.qry.query(rquery) |
| 117 |
|
| 118 |
def prj_rebuild(self): |
| 119 |
return self.qry.rebuild() |
| 120 |
|
| 121 |
def prj_query_fl(self): |
| 122 |
return self.qry.query_fl() |
| 123 |
|
| 124 |
def prj_type(self): |
| 125 |
return self.conf.prj_type |
| 126 |
|
| 127 |
def prj_feature(self): |
| 128 |
return self.feat |
| 129 |
|
| 130 |
def prj_settings_get(self): |
| 131 |
proj_args = self.prj_conf() |
| 132 |
return proj_args |
| 133 |
|
| 134 |
def prj_settings_update(self, proj_args): |
| 135 |
NOT_IMPLEMENTED(__name__, __func__) |
| 136 |
return |
| 137 |
|
| 138 |
class ConfigBase: |
| 139 |
def __init__(self, ptype): |
| 140 |
self.prj_type = ptype |
| 141 |
self.c_dir = '' |
| 142 |
self.c_opt = '' |
| 143 |
self.c_flist = [] |
| 144 |
|
| 145 |
def get_proj_name(self): |
| 146 |
return os.path.split(self.c_dir)[1] |
| 147 |
|
| 148 |
def get_proj_src_files(self): |
| 149 |
fl = self.c_flist |
| 150 |
return fl |
| 151 |
|
| 152 |
def get_proj_conf(self): |
| 153 |
return (self.c_dir, self.c_opt, self.c_flist) |
| 154 |
|
| 155 |
def read_config(self): |
| 156 |
pass |
| 157 |
def write_config(self): |
| 158 |
pass |
| 159 |
|
| 160 |
def proj_start(self): |
| 161 |
pass |
| 162 |
|
| 163 |
def proj_open(self, proj_path): |
| 164 |
self.c_dir = proj_path |
| 165 |
self.read_config() |
| 166 |
self.proj_start() |
| 167 |
|
| 168 |
def proj_update(self, proj_args): |
| 169 |
self.proj_new(proj_args) |
| 170 |
|
| 171 |
def proj_new(self, proj_args): |
| 172 |
self.proj_args = proj_args |
| 173 |
(self.c_dir, self.c_opt, self.c_flist) = proj_args |
| 174 |
self.write_config() |
| 175 |
self.proj_start() |
| 176 |
|
| 177 |
def proj_close(self): |
| 178 |
pass |
| 179 |
|
| 180 |
def is_ready(self): |
| 181 |
return True |
| 182 |
|
| 183 |
@staticmethod |
| 184 |
def prepare_menu(menubar): |
| 185 |
pass |
| 186 |
|
| 187 |
class QueryBase: |
| 188 |
@staticmethod |
| 189 |
def prepare_menu(menubar): |
| 190 |
pass |
| 191 |
|
| 192 |
def __init__(self): |
| 193 |
pass |
| 194 |
|
| 195 |
def query(self, rquery): |
| 196 |
cmd_str = rquery['cmd'] |
| 197 |
req = rquery['req'] |
| 198 |
opt = rquery['opt'] |
| 199 |
if opt == None: |
| 200 |
opt = [] |
| 201 |
|
| 202 |
pargs = [] |
| 203 |
if cmd_str == 'GREP': |
| 204 |
pargs = ['grep', '-E', '-R', '-n', '-I'] |
| 205 |
pargs += [ '--', req ] |
| 206 |
pargs += [self.conf.c_dir] |
| 207 |
else: |
| 208 |
assert(false) |
| 209 |
return None |
| 210 |
qsig = PluginProcess(self.conf.c_dir, [cmd_str, req]).run_query_process(pargs, req, rquery) |
| 211 |
return qsig |
| 212 |
|
| 213 |
def rebuild(): |
| 214 |
NOT_IMPLEMENTED(__name__, __func__) |
| 215 |
|
| 216 |
def conf_is_open(self): |
| 217 |
return self.conf != None |
| 218 |
def conf_is_ready(self): |
| 219 |
return self.conf.is_ready() |
| 220 |
|
| 221 |
|
| 222 |
class QueryUiBase: |
| 223 |
def __init__(self): |
| 224 |
pass |
| 225 |
|
| 226 |
class QuerySignal: |
| 227 |
def __init__(self): |
| 228 |
pass |
| 229 |
|
| 230 |
def _relevancy_sort(self, hfile, res): |
| 231 |
pt = [] |
| 232 |
pd = {} |
| 233 |
p = hfile |
| 234 |
(pre, ext) = os.path.splitext(hfile) |
| 235 |
c = None |
| 236 |
while p != c: |
| 237 |
e = [p, [], []] |
| 238 |
pt += [e] |
| 239 |
pd[p] = e |
| 240 |
c = p |
| 241 |
p = os.path.dirname(p) |
| 242 |
for line in res: |
| 243 |
f = line[1] |
| 244 |
d = os.path.dirname(f) |
| 245 |
p = f |
| 246 |
while p not in pd: |
| 247 |
p = os.path.dirname(p) |
| 248 |
e = pd[p] |
| 249 |
if p in [f, d]: |
| 250 |
e[1].append(line) |
| 251 |
else: |
| 252 |
e[2].append(line) |
| 253 |
for e in pt: |
| 254 |
e[1] = sorted(e[1], key=lambda li: li[1]) |
| 255 |
e[2] = sorted(e[2], key=lambda li: li[1]) |
| 256 |
pre = pre + '.*' |
| 257 |
e0 = [] |
| 258 |
e1 = [] |
| 259 |
for e in pt[1][1]: |
| 260 |
if re.match(pre, e[1]): |
| 261 |
e0 += [e] |
| 262 |
else: |
| 263 |
e1 += [e] |
| 264 |
pt[0][1] += e0 |
| 265 |
pt[1][1] = e1 |
| 266 |
|
| 267 |
res1 = [] |
| 268 |
res2 = [] |
| 269 |
for e in pt: |
| 270 |
res1 += e[1] |
| 271 |
res2 += e[2] |
| 272 |
res = res1 + res2 |
| 273 |
return res |
| 274 |
|
| 275 |
def relevancy_sort(self, res): |
| 276 |
if os.getenv('RELEVANCY_SORT', 1) == 0: |
| 277 |
return res |
| 278 |
hint_file = None |
| 279 |
try: |
| 280 |
hint_file = self.rquery['hint_file'] |
| 281 |
except: |
| 282 |
pass |
| 283 |
if not hint_file: |
| 284 |
return res |
| 285 |
if not os.path.isabs(hint_file): |
| 286 |
print('BUG: relevancy_sort: not abs path:', hint_file) |
| 287 |
return res |
| 288 |
if len(res) > 10000: |
| 289 |
return res |
| 290 |
return self._relevancy_sort(hint_file, res) |
| 291 |
|
| 292 |
class PluginProcessBase: |
| 293 |
proc_list = [] |
| 294 |
|
| 295 |
def __init__(self, wdir): |
| 296 |
self.wdir = wdir |
| 297 |
self.proc = None |
| 298 |
self.outd = {} |
| 299 |
|
| 300 |
PluginProcess.proc_list.append(self) |
| 301 |
self.is_rebuild = False |
| 302 |
self.is_query_fl = False |
| 303 |
|
| 304 |
self.sig = QuerySignal() |
| 305 |
|
| 306 |
def _cleanup(self): |
| 307 |
PluginProcess.proc_list.remove(self) |
| 308 |
e = self.outd['err_data'] |
| 309 |
if e: |
| 310 |
s = '<b>' + self.p_cmd + '</b><p>' + '<p>'.join(e.splitlines()) |
| 311 |
print('s') |
| 312 |
r = self.outd['err_data'] |
| 313 |
if r: |
| 314 |
s = '<b>' + self.p_cmd + '</b><p>Summary<p>' + r |
| 315 |
print('s') |
| 316 |
|
| 317 |
def run_process(self, pargs): |
| 318 |
if is_seascope_debug: |
| 319 |
print(self.p_cmd) |
| 320 |
try: |
| 321 |
self.proc = subprocess.Popen(pargs, cwd=self.wdir, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 322 |
(out_data, err_data) = self.proc.communicate() |
| 323 |
print('ANIL2 %s' % out_data.decode()) |
| 324 |
print('ANIL %s' % out_data) |
| 325 |
self.outd['out_data'] = out_data.decode() |
| 326 |
self.outd['err_data'] = err_data.decode() |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
except Exception as e: |
| 334 |
print('PluginProcess.run():', e) |
| 335 |
self.outd['out_data'] = None |
| 336 |
self.outd['err_data'] = str(e) |
| 337 |
|
| 338 |
self._finished_cb() |
| 339 |
|
| 340 |
def run_query_process(self, pargs, sym, rquery=None): |
| 341 |
self.sig.sym = sym |
| 342 |
self.sig.rquery = rquery |
| 343 |
self.p_cmd = ' '.join(pargs) |
| 344 |
self.run_process(pargs) |
| 345 |
return self.outd |
| 346 |
|
| 347 |
def _finished_cb(self): |
| 348 |
if 'out_data' in self.outd: |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
res = self.outd['out_data'] |
| 353 |
self.outd['out_data'] |
| 354 |
|
| 355 |
if self.is_rebuild: |
| 356 |
pass |
| 357 |
elif self.is_query_fl: |
| 358 |
res = self.parse_query_fl(res) |
| 359 |
else: |
| 360 |
|
| 361 |
try: |
| 362 |
res = self.parse_result(res, self.sig) |
| 363 |
res = self.sig.relevancy_sort(res); |
| 364 |
except Exception as e: |
| 365 |
print(e) |
| 366 |
res = [['', '', '', 'error while parsing output of: ' + self.p_cmd]] |
| 367 |
|
| 368 |
self.outd['res'] = res |
| 369 |
self._cleanup() |
| 370 |
|
| 371 |
def run_rebuild_process(self, pargs): |
| 372 |
self.is_rebuild = True |
| 373 |
self.p_cmd = ' '.join(pargs) |
| 374 |
self.run_process(pargs) |
| 375 |
CtagsCache.flush() |
| 376 |
return self.outd |
| 377 |
|
| 378 |
def run_query_fl(self, pargs): |
| 379 |
self.is_query_fl = True |
| 380 |
self.p_cmd = ' '.join(pargs) |
| 381 |
self.run_process(pargs) |
| 382 |
return self.outd |
| 383 |
|
| 384 |
def parse_query_fl(self, text): |
| 385 |
fl = [] |
| 386 |
for f in re.split(r'\r?\n', text.strip()): |
| 387 |
if f == '': |
| 388 |
continue |
| 389 |
fl.append(os.path.join(self.wdir, f)) |
| 390 |
return fl |
| 391 |
|
| 392 |
class PluginProcess(PluginProcessBase): |
| 393 |
def __init__(self, wdir, rq): |
| 394 |
PluginProcessBase.__init__(self, wdir) |
| 395 |
if rq == None: |
| 396 |
rq = ['', ''] |
| 397 |
self.cmd_str = rq[0] |
| 398 |
self.req = rq[1] |
| 399 |
|
| 400 |
def parse_result(self, text, sig): |
| 401 |
text = re.split(r'\r?\n', text) |
| 402 |
|
| 403 |
res = [] |
| 404 |
if self.cmd_str == 'GREP': |
| 405 |
for line in text: |
| 406 |
if line == '': |
| 407 |
continue |
| 408 |
line = ['<unknown>'] + line.split(':', 2) |
| 409 |
res.append(line) |
| 410 |
else: |
| 411 |
assert(false) |
| 412 |
res.append(['', '', '', 'PluginProcess.parse_result: FAILED']) |
| 413 |
return res |
| 414 |
|
| 415 |
res = CtagsCache.CtagsThread(sig).apply_fix(self.cmd_str, res, ['<unknown>']) |
| 416 |
return res |
| 417 |
|
| 418 |
if __name__ == '__main__': |
| 419 |
import sys |
| 420 |
|
| 421 |
def slot_result(sym, res): |
| 422 |
print('slot_result: ', [str(sym), res]) |
| 423 |
sys.exit(0) |
| 424 |
def slot_result_dbg(cmd, res, err_str): |
| 425 |
print('slot_result_dbg:', [str(cmd), str(res).strip().split('\n'), str(err_str)]) |
| 426 |
def slot_rebuild(): |
| 427 |
print('slot_rebuild') |
| 428 |
|
| 429 |
res = PluginProcess('.').run_query_process(['ls'], 'ls') |
| 430 |
|
| 431 |
print('res', res) |