statisticsitems.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. #
  3. # Permission to use, copy, modify, and/or distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. # PERFORMANCE OF THIS SOFTWARE.
  14. """\
  15. This script checks output of gen-statisticsitems.py.
  16. This script checks XML file. Spec file, C++ code and header files syntax is
  17. checked in the other unittests or system tests.
  18. """
  19. import os
  20. import sys
  21. from xml.etree import ElementTree
  22. """\
  23. User-defined exception for parse error. It is thrown if a file is not
  24. formatted as expected.
  25. """
  26. class ParseError(Exception):
  27. pass
  28. """\
  29. Test XML file.
  30. It should have <refsect1> which has <title>STATISTICS DATA</title>.
  31. Inside the section, it should have one or more <varlistentry> of each item
  32. inside <variablelist>.
  33. Each <varlistentry> should have <term> for item name and <simpara> inside
  34. <listitem> for item description.
  35. Example:
  36. <refsect1>
  37. <title>STATISTICS DATA</title>
  38. <variablelist>
  39. <varlistentry>
  40. <term>item1</term>
  41. <listitem>
  42. <simpara>
  43. statistics item
  44. </simpara>
  45. </listitem>
  46. </varlistentry>
  47. <varlistentry>
  48. <term>item2</term>
  49. <listitem>
  50. <simpara>
  51. another statistics item
  52. </simpara>
  53. </listitem>
  54. </varlistentry>
  55. </variablelist>
  56. </refsect1>
  57. """
  58. def testXMLfile(xmlfilepath):
  59. xmltree = ElementTree.parse(xmlfilepath)
  60. root = xmltree.getroot()
  61. # find <refsect1> which has <title> of 'STATISTICS DATA'
  62. stats_node = [t for t in root.findall('./refsect1')
  63. if t.find('./title').text == 'STATISTICS DATA']
  64. if not stats_node:
  65. raise ParseError('Statistics data section does not exist')
  66. # find all <varlistentry> inside <variablelist>
  67. entries = stats_node[0].find('./variablelist').findall('./varlistentry')
  68. if not entries:
  69. raise ParseError('<varlistentry> does not exist inside section')
  70. for entry in entries:
  71. # find <term> for item name
  72. name = entry.find('./term')
  73. if name is None or name.text == '':
  74. raise ParseError('<term> for item name does not exist')
  75. # find <simpara> inside <listitem> for item description
  76. description = entry.find('./listitem/simpara')
  77. if description is None or description.text == '':
  78. raise ParseError('<listitem> nor <simpara> for item description'
  79. ' does not exist')
  80. return
  81. if __name__ == "__main__":
  82. xmlfilepath = sys.argv[1]
  83. testXMLfile(xmlfilepath)