Bump to doxygen 1.9.2
[platform/upstream/doxygen.git] / testing / testsqlite3.py
1 #! /usr/bin/python
2 from xml.etree import cElementTree as ET
3 import os
4 import sqlite3
5 import sys
6 import getopt
7
8 # map XML attributes/elements to SQL rows
9 # --POC: iterate through the children and attributes of the memberdef element
10 #        and search it in doxygen_sqlite3.db
11
12 g_conn=None
13 val=[]
14 def print_unprocessed_attributes(node):
15     for key in node.attrib:
16         print("WARNING: '%s' has unprocessed attr '%s'" % (node.tag,key))
17
18 def extract_attribute(node,attribute,pnl):
19     if not attribute in node.attrib:
20         return
21     pnl.append("%s = ?" % attribute)
22     val.append(node.attrib[attribute])
23     node.attrib.pop(attribute)
24
25 def extract_element(node,chld,pnl):
26     # deal with <tag />
27     if chld.text == None:
28         if len(chld.attrib)==0:
29             node.remove(chld)
30         return
31
32     a=chld.text.strip()
33     if not a == "":
34         pnl.append("%s =?" % chld.tag)
35         val.append(chld.text.strip())
36     else:
37         pnl.append("%s IS NULL OR %s = ''" % (chld.tag,chld.tag))
38     node.remove(chld)
39
40 def process_memberdef(node):
41     q=[]
42     for chld in node.getchildren():
43         if chld.tag == "referencedby":
44             continue
45         if chld.tag == "references":
46             continue
47         if chld.tag == "param":
48             continue
49         if chld.tag == "type":
50             continue
51         if chld.tag == "location":
52             extract_attribute(chld,"line",q)
53             extract_attribute(chld,"column",q)
54             extract_attribute(chld,"bodystart",q)
55             extract_attribute(chld,"bodyend",q)
56
57             q.append("id_bodyfile=(select id from files where name=?)")
58             val.append(chld.attrib["bodyfile"])
59             chld.attrib.pop("bodyfile")
60
61             q.append("id_file=(select id from files where name=?)")
62             val.append(chld.attrib["file"])
63             chld.attrib.pop("file")
64
65             print_unprocessed_attributes(chld)
66             if len(chld.attrib) == 0:
67                 node.remove(chld)
68         else:
69             extract_element(node,chld,q)
70
71     for chld in node.getchildren():
72         print("WARNING: '%s' has unprocessed child elem '%s'" % (node.tag,chld.tag))
73
74     extract_attribute(node,"kind",q)
75     extract_attribute(node,"prot",q)
76     extract_attribute(node,"static",q)
77     extract_attribute(node,"mutable",q)
78     extract_attribute(node,"const",q)
79     extract_attribute(node,"virt",q)
80     extract_attribute(node,"explicit",q)
81     extract_attribute(node,"inline",q)
82
83     q.append("refid=?")
84     val.append(node.attrib['id'])
85     node.attrib.pop('id')
86
87     print_unprocessed_attributes(node)
88
89     query="SELECT * FROM memberdef WHERE %s" % " AND ".join(q)
90     r=[]
91     try:
92         r = g_conn.execute(query,val).fetchall()
93     except(sqlite3.OperationalError,e):
94         print("SQL_ERROR:%s"%e)
95
96     del val[:]
97     if not len(r) > 0:
98         print("TEST_ERROR: Member not found in SQL DB")
99
100
101 def load_xml(name):
102     context = ET.iterparse(name, events=("start", "end"))
103     event, root = context.next()
104     for event, elem in context:
105         if event == "end" and elem.tag == "memberdef":
106             process_memberdef(elem)
107     print("\n== Unprocessed XML ==")
108 #    ET.dump(root)
109
110
111 def open_db(dbname):
112     global g_conn
113
114     if dbname == None:
115         dbname = "doxygen_sqlite3.db"
116
117     if not os.path.isfile(dbname):
118         raise BaseException("No such file %s" % dbname )
119
120     g_conn = sqlite3.connect(dbname)
121     g_conn.execute('PRAGMA temp_store = MEMORY;')
122     g_conn.row_factory = sqlite3.Row
123
124 def main(argv):
125     try:
126         opts, args = getopt.getopt(argv, "hd:x:",["help"])
127     except getopt.GetoptError:
128         sys.exit(1)
129
130     dbname=None
131     xmlfile=None
132
133     for a, o in opts:
134         if a in ('-h', '--help'):
135             sys.exit(0)
136         elif a in ('-d'):
137             dbname=o
138             continue
139         elif a in ('-x'):
140             xmlfile=o
141             continue
142     open_db(dbname)
143     load_xml(xmlfile)
144
145 if __name__ == '__main__':
146     main(sys.argv[1:])