api.py 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://metrixplusplus.sourceforge.net
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Metrix++ is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 3 of the License.
  10. #
  11. # Metrix++ is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Metrix++. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import os.path
  20. import mpp.internal.dbwrap
  21. import mpp.internal.api_impl
  22. ##############################################################################
  23. #
  24. #
  25. #
  26. ##############################################################################
  27. class Data(object):
  28. def __init__(self):
  29. self.data = {}
  30. def get_data(self, namespace, field):
  31. if namespace not in self.data.keys():
  32. return None
  33. if field not in self.data[namespace].keys():
  34. return None
  35. return self.data[namespace][field]
  36. def set_data(self, namespace, field, value):
  37. if namespace not in self.data:
  38. self.data[namespace] = {}
  39. self.data[namespace][field] = value
  40. def iterate_namespaces(self):
  41. for namespace in self.data.keys():
  42. yield namespace
  43. def iterate_fields(self, namespace):
  44. for field in self.data[namespace].keys():
  45. yield (field, self.data[namespace][field])
  46. def get_data_tree(self, namespaces=None):
  47. return self.data
  48. def __repr__(self):
  49. return object.__repr__(self) + " with data " + self.data.__repr__()
  50. class LoadableData(Data):
  51. def __init__(self, loader, file_id, region_id):
  52. Data.__init__(self)
  53. self.loader = loader
  54. self.file_id = file_id
  55. self.region_id = region_id
  56. self.loaded_namespaces = []
  57. self.changed_namespaces = []
  58. def load_namespace(self, namespace):
  59. namespace_obj = self.loader.get_namespace(namespace)
  60. if namespace_obj == None:
  61. return
  62. regions_supported = namespace_obj.are_regions_supported()
  63. if ((self.region_id == None and regions_supported == True) or
  64. (self.region_id != None and regions_supported == False)):
  65. return
  66. row = self.loader.db.get_row(namespace, self.file_id, self.region_id)
  67. if row == None:
  68. return
  69. for column_name in row.keys():
  70. try:
  71. packager = namespace_obj._get_field_packager(column_name)
  72. except mpp.internal.api_impl.PackagerError:
  73. continue
  74. if row[column_name] == None:
  75. continue
  76. Data.set_data(self, namespace, column_name, packager.unpack(row[column_name]))
  77. def set_data(self, namespace, field, value):
  78. if namespace not in self.changed_namespaces:
  79. self.changed_namespaces.append(namespace)
  80. return Data.set_data(self, namespace, field, value)
  81. def get_data(self, namespace, field):
  82. if namespace not in self.loaded_namespaces:
  83. self.loaded_namespaces.append(namespace)
  84. self.load_namespace(namespace)
  85. return Data.get_data(self, namespace, field)
  86. def is_namespace_updated(self, namespace):
  87. return namespace in self.changed_namespaces
  88. def is_namespace_loaded(self, namespace):
  89. return namespace in self.loaded_namespaces
  90. def get_data_tree(self, namespaces=None):
  91. if namespaces == None:
  92. namespaces = self.loader.iterate_namespace_names()
  93. for each in namespaces:
  94. self.load_namespace(each)
  95. return Data.get_data_tree(self)
  96. class Region(LoadableData):
  97. class T(object):
  98. NONE = 0x00
  99. GLOBAL = 0x01
  100. CLASS = 0x02
  101. STRUCT = 0x04
  102. NAMESPACE = 0x08
  103. FUNCTION = 0x10
  104. INTERFACE = 0x20
  105. ANY = 0xFF
  106. def to_str(self, group):
  107. if group == self.NONE:
  108. return "none"
  109. elif group == self.GLOBAL:
  110. return "global"
  111. elif group == self.CLASS:
  112. return "class"
  113. elif group == self.STRUCT:
  114. return "struct"
  115. elif group == self.NAMESPACE:
  116. return "namespace"
  117. elif group == self.FUNCTION:
  118. return "function"
  119. elif group == self.INTERFACE:
  120. return "interface"
  121. else:
  122. assert(False)
  123. def __init__(self, loader, file_id, region_id, region_name, offset_begin, offset_end, line_begin, line_end, cursor_line, group, checksum):
  124. LoadableData.__init__(self, loader, file_id, region_id)
  125. self.name = region_name
  126. self.begin = offset_begin
  127. self.end = offset_end
  128. self.line_begin = line_begin
  129. self.line_end = line_end
  130. self.cursor = cursor_line
  131. self.group = group
  132. self.checksum = checksum
  133. self.children = []
  134. def get_id(self):
  135. return self.region_id
  136. def get_name(self):
  137. return self.name
  138. def get_offset_begin(self):
  139. return self.begin
  140. def get_offset_end(self):
  141. return self.end
  142. def get_line_begin(self):
  143. return self.line_begin
  144. def get_line_end(self):
  145. return self.line_end
  146. def get_cursor(self):
  147. return self.cursor
  148. def get_type(self):
  149. return self.group
  150. def get_checksum(self):
  151. return self.checksum
  152. def iterate_subregion_ids(self):
  153. return self.children
  154. def _register_subregion_id(self, child_id):
  155. self.children.append(child_id)
  156. class Marker(object):
  157. class T(object):
  158. NONE = 0x00
  159. COMMENT = 0x01
  160. STRING = 0x02
  161. PREPROCESSOR = 0x04
  162. CODE = 0x08
  163. ANY = 0xFF
  164. def to_str(self, group):
  165. if group == self.NONE:
  166. return "none"
  167. elif group == self.COMMENT:
  168. return "comment"
  169. elif group == self.STRING:
  170. return "string"
  171. elif group == self.PREPROCESSOR:
  172. return "preprocessor"
  173. elif group == self.CODE:
  174. return "code"
  175. else:
  176. assert(False)
  177. def __init__(self, offset_begin, offset_end, group):
  178. self.begin = offset_begin
  179. self.end = offset_end
  180. self.group = group
  181. def get_offset_begin(self):
  182. return self.begin
  183. def get_offset_end(self):
  184. return self.end
  185. def get_type(self):
  186. return self.group
  187. class FileData(LoadableData):
  188. def __init__(self, loader, path, file_id, checksum, content):
  189. LoadableData.__init__(self, loader, file_id, None)
  190. self.path = path
  191. self.checksum = checksum
  192. self.content = content
  193. self.regions = None
  194. self.markers = None
  195. self.loader = loader
  196. self.loading_tmp = []
  197. def get_id(self):
  198. return self.file_id
  199. def get_path(self):
  200. return self.path
  201. def get_checksum(self):
  202. return self.checksum
  203. def get_content(self):
  204. return self.content
  205. def _internal_append_region(self, region):
  206. # here we apply some magic - we rely on special ordering of coming regions,
  207. # which is supported by code parsers
  208. prev_id = None
  209. while True:
  210. if len(self.loading_tmp) == 0:
  211. break
  212. prev_id = self.loading_tmp.pop()
  213. if self.get_region(prev_id).get_offset_end() > region.get_offset_begin():
  214. self.loading_tmp.append(prev_id) # return back
  215. break
  216. self.loading_tmp.append(region.get_id())
  217. if prev_id != None:
  218. self.get_region(prev_id)._register_subregion_id(region.get_id())
  219. self.regions.append(region)
  220. def load_regions(self):
  221. if self.regions == None:
  222. self.regions = []
  223. for each in self.loader.db.iterate_regions(self.get_id()):
  224. self._internal_append_region(Region(self.loader,
  225. self.get_id(),
  226. each.region_id,
  227. each.name,
  228. each.begin,
  229. each.end,
  230. each.line_begin,
  231. each.line_end,
  232. each.cursor,
  233. each.group,
  234. each.checksum))
  235. assert(len(self.regions) == each.region_id)
  236. def add_region(self, region_name, offset_begin, offset_end, line_begin, line_end, cursor_line, group, checksum):
  237. if self.regions == None:
  238. self.regions = [] # do not load in time of collection
  239. new_id = len(self.regions) + 1
  240. self._internal_append_region(Region(self.loader, self.get_id(), new_id, region_name, offset_begin, offset_end, line_begin, line_end, cursor_line, group, checksum))
  241. self.loader.db.create_region(self.file_id, new_id, region_name, offset_begin, offset_end, line_begin, line_end, cursor_line, group, checksum)
  242. return new_id
  243. def get_region(self, region_id):
  244. self.load_regions()
  245. return self.regions[region_id - 1]
  246. def iterate_regions(self, filter_group = Region.T.ANY):
  247. self.load_regions()
  248. for each in self.regions:
  249. if each.group & filter_group:
  250. yield each
  251. def are_regions_loaded(self):
  252. return self.regions != None
  253. def load_markers(self):
  254. if self.markers == None:
  255. self.markers = []
  256. for each in self.loader.db.iterate_markers(self.get_id()):
  257. self.markers.append(Marker(each.begin, each.end, each.group))
  258. def add_marker(self, offset_begin, offset_end, group):
  259. if self.markers == None:
  260. self.markers = [] # do not load in time of collection
  261. self.markers.append(Marker(offset_begin, offset_end, group))
  262. self.loader.db.create_marker(self.file_id, offset_begin, offset_end, group)
  263. def iterate_markers(self, filter_group = Marker.T.ANY,
  264. region_id = None, exclude_children = True, merge = False):
  265. self.load_markers()
  266. # merged markers
  267. if merge == True:
  268. next_marker = None
  269. for marker in self.iterate_markers(filter_group, region_id, exclude_children, merge = False):
  270. if next_marker != None:
  271. if next_marker.get_offset_end() == marker.get_offset_begin():
  272. # sequential markers
  273. next_marker = Marker(next_marker.get_offset_begin(),
  274. marker.get_offset_end(),
  275. next_marker.get_type() | marker.get_type())
  276. else:
  277. yield next_marker
  278. next_marker = None
  279. if next_marker == None:
  280. next_marker = Marker(marker.get_offset_begin(),
  281. marker.get_offset_end(),
  282. marker.get_type())
  283. if next_marker != None:
  284. yield next_marker
  285. # all markers per file
  286. elif region_id == None:
  287. next_code_marker_start = 0
  288. for marker in self.markers:
  289. if Marker.T.CODE & filter_group and next_code_marker_start < marker.get_offset_begin():
  290. yield Marker(next_code_marker_start, marker.get_offset_begin(), Marker.T.CODE)
  291. if marker.group & filter_group:
  292. yield marker
  293. next_code_marker_start = marker.get_offset_end()
  294. if Marker.T.CODE & filter_group and next_code_marker_start < len(self.get_content()):
  295. yield Marker(next_code_marker_start, len(self.get_content()), Marker.T.CODE)
  296. # markers per region
  297. else:
  298. region = self.get_region(region_id)
  299. if region != None:
  300. # code parsers and database know about non-code markers
  301. # clients want to iterate code as markers as well
  302. # so, we embed code markers in run-time
  303. class CodeMarker(Marker):
  304. pass
  305. # cache markers for all regions if it does not exist
  306. if hasattr(region, '_markers_list') == False:
  307. # subroutine to populate _markers_list attribute
  308. # _markers_list does include code markers
  309. def cache_markers_list_rec(data, region_id, marker_start_ind, next_code_marker_start):
  310. region = data.get_region(region_id)
  311. region._markers_list = []
  312. region._first_marker_ind = marker_start_ind
  313. #next_code_marker_start = region.get_offset_begin()
  314. for sub_id in region.iterate_subregion_ids():
  315. subregion = data.get_region(sub_id)
  316. # cache all markers before the subregion
  317. while len(data.markers) > marker_start_ind and \
  318. subregion.get_offset_begin() > data.markers[marker_start_ind].get_offset_begin():
  319. if next_code_marker_start < data.markers[marker_start_ind].get_offset_begin():
  320. # append code markers coming before non-code marker
  321. region._markers_list.append(CodeMarker(next_code_marker_start,
  322. data.markers[marker_start_ind].get_offset_begin(),
  323. Marker.T.CODE))
  324. next_code_marker_start = data.markers[marker_start_ind].get_offset_end()
  325. region._markers_list.append(marker_start_ind)
  326. marker_start_ind += 1
  327. # cache all code markers before the subregion but after the last marker
  328. if next_code_marker_start < subregion.get_offset_begin():
  329. region._markers_list.append(CodeMarker(next_code_marker_start,
  330. subregion.get_offset_begin(),
  331. Marker.T.CODE))
  332. next_code_marker_start = subregion.get_offset_begin()
  333. # here is the recursive call for all sub-regions
  334. (marker_start_ind, next_code_marker_start) = cache_markers_list_rec(data,
  335. sub_id,
  336. marker_start_ind,
  337. next_code_marker_start)
  338. # cache all markers after the last subregion
  339. while len(data.markers) > marker_start_ind and \
  340. region.get_offset_end() > data.markers[marker_start_ind].get_offset_begin():
  341. # append code markers coming before non-code marker
  342. if next_code_marker_start < data.markers[marker_start_ind].get_offset_begin():
  343. region._markers_list.append(CodeMarker(next_code_marker_start,
  344. data.markers[marker_start_ind].get_offset_begin(),
  345. Marker.T.CODE))
  346. next_code_marker_start = data.markers[marker_start_ind].get_offset_end()
  347. region._markers_list.append(marker_start_ind)
  348. marker_start_ind += 1
  349. # cache the last code segment after the last marker
  350. if next_code_marker_start < region.get_offset_end():
  351. region._markers_list.append(CodeMarker(next_code_marker_start,
  352. region.get_offset_end(),
  353. Marker.T.CODE))
  354. next_code_marker_start = region.get_offset_end()
  355. # return the starting point for the next call of this function
  356. return (marker_start_ind, next_code_marker_start)
  357. # append markers list to all regions recursively
  358. (next_marker_pos, next_code_marker_start) = cache_markers_list_rec(self, 1, 0, 0)
  359. assert(next_marker_pos == len(self.markers))
  360. # excluding subregions
  361. if exclude_children == True:
  362. for marker_ind in region._markers_list:
  363. if isinstance(marker_ind, int):
  364. marker = self.markers[marker_ind]
  365. else:
  366. marker = marker_ind # CodeMarker
  367. if marker.group & filter_group:
  368. yield marker
  369. # including subregions
  370. else:
  371. next_code_marker_start = region.get_offset_begin()
  372. for marker in self.markers[region._first_marker_ind:]:
  373. if marker.get_offset_begin() >= region.get_offset_end():
  374. break
  375. if region.get_offset_begin() > marker.get_offset_begin():
  376. continue
  377. if Marker.T.CODE & filter_group and next_code_marker_start < marker.get_offset_begin():
  378. yield Marker(next_code_marker_start, marker.get_offset_begin(), Marker.T.CODE)
  379. if marker.group & filter_group:
  380. yield marker
  381. next_code_marker_start = marker.get_offset_end()
  382. if Marker.T.CODE & filter_group and next_code_marker_start < region.get_offset_end():
  383. yield Marker(next_code_marker_start, region.get_offset_end(), Marker.T.CODE)
  384. def are_markers_loaded(self):
  385. return self.markers != None
  386. def __repr__(self):
  387. return Data.__repr__(self) + " and regions " + self.regions.__repr__()
  388. class AggregatedData(Data):
  389. def __init__(self, loader, path):
  390. Data.__init__(self)
  391. self.path = path
  392. self.loader = loader
  393. self.subdirs = None
  394. self.subfiles = None
  395. def get_subdirs(self):
  396. if self.subdirs != None:
  397. return self.subdirs
  398. self.subdirs = []
  399. if self.path != None:
  400. for subdir in self.loader.db.iterate_dircontent(self.path, include_subdirs = True, include_subfiles = False):
  401. self.subdirs.append(subdir)
  402. return self.subdirs
  403. def get_subfiles(self):
  404. if self.subfiles != None:
  405. return self.subfiles
  406. self.subfiles = []
  407. if self.path != None:
  408. for subfile in self.loader.db.iterate_dircontent(self.path, include_subdirs = False, include_subfiles = True):
  409. self.subfiles.append(subfile)
  410. return self.subfiles
  411. class SelectData(Data):
  412. def __init__(self, loader, path, file_id, region_id):
  413. Data.__init__(self)
  414. self.loader = loader
  415. self.path = path
  416. self.file_id = file_id
  417. self.region_id = region_id
  418. self.region = None
  419. def get_path(self):
  420. return self.path
  421. def get_region(self):
  422. if self.region == None and self.region_id != None:
  423. row = self.loader.db.get_region(self.file_id, self.region_id)
  424. if row != None:
  425. self.region = Region(self.loader,
  426. self.file_id,
  427. self.region_id,
  428. row.name,
  429. row.begin,
  430. row.end,
  431. row.line_begin,
  432. row.line_end,
  433. row.cursor,
  434. row.group,
  435. row.checksum)
  436. return self.region
  437. class DiffData(Data):
  438. def __init__(self, new_data, old_data):
  439. Data.__init__(self)
  440. self.new_data = new_data
  441. self.old_data = old_data
  442. def get_data(self, namespace, field):
  443. new_data = self.new_data.get_data(namespace, field)
  444. old_data = self.old_data.get_data(namespace, field)
  445. if new_data == None:
  446. return None
  447. if old_data == None:
  448. # non_zero fields has got zero value by default if missed
  449. # the data can be also unavailable,
  450. # because previous collection does not include that
  451. # but external tools (like limit.py) should warn about this,
  452. # using list of registered database properties
  453. old_data = 0
  454. return new_data - old_data
  455. ####################################
  456. # Loader
  457. ####################################
  458. class Namespace(object):
  459. class NamespaceError(Exception):
  460. def __init__(self, namespace, reason):
  461. Exception.__init__(self, "Namespace '"
  462. + namespace
  463. + "': '"
  464. + reason
  465. + "'")
  466. class FieldError(Exception):
  467. def __init__(self, field, reason):
  468. Exception.__init__(self, "Field '"
  469. + field
  470. + "': '"
  471. + reason
  472. + "'")
  473. def __init__(self, db_handle, name, support_regions = False, version='1.0'):
  474. if not isinstance(name, str):
  475. raise Namespace.NamespaceError(name, "name not a string")
  476. self.name = name
  477. self.support_regions = support_regions
  478. self.fields = {}
  479. self.db = db_handle
  480. if self.db.check_table(name) == False:
  481. self.db.create_table(name, support_regions, version)
  482. else:
  483. for column in self.db.iterate_columns(name):
  484. self.add_field(column.name,
  485. mpp.internal.api_impl.PackagerFactory().get_python_type(column.sql_type),
  486. non_zero=column.non_zero)
  487. def get_name(self):
  488. return self.name
  489. def are_regions_supported(self):
  490. return self.support_regions
  491. def add_field(self, field_name, python_type, non_zero=False):
  492. if not isinstance(field_name, str):
  493. raise Namespace.FieldError(field_name, "field_name not a string")
  494. packager = mpp.internal.api_impl.PackagerFactory().create(python_type, non_zero)
  495. if field_name in self.fields.keys():
  496. raise Namespace.FieldError(field_name, "double used")
  497. self.fields[field_name] = packager
  498. if self.db.check_column(self.get_name(), field_name) == False:
  499. # - False if cloned
  500. # - True if created
  501. return self.db.create_column(self.name, field_name, packager.get_sql_type(), non_zero=non_zero)
  502. return None # if double request
  503. def iterate_field_names(self):
  504. for name in self.fields.keys():
  505. yield name
  506. def check_field(self, field_name):
  507. try:
  508. self._get_field_packager(field_name)
  509. except mpp.internal.api_impl.PackagerError:
  510. return False
  511. return True
  512. def get_field_sql_type(self, field_name):
  513. try:
  514. return self._get_field_packager(field_name).get_sql_type()
  515. except mpp.internal.api_impl.PackagerError:
  516. raise Namespace.FieldError(field_name, 'does not exist')
  517. def get_field_python_type(self, field_name):
  518. try:
  519. return self._get_field_packager(field_name).get_python_type()
  520. except mpp.internal.api_impl.PackagerError:
  521. raise Namespace.FieldError(field_name, 'does not exist')
  522. def is_field_non_zero(self, field_name):
  523. try:
  524. return self._get_field_packager(field_name).is_non_zero()
  525. except mpp.internal.api_impl.PackagerError:
  526. raise Namespace.FieldError(field_name, 'does not exist')
  527. def _get_field_packager(self, field_name):
  528. if field_name in self.fields.keys():
  529. return self.fields[field_name]
  530. else:
  531. raise mpp.internal.api_impl.PackagerError("unknown field " + field_name + " requested")
  532. class Loader(object):
  533. def __init__(self):
  534. self.namespaces = {}
  535. self.db = None
  536. self.last_file_data = None # for performance boost reasons
  537. def create_database(self, dbfile, previous_db = None):
  538. self.db = mpp.internal.dbwrap.Database()
  539. try:
  540. self.db.create(dbfile, clone_from=previous_db)
  541. except:
  542. return False
  543. return True
  544. def open_database(self, dbfile, read_only = True):
  545. self.db = mpp.internal.dbwrap.Database()
  546. if os.path.exists(dbfile) == False:
  547. return False
  548. try:
  549. self.db.connect(dbfile, read_only=read_only)
  550. except:
  551. return False
  552. for table in self.db.iterate_tables():
  553. self.create_namespace(table.name, table.support_regions)
  554. return True
  555. def set_property(self, property_name, value):
  556. if self.db == None:
  557. return None
  558. return self.db.set_property(property_name, str(value))
  559. def get_property(self, property_name):
  560. if self.db == None:
  561. return None
  562. return self.db.get_property(property_name)
  563. def iterate_properties(self):
  564. if self.db == None:
  565. return None
  566. return self.db.iterate_properties()
  567. def create_namespace(self, name, support_regions = False, version='1.0'):
  568. if self.db == None:
  569. return None
  570. if name in self.namespaces.keys():
  571. raise Namespace.NamespaceError(name, "double used")
  572. new_namespace = Namespace(self.db, name, support_regions, version)
  573. self.namespaces[name] = new_namespace
  574. return new_namespace
  575. def iterate_namespace_names(self):
  576. for name in self.namespaces.keys():
  577. yield name
  578. def get_namespace(self, name):
  579. if name in self.namespaces.keys():
  580. return self.namespaces[name]
  581. else:
  582. return None
  583. def create_file_data(self, path, checksum, content):
  584. if self.db == None:
  585. return None
  586. (new_id, is_updated) = self.db.create_file(path, checksum)
  587. result = FileData(self, path, new_id, checksum, content)
  588. self.last_file_data = result
  589. return (result, is_updated)
  590. def load_file_data(self, path):
  591. if self.db == None:
  592. return None
  593. if self.last_file_data != None and self.last_file_data.get_path() == path:
  594. return self.last_file_data
  595. data = self.db.get_file(path)
  596. if data == None:
  597. return None
  598. result = FileData(self, data.path, data.id, data.checksum, None)
  599. self.last_file_data = result
  600. return result
  601. class DataNotPackable(Exception):
  602. def __init__(self, namespace, field, value, packager, extra_message):
  603. Exception.__init__(self, "Data '"
  604. + str(value)
  605. + "' of type "
  606. + str(value.__class__)
  607. + " referred by '"
  608. + namespace
  609. + "=>"
  610. + field
  611. + "' is not packable by registered packager '"
  612. + str(packager.__class__)
  613. + "': " + extra_message)
  614. def save_file_data(self, file_data):
  615. if self.db == None:
  616. return None
  617. class DataIterator(object):
  618. def iterate_packed_values(self, data, namespace, support_regions = False):
  619. for each in data.iterate_fields(namespace):
  620. space = self.loader.get_namespace(namespace)
  621. if space == None:
  622. raise Loader.DataNotPackable(namespace, each[0], each[1], None, "The namespace has not been found")
  623. try:
  624. packager = space._get_field_packager(each[0])
  625. except mpp.internal.api_impl.PackagerError:
  626. raise Loader.DataNotPackable(namespace, each[0], each[1], None, "The field has not been found")
  627. if space.support_regions != support_regions:
  628. raise Loader.DataNotPackable(namespace, each[0], each[1], packager, "Incompatible support for regions")
  629. try:
  630. packed_data = packager.pack(each[1])
  631. if packed_data == None:
  632. continue
  633. except mpp.internal.api_impl.PackagerError:
  634. raise Loader.DataNotPackable(namespace, each[0], each[1], packager, "Packager raised exception")
  635. yield (each[0], packed_data)
  636. def __init__(self, loader, data, namespace, support_regions = False):
  637. self.loader = loader
  638. self.iterator = self.iterate_packed_values(data, namespace, support_regions)
  639. def __iter__(self):
  640. return self.iterator
  641. for namespace in file_data.iterate_namespaces():
  642. if file_data.is_namespace_updated(namespace) == False:
  643. continue
  644. self.db.add_row(namespace,
  645. file_data.get_id(),
  646. None,
  647. DataIterator(self, file_data, namespace))
  648. if file_data.are_regions_loaded():
  649. for region in file_data.iterate_regions():
  650. for namespace in region.iterate_namespaces():
  651. if region.is_namespace_updated(namespace) == False:
  652. continue
  653. self.db.add_row(namespace,
  654. file_data.get_id(),
  655. region.get_id(),
  656. DataIterator(self, region, namespace, support_regions = True))
  657. def iterate_file_data(self, path = None, path_like_filter = "%"):
  658. if self.db == None:
  659. return None
  660. final_path_like = path_like_filter
  661. if path != None:
  662. if self.db.check_dir(path) == False and self.db.check_file(path) == False:
  663. return None
  664. final_path_like = path + path_like_filter
  665. class FileDataIterator(object):
  666. def iterate_file_data(self, loader, final_path_like):
  667. for data in loader.db.iterate_files(path_like=final_path_like):
  668. yield FileData(loader, data.path, data.id, data.checksum, None)
  669. def __init__(self, loader, final_path_like):
  670. self.iterator = self.iterate_file_data(loader, final_path_like)
  671. def __iter__(self):
  672. return self.iterator
  673. if self.db == None:
  674. return None
  675. return FileDataIterator(self, final_path_like)
  676. def load_aggregated_data(self, path = None, path_like_filter = "%", namespaces = None):
  677. if self.db == None:
  678. return None
  679. final_path_like = path_like_filter
  680. if path != None:
  681. if self.db.check_dir(path) == False and self.db.check_file(path) == False:
  682. return None
  683. final_path_like = path + path_like_filter
  684. if namespaces == None:
  685. namespaces = self.namespaces.keys()
  686. result = AggregatedData(self, path)
  687. for name in namespaces:
  688. namespace = self.get_namespace(name)
  689. data = self.db.aggregate_rows(name, path_like = final_path_like)
  690. for field in data.keys():
  691. if namespace.get_field_python_type(field) == str:
  692. continue
  693. data[field]['nonzero'] = namespace.is_field_non_zero(field)
  694. distribution = self.db.count_rows(name, path_like = final_path_like, group_by_column = field)
  695. data[field]['distribution-bars'] = []
  696. for each in distribution:
  697. if each[0] == None:
  698. continue
  699. assert(float(data[field]['count'] != 0))
  700. data[field]['distribution-bars'].append({'metric': each[0],
  701. 'count': each[1],
  702. 'ratio': (float(each[1]) / float(data[field]['count']))})
  703. result.set_data(name, field, data[field])
  704. return result
  705. def load_selected_data(self, namespace, fields = None, path = None, path_like_filter = "%", filters = [],
  706. sort_by = None, limit_by = None):
  707. if self.db == None:
  708. return None
  709. final_path_like = path_like_filter
  710. if path != None:
  711. if self.db.check_dir(path) == False and self.db.check_file(path) == False:
  712. return None
  713. final_path_like = path + path_like_filter
  714. namespace_obj = self.get_namespace(namespace)
  715. if namespace_obj == None:
  716. return None
  717. class SelectDataIterator(object):
  718. def iterate_selected_values(self, loader, namespace_obj, final_path_like, fields, filters, sort_by, limit_by):
  719. for row in loader.db.select_rows(namespace_obj.get_name(), path_like=final_path_like, filters=filters,
  720. order_by=sort_by, limit_by=limit_by):
  721. region_id = None
  722. if namespace_obj.are_regions_supported() == True:
  723. region_id = row['region_id']
  724. data = SelectData(loader, row['path'], row['id'], region_id)
  725. field_names = fields
  726. if fields == None:
  727. field_names = namespace_obj.iterate_field_names()
  728. for field in field_names:
  729. data.set_data(namespace, field, row[field])
  730. yield data
  731. def __init__(self, loader, namespace_obj, final_path_like, fields, filters, sort_by, limit_by):
  732. self.iterator = self.iterate_selected_values(loader, namespace_obj, final_path_like, fields, filters, sort_by, limit_by)
  733. def __iter__(self):
  734. return self.iterator
  735. return SelectDataIterator(self, namespace_obj, final_path_like, fields, filters, sort_by, limit_by)
  736. class BasePlugin(object):
  737. def initialize(self):
  738. pass
  739. def terminate(self):
  740. pass
  741. def set_name(self, name):
  742. self.name = name
  743. def get_name(self):
  744. if hasattr(self, 'name') == False:
  745. return None
  746. return self.name
  747. def set_version(self, version):
  748. self.version = version
  749. def get_version(self):
  750. if hasattr(self, 'version') == False:
  751. return None
  752. return self.version
  753. def _set_plugin_loader(self, loader):
  754. self.plugin_loader = loader
  755. def _get_plugin_loader(self):
  756. if hasattr(self, 'plugin_loader') == False:
  757. return None
  758. return self.plugin_loader
  759. def get_plugin(self, plugin_name):
  760. return self._get_plugin_loader().get_plugin(plugin_name)
  761. def get_action(self):
  762. return self._get_plugin_loader().get_action()
  763. class Plugin(BasePlugin):
  764. class Field(object):
  765. def __init__(self, name, ftype, non_zero=False):
  766. self.name = name
  767. self.type = ftype
  768. self.non_zero = non_zero
  769. class Property(object):
  770. def __init__(self, name, value):
  771. self.name = name
  772. self.value = value
  773. def initialize(self, namespace=None, support_regions=True, fields=[], properties=[]):
  774. super(Plugin, self).initialize()
  775. if hasattr(self, 'is_updated') == False:
  776. self.is_updated = False # original initialization
  777. db_loader = self.get_plugin('mpp.dbf').get_loader()
  778. if namespace == None:
  779. namespace = self.get_name()
  780. if (len(fields) != 0 or len(properties) != 0):
  781. prev_version = db_loader.set_property(self.get_name() + ":version", self.get_version())
  782. if prev_version != self.get_version():
  783. self.is_updated = True
  784. for prop in properties:
  785. assert(prop.name != 'version')
  786. prev_prop = db_loader.set_property(self.get_name() + ":" + prop.name, prop.value)
  787. if prev_prop != prop.value:
  788. self.is_updated = True
  789. if len(fields) != 0:
  790. namespace_obj = db_loader.create_namespace(namespace,
  791. support_regions=support_regions,
  792. version=self.get_version())
  793. for field in fields:
  794. is_created = namespace_obj.add_field(field.name, field.type, non_zero=field.non_zero)
  795. assert(is_created != None)
  796. # if field is created (not cloned from the previous db),
  797. # mark the plug-in as updated in order to trigger full rescan
  798. self.is_updated = self.is_updated or is_created
  799. class MetricPluginMixin(object):
  800. class AliasError(Exception):
  801. def __init__(self, alias):
  802. Exception.__init__(self, "Unknown alias: " + str(alias))
  803. def declare_metric(self, is_active, field,
  804. pattern_to_search_or_map_of_patterns,
  805. marker_type_mask=Marker.T.ANY,
  806. region_type_mask=Region.T.ANY,
  807. exclude_subregions=True,
  808. merge_markers=False):
  809. if hasattr(self, '_fields') == False:
  810. self._fields = {}
  811. if isinstance(pattern_to_search_or_map_of_patterns, dict):
  812. map_of_patterns = pattern_to_search_or_map_of_patterns
  813. else:
  814. map_of_patterns = {'*': pattern_to_search_or_map_of_patterns}
  815. if is_active == True:
  816. self._fields[field.name] = (field,
  817. marker_type_mask,
  818. exclude_subregions,
  819. merge_markers,
  820. map_of_patterns,
  821. region_type_mask)
  822. def is_active(self, metric_name = None):
  823. if metric_name == None:
  824. return (len(self._fields.keys()) > 0)
  825. return (metric_name in self._fields.keys())
  826. def get_fields(self):
  827. result = []
  828. for key in self._fields.keys():
  829. result.append(self._fields[key][0])
  830. return result
  831. def callback(self, parent, data, is_updated):
  832. # count if metric is enabled,
  833. # and (optimization for the case of iterative rescan:)
  834. # if file is updated or this plugin's settings are updated
  835. is_updated = is_updated or self.is_updated
  836. if is_updated == True:
  837. for field in self.get_fields():
  838. self.count_if_active(field.name, data, alias=parent.get_name())
  839. # if parent, notify children
  840. if isinstance(self, Parent):
  841. self.notify_children(data, is_updated)
  842. def count_if_active(self, metric_name, data, namespace=None, alias='*'):
  843. if self.is_active(metric_name) == False:
  844. return
  845. if namespace == None:
  846. namespace = self.get_name()
  847. field_data = self._fields[metric_name]
  848. text = data.get_content()
  849. if alias not in field_data[4].keys():
  850. if '*' not in field_data[4].keys():
  851. raise self.AliasError(alias)
  852. else:
  853. alias = '*'
  854. pattern_to_search = field_data[4][alias]
  855. if hasattr(self, '_' + metric_name + '_count'):
  856. counter_callback = self.__getattribute__('_' + metric_name + '_count')
  857. for region in data.iterate_regions(filter_group=field_data[5]):
  858. counter_data = {}
  859. count = 0
  860. if hasattr(self, '_' + metric_name + '_count_initialize'):
  861. (count, counter_data) = self.__getattribute__('_' + metric_name + '_count_initialize')(alias, data, region)
  862. for marker in data.iterate_markers(
  863. filter_group = field_data[1],
  864. region_id = region.get_id(),
  865. exclude_children = field_data[2],
  866. merge=field_data[3]):
  867. begin = marker.get_offset_begin()
  868. end = marker.get_offset_end()
  869. for match in pattern_to_search.finditer(text, begin, end):
  870. count = counter_callback(alias, data, region, marker, match, count, counter_data)
  871. if count != 0 or field_data[0].non_zero == False:
  872. region.set_data(namespace, metric_name, count)
  873. else:
  874. for region in data.iterate_regions(filter_group=field_data[5]):
  875. count = 0
  876. for marker in data.iterate_markers(
  877. filter_group = field_data[1],
  878. region_id = region.get_id(),
  879. exclude_children = field_data[2],
  880. merge=field_data[3]):
  881. count += len(pattern_to_search.findall(text, marker.get_offset_begin(), marker.get_offset_end()))
  882. if count != 0 or field_data[0].non_zero == False:
  883. region.set_data(namespace, metric_name, count)
  884. class InterfaceNotImplemented(Exception):
  885. def __init__(self, obj):
  886. import sys
  887. Exception.__init__(self, "Method '"
  888. + sys._getframe(1).f_code.co_name
  889. + "' has not been implemented for "
  890. + str(obj.__class__))
  891. class IConfigurable(object):
  892. def configure(self, options):
  893. raise InterfaceNotImplemented(self)
  894. def declare_configuration(self, optparser):
  895. raise InterfaceNotImplemented(self)
  896. class IRunable(object):
  897. def run(self, args):
  898. raise InterfaceNotImplemented(self)
  899. class IParser(object):
  900. def process(self, parent, data, is_updated):
  901. raise InterfaceNotImplemented(self)
  902. class ICode(object):
  903. pass
  904. class CallbackNotImplemented(Exception):
  905. def __init__(self, obj, callback_name):
  906. Exception.__init__(self, "Callback '"
  907. + callback_name
  908. + "' has not been implemented for "
  909. + str(obj.__class__))
  910. class Child(object):
  911. def notify(self, parent, callback_name, *args):
  912. if hasattr(self, callback_name) == False:
  913. raise CallbackNotImplemented(self, callback_name)
  914. self.__getattribute__(callback_name)(parent, *args)
  915. def subscribe_by_parents_name(self, parent_name, callback_name='callback'):
  916. self.get_plugin(parent_name).subscribe(self, callback_name)
  917. def subscribe_by_parents_names(self, parent_names, callback_name='callback'):
  918. for parent_name in parent_names:
  919. self.get_plugin(parent_name).subscribe(self, callback_name)
  920. def subscribe_by_parents_interface(self, interface, callback_name='callback'):
  921. for plugin in self._get_plugin_loader().iterate_plugins():
  922. if isinstance(plugin, interface):
  923. plugin.subscribe(self, callback_name)
  924. class Parent(object):
  925. def init_Parent(self):
  926. if hasattr(self, 'children') == False:
  927. self.children = []
  928. def subscribe(self, obj, callback_name):
  929. self.init_Parent()
  930. if (isinstance(obj, Child) == False):
  931. raise TypeError()
  932. self.children.append((obj,callback_name))
  933. def unsubscribe(self, obj, callback_name):
  934. self.init_Parent()
  935. self.children.remove((obj, callback_name))
  936. def notify_children(self, *args):
  937. self.init_Parent()
  938. for child in self.children:
  939. child[0].notify(self, child[1], *args)
  940. def iterate_children(self):
  941. self.init_Parent()
  942. for child in self.children:
  943. yield child