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.html', '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'):
224 ## This file is auto-generated by index.py
228 AM_CPPFLAGS = -I$(top_builddir)/include -I$(top_srcdir)/include -I$(srcdir)/include
229 AM_CFLAGS = $(THREAD_CFLAGS) $(Z_CFLAGS)
230 LDADD = $(RDL_LIBS) $(STATIC_BINARIES) $(top_builddir)/libxml2.la $(THREAD_LIBS) $(Z_LIBS) $(ICONV_LIBS) -lm $(WIN32_EXTRA_LIBADD)
235 rebuild: examples.xml index.html
238 examples.xml: index.py $(noinst_PROGRAMS:=.c)
239 cd $(srcdir) && $(PYTHON) index.py
242 index.html: examples.xml examples.xsl
243 cd $(srcdir) && xsltproc examples.xsl examples.xml && echo "Rebuilt web page"
244 -cd $(srcdir) && xmllint --valid --noout index.html
248 $(MKDIR_P) $(DESTDIR)$(HTML_DIR)
249 -$(INSTALL) -m 0644 $(srcdir)/*.html $(srcdir)/*.c $(srcdir)/*.xml $(srcdir)/*.xsl $(srcdir)/*.res $(DESTDIR)$(HTML_DIR)
252 test -f Makefile.am || rm -f test?.xml
260 EXTRA_DIST = EXTRA_DIST + " \\\n\t" + extra
261 Makefile = Makefile + "EXTRA_DIST =%s\n\n" % (EXTRA_DIST)
263 for example in examples:
264 noinst_PROGRAMS = noinst_PROGRAMS + " \\\n\t" + example
265 Makefile = Makefile + "noinst_PROGRAMS =%s\n\n" % (noinst_PROGRAMS)
266 for example in examples:
267 Makefile = Makefile + "%s_SOURCES = %s.c\n\n" % (example, example)
268 Makefile = Makefile + "valgrind: \n\t$(MAKE) CHECKER='valgrind' tests\n\n"
269 Makefile = Makefile + "tests: $(noinst_PROGRAMS)\n"
270 Makefile = Makefile + "\ttest -f Makefile.am || test -f test1.xml || $(LN_S) $(srcdir)/test?.xml .\n"
271 Makefile = Makefile + "\t@(echo '## examples regression tests')\n"
272 Makefile = Makefile + "\t@(echo > .memdump)\n"
274 Makefile = Makefile + "\t$(CHECKER) %s\n" % (test)
275 Makefile = Makefile + '\t@grep "MORY ALLO" .memdump | grep -v "MEMORY ALLOCATED : 0" ; exit 0\n'
277 old = open("Makefile.am", "r").read()
279 n = open("Makefile.am", "w").write(Makefile)
280 print "Updated Makefile.am"
282 print "Failed to read or save Makefile.am"
284 # # Autogenerate the .cvsignore too ... DEPRECATED
286 # ignore = """.memdump
290 # for example in examples:
291 # ignore = ignore + "%s\n" % (example)
293 # old = open(".cvsignore", "r").read()
295 # n = open(".cvsignore", "w").write(ignore)
296 # print "Updated .cvsignore"
298 # print "Failed to read or save .cvsignore"
300 if __name__ == "__main__":
302 output = open("examples.xml", "w")
303 output.write("<examples>\n")
305 for file in glob.glob('*.c'):
307 examples.append(file[:-2])
310 dump_sections(output)
311 output.write("</examples>\n")