| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
import os, string, re |
| 9 |
|
| 10 |
from ..PluginBase import PluginFeatureBase, ProjectBase, ConfigBase, QueryBase |
| 11 |
from ..PluginBase import PluginProcess |
| 12 |
|
| 13 |
|
| 14 |
class CscopeFeature(PluginFeatureBase): |
| 15 |
def __init__(self): |
| 16 |
PluginFeatureBase.__init__(self) |
| 17 |
|
| 18 |
self.feat_desc = [ |
| 19 |
['REF', '0'], |
| 20 |
['DEF', '1'], |
| 21 |
['<--', '2'], |
| 22 |
['-->', '3'], |
| 23 |
['TXT', '4'], |
| 24 |
['GREP','5'], |
| 25 |
['FIL', '7'], |
| 26 |
['INC', '8'], |
| 27 |
|
| 28 |
['QDEF', '11'], |
| 29 |
['CTREE','12'], |
| 30 |
|
| 31 |
['CLGRAPH', '13'], |
| 32 |
['CLGRAPHD', '14'], |
| 33 |
['FFGRAPH', '14'], |
| 34 |
|
| 35 |
['UPD', '25'], |
| 36 |
] |
| 37 |
|
| 38 |
self.ctree_query_args = [ |
| 39 |
['-->', '--> F', 'Calling tree' ], |
| 40 |
['<--', 'F -->', 'Called tree' ], |
| 41 |
['REF', '==> F', 'Advanced calling tree' ], |
| 42 |
] |
| 43 |
|
| 44 |
def query_dlg_cb(self, req, cmd_str, in_opt): |
| 45 |
if req != '' and in_opt['substring']: |
| 46 |
req = '.*' + req + '.*' |
| 47 |
opt = None |
| 48 |
if in_opt['ignorecase']: |
| 49 |
opt = '-C' |
| 50 |
res = (cmd_str, req, opt) |
| 51 |
return res |
| 52 |
|
| 53 |
class ConfigCscope(ConfigBase): |
| 54 |
def __init__(self): |
| 55 |
ConfigBase.__init__(self, 'cscope') |
| 56 |
self.c_opt = [] |
| 57 |
|
| 58 |
def read_cs_files_common(self, filename): |
| 59 |
fl = [] |
| 60 |
config_file = os.path.join(self.c_dir, filename) |
| 61 |
if (os.path.exists(config_file)): |
| 62 |
cf = open(config_file, 'r') |
| 63 |
for f in cf: |
| 64 |
f = f[:-1] |
| 65 |
fl.append(f) |
| 66 |
cf.close() |
| 67 |
return fl |
| 68 |
|
| 69 |
def read_cs_files(self): |
| 70 |
self.c_flist = self.read_cs_files_common('cscope.files') |
| 71 |
|
| 72 |
def write_cs_files_common(self, filename, fl): |
| 73 |
if (len(fl) <= 0): |
| 74 |
return |
| 75 |
config_file = os.path.join(self.c_dir, filename) |
| 76 |
cf = open(config_file, 'w') |
| 77 |
for f in fl: |
| 78 |
cf.write(f + '\n') |
| 79 |
cf.close() |
| 80 |
|
| 81 |
def write_cs_files(self): |
| 82 |
self.write_cs_files_common("cscope.files", self.c_flist) |
| 83 |
|
| 84 |
def get_config_file(self): |
| 85 |
config_file = 'seascope.opt' |
| 86 |
return os.path.join(self.c_dir, config_file) |
| 87 |
|
| 88 |
def read_seascope_opt(self): |
| 89 |
config_file = self.get_config_file() |
| 90 |
if (not os.path.exists(config_file)): |
| 91 |
return |
| 92 |
cf = open(config_file, 'r') |
| 93 |
for line in cf: |
| 94 |
line = line.split('=', 1) |
| 95 |
key = line[0].strip() |
| 96 |
if (key == 'c_opt'): |
| 97 |
self.c_opt = line[1].split() |
| 98 |
cf.close() |
| 99 |
|
| 100 |
def write_seascope_opt(self): |
| 101 |
config_file = self.get_config_file() |
| 102 |
cf = open(config_file, 'w') |
| 103 |
cf.write('c_opt' + '=' + ' '.join(self.c_opt) + '\n') |
| 104 |
cf.close() |
| 105 |
|
| 106 |
def read_config(self): |
| 107 |
self.read_seascope_opt() |
| 108 |
self.read_cs_files() |
| 109 |
|
| 110 |
def write_config(self): |
| 111 |
self.write_seascope_opt() |
| 112 |
self.write_cs_files() |
| 113 |
|
| 114 |
def is_ready(self): |
| 115 |
return len(self.c_flist) > 0 |
| 116 |
|
| 117 |
class ProjectCscope(ProjectBase): |
| 118 |
def __init__(self): |
| 119 |
ProjectBase.__init__(self) |
| 120 |
|
| 121 |
@staticmethod |
| 122 |
def _prj_new_or_open(conf): |
| 123 |
prj = ProjectCscope() |
| 124 |
prj.feat = CscopeFeature() |
| 125 |
prj.conf = conf |
| 126 |
prj.qry = QueryCscope(prj.conf, prj.feat) |
| 127 |
|
| 128 |
return (prj) |
| 129 |
|
| 130 |
@staticmethod |
| 131 |
def prj_new(proj_args): |
| 132 |
conf = ConfigCscope() |
| 133 |
conf.proj_new(proj_args) |
| 134 |
prj = ProjectCscope._prj_new_or_open(conf) |
| 135 |
return (prj) |
| 136 |
|
| 137 |
@staticmethod |
| 138 |
def prj_open(proj_path): |
| 139 |
conf = ConfigCscope() |
| 140 |
conf.proj_open(proj_path) |
| 141 |
prj = ProjectCscope._prj_new_or_open(conf) |
| 142 |
return (prj) |
| 143 |
|
| 144 |
def prj_settings_update(self, proj_args): |
| 145 |
assert proj_args |
| 146 |
self.prj_update_conf(proj_args) |
| 147 |
return True |
| 148 |
|
| 149 |
|
| 150 |
class CsProcess(PluginProcess): |
| 151 |
def __init__(self, wdir, rq): |
| 152 |
PluginProcess.__init__(self, wdir, rq) |
| 153 |
self.name = 'cscope process' |
| 154 |
|
| 155 |
def parse_result(self, text, sig): |
| 156 |
text = re.split(r'\r?\n', text) |
| 157 |
if self.cmd_str == 'FIL': |
| 158 |
res = [ ['', line[0], '', ''] for line in text if line != '' ] |
| 159 |
return res |
| 160 |
res = [] |
| 161 |
for line in text: |
| 162 |
if line == '': |
| 163 |
continue |
| 164 |
line = line.split(' ', 3) |
| 165 |
line = [line[1], line[0], line[2], line[3]] |
| 166 |
res.append(line) |
| 167 |
return res |
| 168 |
|
| 169 |
class QueryCscope(QueryBase): |
| 170 |
def __init__(self, conf, feat): |
| 171 |
QueryBase.__init__(self) |
| 172 |
self.conf = conf |
| 173 |
self.feat = feat |
| 174 |
|
| 175 |
def query(self, rquery): |
| 176 |
if (not self.conf or not self.conf.is_ready()): |
| 177 |
print("pm_query not is_ready") |
| 178 |
return None |
| 179 |
cmd_str = rquery['cmd'] |
| 180 |
req = rquery['req'] |
| 181 |
opt = rquery['opt'] |
| 182 |
cmd_id = self.feat.cmd_str2id[cmd_str] |
| 183 |
if opt == None or opt == '': |
| 184 |
opt = [] |
| 185 |
else: |
| 186 |
opt = opt.split() |
| 187 |
pargs = [ 'cscope' ] + self.conf.c_opt + opt + [ '-L', '-d', '-' + str(cmd_id), req ] |
| 188 |
qsig = CsProcess(self.conf.c_dir, [cmd_str, req]).run_query_process(pargs, req, rquery) |
| 189 |
return qsig |
| 190 |
|
| 191 |
def rebuild(self): |
| 192 |
if (not self.conf.is_ready()): |
| 193 |
print("pm_query not is_ready") |
| 194 |
return None |
| 195 |
pargs = [ 'cscope' ] + self.conf.c_opt + [ '-L' ] |
| 196 |
qsig = CsProcess(self.conf.c_dir, None).run_rebuild_process(pargs) |
| 197 |
return qsig |
| 198 |
|
| 199 |
def query_fl(self): |
| 200 |
fl = self.conf.get_proj_src_files() |
| 201 |
return fl |
| 202 |
|
| 203 |
def cs_is_open(self): |
| 204 |
return self.conf != None |
| 205 |
def cs_is_ready(self): |
| 206 |
return self.conf.is_ready() |