Blame doc/examples/index.py

Packit Service a31ea6
#!/usr/bin/python -u
Packit Service a31ea6
#
Packit Service a31ea6
# Indexes the examples and build an XML description
Packit Service a31ea6
#
Packit Service a31ea6
import string
Packit Service a31ea6
import glob
Packit Service a31ea6
import sys
Packit Service a31ea6
try:
Packit Service a31ea6
    import libxml2
Packit Service a31ea6
except:
Packit Service a31ea6
    sys.exit(1)
Packit Service a31ea6
sys.path.insert(0, "..")
Packit Service a31ea6
from apibuild import CParser, escape
Packit Service a31ea6
Packit Service a31ea6
examples = []
Packit Service a31ea6
extras = ['examples.xsl', 'index.html', 'index.py']
Packit Service a31ea6
tests = []
Packit Service a31ea6
sections = {}
Packit Service a31ea6
symbols = {}
Packit Service a31ea6
api_dict = None
Packit Service a31ea6
api_doc = None
Packit Service a31ea6
Packit Service a31ea6
def load_api():
Packit Service a31ea6
    global api_dict
Packit Service a31ea6
    global api_doc
Packit Service a31ea6
Packit Service a31ea6
    if api_dict != None:
Packit Service a31ea6
        return
Packit Service a31ea6
    api_dict = {}
Packit Service a31ea6
    try:
Packit Service a31ea6
        print "loading ../libxml2-api.xml"
Packit Service a31ea6
        api_doc = libxml2.parseFile("../libxml2-api.xml")
Packit Service a31ea6
    except:
Packit Service a31ea6
        print "failed to parse ../libxml2-api.xml"
Packit Service a31ea6
	sys.exit(1)
Packit Service a31ea6
Packit Service a31ea6
def find_symbol(name):
Packit Service a31ea6
    global api_dict
Packit Service a31ea6
    global api_doc
Packit Service a31ea6
Packit Service a31ea6
    if api_doc == None:
Packit Service a31ea6
        load_api()
Packit Service a31ea6
Packit Service a31ea6
    if name == None:
Packit Service a31ea6
        return
Packit Service a31ea6
    if api_dict.has_key(name):
Packit Service a31ea6
        return api_dict[name]
Packit Service a31ea6
    ctxt = api_doc.xpathNewContext()
Packit Service a31ea6
    res = ctxt.xpathEval("/api/symbols/*[@name = '%s']" % (name))
Packit Service a31ea6
    if type(res) == type([]) and len(res) >= 1:
Packit Service a31ea6
        if len(res) > 1:
Packit Service a31ea6
	    print "Found %d references to %s in the API" % (len(res), name)
Packit Service a31ea6
	node = res[0]
Packit Service a31ea6
	typ = node.name
Packit Service a31ea6
	file = node.xpathEval("string(@file)")
Packit Service a31ea6
	info = node.xpathEval("string(info)")
Packit Service a31ea6
    else:
Packit Service a31ea6
        print "Reference %s not found in the API" % (name)
Packit Service a31ea6
	return None
Packit Service a31ea6
    ret = (typ, file, info)
Packit Service a31ea6
    api_dict[name] = ret
Packit Service a31ea6
    return ret
Packit Service a31ea6
Packit Service a31ea6
def parse_top_comment(filename, comment):
Packit Service a31ea6
    res = {}
Packit Service a31ea6
    lines = string.split(comment, "\n")
Packit Service a31ea6
    item = None
Packit Service a31ea6
    for line in lines:
Packit Service a31ea6
        while line != "" and (line[0] == ' ' or line[0] == '\t'):
Packit Service a31ea6
	    line = line[1:]
Packit Service a31ea6
        while line != "" and line[0] == '*':
Packit Service a31ea6
	    line = line[1:]
Packit Service a31ea6
        while line != "" and (line[0] == ' ' or line[0] == '\t'):
Packit Service a31ea6
	    line = line[1:]
Packit Service a31ea6
	try:
Packit Service a31ea6
	    (it, line) = string.split(line, ":", 1)
Packit Service a31ea6
	    item = it
Packit Service a31ea6
	    while line != "" and (line[0] == ' ' or line[0] == '\t'):
Packit Service a31ea6
		line = line[1:]
Packit Service a31ea6
	    if res.has_key(item):
Packit Service a31ea6
	        res[item] = res[item] + " " + line
Packit Service a31ea6
	    else:
Packit Service a31ea6
		res[item] = line
Packit Service a31ea6
	except:
Packit Service a31ea6
	    if item != None:
Packit Service a31ea6
	        if res.has_key(item):
Packit Service a31ea6
		    res[item] = res[item] + " " + line
Packit Service a31ea6
		else:
Packit Service a31ea6
		    res[item] = line
