debug.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 cgi
  22. import core.log
  23. import core.cmdparser
  24. import core.db.post
  25. import core.db.loader
  26. def main():
  27. log_plugin = core.log.Plugin()
  28. db_plugin = core.db.post.Plugin()
  29. parser = core.cmdparser.MultiOptionParser(usage="Usage: %prog [options] -- [path 1] ... [path N]")
  30. log_plugin.declare_configuration(parser)
  31. db_plugin.declare_configuration(parser)
  32. parser.add_option("-m", "--general.mode", default='dumphtml', choices=['dumphtml'],
  33. help="'dumphtml' - prints html code with code highlights for each given path [default: %default]")
  34. (options, args) = parser.parse_args()
  35. log_plugin.configure(options)
  36. db_plugin.configure(options)
  37. loader = core.db.loader.Loader()
  38. loader.open_database(db_plugin.dbfile)
  39. if options.__dict__['general.mode'] == 'dumphtml':
  40. return dumphtml(args, loader)
  41. assert(False)
  42. def dumphtml(args, loader):
  43. exit_code = 0
  44. result = ""
  45. result += '<html><body>'
  46. for path in args:
  47. data = loader.load_file_data(path)
  48. if data == None:
  49. logging.error("Specified path '" + path + "' is invalid (not found in the database records)")
  50. exit_code += 1
  51. continue
  52. file_name = data.get_path()
  53. fh = open(file_name, 'r')
  54. if fh == None:
  55. logging.error("can not open file '" + path + "' for reading")
  56. exit_code += 1
  57. continue
  58. text = fh.read()
  59. fh.close()
  60. result += '<table><tr><td><pre>'
  61. last_pos = 0
  62. for marker in data.iterate_markers():
  63. result += (cgi.escape(text[last_pos:marker.begin]))
  64. if marker.get_type() == data.get_marker_types().STRING:
  65. result += ('<span style="color:#0000FF">')
  66. elif marker.get_type() == data.get_marker_types().COMMENT:
  67. result += ('<span style="color:#009900">')
  68. elif marker.get_type() == data.get_marker_types().PREPROCESSOR:
  69. result += ('<span style="color:#990000">')
  70. result += (cgi.escape(text[marker.begin:marker.end]))
  71. result += ('</span>')
  72. last_pos = marker.end
  73. result += (cgi.escape(text[last_pos:]))
  74. last_pos = 0
  75. result += ('</pre></td><td><pre>')
  76. styles = ['<span style="background-color:#ffff80">', '<span style="background-color:#ff80ff">']
  77. for item in enumerate(data.iterate_regions(filter_group=data.get_region_types().FUNCTION)):
  78. reg = item[1]
  79. result += (cgi.escape(text[last_pos:reg.get_offset_begin()]))
  80. result += (styles[item[0] % 2])
  81. result += ('<a href="#line' + str(reg.get_cursor()) + '" id=line"' + str(reg.get_cursor()) + '"></a>')
  82. result += (cgi.escape(text[reg.get_offset_begin():reg.get_offset_end()]))
  83. result += ('</span>')
  84. last_pos = reg.get_offset_end()
  85. result += (cgi.escape(text[last_pos:]))
  86. result += ('</pre></td></tr></table>')
  87. result += ('</body></html>')
  88. print result
  89. return exit_code
  90. if __name__ == '__main__':
  91. ts = time.time()
  92. core.log.set_default_format()
  93. exit_code = main()
  94. logging.warning("Exit code: " + str(exit_code) + ". Time spent: " + str(round((time.time() - ts), 2)) + " seconds. Done")
  95. exit(exit_code) # number of reported messages, errors are reported as non-handled exceptions