3 # Indexes the examples and build an XML description
12 sys.path.insert(0, "..")
13 from apibuild import CParser, escape
16 extras = ['examples.xsl', 'index.py']
31 print "loading ../libxml2-api.xml"
32 api_doc = libxml2.parseFile("../libxml2-api.xml")
34 print "failed to parse ../libxml2-api.xml"
37 def find_symbol(name):
46 if api_dict.has_key(name):
48 ctxt = api_doc.xpathNewContext()
49 res = ctxt.xpathEval("/api/symbols/*[@name = '%s']" % (name))
50 if type(res) == type([]) and len(res) >= 1:
52 print "Found %d references to %s in the API" % (len(res), name)
55 file = node.xpathEval("string(@file)")
56 info = node.xpathEval("string(info)")
58 print "Reference %s not found in the API" % (name)
60 ret = (typ, file, info)
64 def parse_top_comment(filename, comment):
66 lines = string.split(comment, "\n")
69 while line != "" and (line[0] == ' ' or line[0] == '\t'):
71 while line != "" and line[0] == '*':
73 while line != "" and (line[0] == ' ' or line[0] == '\t'):
76 (it, line) = string.split(line, ":", 1)
78 while line != "" and (line[0] == ' ' or line[0] == '\t'):
81 res[item] = res[item] + " " + line
87 res[item] = res[item] + " " + line
92 def parse(filename, output):
96 parser = CParser(filename)
97 parser.collect_references()
99 info = parse_top_comment(filename, parser.top_comment)
100 output.write(" <example filename='%s'>\n" % filename)
102 synopsis = info['synopsis']
103 output.write(" <synopsis>%s</synopsis>\n" % escape(synopsis));
105 print "Example %s lacks a synopsis description" % (filename)
107 purpose = info['purpose']
108 output.write(" <purpose>%s</purpose>\n" % escape(purpose));
110 print "Example %s lacks a purpose description" % (filename)
112 usage = info['usage']
113 output.write(" <usage>%s</usage>\n" % escape(usage));
115 print "Example %s lacks an usage description" % (filename)
118 output.write(" <test>%s</test>\n" % escape(test));
119 progname=filename[0:-2]
120 command=string.replace(test, progname, './' + progname, 1)
121 tests.append(command)
125 author = info['author']
126 output.write(" <author>%s</author>\n" % escape(author));
128 print "Example %s lacks an author description" % (filename)
131 output.write(" <copy>%s</copy>\n" % escape(copy));
133 print "Example %s lacks a copyright description" % (filename)
135 section = info['section']
136 output.write(" <section>%s</section>\n" % escape(section));
137 if sections.has_key(section):
138 sections[section].append(filename)
140 sections[section] = [filename]
142 print "Example %s lacks a section description" % (filename)
143 for topic in info.keys():
144 if topic != "purpose" and topic != "usage" and \
145 topic != "author" and topic != "copy" and \
146 topic != "section" and topic != "synopsis" and topic != "test":
148 output.write(" <extra topic='%s'>%s</extra>\n" % (
149 escape(topic), escape(str)))
150 output.write(" <includes>\n")
151 for include in idx.includes.keys():
152 if include.find("libxml") != -1:
153 output.write(" <include>%s</include>\n" % (escape(include)))
154 output.write(" </includes>\n")
155 output.write(" <uses>\n")
156 for ref in idx.references.keys():
157 id = idx.references[ref]
159 line = id.get_lineno()
160 if symbols.has_key(name):
161 sinfo = symbols[name]
163 # gather at most 5 references per symbols
166 sinfo.append(filename)
169 symbols[name] = [1, filename]
170 info = find_symbol(name)
174 output.write(" <%s line='%d' file='%s' name='%s'/>\n" % (type,
178 output.write(" <%s line='%d' name='%s'/>\n" % (type,
181 output.write(" </uses>\n")
182 output.write(" </example>\n")
186 def dump_symbols(output):
189 output.write(" <symbols>\n")
190 keys = symbols.keys()
193 output.write(" <symbol name='%s'>\n" % (symbol))
194 info = symbols[symbol]
197 output.write(" <ref filename='%s'/>\n" % (info[i]))
199 output.write(" </symbol>\n")
200 output.write(" </symbols>\n")
202 def dump_sections(output):
205 output.write(" <sections>\n")
206 keys = sections.keys()
209 output.write(" <section name='%s'>\n" % (section))
210 info = sections[section]
213 output.write(" <example filename='%s'/>\n" % (info[i]))
215 output.write(" </section>\n")
216 output.write(" </sections>\n")
219 for file in glob.glob('*.xml'):
221 for file in glob.glob('*.res'):
223 Makefile="""# Beware this is autogenerated by index.py
224 INCLUDES = -I$(top_builddir)/include -I$(top_srcdir)/include -I@srcdir@/include @THREAD_CFLAGS@ @Z_CFLAGS@
225 DEPS = $(top_builddir)/libxml2.la
226 LDADDS = @STATIC_BINARIES@ $(top_builddir)/libxml2.la @THREAD_LIBS@ @Z_LIBS@ $(ICONV_LIBS) -lm @WIN32_EXTRA_LIBADD@
228 rebuild: examples.xml index.html
230 examples.xml: index.py *.c
231 -@($(srcdir)/index.py)
233 index.html: examples.xml examples.xsl
234 -@(xsltproc examples.xsl examples.xml && echo "Rebuilt web page" && xmllint --valid --noout index.html)
237 $(mkinstalldirs) $(DESTDIR)$(HTML_DIR)
238 -@INSTALL@ -m 0644 $(srcdir)/*.html $(srcdir)/*.c $(srcdir)/*.xml $(srcdir)/*.xsl $(srcdir)/*.res $(DESTDIR)$(HTML_DIR)
243 EXTRA_DIST = EXTRA_DIST + extra + " "
244 Makefile = Makefile + "EXTRA_DIST=%s\n\n" % (EXTRA_DIST)
246 for example in examples:
247 noinst_PROGRAMS = noinst_PROGRAMS + example + " "
248 Makefile = Makefile + "noinst_PROGRAMS=%s\n\n" % (noinst_PROGRAMS)
249 for example in examples:
250 Makefile = Makefile + "%s_SOURCES=%s.c\n%s_LDFLAGS=\n%s_DEPENDENCIES= $(DEPS)\n%s_LDADD= @RDL_LIBS@ $(LDADDS)\n\n" % (example, example, example,
252 Makefile = Makefile + "valgrind: \n\t$(MAKE) CHECKER='valgrind' tests\n\n"
253 Makefile = Makefile + "tests: $(noinst_PROGRAMS)\n"
254 Makefile = Makefile + "\t@(echo '## examples regression tests')\n"
255 Makefile = Makefile + "\t@(echo > .memdump)\n"
257 Makefile = Makefile + "\t@($(CHECKER) %s)\n" % (test)
258 Makefile = Makefile + '\t@(grep "MORY ALLO" .memdump | grep -v "MEMORY ALLOCATED : 0" ; exit 0)\n'
259 Makefile = Makefile + "\n\n"
261 old = open("Makefile.am", "r").read()
263 n = open("Makefile.am", "w").write(Makefile)
264 print "Updated Makefile.am"
266 print "Failed to read or save Makefile.am"
268 # Autogenerate the .cvsignore too ...
274 for example in examples:
275 ignore = ignore + "%s\n" % (example)
277 old = open(".cvsignore", "r").read()
279 n = open(".cvsignore", "w").write(ignore)
280 print "Updated .cvsignore"
282 print "Failed to read or save .cvsignore"
284 if __name__ == "__main__":
286 output = open("examples.xml", "w")
287 output.write("<examples>\n")
289 for file in glob.glob('*.c'):
291 examples.append(file[:-2])
294 dump_sections(output)
295 output.write("</examples>\n")