Packit Service a31ea6
    return res
Packit Service a31ea6
Packit Service a31ea6
def parse(filename, output):
Packit Service a31ea6
    global symbols
Packit Service a31ea6
    global sections
Packit Service a31ea6
Packit Service a31ea6
    parser = CParser(filename)
Packit Service a31ea6
    parser.collect_references()
Packit Service a31ea6
    idx = parser.parse()
Packit Service a31ea6
    info = parse_top_comment(filename, parser.top_comment)
Packit Service a31ea6
    output.write("  <example filename='%s'>\n" % filename)
Packit Service a31ea6
    try:
Packit Service a31ea6
        synopsis = info['synopsis']
Packit Service a31ea6
	output.write("    <synopsis>%s</synopsis>\n" % escape(synopsis));
Packit Service a31ea6
    except:
Packit Service a31ea6
        print "Example %s lacks a synopsis description" % (filename)
Packit Service a31ea6
    try:
Packit Service a31ea6
        purpose = info['purpose']
Packit Service a31ea6
	output.write("    <purpose>%s</purpose>\n" % escape(purpose));
Packit Service a31ea6
    except:
Packit Service a31ea6
        print "Example %s lacks a purpose description" % (filename)
Packit Service a31ea6
    try:
Packit Service a31ea6
        usage = info['usage']
Packit Service a31ea6
	output.write("    <usage>%s</usage>\n" % escape(usage));
Packit Service a31ea6
    except:
Packit Service a31ea6
        print "Example %s lacks an usage description" % (filename)
Packit Service a31ea6
    try:
Packit Service a31ea6
        test = info['test']
Packit Service a31ea6
	output.write("    <test>%s</test>\n" % escape(test));
Packit Service a31ea6
	progname=filename[0:-2]
Packit Service a31ea6
	command=string.replace(test, progname, './' + progname, 1)
Packit Service a31ea6
	tests.append(command)
Packit Service a31ea6
    except:
Packit Service a31ea6
        pass
Packit Service a31ea6
    try:
Packit Service a31ea6
        author = info['author']
Packit Service a31ea6
	output.write("    <author>%s</author>\n" % escape(author));
Packit Service a31ea6
    except:
Packit Service a31ea6
        print "Example %s lacks an author description" % (filename)
Packit Service a31ea6
    try:
Packit Service a31ea6
        copy = info['copy']
Packit Service a31ea6
	output.write("    <copy>%s</copy>\n" % escape(copy));
Packit Service a31ea6
    except:
Packit Service a31ea6
        print "Example %s lacks a copyright description" % (filename)
Packit Service a31ea6
    try:
Packit Service a31ea6
        section = info['section']
Packit Service a31ea6
	output.write("    <section>%s</section>\n" % escape(section));
Packit Service a31ea6
	if sections.has_key(section):
Packit Service a31ea6
	    sections[section].append(filename)
Packit Service a31ea6
	else:
Packit Service a31ea6
	    sections[section] = [filename]
Packit Service a31ea6
    except:
Packit Service a31ea6
        print "Example %s lacks a section description" % (filename)
Packit Service a31ea6
    for topic in info.keys():
Packit Service a31ea6
        if topic != "purpose" and topic != "usage" and \
Packit Service a31ea6
	   topic != "author" and topic != "copy" and \
Packit Service a31ea6
	   topic != "section" and topic != "synopsis" and topic != "test":
Packit Service a31ea6
	    str = info[topic]
Packit Service a31ea6
	    output.write("    <extra topic='%s'>%s</extra>\n" % (
Packit Service a31ea6
	                 escape(topic), escape(str)))
Packit Service a31ea6
    output.write("    <includes>\n")
Packit Service a31ea6
    for include in idx.includes.keys():
Packit Service a31ea6
        if include.find("libxml") != -1:
Packit Service a31ea6
	    output.write("      <include>%s</include>\n" % (escape(include)))
Packit Service a31ea6
    output.write("    </includes>\n")
Packit Service a31ea6
    output.write("    <uses>\n")
Packit Service a31ea6
    for ref in idx.references.keys():
Packit Service a31ea6
        id = idx.references[ref]
Packit Service a31ea6
	name = id.get_name()
Packit Service a31ea6
	line = id.get_lineno()
Packit Service a31ea6
	if symbols.has_key(name):
Packit Service a31ea6
	    sinfo = symbols[name]
Packit Service a31ea6
	    refs = sinfo[0]
Packit Service a31ea6
	    # gather at most 5 references per symbols
Packit Service a31ea6
	    if refs > 5:
Packit Service a31ea6
	        continue
Packit Service a31ea6
	    sinfo.append(filename)
Packit Service a31ea6
	    sinfo[0] = refs + 1
Packit Service a31ea6
	else:
