export.py 7.5 KB

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