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