Packit Service a31ea6
	    symbols[name] = [1, filename]
Packit Service a31ea6
	info = find_symbol(name)
Packit Service a31ea6
	if info != None:
Packit Service a31ea6
	    type = info[0]
Packit Service a31ea6
	    file = info[1]
Packit Service a31ea6
	    output.write("      <%s line='%d' file='%s' name='%s'/>\n" % (type,
Packit Service a31ea6
	                 line, file, name))
Packit Service a31ea6
	else:
Packit Service a31ea6
	    type = id.get_type()
Packit Service a31ea6
	    output.write("      <%s line='%d' name='%s'/>\n" % (type,
Packit Service a31ea6
	                 line, name))
Packit Service a31ea6
Packit Service a31ea6
    output.write("    </uses>\n")
Packit Service a31ea6
    output.write("  </example>\n")
Packit Service a31ea6
Packit Service a31ea6
    return idx
Packit Service a31ea6
Packit Service a31ea6
def dump_symbols(output):
Packit Service a31ea6
    global symbols
Packit Service a31ea6
Packit Service a31ea6
    output.write("  <symbols>\n")
Packit Service a31ea6
    keys = symbols.keys()
Packit Service a31ea6
    keys.sort()
Packit Service a31ea6
    for symbol in keys:
Packit Service a31ea6
        output.write("    <symbol name='%s'>\n" % (symbol))
Packit Service a31ea6
	info = symbols[symbol]
Packit Service a31ea6
	i = 1
Packit Service a31ea6
	while i < len(info):
Packit Service a31ea6
	    output.write("      <ref filename='%s'/>\n" % (info[i]))
Packit Service a31ea6
	    i = i + 1
Packit Service a31ea6
        output.write("    </symbol>\n")
Packit Service a31ea6
    output.write("  </symbols>\n")
Packit Service a31ea6
Packit Service a31ea6
def dump_sections(output):
Packit Service a31ea6
    global sections
Packit Service a31ea6
Packit Service a31ea6
    output.write("  <sections>\n")
Packit Service a31ea6
    keys = sections.keys()
Packit Service a31ea6
    keys.sort()
Packit Service a31ea6
    for section in keys:
Packit Service a31ea6
        output.write("    <section name='%s'>\n" % (section))
Packit Service a31ea6
	info = sections[section]
Packit Service a31ea6
	i = 0
Packit Service a31ea6
	while i < len(info):
Packit Service a31ea6
	    output.write("      <example filename='%s'/>\n" % (info[i]))
Packit Service a31ea6
	    i = i + 1
Packit Service a31ea6
        output.write("    </section>\n")
Packit Service a31ea6
    output.write("  </sections>\n")
Packit Service a31ea6
Packit Service a31ea6
def dump_Makefile():
Packit Service a31ea6
    for file in glob.glob('*.xml'):
Packit Service a31ea6
        extras.append(file)
Packit Service a31ea6
    for file in glob.glob('*.res'):
Packit Service a31ea6
        extras.append(file)
Packit Service a31ea6
    Makefile="""##
Packit Service a31ea6
## This file is auto-generated by index.py
Packit Service a31ea6
## DO NOT EDIT !!!
Packit Service a31ea6
##
Packit Service a31ea6
Packit Service a31ea6
AM_CPPFLAGS = -I$(top_builddir)/include -I$(top_srcdir)/include -I$(srcdir)/include
Packit Service a31ea6
AM_CFLAGS = $(THREAD_CFLAGS) $(Z_CFLAGS)
Packit Service a31ea6
LDADD = $(RDL_LIBS) $(STATIC_BINARIES) $(top_builddir)/libxml2.la $(THREAD_LIBS) $(Z_LIBS) $(ICONV_LIBS) -lm $(WIN32_EXTRA_LIBADD)
Packit Service a31ea6
Packit Service a31ea6
CLEANFILES = *.tmp
Packit Service a31ea6
Packit Service a31ea6
if REBUILD_DOCS
Packit Service a31ea6
rebuild: examples.xml index.html
Packit Service a31ea6
.PHONY: rebuild
Packit Service a31ea6
Packit Service a31ea6
examples.xml: index.py $(check_PROGRAMS:=.c)
Packit Service a31ea6
	cd $(srcdir) && $(PYTHON) index.py
Packit Service a31ea6
	$(MAKE) Makefile
Packit Service a31ea6
Packit Service a31ea6
index.html: examples.xml examples.xsl
Packit Service a31ea6
	cd $(srcdir) && xsltproc examples.xsl examples.xml && echo "Rebuilt web page"
Packit Service a31ea6
	-cd $(srcdir) && xmllint --valid --noout index.html
Packit Service a31ea6
endif
Packit Service a31ea6
Packit Service a31ea6
install-data-local: 
Packit Service a31ea6
	$(MKDIR_P) $(DESTDIR)$(HTML_DIR)
Packit Service a31ea6
	-$(INSTALL) -m 0644 $(srcdir)/*.html $(srcdir)/*.c $(srcdir)/*.xml $(srcdir)/*.xsl $(srcdir)/*.res $(DESTDIR)$(HTML_DIR)
Packit Service a31ea6
Packit Service a31ea6
clean-local:
Packit Service a31ea6
	test -f Makefile.am || rm -f test?.xml
Packit Service a31ea6
Packit Service a31ea6
"""
Packit Service a31ea6
    examples.sort()
