export.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 logging
  20. import time
  21. import re
  22. import core.log
  23. import core.db.loader
  24. import core.db.post
  25. import core.db.utils
  26. import core.cmdparser
  27. import core.export.convert
  28. def main():
  29. exit_code = 0
  30. log_plugin = core.log.Plugin()
  31. db_plugin = core.db.post.Plugin()
  32. parser = core.cmdparser.MultiOptionParser(usage="Usage: %prog [options] -- [path 1] ... [path N]")
  33. log_plugin.declare_configuration(parser)
  34. db_plugin.declare_configuration(parser)
  35. parser.add_option("--general.format", default='xml', choices=['txt', 'xml', 'python'], help="Format of the output data. "
  36. "Possible values are 'xml', 'txt' or 'python' [default: %default]")
  37. parser.add_option("--general.namespaces", default=None, help="Allows to enumerate namespaces of interest."
  38. " If not defined all namespaces available in database file will be processed."
  39. " Separate several namespaces by comma, for example 'general,std.code.complexity'"
  40. " [default: %default]")
  41. (options, args) = parser.parse_args()
  42. log_plugin.configure(options)
  43. db_plugin.configure(options)
  44. out_format = options.__dict__['general.format']
  45. namespaces = None
  46. if options.__dict__['general.namespaces'] != None:
  47. namespaces = re.split(',', options.__dict__['general.namespaces'])
  48. loader_prev = core.db.loader.Loader()
  49. if db_plugin.dbfile_prev != None:
  50. loader_prev.open_database(db_plugin.dbfile_prev)
  51. loader = core.db.loader.Loader()
  52. loader.open_database(db_plugin.dbfile)
  53. paths = None
  54. if len(args) == 0:
  55. paths = [""]
  56. else:
  57. paths = args
  58. if out_format == 'txt':
  59. print "=" * 80 + "\n" + "Export" + "\n" + "_" * 80 + "\n"
  60. elif out_format == 'xml':
  61. print "<export>"
  62. elif out_format == 'python':
  63. print "{'export': ["
  64. for (ind, path) in enumerate(paths):
  65. logging.info("Processing: " + re.sub(r'''[\\]''', "/", path))
  66. aggregated_data = loader.load_aggregated_data(path, namespaces=namespaces)
  67. aggregated_data_tree = {}
  68. subdirs = []
  69. subfiles = []
  70. if aggregated_data != None:
  71. aggregated_data_tree = aggregated_data.get_data_tree(namespaces=namespaces)
  72. subdirs = aggregated_data.get_subdirs()
  73. subfiles = aggregated_data.get_subfiles()
  74. else:
  75. logging.error("Specified path '" + path + "' is invalid (not found in the database records)")
  76. exit_code += 1
  77. aggregated_data_prev = loader_prev.load_aggregated_data(path, namespaces=namespaces)
  78. if aggregated_data_prev != None:
  79. aggregated_data_tree = append_diff(aggregated_data_tree,
  80. aggregated_data_prev.get_data_tree(namespaces=namespaces))
  81. file_data = loader.load_file_data(path)
  82. file_data_tree = {}
  83. if file_data != None:
  84. file_data_tree = file_data.get_data_tree(namespaces=namespaces)
  85. file_data_prev = loader_prev.load_file_data(path)
  86. regions_matcher = None
  87. if file_data_prev != None:
  88. file_data_tree = append_diff(file_data_tree,
  89. file_data_prev.get_data_tree(namespaces=namespaces))
  90. regions_matcher = core.db.utils.FileRegionsMatcher(file_data, file_data_prev)
  91. regions = []
  92. for each in file_data.iterate_regions():
  93. region_data_tree = each.get_data_tree(namespaces=namespaces)
  94. if regions_matcher != None and regions_matcher.is_matched(each.get_id()):
  95. region_data_prev = file_data_prev.get_region(regions_matcher.get_prev_id(each.get_id()))
  96. region_data_tree = append_diff(region_data_tree,
  97. region_data_prev.get_data_tree(namespaces=namespaces))
  98. regions.append({"info": {"name" : each.name,
  99. 'type' : file_data.get_region_types()().to_str(each.get_type()),
  100. "cursor" : each.cursor,
  101. 'line_begin': each.line_begin,
  102. 'line_end': each.line_end,
  103. 'offset_begin': each.begin,
  104. 'offset_end': each.end},
  105. "data": region_data_tree})
  106. file_data_tree['regions'] = regions
  107. data = {"info": {"path": path, "id": ind + 1},
  108. "aggregated-data": aggregated_data_tree,
  109. "file-data": file_data_tree,
  110. "subdirs": subdirs,
  111. "subfiles": subfiles}
  112. if out_format == 'txt':
  113. print core.export.convert.to_txt(data, root_name = "data")
  114. elif out_format == 'xml':
  115. print core.export.convert.to_xml(data, root_name = "data")
  116. elif out_format == 'python':
  117. postfix = ""
  118. if ind < len(paths) - 1:
  119. postfix = ", "
  120. print core.export.convert.to_python(data, root_name = "data") + postfix
  121. if out_format == 'txt':
  122. print "\n"
  123. elif out_format == 'xml':
  124. print "</export>"
  125. elif out_format == 'python':
  126. print "]}"
  127. return exit_code
  128. def append_diff(main_tree, prev_tree):
  129. assert(main_tree != None)
  130. assert(prev_tree != None)
  131. for name in main_tree.keys():
  132. if name not in prev_tree.keys():
  133. continue
  134. for field in main_tree[name].keys():
  135. if field not in prev_tree[name].keys():
  136. continue
  137. if isinstance(main_tree[name][field], dict) and isinstance(prev_tree[name][field], dict):
  138. diff = {}
  139. for key in main_tree[name][field].keys():
  140. if key not in prev_tree[name][field].keys():
  141. continue
  142. main_val = main_tree[name][field][key]
  143. prev_val = prev_tree[name][field][key]
  144. if main_val == None:
  145. main_val = 0
  146. if prev_val == None:
  147. prev_val = 0
  148. diff[key] = main_val - prev_val
  149. main_tree[name][field]['__diff__'] = diff
  150. elif (not isinstance(main_tree[name][field], dict)) and (not isinstance(prev_tree[name][field], dict)):
  151. if '__diff__' not in main_tree[name]:
  152. main_tree[name]['__diff__'] = {}
  153. main_tree[name]['__diff__'][field] = main_tree[name][field] - prev_tree[name][field]
  154. return main_tree
  155. if __name__ == '__main__':
  156. ts = time.time()
  157. core.log.set_default_format()
  158. exit_code = main()
  159. logging.warning("Exit code: " + str(exit_code) + ". Time spent: " + str(round((time.time() - ts), 2)) + " seconds. Done")
  160. exit(exit_code)