debug.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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:#F0F010">',
  77. '<span style="background-color:#F010F0">'),
  78. ('<span style="background-color:#F0F030">',
  79. '<span style="background-color:#F030F0">'),
  80. ('<span style="background-color:#F0F050">',
  81. '<span style="background-color:#F050F0">'),
  82. ('<span style="background-color:#F0F070">',
  83. '<span style="background-color:#F070F0">'),
  84. ('<span style="background-color:#F0F090">',
  85. '<span style="background-color:#F090F0">'),
  86. ('<span style="background-color:#F0F0B0">',
  87. '<span style="background-color:#F0B0F0">'),
  88. ('<span style="background-color:#F0F0D0">',
  89. '<span style="background-color:#F0D0F0">'),
  90. ('<span style="background-color:#F0F0E0">',
  91. '<span style="background-color:#F0E0F0">')]
  92. def proc_rec(region_id, file_data, styles, indent, pos):
  93. result = (styles[indent % len(styles)][pos % 2])
  94. region = file_data.get_region(region_id)
  95. result += ('<a href="#line' + str(region.get_cursor()) + '" id=line"' + str(region.get_cursor()) + '"></a>')
  96. last_pos = region.get_offset_begin()
  97. for (ind, sub_id) in enumerate(file_data.get_region(region_id).iterate_subregion_ids()):
  98. subregion = file_data.get_region(sub_id)
  99. result += (cgi.escape(text[last_pos:subregion.get_offset_begin()]))
  100. result += proc_rec(sub_id, file_data, styles, indent + 3, ind)
  101. last_pos = subregion.get_offset_end()
  102. result += (cgi.escape(text[last_pos:region.get_offset_end()]))
  103. result += ('</span>')
  104. return result
  105. result += proc_rec(1, data, styles, 0, 0)
  106. result += ('</pre></td></tr></table>')
  107. result += ('</body></html>')
  108. print result
  109. return exit_code
  110. if __name__ == '__main__':
  111. ts = time.time()
  112. core.log.set_default_format()
  113. exit_code = main()
  114. logging.warning("Exit code: " + str(exit_code) + ". Time spent: " + str(round((time.time() - ts), 2)) + " seconds. Done")
  115. exit(exit_code) # number of reported messages, errors are reported as non-handled exceptions