index.py revision 4773df2a58be83e1b9b1b55840371acf37386820
1#!/usr/bin/python -u
2#
3# Indexes the examples and build an XML description
4#
5import string
6import glob
7import sys
8try:
9    import libxml2
10except:
11    sys.exit(1)
12sys.path.append("..")
13from apibuild import CParser, escape
14
15examples = []
16extras = ['examples.xsl', 'index.py']
17tests = []
18sections = {}
19symbols = {}
20api_dict = None
21api_doc = None
22
23def load_api():
24    global api_dict
25    global api_doc
26
27    if api_dict != None:
28        return
29    api_dict = {}
30    try:
31        print "loading ../libxml2-api.xml"
32        api_doc = libxml2.parseFile("../libxml2-api.xml")
33    except:
34        print "failed to parse ../libxml2-api.xml"
35	sys.exit(1)
36
37def find_symbol(name):
38    global api_dict
39    global api_doc
40
41    if api_doc == None:
42        load_api()
43
44    if name == None:
45        return
46    if api_dict.has_key(name):
47        return api_dict[name]
48    ctxt = api_doc.xpathNewContext()
49    res = ctxt.xpathEval("/api/symbols/*[@name = '%s']" % (name))
50    if type(res) == type([]) and len(res) >= 1:
51        if len(res) > 1:
52	    print "Found %d references to %s in the API" % (len(res), name)
53	node = res[0]
54	typ = node.name
55	file = node.xpathEval("string(@file)")
56	info = node.xpathEval("string(info)")
57    else:
58        print "Reference %s not found in the API" % (name)
59	return None
60    ret = (typ, file, info)
61    api_dict[name] = ret
62    return ret
63
64def parse_top_comment(filename, comment):
65    res = {}
66    lines = string.split(comment, "\n")
67    item = None
68    for line in lines:
69        while line != "" and (line[0] == ' ' or line[0] == '\t'):
70	    line = line[1:]
71        while line != "" and line[0] == '*':
72	    line = line[1:]
73        while line != "" and (line[0] == ' ' or line[0] == '\t'):
74	    line = line[1:]
75	try:
76	    (it, line) = string.split(line, ":", 1)
77	    item = it
78	    while line != "" and (line[0] == ' ' or line[0] == '\t'):
79		line = line[1:]
80	    if res.has_key(item):
81	        res[item] = res[item] + " " + line
82	    else:
83		res[item] = line
84	except:
85	    if item != None:
86	        if res.has_key(item):
87		    res[item] = res[item] + " " + line
88		else:
89		    res[item] = line
90    return res
91
92def parse(filename, output):
93    global symbols
94    global sections
95
96    parser = CParser(filename)
97    parser.collect_references()
98    idx = parser.parse()
99    info = parse_top_comment(filename, parser.top_comment)
100    output.write("  <example filename='%s'>\n" % filename)
101    try:
102        synopsis = info['synopsis']
103	output.write("    <synopsis>%s</synopsis>\n" % escape(synopsis));
104    except:
105        print "Example %s lacks a synopsis description" % (filename)
106    try:
107        purpose = info['purpose']
108	output.write("    <purpose>%s</purpose>\n" % escape(purpose));
109    except:
110        print "Example %s lacks a purpose description" % (filename)
111    try:
112        usage = info['usage']
113	output.write("    <usage>%s</usage>\n" % escape(usage));
114    except:
115        print "Example %s lacks an usage description" % (filename)
116    try:
117        test = info['test']
118	output.write("    <test>%s</test>\n" % escape(test));
119	tests.append(test)
120    except:
121        pass
122    try:
123        author = info['author']
124	output.write("    <author>%s</author>\n" % escape(author));
125    except:
126        print "Example %s lacks an author description" % (filename)
127    try:
128        copy = info['copy']
129	output.write("    <copy>%s</copy>\n" % escape(copy));
130    except:
131        print "Example %s lacks a copyright description" % (filename)
132    try:
133        section = info['section']
134	output.write("    <section>%s</section>\n" % escape(section));
135	if sections.has_key(section):
136	    sections[section].append(filename)
137	else:
138	    sections[section] = [filename]
139    except:
140        print "Example %s lacks a section description" % (filename)
141    for topic in info.keys():
142        if topic != "purpose" and topic != "usage" and \
143	   topic != "author" and topic != "copy" and \
144	   topic != "section" and topic != "synopsis" and topic != "test":
145	    str = info[topic]
146	    output.write("    <extra topic='%s'>%s</extra>\n" % (
147	                 escape(topic), escape(str)))
148    output.write("    <includes>\n")
149    for include in idx.includes.keys():
150        if include.find("libxml") != -1:
151	    output.write("      <include>%s</include>\n" % (escape(include)))
152    output.write("    </includes>\n")
153    output.write("    <uses>\n")
154    for ref in idx.references.keys():
155        id = idx.references[ref]
156	name = id.get_name()
157	line = id.get_lineno()
158	if symbols.has_key(name):
159	    sinfo = symbols[name]
160	    refs = sinfo[0]
161	    # gather at most 5 references per symbols
162	    if refs > 5:
163	        continue
164	    sinfo.append(filename)
165	    sinfo[0] = refs + 1
166	else:
167	    symbols[name] = [1, filename]
168	info = find_symbol(name)
169	if info != None:
170	    type = info[0]
171	    file = info[1]
172	    output.write("      <%s line='%d' file='%s' name='%s'/>\n" % (type,
173	                 line, file, name))
174	else:
175	    type = id.get_type()
176	    output.write("      <%s line='%d' name='%s'/>\n" % (type,
177	                 line, name))
178
179    output.write("    </uses>\n")
180    output.write("  </example>\n")
181
182    return idx
183
184def dump_symbols(output):
185    global symbols
186
187    output.write("  <symbols>\n")
188    keys = symbols.keys()
189    keys.sort()
190    for symbol in keys:
191        output.write("    <symbol name='%s'>\n" % (symbol))
192	info = symbols[symbol]
193	i = 1
194	while i < len(info):
195	    output.write("      <ref filename='%s'/>\n" % (info[i]))
196	    i = i + 1
197        output.write("    </symbol>\n")
198    output.write("  </symbols>\n")
199
200def dump_sections(output):
201    global sections
202
203    output.write("  <sections>\n")
204    keys = sections.keys()
205    keys.sort()
206    for section in keys:
207        output.write("    <section name='%s'>\n" % (section))
208	info = sections[section]
209	i = 0
210	while i < len(info):
211	    output.write("      <example filename='%s'/>\n" % (info[i]))
212	    i = i + 1
213        output.write("    </section>\n")
214    output.write("  </sections>\n")
215
216def dump_Makefile():
217    for file in glob.glob('*.xml'):
218        extras.append(file)
219    for file in glob.glob('*.res'):
220        extras.append(file)
221    Makefile="""# Beware this is autogenerated by index.py
222HTML_DIR=$(datadir)/doc
223DOC_MODULE=libxml2-$(VERSION)
224TARGET_DIR=$(HTML_DIR)/$(DOC_MODULE)/examples
225
226INCLUDES = -I$(top_builddir)/include -I$(top_srcdir)/include -I@srcdir@/include @THREAD_CFLAGS@ @Z_CFLAGS@
227DEPS = $(top_builddir)/libxml2.la
228LDADDS = @STATIC_BINARIES@ $(top_builddir)/libxml2.la @THREAD_LIBS@ @Z_LIBS@ $(ICONV_LIBS) -lm @WIN32_EXTRA_LIBADD@
229
230rebuild: examples.xml index.html
231
232examples.xml: index.py *.c
233	-@($(srcdir)/index.py)
234
235index.html: examples.xml examples.xsl
236	-@(xsltproc examples.xsl examples.xml && echo "Rebuilt web page" && xmllint --valid --noout index.html)
237
238install-data-local:
239	$(mkinstalldirs) $(DESTDIR)$(TARGET_DIR)
240	-@INSTALL@ -m 0644 $(srcdir)/*.html $(srcdir)/*.c $(srcdir)/*.xml $(srcdir)/*.xsl $(srcdir)/*.res $(DESTDIR)$(TARGET_DIR)
241
242"""
243    EXTRA_DIST=""
244    for extra in extras:
245        EXTRA_DIST = EXTRA_DIST + extra + " "
246    Makefile = Makefile + "EXTRA_DIST=%s\n\n" % (EXTRA_DIST)
247    noinst_PROGRAMS=""
248    for example in examples:
249        noinst_PROGRAMS = noinst_PROGRAMS + example + " "
250    Makefile = Makefile + "noinst_PROGRAMS=%s\n\n" % (noinst_PROGRAMS)
251    for example in examples:
252        Makefile = Makefile + "%s_SOURCES=%s.c\n%s_LDFLAGS=\n%s_DEPENDENCIES= $(DEPS)\n%s_LDADD= @RDL_LIBS@ $(LDADDS)\n\n" % (example, example, example,
253	       example, example)
254    Makefile = Makefile + "tests: $(noinst_PROGRAMS)\n"
255    Makefile = Makefile + "\t@(echo > .memdump)\n"
256    for test in tests:
257        Makefile = Makefile + "\t@(%s)\n" % (test)
258        Makefile = Makefile + '\t@(grep "MORY ALLO" .memdump  | grep -v "MEMORY ALLOCATED : 0" ; exit 0)\n'
259    Makefile = Makefile + "\n\n"
260    try:
261	old = open("Makefile.am", "r").read()
262	if old != Makefile:
263	    n = open("Makefile.am", "w").write(Makefile)
264	    print "Updated Makefile.am"
265    except:
266        print "Failed to read or save Makefile.am"
267    #
268    # Autogenerate the .cvsignore too ...
269    #
270    ignore = """.memdump
271Makefile.in
272Makefile
273"""
274    for example in examples:
275        ignore = ignore + "%s\n" % (example)
276    try:
277	old = open(".cvsignore", "r").read()
278	if old != ignore:
279	    n = open(".cvsignore", "w").write(ignore)
280	    print "Updated .cvsignore"
281    except:
282        print "Failed to read or save .cvsignore"
283
284if __name__ == "__main__":
285    load_api()
286    output = open("examples.xml", "w")
287    output.write("<examples>\n")
288
289    for file in glob.glob('*.c'):
290	parse(file, output)
291	examples.append(file[:-2])
292
293    dump_symbols(output)
294    dump_sections(output)
295    output.write("</examples>\n")
296    output.close()
297    dump_Makefile()
298
299