debug.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 cgi
  21. import core.api
  22. import core.log
  23. import core.cmdparser
  24. import core.db.post
  25. import core.utils
  26. class Tool(core.api.ITool):
  27. def run(self, tool_args):
  28. return main(tool_args)
  29. def main(tool_args):
  30. log_plugin = core.log.Plugin()
  31. db_plugin = core.db.post.Plugin()
  32. parser = core.cmdparser.MultiOptionParser(usage="Usage: %prog debug [options] -- [path 1] ... [path N]")
  33. log_plugin.declare_configuration(parser)
  34. db_plugin.declare_configuration(parser)
  35. parser.add_option("-m", "--mode", default='dumphtml', choices=['dumphtml'],
  36. help="'dumphtml' - prints html code with code highlights for each given path [default: %default]")
  37. (options, args) = parser.parse_args(tool_args)
  38. log_plugin.configure(options)
  39. db_plugin.configure(options)
  40. loader = core.api.Loader()
  41. if loader.open_database(db_plugin.dbfile) == False:
  42. parser.error("Can not open file: " + db_plugin.dbfile)
  43. if options.__dict__['mode'] == 'dumphtml':
  44. return dumphtml(args, loader)
  45. assert(False)
  46. def dumphtml(args, loader):
  47. exit_code = 0
  48. result = ""
  49. result += '<html><body>'
  50. for path in args:
  51. path = core.utils.preprocess_path(path)
  52. data = loader.load_file_data(path)
  53. if data == None:
  54. core.utils.report_bad_path(path)
  55. exit_code += 1
  56. continue
  57. file_name = data.get_path()
  58. fh = open(file_name, 'r')
  59. if fh == None:
  60. logging.error("can not open file '" + path + "' for reading")
  61. exit_code += 1
  62. continue
  63. text = fh.read()
  64. fh.close()
  65. result += '<table><tr><td><pre>'
  66. last_pos = 0
  67. for marker in data.iterate_markers():
  68. result += (cgi.escape(text[last_pos:marker.begin]))
  69. if marker.get_type() == data.get_marker_types().STRING:
  70. result += ('<span style="color:#0000FF">')
  71. elif marker.get_type() == data.get_marker_types().COMMENT:
  72. result += ('<span style="color:#009900">')
  73. elif marker.get_type() == data.get_marker_types().PREPROCESSOR:
  74. result += ('<span style="color:#990000">')
  75. else:
  76. # TODO add tests for debug tool
  77. assert False, "Uknown marker type"
  78. result += (cgi.escape(text[marker.begin:marker.end]))
  79. result += ('</span>')
  80. last_pos = marker.end
  81. result += (cgi.escape(text[last_pos:]))
  82. last_pos = 0
  83. result += ('</pre></td><td><pre>')
  84. styles = [('<span style="background-color:#F0F010">',
  85. '<span style="background-color:#F010F0">'),
  86. ('<span style="background-color:#F0F030">',
  87. '<span style="background-color:#F030F0">'),
  88. ('<span style="background-color:#F0F050">',
  89. '<span style="background-color:#F050F0">'),
  90. ('<span style="background-color:#F0F070">',
  91. '<span style="background-color:#F070F0">'),
  92. ('<span style="background-color:#F0F090">',
  93. '<span style="background-color:#F090F0">'),
  94. ('<span style="background-color:#F0F0B0">',
  95. '<span style="background-color:#F0B0F0">'),
  96. ('<span style="background-color:#F0F0D0">',
  97. '<span style="background-color:#F0D0F0">'),
  98. ('<span style="background-color:#F0F0E0">',
  99. '<span style="background-color:#F0E0F0">')]
  100. def proc_rec(region_id, file_data, styles, indent, pos):
  101. result = (styles[indent % len(styles)][pos % 2])
  102. region = file_data.get_region(region_id)
  103. result += ('<a href="#line' + str(region.get_cursor()) + '" id=line"' + str(region.get_cursor()) + '"></a>')
  104. last_pos = region.get_offset_begin()
  105. for (ind, sub_id) in enumerate(file_data.get_region(region_id).iterate_subregion_ids()):
  106. subregion = file_data.get_region(sub_id)
  107. result += (cgi.escape(text[last_pos:subregion.get_offset_begin()]))
  108. result += proc_rec(sub_id, file_data, styles, indent + 3, ind)
  109. last_pos = subregion.get_offset_end()
  110. result += (cgi.escape(text[last_pos:region.get_offset_end()]))
  111. result += ('</span>')
  112. return result
  113. result += proc_rec(1, data, styles, 0, 0)
  114. result += ('</pre></td></tr></table>')
  115. result += ('</body></html>')
  116. print result
  117. return exit_code