/testing/library/tools/initPromLibrary.py

http://datanucleus-appengine.googlecode.com/ · Python · 156 lines · 102 code · 17 blank · 37 comment · 13 complexity · 76cac77727f5ece54763b9dad4edb513 MD5 · raw file

  1. #!/usr/bin/python2.4
  2. import os
  3. import sys
  4. import getopt
  5. import xml.dom.minidom
  6. # Starts parsing the XML file containing the book data entries
  7. #
  8. default_local = "http://localhost:8080/library"
  9. default_corp = "http://gptestshop.prom.corp.google.com/library"
  10. default_uri = default_corp
  11. default_files = ("book.xml", "tech.xml")
  12. class initPromLibrary:
  13. """Utility to initialize the Prometheus Library
  14. We repeatedly use wget to send Add actions to the
  15. Prometheus Library with lastname, firstname, year
  16. and title parameters in order to build up a catalaog
  17. of entries to run the automated Selenium tests.
  18. The catalog name in the xml file is used to specify
  19. the PromLibrary entityname parameter.
  20. The catalog is built up from a set of xml files
  21. currently consisting of book.xml and tech.xml.
  22. The latter is a small catalog that illustrates all of
  23. the fields.
  24. <?xml version="1.0" encoding="utf-8"?>
  25. <catalog>
  26. <name>TechBooks</name>
  27. <book>
  28. <title>Python in a Nutshell</title>
  29. <author>Alex Martelli</author>
  30. <year>2003</year>
  31. </book>
  32. <book>
  33. <title>HTML and XHTML</title>
  34. <author>Bill Kennedy</author>
  35. <year>2007</year>
  36. </book>
  37. </catalog>
  38. Each book entry consists of:
  39. title - the title of the book
  40. author - the author of the book which gets divided into
  41. a lastname and firstname
  42. year - the year of the book
  43. The uri is specified with the -u option on the command line.
  44. The default uri is http://gptestshop.prom.corp.google.com/library
  45. which is the version on the Prometheus corp cluster.
  46. This is also the uri that is used if -u corp is passed.
  47. If -u local is used, the the uri is http://localhost:8080/library
  48. or the locally deployed version.
  49. """
  50. def handleCatalog(self, catalog, uri):
  51. """Parse the XML file and get the catalog name
  52. and call handleBooks to deal with the list of books
  53. """
  54. XmlBooks = catalog.getElementsByTagName("book")
  55. Xmlname = catalog.getElementsByTagName("name")[0]
  56. name = Xmlname.childNodes[0].data
  57. self.handleBooks(XmlBooks, name, uri)
  58. def handleBooks(self, XmlBooks , name, uri):
  59. """For each book in the list call handleBook
  60. to extract the entry info.
  61. """
  62. for XmlBook in XmlBooks:
  63. self.handleBook(XmlBook, name, uri)
  64. def handleBook(self, XmlBook, entityname, uri):
  65. """Extracts the book data entries from the XML file and
  66. calls AddBook to send it to the Prometheus Library
  67. """
  68. Xmltitle = XmlBook.getElementsByTagName("title")[0]
  69. XmlAuthor = XmlBook.getElementsByTagName("author")[0]
  70. Xmldate = XmlBook.getElementsByTagName("year")[0]
  71. title = Xmltitle.childNodes[0].data
  72. year = Xmldate.childNodes[0].data
  73. fullname = XmlAuthor.childNodes[0].data.split(None,2)
  74. firstname = fullname[0]
  75. lastname = fullname[1]
  76. self.AddBook(uri, lastname, firstname, title, year, entityname)
  77. def AddBook(self, uri, lastname, firstname, title, year, entityname):
  78. """Add a Book to the Prometheus library by building the
  79. parameter query string and calling wget. The results
  80. from wget are redirected to /dev/null. This is used
  81. rather than --spider because these types of requests
  82. are not honored by Prometheus.
  83. """
  84. querystring = '?'
  85. for arg in [ "lastname=%s" % lastname, "firstname=%s" % firstname,
  86. "title=%s" % title, "year=%s" % year, "action_type=Add",
  87. "entity=%s" % entityname ]:
  88. querystring = "%s&%s" % (querystring, arg)
  89. postdatastring = '--post-data "%s"' % (querystring)
  90. cmd = 'wget'
  91. opt = "-O /dev/null"
  92. cmd_str = "%s %s %s %s" % (cmd, opt, postdatastring, uri)
  93. print cmd_str
  94. os.system(cmd_str)
  95. def main(argv):
  96. """Perform initialization, parse command line options and
  97. arguments, and output usage messages.
  98. If multiple filenames are provided, loop through
  99. each one and call the handleCatalog to process the XML.
  100. """
  101. uri = default_uri
  102. cmdline_params = argv[1:]
  103. try:
  104. optlist, args = getopt.getopt(cmdline_params, 'u:', ['uri='])
  105. except getopt.GetoptError:
  106. print 'Usage: initPromLibrary.py [-u uri] [files]'
  107. print ' Default files = %s %s' % (default_files[0], default_files[1])
  108. print ' Default uri = %s' % (default_uri)
  109. print " Shorthand uri's"
  110. print ' -u corp = %s' % (default_corp)
  111. print ' -u local = %s' % (default_local)
  112. sys.exit(2)
  113. if optlist:
  114. uri = optlist[0][1]
  115. if uri == 'local':
  116. uri = default_local
  117. if uri == 'corp':
  118. uri = default_corp
  119. if not args:
  120. args = default_files
  121. for arg in args:
  122. if arg:
  123. print arg
  124. if arg[0] == '/':
  125. xmlfilename = arg
  126. else:
  127. xmlfilename = os.path.join(os.path.dirname(__file__), arg)
  128. dom = xml.dom.minidom.parse(xmlfilename)
  129. cat = initPromLibrary()
  130. cat.handleCatalog(dom, uri)
  131. if __name__ == '__main__':
  132. main(sys.argv)