Debian Codegraph @

src/web/backend/plugins/cscope/CscopeProject.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 # Copyright (c) 2010 Anil Kumar
4 # All rights reserved.
5 #
6 # License: BSD
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 class ConfigCscope(ConfigBase):
45 def __init__(self):
46 ConfigBase.__init__(self, 'cscope')
47 self.c_opt = []
48
49 def read_cs_files_common(self, filename):
50 fl = []
51 config_file = os.path.join(self.c_dir, filename)
52 if (os.path.exists(config_file)):
53 cf = open(config_file, 'r')
54 for f in cf:
55 f = f[:-1]
56 fl.append(f)
57 cf.close()
58 return fl
59
60 def read_cs_files(self):
61 self.c_flist = self.read_cs_files_common('cscope.files')
62
63 def write_cs_files_common(self, filename, fl):
64 if (len(fl) <= 0):
65 return
66 config_file = os.path.join(self.c_dir, filename)
67 cf = open(config_file, 'w')
68 for f in fl:
69 cf.write(f + '\n')
70 cf.close()
71
72 def write_cs_files(self):
73 self.write_cs_files_common("cscope.files", self.c_flist)
74
75 def get_config_file(self):
76 config_file = 'seascope.opt'
77 return os.path.join(self.c_dir, config_file)
78
79 def read_seascope_opt(self):
80 config_file = self.get_config_file()
81 if (not os.path.exists(config_file)):
82 return
83 cf = open(config_file, 'r')
84 for line in cf:
85 line = line.split('=', 1)
86 key = line[0].strip()
87 if (key == 'c_opt'):
88 self.c_opt = line[1].split()
89 cf.close()
90
91 def write_seascope_opt(self):
92 config_file = self.get_config_file()
93 cf = open(config_file, 'w')
94 cf.write('c_opt' + '=' + ' '.join(self.c_opt)+ '\n')
95 cf.close()
96
97 def read_config(self):
98 self.read_seascope_opt()
99 self.read_cs_files()
100
101 def write_config(self):
102 self.write_seascope_opt()
103 self.write_cs_files()
104
105 def is_ready(self):
106 return len(self.c_flist) > 0
107
108 class ProjectCscope(ProjectBase):
109 def __init__(self):
110 ProjectBase.__init__(self)
111
112 @staticmethod
113 def _prj_new_or_open(conf):
114 prj = ProjectCscope()
115 prj.feat = CscopeFeature()
116 prj.conf = conf
117 prj.qry = QueryCscope(prj.conf, prj.feat)
118
119 return (prj)
120
121 @staticmethod
122 def prj_new(proj_args):
123 conf = ConfigCscope()
124 conf.proj_new(proj_args)
125 prj = ProjectCscope._prj_new_or_open(conf)
126 return (prj)
127
128 @staticmethod
129 def prj_open(proj_path):
130 conf = ConfigCscope()
131 conf.proj_open(proj_path)
132 prj = ProjectCscope._prj_new_or_open(conf)
133 return (prj)
134
135 def prj_settings_update(self, proj_args):
136 assert proj_args
137 self.prj_update_conf(proj_args)
138 return True
139
140
141 class CsProcess(PluginProcess):
142 def __init__(self, wdir, rq):
143 PluginProcess.__init__(self, wdir, rq)
144 self.name = 'cscope process'
145
146 def parse_result(self, text, sig):
147 text = re.split(r'\r?\n', text)
148 if self.cmd_str == 'FIL':
149 res = [ ['', line[0], '', ''] for line in text if line != '' ]
150 return res
151 res = []
152 for line in text:
153 if line == '':
154 continue
155 line = line.split(' ', 3)
156 line = [line[1], line[0], line[2], line[3]]
157 res.append(line)
158 return res
159
160 class QueryCscope(QueryBase):
161 def __init__(self, conf, feat):
162 QueryBase.__init__(self)
163 self.conf = conf
164 self.feat = feat
165
166 def query(self, rquery):
167 if (not self.conf or not self.conf.is_ready()):
168 print("pm_query not is_ready")
169 return None
170 cmd_str = rquery['cmd']
171 req = rquery['req']
172 opt = rquery['opt']
173 cmd_id = self.feat.cmd_str2id[cmd_str]
174
175 if opt == None:
176 opt = []
177 opt_args = []
178 if 'substring' in opt:
179 req = '.*' + req + '.*'
180 if 'ignorecase' in opt:
181 opt_args += ['-C'];
182
183 pargs = [ 'cscope' ] + self.conf.c_opt + opt_args + [ '-L', '-d', '-' + str(cmd_id), req ]
184 qsig = CsProcess(self.conf.c_dir, [cmd_str, req]).run_query_process(pargs, req, rquery)
185 return qsig
186
187 def rebuild(self):
188 if (not self.conf.is_ready()):
189 print("pm_query not is_ready")
190 return None
191 pargs = [ 'cscope' ] + self.conf.c_opt + [ '-L' ]
192 qsig = CsProcess(self.conf.c_dir, None).run_rebuild_process(pargs)
193 return qsig
194
195 def query_fl(self):
196 fl = self.conf.get_proj_src_files()
197 return fl
198
199 def cs_is_open(self):
200 return self.conf != None
201 def cs_is_ready(self):
202 return self.conf.is_ready()