exported-sql-viewer.py 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  1. #!/usr/bin/python2
  2. # SPDX-License-Identifier: GPL-2.0
  3. # exported-sql-viewer.py: view data from sql database
  4. # Copyright (c) 2014-2018, Intel Corporation.
  5. # To use this script you will need to have exported data using either the
  6. # export-to-sqlite.py or the export-to-postgresql.py script. Refer to those
  7. # scripts for details.
  8. #
  9. # Following on from the example in the export scripts, a
  10. # call-graph can be displayed for the pt_example database like this:
  11. #
  12. # python tools/perf/scripts/python/exported-sql-viewer.py pt_example
  13. #
  14. # Note that for PostgreSQL, this script supports connecting to remote databases
  15. # by setting hostname, port, username, password, and dbname e.g.
  16. #
  17. # python tools/perf/scripts/python/exported-sql-viewer.py "hostname=myhost username=myuser password=mypassword dbname=pt_example"
  18. #
  19. # The result is a GUI window with a tree representing a context-sensitive
  20. # call-graph. Expanding a couple of levels of the tree and adjusting column
  21. # widths to suit will display something like:
  22. #
  23. # Call Graph: pt_example
  24. # Call Path Object Count Time(ns) Time(%) Branch Count Branch Count(%)
  25. # v- ls
  26. # v- 2638:2638
  27. # v- _start ld-2.19.so 1 10074071 100.0 211135 100.0
  28. # |- unknown unknown 1 13198 0.1 1 0.0
  29. # >- _dl_start ld-2.19.so 1 1400980 13.9 19637 9.3
  30. # >- _d_linit_internal ld-2.19.so 1 448152 4.4 11094 5.3
  31. # v-__libc_start_main@plt ls 1 8211741 81.5 180397 85.4
  32. # >- _dl_fixup ld-2.19.so 1 7607 0.1 108 0.1
  33. # >- __cxa_atexit libc-2.19.so 1 11737 0.1 10 0.0
  34. # >- __libc_csu_init ls 1 10354 0.1 10 0.0
  35. # |- _setjmp libc-2.19.so 1 0 0.0 4 0.0
  36. # v- main ls 1 8182043 99.6 180254 99.9
  37. #
  38. # Points to note:
  39. # The top level is a command name (comm)
  40. # The next level is a thread (pid:tid)
  41. # Subsequent levels are functions
  42. # 'Count' is the number of calls
  43. # 'Time' is the elapsed time until the function returns
  44. # Percentages are relative to the level above
  45. # 'Branch Count' is the total number of branches for that function and all
  46. # functions that it calls
  47. import sys
  48. import weakref
  49. import threading
  50. import string
  51. import cPickle
  52. import re
  53. import os
  54. from PySide.QtCore import *
  55. from PySide.QtGui import *
  56. from PySide.QtSql import *
  57. from decimal import *
  58. from ctypes import *
  59. from multiprocessing import Process, Array, Value, Event
  60. # Data formatting helpers
  61. def dsoname(name):
  62. if name == "[kernel.kallsyms]":
  63. return "[kernel]"
  64. return name
  65. # Percent to one decimal place
  66. def PercentToOneDP(n, d):
  67. if not d:
  68. return "0.0"
  69. x = (n * Decimal(100)) / d
  70. return str(x.quantize(Decimal(".1"), rounding=ROUND_HALF_UP))
  71. # Helper for queries that must not fail
  72. def QueryExec(query, stmt):
  73. ret = query.exec_(stmt)
  74. if not ret:
  75. raise Exception("Query failed: " + query.lastError().text())
  76. # Background thread
  77. class Thread(QThread):
  78. done = Signal(object)
  79. def __init__(self, task, param=None, parent=None):
  80. super(Thread, self).__init__(parent)
  81. self.task = task
  82. self.param = param
  83. def run(self):
  84. while True:
  85. if self.param is None:
  86. done, result = self.task()
  87. else:
  88. done, result = self.task(self.param)
  89. self.done.emit(result)
  90. if done:
  91. break
  92. # Tree data model
  93. class TreeModel(QAbstractItemModel):
  94. def __init__(self, root, parent=None):
  95. super(TreeModel, self).__init__(parent)
  96. self.root = root
  97. self.last_row_read = 0
  98. def Item(self, parent):
  99. if parent.isValid():
  100. return parent.internalPointer()
  101. else:
  102. return self.root
  103. def rowCount(self, parent):
  104. result = self.Item(parent).childCount()
  105. if result < 0:
  106. result = 0
  107. self.dataChanged.emit(parent, parent)
  108. return result
  109. def hasChildren(self, parent):
  110. return self.Item(parent).hasChildren()
  111. def headerData(self, section, orientation, role):
  112. if role == Qt.TextAlignmentRole:
  113. return self.columnAlignment(section)
  114. if role != Qt.DisplayRole:
  115. return None
  116. if orientation != Qt.Horizontal:
  117. return None
  118. return self.columnHeader(section)
  119. def parent(self, child):
  120. child_item = child.internalPointer()
  121. if child_item is self.root:
  122. return QModelIndex()
  123. parent_item = child_item.getParentItem()
  124. return self.createIndex(parent_item.getRow(), 0, parent_item)
  125. def index(self, row, column, parent):
  126. child_item = self.Item(parent).getChildItem(row)
  127. return self.createIndex(row, column, child_item)
  128. def DisplayData(self, item, index):
  129. return item.getData(index.column())
  130. def FetchIfNeeded(self, row):
  131. if row > self.last_row_read:
  132. self.last_row_read = row
  133. if row + 10 >= self.root.child_count:
  134. self.fetcher.Fetch(glb_chunk_sz)
  135. def columnAlignment(self, column):
  136. return Qt.AlignLeft
  137. def columnFont(self, column):
  138. return None
  139. def data(self, index, role):
  140. if role == Qt.TextAlignmentRole:
  141. return self.columnAlignment(index.column())
  142. if role == Qt.FontRole:
  143. return self.columnFont(index.column())
  144. if role != Qt.DisplayRole:
  145. return None
  146. item = index.internalPointer()
  147. return self.DisplayData(item, index)
  148. # Table data model
  149. class TableModel(QAbstractTableModel):
  150. def __init__(self, parent=None):
  151. super(TableModel, self).__init__(parent)
  152. self.child_count = 0
  153. self.child_items = []
  154. self.last_row_read = 0
  155. def Item(self, parent):
  156. if parent.isValid():
  157. return parent.internalPointer()
  158. else:
  159. return self
  160. def rowCount(self, parent):
  161. return self.child_count
  162. def headerData(self, section, orientation, role):
  163. if role == Qt.TextAlignmentRole:
  164. return self.columnAlignment(section)
  165. if role != Qt.DisplayRole:
  166. return None
  167. if orientation != Qt.Horizontal:
  168. return None
  169. return self.columnHeader(section)
  170. def index(self, row, column, parent):
  171. return self.createIndex(row, column, self.child_items[row])
  172. def DisplayData(self, item, index):
  173. return item.getData(index.column())
  174. def FetchIfNeeded(self, row):
  175. if row > self.last_row_read:
  176. self.last_row_read = row
  177. if row + 10 >= self.child_count:
  178. self.fetcher.Fetch(glb_chunk_sz)
  179. def columnAlignment(self, column):
  180. return Qt.AlignLeft
  181. def columnFont(self, column):
  182. return None
  183. def data(self, index, role):
  184. if role == Qt.TextAlignmentRole:
  185. return self.columnAlignment(index.column())
  186. if role == Qt.FontRole:
  187. return self.columnFont(index.column())
  188. if role != Qt.DisplayRole:
  189. return None
  190. item = index.internalPointer()
  191. return self.DisplayData(item, index)
  192. # Model cache
  193. model_cache = weakref.WeakValueDictionary()
  194. model_cache_lock = threading.Lock()
  195. def LookupCreateModel(model_name, create_fn):
  196. model_cache_lock.acquire()
  197. try:
  198. model = model_cache[model_name]
  199. except:
  200. model = None
  201. if model is None:
  202. model = create_fn()
  203. model_cache[model_name] = model
  204. model_cache_lock.release()
  205. return model
  206. # Find bar
  207. class FindBar():
  208. def __init__(self, parent, finder, is_reg_expr=False):
  209. self.finder = finder
  210. self.context = []
  211. self.last_value = None
  212. self.last_pattern = None
  213. label = QLabel("Find:")
  214. label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
  215. self.textbox = QComboBox()
  216. self.textbox.setEditable(True)
  217. self.textbox.currentIndexChanged.connect(self.ValueChanged)
  218. self.progress = QProgressBar()
  219. self.progress.setRange(0, 0)
  220. self.progress.hide()
  221. if is_reg_expr:
  222. self.pattern = QCheckBox("Regular Expression")
  223. else:
  224. self.pattern = QCheckBox("Pattern")
  225. self.pattern.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
  226. self.next_button = QToolButton()
  227. self.next_button.setIcon(parent.style().standardIcon(QStyle.SP_ArrowDown))
  228. self.next_button.released.connect(lambda: self.NextPrev(1))
  229. self.prev_button = QToolButton()
  230. self.prev_button.setIcon(parent.style().standardIcon(QStyle.SP_ArrowUp))
  231. self.prev_button.released.connect(lambda: self.NextPrev(-1))
  232. self.close_button = QToolButton()
  233. self.close_button.setIcon(parent.style().standardIcon(QStyle.SP_DockWidgetCloseButton))
  234. self.close_button.released.connect(self.Deactivate)
  235. self.hbox = QHBoxLayout()
  236. self.hbox.setContentsMargins(0, 0, 0, 0)
  237. self.hbox.addWidget(label)
  238. self.hbox.addWidget(self.textbox)
  239. self.hbox.addWidget(self.progress)
  240. self.hbox.addWidget(self.pattern)
  241. self.hbox.addWidget(self.next_button)
  242. self.hbox.addWidget(self.prev_button)
  243. self.hbox.addWidget(self.close_button)
  244. self.bar = QWidget()
  245. self.bar.setLayout(self.hbox);
  246. self.bar.hide()
  247. def Widget(self):
  248. return self.bar
  249. def Activate(self):
  250. self.bar.show()
  251. self.textbox.setFocus()
  252. def Deactivate(self):
  253. self.bar.hide()
  254. def Busy(self):
  255. self.textbox.setEnabled(False)
  256. self.pattern.hide()
  257. self.next_button.hide()
  258. self.prev_button.hide()
  259. self.progress.show()
  260. def Idle(self):
  261. self.textbox.setEnabled(True)
  262. self.progress.hide()
  263. self.pattern.show()
  264. self.next_button.show()
  265. self.prev_button.show()
  266. def Find(self, direction):
  267. value = self.textbox.currentText()
  268. pattern = self.pattern.isChecked()
  269. self.last_value = value
  270. self.last_pattern = pattern
  271. self.finder.Find(value, direction, pattern, self.context)
  272. def ValueChanged(self):
  273. value = self.textbox.currentText()
  274. pattern = self.pattern.isChecked()
  275. index = self.textbox.currentIndex()
  276. data = self.textbox.itemData(index)
  277. # Store the pattern in the combo box to keep it with the text value
  278. if data == None:
  279. self.textbox.setItemData(index, pattern)
  280. else:
  281. self.pattern.setChecked(data)
  282. self.Find(0)
  283. def NextPrev(self, direction):
  284. value = self.textbox.currentText()
  285. pattern = self.pattern.isChecked()
  286. if value != self.last_value:
  287. index = self.textbox.findText(value)
  288. # Allow for a button press before the value has been added to the combo box
  289. if index < 0:
  290. index = self.textbox.count()
  291. self.textbox.addItem(value, pattern)
  292. self.textbox.setCurrentIndex(index)
  293. return
  294. else:
  295. self.textbox.setItemData(index, pattern)
  296. elif pattern != self.last_pattern:
  297. # Keep the pattern recorded in the combo box up to date
  298. index = self.textbox.currentIndex()
  299. self.textbox.setItemData(index, pattern)
  300. self.Find(direction)
  301. def NotFound(self):
  302. QMessageBox.information(self.bar, "Find", "'" + self.textbox.currentText() + "' not found")
  303. # Context-sensitive call graph data model item base
  304. class CallGraphLevelItemBase(object):
  305. def __init__(self, glb, row, parent_item):
  306. self.glb = glb
  307. self.row = row
  308. self.parent_item = parent_item
  309. self.query_done = False;
  310. self.child_count = 0
  311. self.child_items = []
  312. def getChildItem(self, row):
  313. return self.child_items[row]
  314. def getParentItem(self):
  315. return self.parent_item
  316. def getRow(self):
  317. return self.row
  318. def childCount(self):
  319. if not self.query_done:
  320. self.Select()
  321. if not self.child_count:
  322. return -1
  323. return self.child_count
  324. def hasChildren(self):
  325. if not self.query_done:
  326. return True
  327. return self.child_count > 0
  328. def getData(self, column):
  329. return self.data[column]
  330. # Context-sensitive call graph data model level 2+ item base
  331. class CallGraphLevelTwoPlusItemBase(CallGraphLevelItemBase):
  332. def __init__(self, glb, row, comm_id, thread_id, call_path_id, time, branch_count, parent_item):
  333. super(CallGraphLevelTwoPlusItemBase, self).__init__(glb, row, parent_item)
  334. self.comm_id = comm_id
  335. self.thread_id = thread_id
  336. self.call_path_id = call_path_id
  337. self.branch_count = branch_count
  338. self.time = time
  339. def Select(self):
  340. self.query_done = True;
  341. query = QSqlQuery(self.glb.db)
  342. QueryExec(query, "SELECT call_path_id, name, short_name, COUNT(calls.id), SUM(return_time - call_time), SUM(branch_count)"
  343. " FROM calls"
  344. " INNER JOIN call_paths ON calls.call_path_id = call_paths.id"
  345. " INNER JOIN symbols ON call_paths.symbol_id = symbols.id"
  346. " INNER JOIN dsos ON symbols.dso_id = dsos.id"
  347. " WHERE parent_call_path_id = " + str(self.call_path_id) +
  348. " AND comm_id = " + str(self.comm_id) +
  349. " AND thread_id = " + str(self.thread_id) +
  350. " GROUP BY call_path_id, name, short_name"
  351. " ORDER BY call_path_id")
  352. while query.next():
  353. child_item = CallGraphLevelThreeItem(self.glb, self.child_count, self.comm_id, self.thread_id, query.value(0), query.value(1), query.value(2), query.value(3), int(query.value(4)), int(query.value(5)), self)
  354. self.child_items.append(child_item)
  355. self.child_count += 1
  356. # Context-sensitive call graph data model level three item
  357. class CallGraphLevelThreeItem(CallGraphLevelTwoPlusItemBase):
  358. def __init__(self, glb, row, comm_id, thread_id, call_path_id, name, dso, count, time, branch_count, parent_item):
  359. super(CallGraphLevelThreeItem, self).__init__(glb, row, comm_id, thread_id, call_path_id, time, branch_count, parent_item)
  360. dso = dsoname(dso)
  361. self.data = [ name, dso, str(count), str(time), PercentToOneDP(time, parent_item.time), str(branch_count), PercentToOneDP(branch_count, parent_item.branch_count) ]
  362. self.dbid = call_path_id
  363. # Context-sensitive call graph data model level two item
  364. class CallGraphLevelTwoItem(CallGraphLevelTwoPlusItemBase):
  365. def __init__(self, glb, row, comm_id, thread_id, pid, tid, parent_item):
  366. super(CallGraphLevelTwoItem, self).__init__(glb, row, comm_id, thread_id, 1, 0, 0, parent_item)
  367. self.data = [str(pid) + ":" + str(tid), "", "", "", "", "", ""]
  368. self.dbid = thread_id
  369. def Select(self):
  370. super(CallGraphLevelTwoItem, self).Select()
  371. for child_item in self.child_items:
  372. self.time += child_item.time
  373. self.branch_count += child_item.branch_count
  374. for child_item in self.child_items:
  375. child_item.data[4] = PercentToOneDP(child_item.time, self.time)
  376. child_item.data[6] = PercentToOneDP(child_item.branch_count, self.branch_count)
  377. # Context-sensitive call graph data model level one item
  378. class CallGraphLevelOneItem(CallGraphLevelItemBase):
  379. def __init__(self, glb, row, comm_id, comm, parent_item):
  380. super(CallGraphLevelOneItem, self).__init__(glb, row, parent_item)
  381. self.data = [comm, "", "", "", "", "", ""]
  382. self.dbid = comm_id
  383. def Select(self):
  384. self.query_done = True;
  385. query = QSqlQuery(self.glb.db)
  386. QueryExec(query, "SELECT thread_id, pid, tid"
  387. " FROM comm_threads"
  388. " INNER JOIN threads ON thread_id = threads.id"
  389. " WHERE comm_id = " + str(self.dbid))
  390. while query.next():
  391. child_item = CallGraphLevelTwoItem(self.glb, self.child_count, self.dbid, query.value(0), query.value(1), query.value(2), self)
  392. self.child_items.append(child_item)
  393. self.child_count += 1
  394. # Context-sensitive call graph data model root item
  395. class CallGraphRootItem(CallGraphLevelItemBase):
  396. def __init__(self, glb):
  397. super(CallGraphRootItem, self).__init__(glb, 0, None)
  398. self.dbid = 0
  399. self.query_done = True;
  400. query = QSqlQuery(glb.db)
  401. QueryExec(query, "SELECT id, comm FROM comms")
  402. while query.next():
  403. if not query.value(0):
  404. continue
  405. child_item = CallGraphLevelOneItem(glb, self.child_count, query.value(0), query.value(1), self)
  406. self.child_items.append(child_item)
  407. self.child_count += 1
  408. # Context-sensitive call graph data model
  409. class CallGraphModel(TreeModel):
  410. def __init__(self, glb, parent=None):
  411. super(CallGraphModel, self).__init__(CallGraphRootItem(glb), parent)
  412. self.glb = glb
  413. def columnCount(self, parent=None):
  414. return 7
  415. def columnHeader(self, column):
  416. headers = ["Call Path", "Object", "Count ", "Time (ns) ", "Time (%) ", "Branch Count ", "Branch Count (%) "]
  417. return headers[column]
  418. def columnAlignment(self, column):
  419. alignment = [ Qt.AlignLeft, Qt.AlignLeft, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight ]
  420. return alignment[column]
  421. def FindSelect(self, value, pattern, query):
  422. if pattern:
  423. # postgresql and sqlite pattern patching differences:
  424. # postgresql LIKE is case sensitive but sqlite LIKE is not
  425. # postgresql LIKE allows % and _ to be escaped with \ but sqlite LIKE does not
  426. # postgresql supports ILIKE which is case insensitive
  427. # sqlite supports GLOB (text only) which uses * and ? and is case sensitive
  428. if not self.glb.dbref.is_sqlite3:
  429. # Escape % and _
  430. s = value.replace("%", "\%")
  431. s = s.replace("_", "\_")
  432. # Translate * and ? into SQL LIKE pattern characters % and _
  433. trans = string.maketrans("*?", "%_")
  434. match = " LIKE '" + str(s).translate(trans) + "'"
  435. else:
  436. match = " GLOB '" + str(value) + "'"
  437. else:
  438. match = " = '" + str(value) + "'"
  439. QueryExec(query, "SELECT call_path_id, comm_id, thread_id"
  440. " FROM calls"
  441. " INNER JOIN call_paths ON calls.call_path_id = call_paths.id"
  442. " INNER JOIN symbols ON call_paths.symbol_id = symbols.id"
  443. " WHERE symbols.name" + match +
  444. " GROUP BY comm_id, thread_id, call_path_id"
  445. " ORDER BY comm_id, thread_id, call_path_id")
  446. def FindPath(self, query):
  447. # Turn the query result into a list of ids that the tree view can walk
  448. # to open the tree at the right place.
  449. ids = []
  450. parent_id = query.value(0)
  451. while parent_id:
  452. ids.insert(0, parent_id)
  453. q2 = QSqlQuery(self.glb.db)
  454. QueryExec(q2, "SELECT parent_id"
  455. " FROM call_paths"
  456. " WHERE id = " + str(parent_id))
  457. if not q2.next():
  458. break
  459. parent_id = q2.value(0)
  460. # The call path root is not used
  461. if ids[0] == 1:
  462. del ids[0]
  463. ids.insert(0, query.value(2))
  464. ids.insert(0, query.value(1))
  465. return ids
  466. def Found(self, query, found):
  467. if found:
  468. return self.FindPath(query)
  469. return []
  470. def FindValue(self, value, pattern, query, last_value, last_pattern):
  471. if last_value == value and pattern == last_pattern:
  472. found = query.first()
  473. else:
  474. self.FindSelect(value, pattern, query)
  475. found = query.next()
  476. return self.Found(query, found)
  477. def FindNext(self, query):
  478. found = query.next()
  479. if not found:
  480. found = query.first()
  481. return self.Found(query, found)
  482. def FindPrev(self, query):
  483. found = query.previous()
  484. if not found:
  485. found = query.last()
  486. return self.Found(query, found)
  487. def FindThread(self, c):
  488. if c.direction == 0 or c.value != c.last_value or c.pattern != c.last_pattern:
  489. ids = self.FindValue(c.value, c.pattern, c.query, c.last_value, c.last_pattern)
  490. elif c.direction > 0:
  491. ids = self.FindNext(c.query)
  492. else:
  493. ids = self.FindPrev(c.query)
  494. return (True, ids)
  495. def Find(self, value, direction, pattern, context, callback):
  496. class Context():
  497. def __init__(self, *x):
  498. self.value, self.direction, self.pattern, self.query, self.last_value, self.last_pattern = x
  499. def Update(self, *x):
  500. self.value, self.direction, self.pattern, self.last_value, self.last_pattern = x + (self.value, self.pattern)
  501. if len(context):
  502. context[0].Update(value, direction, pattern)
  503. else:
  504. context.append(Context(value, direction, pattern, QSqlQuery(self.glb.db), None, None))
  505. # Use a thread so the UI is not blocked during the SELECT
  506. thread = Thread(self.FindThread, context[0])
  507. thread.done.connect(lambda ids, t=thread, c=callback: self.FindDone(t, c, ids), Qt.QueuedConnection)
  508. thread.start()
  509. def FindDone(self, thread, callback, ids):
  510. callback(ids)
  511. # Vertical widget layout
  512. class VBox():
  513. def __init__(self, w1, w2, w3=None):
  514. self.vbox = QWidget()
  515. self.vbox.setLayout(QVBoxLayout());
  516. self.vbox.layout().setContentsMargins(0, 0, 0, 0)
  517. self.vbox.layout().addWidget(w1)
  518. self.vbox.layout().addWidget(w2)
  519. if w3:
  520. self.vbox.layout().addWidget(w3)
  521. def Widget(self):
  522. return self.vbox
  523. # Context-sensitive call graph window
  524. class CallGraphWindow(QMdiSubWindow):
  525. def __init__(self, glb, parent=None):
  526. super(CallGraphWindow, self).__init__(parent)
  527. self.model = LookupCreateModel("Context-Sensitive Call Graph", lambda x=glb: CallGraphModel(x))
  528. self.view = QTreeView()
  529. self.view.setModel(self.model)
  530. for c, w in ((0, 250), (1, 100), (2, 60), (3, 70), (4, 70), (5, 100)):
  531. self.view.setColumnWidth(c, w)
  532. self.find_bar = FindBar(self, self)
  533. self.vbox = VBox(self.view, self.find_bar.Widget())
  534. self.setWidget(self.vbox.Widget())
  535. AddSubWindow(glb.mainwindow.mdi_area, self, "Context-Sensitive Call Graph")
  536. def DisplayFound(self, ids):
  537. if not len(ids):
  538. return False
  539. parent = QModelIndex()
  540. for dbid in ids:
  541. found = False
  542. n = self.model.rowCount(parent)
  543. for row in xrange(n):
  544. child = self.model.index(row, 0, parent)
  545. if child.internalPointer().dbid == dbid:
  546. found = True
  547. self.view.setCurrentIndex(child)
  548. parent = child
  549. break
  550. if not found:
  551. break
  552. return found
  553. def Find(self, value, direction, pattern, context):
  554. self.view.setFocus()
  555. self.find_bar.Busy()
  556. self.model.Find(value, direction, pattern, context, self.FindDone)
  557. def FindDone(self, ids):
  558. found = True
  559. if not self.DisplayFound(ids):
  560. found = False
  561. self.find_bar.Idle()
  562. if not found:
  563. self.find_bar.NotFound()
  564. # Child data item finder
  565. class ChildDataItemFinder():
  566. def __init__(self, root):
  567. self.root = root
  568. self.value, self.direction, self.pattern, self.last_value, self.last_pattern = (None,) * 5
  569. self.rows = []
  570. self.pos = 0
  571. def FindSelect(self):
  572. self.rows = []
  573. if self.pattern:
  574. pattern = re.compile(self.value)
  575. for child in self.root.child_items:
  576. for column_data in child.data:
  577. if re.search(pattern, str(column_data)) is not None:
  578. self.rows.append(child.row)
  579. break
  580. else:
  581. for child in self.root.child_items:
  582. for column_data in child.data:
  583. if self.value in str(column_data):
  584. self.rows.append(child.row)
  585. break
  586. def FindValue(self):
  587. self.pos = 0
  588. if self.last_value != self.value or self.pattern != self.last_pattern:
  589. self.FindSelect()
  590. if not len(self.rows):
  591. return -1
  592. return self.rows[self.pos]
  593. def FindThread(self):
  594. if self.direction == 0 or self.value != self.last_value or self.pattern != self.last_pattern:
  595. row = self.FindValue()
  596. elif len(self.rows):
  597. if self.direction > 0:
  598. self.pos += 1
  599. if self.pos >= len(self.rows):
  600. self.pos = 0
  601. else:
  602. self.pos -= 1
  603. if self.pos < 0:
  604. self.pos = len(self.rows) - 1
  605. row = self.rows[self.pos]
  606. else:
  607. row = -1
  608. return (True, row)
  609. def Find(self, value, direction, pattern, context, callback):
  610. self.value, self.direction, self.pattern, self.last_value, self.last_pattern = (value, direction,pattern, self.value, self.pattern)
  611. # Use a thread so the UI is not blocked
  612. thread = Thread(self.FindThread)
  613. thread.done.connect(lambda row, t=thread, c=callback: self.FindDone(t, c, row), Qt.QueuedConnection)
  614. thread.start()
  615. def FindDone(self, thread, callback, row):
  616. callback(row)
  617. # Number of database records to fetch in one go
  618. glb_chunk_sz = 10000
  619. # size of pickled integer big enough for record size
  620. glb_nsz = 8
  621. # Background process for SQL data fetcher
  622. class SQLFetcherProcess():
  623. def __init__(self, dbref, sql, buffer, head, tail, fetch_count, fetching_done, process_target, wait_event, fetched_event, prep):
  624. # Need a unique connection name
  625. conn_name = "SQLFetcher" + str(os.getpid())
  626. self.db, dbname = dbref.Open(conn_name)
  627. self.sql = sql
  628. self.buffer = buffer
  629. self.head = head
  630. self.tail = tail
  631. self.fetch_count = fetch_count
  632. self.fetching_done = fetching_done
  633. self.process_target = process_target
  634. self.wait_event = wait_event
  635. self.fetched_event = fetched_event
  636. self.prep = prep
  637. self.query = QSqlQuery(self.db)
  638. self.query_limit = 0 if "$$last_id$$" in sql else 2
  639. self.last_id = -1
  640. self.fetched = 0
  641. self.more = True
  642. self.local_head = self.head.value
  643. self.local_tail = self.tail.value
  644. def Select(self):
  645. if self.query_limit:
  646. if self.query_limit == 1:
  647. return
  648. self.query_limit -= 1
  649. stmt = self.sql.replace("$$last_id$$", str(self.last_id))
  650. QueryExec(self.query, stmt)
  651. def Next(self):
  652. if not self.query.next():
  653. self.Select()
  654. if not self.query.next():
  655. return None
  656. self.last_id = self.query.value(0)
  657. return self.prep(self.query)
  658. def WaitForTarget(self):
  659. while True:
  660. self.wait_event.clear()
  661. target = self.process_target.value
  662. if target > self.fetched or target < 0:
  663. break
  664. self.wait_event.wait()
  665. return target
  666. def HasSpace(self, sz):
  667. if self.local_tail <= self.local_head:
  668. space = len(self.buffer) - self.local_head
  669. if space > sz:
  670. return True
  671. if space >= glb_nsz:
  672. # Use 0 (or space < glb_nsz) to mean there is no more at the top of the buffer
  673. nd = cPickle.dumps(0, cPickle.HIGHEST_PROTOCOL)
  674. self.buffer[self.local_head : self.local_head + len(nd)] = nd
  675. self.local_head = 0
  676. if self.local_tail - self.local_head > sz:
  677. return True
  678. return False
  679. def WaitForSpace(self, sz):
  680. if self.HasSpace(sz):
  681. return
  682. while True:
  683. self.wait_event.clear()
  684. self.local_tail = self.tail.value
  685. if self.HasSpace(sz):
  686. return
  687. self.wait_event.wait()
  688. def AddToBuffer(self, obj):
  689. d = cPickle.dumps(obj, cPickle.HIGHEST_PROTOCOL)
  690. n = len(d)
  691. nd = cPickle.dumps(n, cPickle.HIGHEST_PROTOCOL)
  692. sz = n + glb_nsz
  693. self.WaitForSpace(sz)
  694. pos = self.local_head
  695. self.buffer[pos : pos + len(nd)] = nd
  696. self.buffer[pos + glb_nsz : pos + sz] = d
  697. self.local_head += sz
  698. def FetchBatch(self, batch_size):
  699. fetched = 0
  700. while batch_size > fetched:
  701. obj = self.Next()
  702. if obj is None:
  703. self.more = False
  704. break
  705. self.AddToBuffer(obj)
  706. fetched += 1
  707. if fetched:
  708. self.fetched += fetched
  709. with self.fetch_count.get_lock():
  710. self.fetch_count.value += fetched
  711. self.head.value = self.local_head
  712. self.fetched_event.set()
  713. def Run(self):
  714. while self.more:
  715. target = self.WaitForTarget()
  716. if target < 0:
  717. break
  718. batch_size = min(glb_chunk_sz, target - self.fetched)
  719. self.FetchBatch(batch_size)
  720. self.fetching_done.value = True
  721. self.fetched_event.set()
  722. def SQLFetcherFn(*x):
  723. process = SQLFetcherProcess(*x)
  724. process.Run()
  725. # SQL data fetcher
  726. class SQLFetcher(QObject):
  727. done = Signal(object)
  728. def __init__(self, glb, sql, prep, process_data, parent=None):
  729. super(SQLFetcher, self).__init__(parent)
  730. self.process_data = process_data
  731. self.more = True
  732. self.target = 0
  733. self.last_target = 0
  734. self.fetched = 0
  735. self.buffer_size = 16 * 1024 * 1024
  736. self.buffer = Array(c_char, self.buffer_size, lock=False)
  737. self.head = Value(c_longlong)
  738. self.tail = Value(c_longlong)
  739. self.local_tail = 0
  740. self.fetch_count = Value(c_longlong)
  741. self.fetching_done = Value(c_bool)
  742. self.last_count = 0
  743. self.process_target = Value(c_longlong)
  744. self.wait_event = Event()
  745. self.fetched_event = Event()
  746. glb.AddInstanceToShutdownOnExit(self)
  747. self.process = Process(target=SQLFetcherFn, args=(glb.dbref, sql, self.buffer, self.head, self.tail, self.fetch_count, self.fetching_done, self.process_target, self.wait_event, self.fetched_event, prep))
  748. self.process.start()
  749. self.thread = Thread(self.Thread)
  750. self.thread.done.connect(self.ProcessData, Qt.QueuedConnection)
  751. self.thread.start()
  752. def Shutdown(self):
  753. # Tell the thread and process to exit
  754. self.process_target.value = -1
  755. self.wait_event.set()
  756. self.more = False
  757. self.fetching_done.value = True
  758. self.fetched_event.set()
  759. def Thread(self):
  760. if not self.more:
  761. return True, 0
  762. while True:
  763. self.fetched_event.clear()
  764. fetch_count = self.fetch_count.value
  765. if fetch_count != self.last_count:
  766. break
  767. if self.fetching_done.value:
  768. self.more = False
  769. return True, 0
  770. self.fetched_event.wait()
  771. count = fetch_count - self.last_count
  772. self.last_count = fetch_count
  773. self.fetched += count
  774. return False, count
  775. def Fetch(self, nr):
  776. if not self.more:
  777. # -1 inidcates there are no more
  778. return -1
  779. result = self.fetched
  780. extra = result + nr - self.target
  781. if extra > 0:
  782. self.target += extra
  783. # process_target < 0 indicates shutting down
  784. if self.process_target.value >= 0:
  785. self.process_target.value = self.target
  786. self.wait_event.set()
  787. return result
  788. def RemoveFromBuffer(self):
  789. pos = self.local_tail
  790. if len(self.buffer) - pos < glb_nsz:
  791. pos = 0
  792. n = cPickle.loads(self.buffer[pos : pos + glb_nsz])
  793. if n == 0:
  794. pos = 0
  795. n = cPickle.loads(self.buffer[0 : glb_nsz])
  796. pos += glb_nsz
  797. obj = cPickle.loads(self.buffer[pos : pos + n])
  798. self.local_tail = pos + n
  799. return obj
  800. def ProcessData(self, count):
  801. for i in xrange(count):
  802. obj = self.RemoveFromBuffer()
  803. self.process_data(obj)
  804. self.tail.value = self.local_tail
  805. self.wait_event.set()
  806. self.done.emit(count)
  807. # Fetch more records bar
  808. class FetchMoreRecordsBar():
  809. def __init__(self, model, parent):
  810. self.model = model
  811. self.label = QLabel("Number of records (x " + "{:,}".format(glb_chunk_sz) + ") to fetch:")
  812. self.label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
  813. self.fetch_count = QSpinBox()
  814. self.fetch_count.setRange(1, 1000000)
  815. self.fetch_count.setValue(10)
  816. self.fetch_count.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
  817. self.fetch = QPushButton("Go!")
  818. self.fetch.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
  819. self.fetch.released.connect(self.FetchMoreRecords)
  820. self.progress = QProgressBar()
  821. self.progress.setRange(0, 100)
  822. self.progress.hide()
  823. self.done_label = QLabel("All records fetched")
  824. self.done_label.hide()
  825. self.spacer = QLabel("")
  826. self.close_button = QToolButton()
  827. self.close_button.setIcon(parent.style().standardIcon(QStyle.SP_DockWidgetCloseButton))
  828. self.close_button.released.connect(self.Deactivate)
  829. self.hbox = QHBoxLayout()
  830. self.hbox.setContentsMargins(0, 0, 0, 0)
  831. self.hbox.addWidget(self.label)
  832. self.hbox.addWidget(self.fetch_count)
  833. self.hbox.addWidget(self.fetch)
  834. self.hbox.addWidget(self.spacer)
  835. self.hbox.addWidget(self.progress)
  836. self.hbox.addWidget(self.done_label)
  837. self.hbox.addWidget(self.close_button)
  838. self.bar = QWidget()
  839. self.bar.setLayout(self.hbox);
  840. self.bar.show()
  841. self.in_progress = False
  842. self.model.progress.connect(self.Progress)
  843. self.done = False
  844. if not model.HasMoreRecords():
  845. self.Done()
  846. def Widget(self):
  847. return self.bar
  848. def Activate(self):
  849. self.bar.show()
  850. self.fetch.setFocus()
  851. def Deactivate(self):
  852. self.bar.hide()
  853. def Enable(self, enable):
  854. self.fetch.setEnabled(enable)
  855. self.fetch_count.setEnabled(enable)
  856. def Busy(self):
  857. self.Enable(False)
  858. self.fetch.hide()
  859. self.spacer.hide()
  860. self.progress.show()
  861. def Idle(self):
  862. self.in_progress = False
  863. self.Enable(True)
  864. self.progress.hide()
  865. self.fetch.show()
  866. self.spacer.show()
  867. def Target(self):
  868. return self.fetch_count.value() * glb_chunk_sz
  869. def Done(self):
  870. self.done = True
  871. self.Idle()
  872. self.label.hide()
  873. self.fetch_count.hide()
  874. self.fetch.hide()
  875. self.spacer.hide()
  876. self.done_label.show()
  877. def Progress(self, count):
  878. if self.in_progress:
  879. if count:
  880. percent = ((count - self.start) * 100) / self.Target()
  881. if percent >= 100:
  882. self.Idle()
  883. else:
  884. self.progress.setValue(percent)
  885. if not count:
  886. # Count value of zero means no more records
  887. self.Done()
  888. def FetchMoreRecords(self):
  889. if self.done:
  890. return
  891. self.progress.setValue(0)
  892. self.Busy()
  893. self.in_progress = True
  894. self.start = self.model.FetchMoreRecords(self.Target())
  895. # SQL data preparation
  896. def SQLTableDataPrep(query, count):
  897. data = []
  898. for i in xrange(count):
  899. data.append(query.value(i))
  900. return data
  901. # SQL table data model item
  902. class SQLTableItem():
  903. def __init__(self, row, data):
  904. self.row = row
  905. self.data = data
  906. def getData(self, column):
  907. return self.data[column]
  908. # SQL table data model
  909. class SQLTableModel(TableModel):
  910. progress = Signal(object)
  911. def __init__(self, glb, sql, column_count, parent=None):
  912. super(SQLTableModel, self).__init__(parent)
  913. self.glb = glb
  914. self.more = True
  915. self.populated = 0
  916. self.fetcher = SQLFetcher(glb, sql, lambda x, y=column_count: SQLTableDataPrep(x, y), self.AddSample)
  917. self.fetcher.done.connect(self.Update)
  918. self.fetcher.Fetch(glb_chunk_sz)
  919. def DisplayData(self, item, index):
  920. self.FetchIfNeeded(item.row)
  921. return item.getData(index.column())
  922. def AddSample(self, data):
  923. child = SQLTableItem(self.populated, data)
  924. self.child_items.append(child)
  925. self.populated += 1
  926. def Update(self, fetched):
  927. if not fetched:
  928. self.more = False
  929. self.progress.emit(0)
  930. child_count = self.child_count
  931. count = self.populated - child_count
  932. if count > 0:
  933. parent = QModelIndex()
  934. self.beginInsertRows(parent, child_count, child_count + count - 1)
  935. self.insertRows(child_count, count, parent)
  936. self.child_count += count
  937. self.endInsertRows()
  938. self.progress.emit(self.child_count)
  939. def FetchMoreRecords(self, count):
  940. current = self.child_count
  941. if self.more:
  942. self.fetcher.Fetch(count)
  943. else:
  944. self.progress.emit(0)
  945. return current
  946. def HasMoreRecords(self):
  947. return self.more
  948. # SQL automatic table data model
  949. class SQLAutoTableModel(SQLTableModel):
  950. def __init__(self, glb, table_name, parent=None):
  951. sql = "SELECT * FROM " + table_name + " WHERE id > $$last_id$$ ORDER BY id LIMIT " + str(glb_chunk_sz)
  952. if table_name == "comm_threads_view":
  953. # For now, comm_threads_view has no id column
  954. sql = "SELECT * FROM " + table_name + " WHERE comm_id > $$last_id$$ ORDER BY comm_id LIMIT " + str(glb_chunk_sz)
  955. self.column_headers = []
  956. query = QSqlQuery(glb.db)
  957. if glb.dbref.is_sqlite3:
  958. QueryExec(query, "PRAGMA table_info(" + table_name + ")")
  959. while query.next():
  960. self.column_headers.append(query.value(1))
  961. if table_name == "sqlite_master":
  962. sql = "SELECT * FROM " + table_name
  963. else:
  964. if table_name[:19] == "information_schema.":
  965. sql = "SELECT * FROM " + table_name
  966. select_table_name = table_name[19:]
  967. schema = "information_schema"
  968. else:
  969. select_table_name = table_name
  970. schema = "public"
  971. QueryExec(query, "SELECT column_name FROM information_schema.columns WHERE table_schema = '" + schema + "' and table_name = '" + select_table_name + "'")
  972. while query.next():
  973. self.column_headers.append(query.value(0))
  974. super(SQLAutoTableModel, self).__init__(glb, sql, len(self.column_headers), parent)
  975. def columnCount(self, parent=None):
  976. return len(self.column_headers)
  977. def columnHeader(self, column):
  978. return self.column_headers[column]
  979. # Base class for custom ResizeColumnsToContents
  980. class ResizeColumnsToContentsBase(QObject):
  981. def __init__(self, parent=None):
  982. super(ResizeColumnsToContentsBase, self).__init__(parent)
  983. def ResizeColumnToContents(self, column, n):
  984. # Using the view's resizeColumnToContents() here is extrememly slow
  985. # so implement a crude alternative
  986. font = self.view.font()
  987. metrics = QFontMetrics(font)
  988. max = 0
  989. for row in xrange(n):
  990. val = self.data_model.child_items[row].data[column]
  991. len = metrics.width(str(val) + "MM")
  992. max = len if len > max else max
  993. val = self.data_model.columnHeader(column)
  994. len = metrics.width(str(val) + "MM")
  995. max = len if len > max else max
  996. self.view.setColumnWidth(column, max)
  997. def ResizeColumnsToContents(self):
  998. n = min(self.data_model.child_count, 100)
  999. if n < 1:
  1000. # No data yet, so connect a signal to notify when there is
  1001. self.data_model.rowsInserted.connect(self.UpdateColumnWidths)
  1002. return
  1003. columns = self.data_model.columnCount()
  1004. for i in xrange(columns):
  1005. self.ResizeColumnToContents(i, n)
  1006. def UpdateColumnWidths(self, *x):
  1007. # This only needs to be done once, so disconnect the signal now
  1008. self.data_model.rowsInserted.disconnect(self.UpdateColumnWidths)
  1009. self.ResizeColumnsToContents()
  1010. # Table window
  1011. class TableWindow(QMdiSubWindow, ResizeColumnsToContentsBase):
  1012. def __init__(self, glb, table_name, parent=None):
  1013. super(TableWindow, self).__init__(parent)
  1014. self.data_model = LookupCreateModel(table_name + " Table", lambda: SQLAutoTableModel(glb, table_name))
  1015. self.model = QSortFilterProxyModel()
  1016. self.model.setSourceModel(self.data_model)
  1017. self.view = QTableView()
  1018. self.view.setModel(self.model)
  1019. self.view.setEditTriggers(QAbstractItemView.NoEditTriggers)
  1020. self.view.verticalHeader().setVisible(False)
  1021. self.view.sortByColumn(-1, Qt.AscendingOrder)
  1022. self.view.setSortingEnabled(True)
  1023. self.ResizeColumnsToContents()
  1024. self.find_bar = FindBar(self, self, True)
  1025. self.finder = ChildDataItemFinder(self.data_model)
  1026. self.fetch_bar = FetchMoreRecordsBar(self.data_model, self)
  1027. self.vbox = VBox(self.view, self.find_bar.Widget(), self.fetch_bar.Widget())
  1028. self.setWidget(self.vbox.Widget())
  1029. AddSubWindow(glb.mainwindow.mdi_area, self, table_name + " Table")
  1030. def Find(self, value, direction, pattern, context):
  1031. self.view.setFocus()
  1032. self.find_bar.Busy()
  1033. self.finder.Find(value, direction, pattern, context, self.FindDone)
  1034. def FindDone(self, row):
  1035. self.find_bar.Idle()
  1036. if row >= 0:
  1037. self.view.setCurrentIndex(self.model.index(row, 0, QModelIndex()))
  1038. else:
  1039. self.find_bar.NotFound()
  1040. # Table list
  1041. def GetTableList(glb):
  1042. tables = []
  1043. query = QSqlQuery(glb.db)
  1044. if glb.dbref.is_sqlite3:
  1045. QueryExec(query, "SELECT name FROM sqlite_master WHERE type IN ( 'table' , 'view' ) ORDER BY name")
  1046. else:
  1047. QueryExec(query, "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type IN ( 'BASE TABLE' , 'VIEW' ) ORDER BY table_name")
  1048. while query.next():
  1049. tables.append(query.value(0))
  1050. if glb.dbref.is_sqlite3:
  1051. tables.append("sqlite_master")
  1052. else:
  1053. tables.append("information_schema.tables")
  1054. tables.append("information_schema.views")
  1055. tables.append("information_schema.columns")
  1056. return tables
  1057. # Action Definition
  1058. def CreateAction(label, tip, callback, parent=None, shortcut=None):
  1059. action = QAction(label, parent)
  1060. if shortcut != None:
  1061. action.setShortcuts(shortcut)
  1062. action.setStatusTip(tip)
  1063. action.triggered.connect(callback)
  1064. return action
  1065. # Typical application actions
  1066. def CreateExitAction(app, parent=None):
  1067. return CreateAction("&Quit", "Exit the application", app.closeAllWindows, parent, QKeySequence.Quit)
  1068. # Typical MDI actions
  1069. def CreateCloseActiveWindowAction(mdi_area):
  1070. return CreateAction("Cl&ose", "Close the active window", mdi_area.closeActiveSubWindow, mdi_area)
  1071. def CreateCloseAllWindowsAction(mdi_area):
  1072. return CreateAction("Close &All", "Close all the windows", mdi_area.closeAllSubWindows, mdi_area)
  1073. def CreateTileWindowsAction(mdi_area):
  1074. return CreateAction("&Tile", "Tile the windows", mdi_area.tileSubWindows, mdi_area)
  1075. def CreateCascadeWindowsAction(mdi_area):
  1076. return CreateAction("&Cascade", "Cascade the windows", mdi_area.cascadeSubWindows, mdi_area)
  1077. def CreateNextWindowAction(mdi_area):
  1078. return CreateAction("Ne&xt", "Move the focus to the next window", mdi_area.activateNextSubWindow, mdi_area, QKeySequence.NextChild)
  1079. def CreatePreviousWindowAction(mdi_area):
  1080. return CreateAction("Pre&vious", "Move the focus to the previous window", mdi_area.activatePreviousSubWindow, mdi_area, QKeySequence.PreviousChild)
  1081. # Typical MDI window menu
  1082. class WindowMenu():
  1083. def __init__(self, mdi_area, menu):
  1084. self.mdi_area = mdi_area
  1085. self.window_menu = menu.addMenu("&Windows")
  1086. self.close_active_window = CreateCloseActiveWindowAction(mdi_area)
  1087. self.close_all_windows = CreateCloseAllWindowsAction(mdi_area)
  1088. self.tile_windows = CreateTileWindowsAction(mdi_area)
  1089. self.cascade_windows = CreateCascadeWindowsAction(mdi_area)
  1090. self.next_window = CreateNextWindowAction(mdi_area)
  1091. self.previous_window = CreatePreviousWindowAction(mdi_area)
  1092. self.window_menu.aboutToShow.connect(self.Update)
  1093. def Update(self):
  1094. self.window_menu.clear()
  1095. sub_window_count = len(self.mdi_area.subWindowList())
  1096. have_sub_windows = sub_window_count != 0
  1097. self.close_active_window.setEnabled(have_sub_windows)
  1098. self.close_all_windows.setEnabled(have_sub_windows)
  1099. self.tile_windows.setEnabled(have_sub_windows)
  1100. self.cascade_windows.setEnabled(have_sub_windows)
  1101. self.next_window.setEnabled(have_sub_windows)
  1102. self.previous_window.setEnabled(have_sub_windows)
  1103. self.window_menu.addAction(self.close_active_window)
  1104. self.window_menu.addAction(self.close_all_windows)
  1105. self.window_menu.addSeparator()
  1106. self.window_menu.addAction(self.tile_windows)
  1107. self.window_menu.addAction(self.cascade_windows)
  1108. self.window_menu.addSeparator()
  1109. self.window_menu.addAction(self.next_window)
  1110. self.window_menu.addAction(self.previous_window)
  1111. if sub_window_count == 0:
  1112. return
  1113. self.window_menu.addSeparator()
  1114. nr = 1
  1115. for sub_window in self.mdi_area.subWindowList():
  1116. label = str(nr) + " " + sub_window.name
  1117. if nr < 10:
  1118. label = "&" + label
  1119. action = self.window_menu.addAction(label)
  1120. action.setCheckable(True)
  1121. action.setChecked(sub_window == self.mdi_area.activeSubWindow())
  1122. action.triggered.connect(lambda x=nr: self.setActiveSubWindow(x))
  1123. self.window_menu.addAction(action)
  1124. nr += 1
  1125. def setActiveSubWindow(self, nr):
  1126. self.mdi_area.setActiveSubWindow(self.mdi_area.subWindowList()[nr - 1])
  1127. # Font resize
  1128. def ResizeFont(widget, diff):
  1129. font = widget.font()
  1130. sz = font.pointSize()
  1131. font.setPointSize(sz + diff)
  1132. widget.setFont(font)
  1133. def ShrinkFont(widget):
  1134. ResizeFont(widget, -1)
  1135. def EnlargeFont(widget):
  1136. ResizeFont(widget, 1)
  1137. # Unique name for sub-windows
  1138. def NumberedWindowName(name, nr):
  1139. if nr > 1:
  1140. name += " <" + str(nr) + ">"
  1141. return name
  1142. def UniqueSubWindowName(mdi_area, name):
  1143. nr = 1
  1144. while True:
  1145. unique_name = NumberedWindowName(name, nr)
  1146. ok = True
  1147. for sub_window in mdi_area.subWindowList():
  1148. if sub_window.name == unique_name:
  1149. ok = False
  1150. break
  1151. if ok:
  1152. return unique_name
  1153. nr += 1
  1154. # Add a sub-window
  1155. def AddSubWindow(mdi_area, sub_window, name):
  1156. unique_name = UniqueSubWindowName(mdi_area, name)
  1157. sub_window.setMinimumSize(200, 100)
  1158. sub_window.resize(800, 600)
  1159. sub_window.setWindowTitle(unique_name)
  1160. sub_window.setAttribute(Qt.WA_DeleteOnClose)
  1161. sub_window.setWindowIcon(sub_window.style().standardIcon(QStyle.SP_FileIcon))
  1162. sub_window.name = unique_name
  1163. mdi_area.addSubWindow(sub_window)
  1164. sub_window.show()
  1165. # Main window
  1166. class MainWindow(QMainWindow):
  1167. def __init__(self, glb, parent=None):
  1168. super(MainWindow, self).__init__(parent)
  1169. self.glb = glb
  1170. self.setWindowTitle("Exported SQL Viewer: " + glb.dbname)
  1171. self.setWindowIcon(self.style().standardIcon(QStyle.SP_ComputerIcon))
  1172. self.setMinimumSize(200, 100)
  1173. self.mdi_area = QMdiArea()
  1174. self.mdi_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
  1175. self.mdi_area.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
  1176. self.setCentralWidget(self.mdi_area)
  1177. menu = self.menuBar()
  1178. file_menu = menu.addMenu("&File")
  1179. file_menu.addAction(CreateExitAction(glb.app, self))
  1180. edit_menu = menu.addMenu("&Edit")
  1181. edit_menu.addAction(CreateAction("&Find...", "Find items", self.Find, self, QKeySequence.Find))
  1182. edit_menu.addAction(CreateAction("Fetch &more records...", "Fetch more records", self.FetchMoreRecords, self, [QKeySequence(Qt.Key_F8)]))
  1183. edit_menu.addAction(CreateAction("&Shrink Font", "Make text smaller", self.ShrinkFont, self, [QKeySequence("Ctrl+-")]))
  1184. edit_menu.addAction(CreateAction("&Enlarge Font", "Make text bigger", self.EnlargeFont, self, [QKeySequence("Ctrl++")]))
  1185. reports_menu = menu.addMenu("&Reports")
  1186. reports_menu.addAction(CreateAction("Context-Sensitive Call &Graph", "Create a new window containing a context-sensitive call graph", self.NewCallGraph, self))
  1187. self.TableMenu(GetTableList(glb), menu)
  1188. self.window_menu = WindowMenu(self.mdi_area, menu)
  1189. def Find(self):
  1190. win = self.mdi_area.activeSubWindow()
  1191. if win:
  1192. try:
  1193. win.find_bar.Activate()
  1194. except:
  1195. pass
  1196. def FetchMoreRecords(self):
  1197. win = self.mdi_area.activeSubWindow()
  1198. if win:
  1199. try:
  1200. win.fetch_bar.Activate()
  1201. except:
  1202. pass
  1203. def ShrinkFont(self):
  1204. win = self.mdi_area.activeSubWindow()
  1205. ShrinkFont(win.view)
  1206. def EnlargeFont(self):
  1207. win = self.mdi_area.activeSubWindow()
  1208. EnlargeFont(win.view)
  1209. def TableMenu(self, tables, menu):
  1210. table_menu = menu.addMenu("&Tables")
  1211. for table in tables:
  1212. table_menu.addAction(CreateAction(table, "Create a new window containing a table view", lambda t=table: self.NewTableView(t), self))
  1213. def NewCallGraph(self):
  1214. CallGraphWindow(self.glb, self)
  1215. def NewTableView(self, table_name):
  1216. TableWindow(self.glb, table_name, self)
  1217. # Global data
  1218. class Glb():
  1219. def __init__(self, dbref, db, dbname):
  1220. self.dbref = dbref
  1221. self.db = db
  1222. self.dbname = dbname
  1223. self.app = None
  1224. self.mainwindow = None
  1225. self.instances_to_shutdown_on_exit = weakref.WeakSet()
  1226. def AddInstanceToShutdownOnExit(self, instance):
  1227. self.instances_to_shutdown_on_exit.add(instance)
  1228. # Shutdown any background processes or threads
  1229. def ShutdownInstances(self):
  1230. for x in self.instances_to_shutdown_on_exit:
  1231. try:
  1232. x.Shutdown()
  1233. except:
  1234. pass
  1235. # Database reference
  1236. class DBRef():
  1237. def __init__(self, is_sqlite3, dbname):
  1238. self.is_sqlite3 = is_sqlite3
  1239. self.dbname = dbname
  1240. def Open(self, connection_name):
  1241. dbname = self.dbname
  1242. if self.is_sqlite3:
  1243. db = QSqlDatabase.addDatabase("QSQLITE", connection_name)
  1244. else:
  1245. db = QSqlDatabase.addDatabase("QPSQL", connection_name)
  1246. opts = dbname.split()
  1247. for opt in opts:
  1248. if "=" in opt:
  1249. opt = opt.split("=")
  1250. if opt[0] == "hostname":
  1251. db.setHostName(opt[1])
  1252. elif opt[0] == "port":
  1253. db.setPort(int(opt[1]))
  1254. elif opt[0] == "username":
  1255. db.setUserName(opt[1])
  1256. elif opt[0] == "password":
  1257. db.setPassword(opt[1])
  1258. elif opt[0] == "dbname":
  1259. dbname = opt[1]
  1260. else:
  1261. dbname = opt
  1262. db.setDatabaseName(dbname)
  1263. if not db.open():
  1264. raise Exception("Failed to open database " + dbname + " error: " + db.lastError().text())
  1265. return db, dbname
  1266. # Main
  1267. def Main():
  1268. if (len(sys.argv) < 2):
  1269. print >> sys.stderr, "Usage is: exported-sql-viewer.py <database name>"
  1270. raise Exception("Too few arguments")
  1271. dbname = sys.argv[1]
  1272. is_sqlite3 = False
  1273. try:
  1274. f = open(dbname)
  1275. if f.read(15) == "SQLite format 3":
  1276. is_sqlite3 = True
  1277. f.close()
  1278. except:
  1279. pass
  1280. dbref = DBRef(is_sqlite3, dbname)
  1281. db, dbname = dbref.Open("main")
  1282. glb = Glb(dbref, db, dbname)
  1283. app = QApplication(sys.argv)
  1284. glb.app = app
  1285. mainwindow = MainWindow(glb)
  1286. glb.mainwindow = mainwindow
  1287. mainwindow.show()
  1288. err = app.exec_()
  1289. glb.ShutdownInstances()
  1290. db.close()
  1291. sys.exit(err)
  1292. if __name__ == "__main__":
  1293. Main()