spotbugsSummary.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. # SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  3. # SPDX-FileCopyrightText: 2017 Tobias Kaminsky <tobias@kaminsky.me>
  4. # SPDX-License-Identifier: GPL-3.0-or-later
  5. import argparse
  6. import defusedxml.ElementTree as ET
  7. def get_counts(tree):
  8. category_counts = {}
  9. category_names = {}
  10. for child in tree.getroot():
  11. if child.tag == "BugInstance":
  12. category = child.attrib['category']
  13. if category in category_counts:
  14. category_counts[category] = category_counts[category] + 1
  15. else:
  16. category_counts[category] = 1
  17. elif child.tag == "BugCategory":
  18. category = child.attrib['category']
  19. category_names[category] = child[0].text
  20. summary = {}
  21. for category in category_counts.keys():
  22. summary[category_names[category]] = category_counts[category]
  23. return summary
  24. def print_html(summary):
  25. output = "<table><tr><th>Category</th><th>Count</th></tr>"
  26. categories = sorted(summary.keys())
  27. for category in categories:
  28. output += "<tr>"
  29. output += f"<td>{category}</td>"
  30. output += f"<td>{summary[category]}</td>"
  31. output += "</tr>"
  32. output += "<tr>"
  33. output += "<td><b>Total</b></td>"
  34. output += f"<td><b>{sum(summary.values())}</b></td>"
  35. output += "</tr>"
  36. output += "</table>"
  37. print(output)
  38. def print_total(summary):
  39. print(sum(summary.values()))
  40. if __name__ == "__main__":
  41. parser = argparse.ArgumentParser()
  42. parser.add_argument("--total", help="print total count instead of summary HTML",
  43. action="store_true")
  44. parser.add_argument("--file", help="file to parse", default="app/build/reports/spotbugs/gplayDebug.xml")
  45. args = parser.parse_args()
  46. tree = ET.parse(args.file)
  47. summary = get_counts(tree)
  48. if args.total:
  49. print_total(summary)
  50. else:
  51. print_html(summary)