| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from PyQt6.QtWidgets import * |
| 9 |
from PyQt6.QtGui import * |
| 10 |
from PyQt6.QtCore import * |
| 11 |
from PyQt6.QtSvgWidgets import * |
| 12 |
import os |
| 13 |
import sys |
| 14 |
|
| 15 |
if __name__ == '__main__': |
| 16 |
import sys |
| 17 |
import os |
| 18 |
app_dir = os.path.dirname(os.path.realpath(__file__)) |
| 19 |
os.chdir(app_dir) |
| 20 |
sys.path.insert(0, os.path.abspath('..')) |
| 21 |
os.chdir(os.path.abspath('..')) |
| 22 |
|
| 23 |
from backend.plugins.PluginBase import PluginProcess |
| 24 |
|
| 25 |
class FileFuncGraphProcess(PluginProcess): |
| 26 |
def __init__(self, wdir, rq): |
| 27 |
PluginProcess.__init__(self, wdir, rq) |
| 28 |
self.name = 'call graph process' |
| 29 |
|
| 30 |
def parse_result(self, text, sig): |
| 31 |
return [text] |
| 32 |
|
| 33 |
|
| 34 |
class FileFuncGraphWidget(QWidget): |
| 35 |
def __init__(self, parent, cmd_func, cmd_id, cmd_opt): |
| 36 |
QWidget.__init__(self, parent) |
| 37 |
self.is_busy = False |
| 38 |
self.is_done = False |
| 39 |
self.cmd_func = cmd_func |
| 40 |
self.cmd_id = cmd_id |
| 41 |
self.cmd_opt = cmd_opt |
| 42 |
self.is_debug = os.getenv('SEASCOPE_FILE_FUNC_GRAPH_VIEW_DEBUG') |
| 43 |
|
| 44 |
self.vlay1 = QVBoxLayout() |
| 45 |
self.setLayout(self.vlay1) |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
self.lbl = QLabel() |
| 50 |
self.vlay1.addWidget(self.lbl) |
| 51 |
|
| 52 |
self.vlay2 = QVBoxLayout() |
| 53 |
self.scrolla = QScrollArea() |
| 54 |
self.scrolla.setLayout(self.vlay2) |
| 55 |
self.vlay1.addWidget(self.scrolla) |
| 56 |
|
| 57 |
self.warn_experimental_lbl = QLabel('Warning: this is an EXPERIMENTAL feature based on cscope, might not work on all files for now.') |
| 58 |
self.warn_experimental_lbl.setStyleSheet("QLabel { color : red; }") |
| 59 |
self.vlay1.addWidget(self.warn_experimental_lbl) |
| 60 |
|
| 61 |
def startQuery(self, req, dname, proj_dir, inx): |
| 62 |
if self.is_done: |
| 63 |
return |
| 64 |
|
| 65 |
name = req if req else dname |
| 66 |
labelList = [ |
| 67 |
'File functions graph(%s)' % name, |
| 68 |
'File functions and external graph(%s)' %name, |
| 69 |
'Directory functions graph(%s)' % os.path.dirname(name), |
| 70 |
'Directory functions and external graph(%s)' % os.path.dirname(name) |
| 71 |
] |
| 72 |
self.lbl.setText(labelList[inx]) |
| 73 |
|
| 74 |
tool_path = os.path.join('tools', 'FileFuncGraph.py') |
| 75 |
pargs = [sys.executable, tool_path] |
| 76 |
if inx == 0: |
| 77 |
pass |
| 78 |
elif inx == 1: |
| 79 |
pargs += ['-e'] |
| 80 |
elif inx == 2: |
| 81 |
dname = os.path.dirname(name) |
| 82 |
elif inx == 3: |
| 83 |
pargs += ['-e'] |
| 84 |
dname = os.path.dirname(name) |
| 85 |
|
| 86 |
pargs += ['-d', dname] |
| 87 |
sig_res = FileFuncGraphProcess('.', None).run_query_process(pargs, req) |
| 88 |
sig_res[0].connect(self.clgraph_add_result) |
| 89 |
self.is_busy = True |
| 90 |
self.show_progress_bar() |
| 91 |
|
| 92 |
def set_current(self, btn): |
| 93 |
inx = self.bgrp.id(btn) |
| 94 |
|
| 95 |
|
| 96 |
if inx == 0: |
| 97 |
print(self.svgw.renderer().defaultSize()) |
| 98 |
self.svgw.setMinimumSize(0, 0) |
| 99 |
self.svgw.setMinimumSize(self.svgw.sizeHint()) |
| 100 |
|
| 101 |
print(self.scrolla.sizeHint()) |
| 102 |
if inx == 1: |
| 103 |
print(self.svgw.renderer().defaultSize()) |
| 104 |
self.svgw.setMinimumSize(0, 0) |
| 105 |
|
| 106 |
print(self.scrolla.sizeHint()) |
| 107 |
if inx == 2: |
| 108 |
print(self.svgw.renderer().defaultSize()) |
| 109 |
self.svgw.setMinimumSize(0, 0) |
| 110 |
self.svgw.resize(self.scrolla.size()) |
| 111 |
|
| 112 |
print(self.scrolla.sizeHint()) |
| 113 |
|
| 114 |
def add_buttons(self, hlay): |
| 115 |
self.bgrp = QButtonGroup() |
| 116 |
self.bgrp.buttonClicked.connect(self.set_current) |
| 117 |
self.bgrp.setExclusive(True) |
| 118 |
for inx in range(3): |
| 119 |
btn = QToolButton() |
| 120 |
btn.setText(str(inx)) |
| 121 |
btn.setToolTip(str(inx)) |
| 122 |
|
| 123 |
btn.setCheckable(True) |
| 124 |
self.bgrp.addButton(btn, inx) |
| 125 |
hlay.addWidget(btn) |
| 126 |
|
| 127 |
def clgraph_add_result(self, req, res): |
| 128 |
if self.is_debug: |
| 129 |
print(res) |
| 130 |
self.is_busy = False |
| 131 |
self.is_done = True |
| 132 |
self.remove_progress_bar() |
| 133 |
|
| 134 |
self.svgw = QSvgWidget() |
| 135 |
self.scrolla.setWidget(self.svgw) |
| 136 |
self.svgw.load(QByteArray(res[0].encode())) |
| 137 |
|
| 138 |
|
| 139 |
sz = self.svgw.sizeHint() |
| 140 |
scale = 1 |
| 141 |
if sz.width() > 1024: |
| 142 |
scale = 0.8 |
| 143 |
self.svgw.setMinimumSize(int(sz.width() * scale), int(sz.height() * scale)) |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
def show_progress_bar(self): |
| 148 |
self.pbar = QProgressBar(self.scrolla) |
| 149 |
self.pbar.setMinimum(0) |
| 150 |
self.pbar.setMaximum(0) |
| 151 |
self.pbar.show() |
| 152 |
|
| 153 |
def remove_progress_bar(self): |
| 154 |
if self.pbar: |
| 155 |
self.pbar.hide() |
| 156 |
self.pbar.setParent(None) |
| 157 |
self.pbar = None |
| 158 |
|
| 159 |
class FileFuncGraphWindow(QMainWindow): |
| 160 |
parent = None |
| 161 |
|
| 162 |
def __init__(self, req, dname, proj_dir, cmd_func, cmd_args, cmd_opt): |
| 163 |
QMainWindow.__init__(self, FileFuncGraphWindow.parent) |
| 164 |
self.req = req |
| 165 |
self.dname = dname |
| 166 |
self.proj_dir = proj_dir |
| 167 |
|
| 168 |
if req: |
| 169 |
self.setWindowTitle(req) |
| 170 |
else: |
| 171 |
self.setWindowTitle(dname) |
| 172 |
|
| 173 |
self.setFont(QFont("San Serif", 8)) |
| 174 |
|
| 175 |
w = QWidget() |
| 176 |
self.setCentralWidget(w) |
| 177 |
self.vlay = QVBoxLayout() |
| 178 |
w.setLayout(self.vlay) |
| 179 |
|
| 180 |
self.sw = QStackedWidget() |
| 181 |
|
| 182 |
self.hlay = QHBoxLayout() |
| 183 |
self.vlay.addLayout(self.hlay) |
| 184 |
self.vlay.addWidget(self.sw) |
| 185 |
|
| 186 |
self.bgrp = QButtonGroup() |
| 187 |
self.bgrp.buttonClicked.connect(self.set_current) |
| 188 |
self.bgrp.setExclusive(True) |
| 189 |
|
| 190 |
self.btn = [] |
| 191 |
self.ctree = [] |
| 192 |
for inx in range(len(cmd_args)): |
| 193 |
|
| 194 |
cmd = cmd_args[inx] |
| 195 |
|
| 196 |
btn = QToolButton() |
| 197 |
btn.setText(cmd[1]) |
| 198 |
btn.setToolTip(cmd[2]) |
| 199 |
|
| 200 |
btn.setCheckable(True) |
| 201 |
self.bgrp.addButton(btn, inx) |
| 202 |
self.hlay.addWidget(btn) |
| 203 |
|
| 204 |
ct = FileFuncGraphWidget(self, cmd_func, cmd[0], cmd_opt) |
| 205 |
self.sw.addWidget(ct) |
| 206 |
|
| 207 |
self.btn.append(btn) |
| 208 |
self.ctree.append(ct) |
| 209 |
self.hlay.addStretch(0) |
| 210 |
self.set_current(self.btn[0]) |
| 211 |
|
| 212 |
def set_current(self, btn): |
| 213 |
inx = self.bgrp.id(btn) |
| 214 |
self.btn[inx].setChecked(True) |
| 215 |
self.sw.setCurrentIndex(inx) |
| 216 |
ct = self.ctree[inx] |
| 217 |
ct.setFocus() |
| 218 |
|
| 219 |
ct.startQuery(self.req, self.dname, self.proj_dir, inx) |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
def create_page(req, dname, proj_dir, cmd_func, cmd_args, cmd_opt): |
| 224 |
w = FileFuncGraphWindow(req, dname, proj_dir, cmd_func, cmd_args, cmd_opt) |
| 225 |
w.resize(900, 600) |
| 226 |
w.show() |
| 227 |
return w |
| 228 |
|
| 229 |
if __name__ == '__main__': |
| 230 |
import optparse |
| 231 |
usage = "usage: %prog (-d <code_dir/file> | -p <idutils_proj>) [symbol]" |
| 232 |
op = optparse.OptionParser(usage=usage) |
| 233 |
op.add_option("-d", "--codedir", dest="code_dir", help="Code dir", metavar="CODE_DIR") |
| 234 |
op.add_option("-p", "--project", dest="id_path", help="Idutils project dir", metavar="PROJECT") |
| 235 |
(options, args) = op.parse_args() |
| 236 |
|
| 237 |
sym = '' |
| 238 |
dname = '' |
| 239 |
id_path = None |
| 240 |
|
| 241 |
if (not any([options.code_dir, options.id_path]) or |
| 242 |
all([options.code_dir, options.id_path])): |
| 243 |
print('Specify one among -d or -p', file=sys.stderr) |
| 244 |
sys.exit(-1) |
| 245 |
|
| 246 |
if len(args): |
| 247 |
if len(args) != 1: |
| 248 |
print('Please specify a symbol', file=sys.stderr) |
| 249 |
sys.exit(-4) |
| 250 |
sym = args[0] |
| 251 |
|
| 252 |
if options.code_dir: |
| 253 |
dname = options.code_dir |
| 254 |
if not os.path.exists(dname): |
| 255 |
print('"%s": does not exist' % dname, file=sys.stderr) |
| 256 |
sys.exit(-2) |
| 257 |
if options.id_path: |
| 258 |
if not sym: |
| 259 |
print('-p option needs a symbol', file=sys.stderr) |
| 260 |
sys.exit(-3) |
| 261 |
id_path = os.path.normpath(options.id_path) |
| 262 |
if not os.path.exists(os.path.join(id_path, 'ID')): |
| 263 |
print('idutils project path does not exist', file=sys.stderr) |
| 264 |
sys.exit(-4) |
| 265 |
|
| 266 |
app = QApplication(sys.argv) |
| 267 |
cmd_args = [ |
| 268 |
['FFGRAPH', 'F', 'File functions graph'], |
| 269 |
['FFGRAPH_E', 'F+E', 'File functions + external graph'], |
| 270 |
['FFGRAPH_D', 'D', 'Directory functions graph'], |
| 271 |
['FFGRAPH_DE', 'D+E', 'Directory functions + external graph'] |
| 272 |
] |
| 273 |
|
| 274 |
w = create_page(sym, dname, id_path, None, cmd_args, None) |
| 275 |
|
| 276 |
sys.exit(app.exec()) |