Packit Service a31ea6
    extras.sort()
Packit Service a31ea6
    tests.sort()
Packit Service a31ea6
    EXTRA_DIST=""
Packit Service a31ea6
    for extra in extras:
Packit Service a31ea6
        EXTRA_DIST = EXTRA_DIST + " \\\n\t" + extra
Packit Service a31ea6
    Makefile = Makefile + "EXTRA_DIST =%s\n\n" % (EXTRA_DIST)
Packit Service a31ea6
    check_PROGRAMS=""
Packit Service a31ea6
    for example in examples:
Packit Service a31ea6
        check_PROGRAMS = check_PROGRAMS + " \\\n\t" + example
Packit Service a31ea6
    Makefile = Makefile + "check_PROGRAMS =%s\n\n" % (check_PROGRAMS)
Packit Service a31ea6
    for example in examples:
Packit Service a31ea6
        Makefile = Makefile + "%s_SOURCES = %s.c\n\n" % (example, example)
Packit Service a31ea6
    Makefile = Makefile + "valgrind: \n\t$(MAKE) CHECKER='valgrind' tests\n\n"
Packit Service a31ea6
    Makefile = Makefile + "tests: $(check_PROGRAMS)\n"
Packit Service a31ea6
    Makefile = Makefile + "\t@test -f Makefile.am || test -f test1.xml || $(LN_S) $(srcdir)/test?.xml .\n"
Packit Service a31ea6
    Makefile = Makefile + "\t@(echo '## examples regression tests')\n"
Packit Service a31ea6
    Makefile = Makefile + "\t@(echo > .memdump)\n"
Packit Service a31ea6
    for test in tests:
Packit Service a31ea6
        Makefile = Makefile + "\t@$(CHECKER) %s\n" % (test)
Packit Service a31ea6
        Makefile = Makefile + '\t@grep "MORY ALLO" .memdump | grep -v "MEMORY ALLOCATED : 0" ; exit 0\n'
Packit Service a31ea6
    Makefile = Makefile + "\t@rm *.tmp\n"
Packit Service a31ea6
    try:
Packit Service a31ea6
	old = open("Makefile.am", "r").read()
Packit Service a31ea6
	if old != Makefile:
Packit Service a31ea6
	    n = open("Makefile.am", "w").write(Makefile)
Packit Service a31ea6
	    print "Updated Makefile.am"
Packit Service a31ea6
    except:
Packit Service a31ea6
        print "Failed to read or save Makefile.am"
Packit Service a31ea6
#    #
Packit Service a31ea6
#    # Autogenerate the .cvsignore too ... DEPRECATED
Packit Service a31ea6
#    #
Packit Service a31ea6
#    ignore = """.memdump
Packit Service a31ea6
#Makefile.in
Packit Service a31ea6
#Makefile
Packit Service a31ea6
#"""
Packit Service a31ea6
#    for example in examples:
Packit Service a31ea6
#        ignore = ignore + "%s\n" % (example)
Packit Service a31ea6
#    try:
Packit Service a31ea6
#	old = open(".cvsignore", "r").read()
Packit Service a31ea6
#	if old != ignore:
Packit Service a31ea6
#	    n = open(".cvsignore", "w").write(ignore)
Packit Service a31ea6
#	    print "Updated .cvsignore"
Packit Service a31ea6
#    except:
Packit Service a31ea6
#        print "Failed to read or save .cvsignore"
Packit Service a31ea6
Packit Service a31ea6
if __name__ == "__main__":
Packit Service a31ea6
    load_api()
Packit Service a31ea6
    output = open("examples.xml", "w")
Packit Service a31ea6
    output.write("<examples>\n")
Packit Service a31ea6
Packit Service a31ea6
    for file in sorted(glob.glob('*.c')):
Packit Service a31ea6
	parse(file, output)
Packit Service a31ea6
	examples.append(file[:-2])
Packit Service a31ea6
Packit Service a31ea6
    dump_symbols(output)
Packit Service a31ea6
    dump_sections(output)
Packit Service a31ea6
    output.write("</examples>\n")
Packit Service a31ea6
    output.close()
Packit Service a31ea6
    dump_Makefile()
Packit Service a31ea6