debug.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.log
  22. import core.cmdparser
  23. import core.db.post
  24. import core.db.loader
  25. import core.api
  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", "--general.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.db.loader.Loader()
  41. loader.open_database(db_plugin.dbfile)
  42. if options.__dict__['general.mode'] == 'dumphtml':
  43. return dumphtml(args, loader)
  44. assert(False)
  45. def dumphtml(args, loader):
  46. exit_code = 0
  47. result = ""
  48. result += '<html><body>'
  49. for path in args:
  50. data = loader.load_file_data(path)
  51. if data == None:
  52. logging.error("Specified path '" + path + "' is invalid (not found in the database records)")
  53. exit_code += 1
  54. continue
  55. file_name = data.get_path()
  56. fh = open(file_name, 'r')
  57. if fh == None:
  58. logging.error("can not open file '" + path + "' for reading")
  59. exit_code += 1
  60. continue
  61. text = fh.read()
  62. fh.close()
  63. result += '<table><tr><td><pre>'
  64. last_pos = 0
  65. for marker in data.iterate_markers():
  66. result += (cgi.escape(text[last_pos:marker.begin]))
  67. if marker.get_type() == data.get_marker_types().STRING:
  68. result += ('<span style="color:#0000FF">')
  69. elif marker.get_type() == data.get_marker_types().COMMENT:
  70. result += ('<span style="color:#009900">')
  71. elif marker.get_type() == data.get_marker_types().PREPROCESSOR:
  72. result += ('<span style="color:#990000">')
  73. result += (cgi.escape(text[marker.begin:marker.end]))
  74. result += ('</span>')
  75. last_pos = marker.end
  76. result += (cgi.escape(text[last_pos:]))
  77. last_pos = 0
  78. result += ('</pre></td><td><pre>')
  79. styles = [('<span style="background-color:#F0F010">',
  80. '<span style="background-color:#F010F0">'),
  81. ('<span style="background-color:#F0F030">',
  82. '<span style="background-color:#F030F0">'),
  83. ('<span style="background-color:#F0F050">',
  84. '<span style="background-color:#F050F0">'),
  85. ('<span style="background-color:#F0F070">',
  86. '<span style="background-color:#F070F0">'),
  87. ('<span style="background-color:#F0F090">',
  88. '<span style="background-color:#F090F0">'),
  89. ('<span style="background-color:#F0F0B0">',
  90. '<span style="background-color:#F0B0F0">'),
  91. ('<span style="background-color:#F0F0D0">',
  92. '<span style="background-color:#F0D0F0">'),
  93. ('<span style="background-color:#F0F0E0">',
  94. '<span style="background-color:#F0E0F0">')]
  95. def proc_rec(region_id, file_data, styles, indent, pos):
  96. result = (styles[indent % len(styles)][pos % 2])
  97. region = file_data.get_region(region_id)
  98. result += ('<a href="#line' + str(region.get_cursor()) + '" id=line"' + str(region.get_cursor()) + '"></a>')
  99. last_pos = region.get_offset_begin()
  100. for (ind, sub_id) in enumerate(file_data.get_region(region_id).iterate_subregion_ids()):
  101. subregion = file_data.get_region(sub_id)
  102. result += (cgi.escape(text[last_pos:subregion.get_offset_begin()]))
  103. result += proc_rec(sub_id, file_data, styles, indent + 3, ind)
  104. last_pos = subregion.get_offset_end()
  105. result += (cgi.escape(text[last_pos:region.get_offset_end()]))
  106. result += ('</span>')
  107. return result
  108. result += proc_rec(1, data, styles, 0, 0)
  109. result += ('</pre></td></tr></table>')
  110. result += ('</body></html>')
  111. print result
  112. return exit_code