Chris PeBenito 89ec23
#!/usr/bin/python
Chris PeBenito 89ec23
Chris PeBenito 89ec23
#  Author: Joshua Brindle <jbrindle@tresys.com>
Chris PeBenito 89ec23
#
Chris PeBenito 89ec23
# Copyright (C) 2003 - 2005 Tresys Technology, LLC
Chris PeBenito 89ec23
#      This program is free software; you can redistribute it and/or modify
Chris PeBenito 89ec23
#      it under the terms of the GNU General Public License as published by
Chris PeBenito 89ec23
#      the Free Software Foundation, version 2.
Chris PeBenito 89ec23
Chris PeBenito 89ec23
"""
Chris PeBenito 89ec23
	this does dstuff
Chris PeBenito 89ec23
"""
Chris PeBenito 89ec23
Chris PeBenito 89ec23
import sys
Chris PeBenito 89ec23
import getopt
Chris PeBenito 89ec23
import pyplate
Chris PeBenito 89ec23
from xml.dom.ext import *
Chris PeBenito 89ec23
from xml.dom.ext.reader import Sax2
Chris PeBenito 89ec23
Chris PeBenito 89ec23
def read_policy_xml(filename):
Chris PeBenito 89ec23
	try:
Chris PeBenito 89ec23
		reader = Sax2.Reader()
Chris PeBenito 89ec23
		doc = reader.fromStream(filename)
Chris PeBenito 89ec23
	except: 
Chris PeBenito 89ec23
		error("Error while parsing xml")
Chris PeBenito 89ec23
	
Chris PeBenito 89ec23
	return doc
Chris PeBenito 89ec23
Chris PeBenito 89ec23
def gen_tunable_conf(doc, file):
Chris PeBenito 89ec23
	for node in doc.getElementsByTagName("tunable"):
Chris PeBenito 89ec23
		s = string.split(node.firstChild.data, "\n")
Chris PeBenito 89ec23
		for line in s:
Chris PeBenito 89ec23
			file.write("# %s\n" % line)
Chris PeBenito 89ec23
		tun_name = tun_val = None
Chris PeBenito 89ec23
        	for (name, value) in node.attributes.items():
Chris PeBenito 89ec23
			if name[1] == "name":
Chris PeBenito 89ec23
				tun_name = value.value
Chris PeBenito 89ec23
			elif name[1] == "dftval":
Chris PeBenito 89ec23
				tun_val = value.value
Chris PeBenito 89ec23
Chris PeBenito 89ec23
			if tun_name and tun_val:
Chris PeBenito 89ec23
	            		file.write("%s = %s\n\n" % (tun_name, tun_val))
Chris PeBenito 89ec23
				tun_name = tun_val = None
Chris PeBenito 89ec23
Chris PeBenito 89ec23
def gen_module_conf(doc, file):
Chris PeBenito 89ec23
	for node in doc.getElementsByTagName("module"):
Chris PeBenito 89ec23
		for desc in node.getElementsByTagName("summary"):
Chris PeBenito 89ec23
			s = string.split(desc.firstChild.data, "\n")
Chris PeBenito 89ec23
			for line in s:
Chris PeBenito 89ec23
				file.write("# %s\n" % line)	
Chris PeBenito 89ec23
			file.write("#\n")
Chris PeBenito 89ec23
			for (name, value) in node.attributes.items():
Chris PeBenito 89ec23
				if name[1] == "name":
Chris PeBenito 89ec23
					file.write("# %s\n\n" % value.value)
Chris PeBenito 89ec23
Chris PeBenito 89ec23
def gen_docs(doc, file):
Chris PeBenito 89ec23
	try:
Chris PeBenito 89ec23
		bodyfile = open("templates/header.html", "r")
Chris PeBenito 89ec23
		intfile = open("templates/interface.html", "r")
Chris PeBenito 89ec23
	except:
Chris PeBenito 89ec23
		error("Could not open templates")
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	interface_buf = None
Chris PeBenito 89ec23
	interface_parameters = {}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	for node in doc.getElementsByTagName("module"):
Chris PeBenito 89ec23
		for interface in node.getElementsByTagName("interface"):
Chris PeBenito 89ec23
			interface_tpl = pyplate.Template(intfile.read())
Chris PeBenito 89ec23
			for i,v in interface.attributes.items():
Chris PeBenito 89ec23
				interface_name = v
Chris PeBenito 89ec23
			for desc in interface.getElementsByTagName("description"):
Chris PeBenito 89ec23
				interface_desc = desc.firstChild.data
Chris PeBenito 89ec23
			for desc in interface.getElementsByTagName("securitydesc"):
Chris PeBenito 89ec23
				if desc:
Chris PeBenito 89ec23
					interface_secdesc = desc.firstChild.data
Chris PeBenito 89ec23
				else:
Chris PeBenito 89ec23
					interface_secdesc = None
Chris PeBenito 89ec23
			
Chris PeBenito 89ec23
			for args in interface.getElementsByTagName("parameter"):
Chris PeBenito 89ec23
				paramdesc = args.firstChild.data
Chris PeBenito 89ec23
				for i,v in interface.attributes.items():
Chris PeBenito 89ec23
					arg = { "name" : v,
Chris PeBenito 89ec23
						"desc" : paramdesc }
Chris PeBenito 89ec23
					
Chris PeBenito 89ec23
Chris PeBenito 89ec23
def error(error):
Chris PeBenito 89ec23
        sys.stderr.write("%s exiting for: " % sys.argv[0])
Chris PeBenito 89ec23
        sys.stderr.write("%s\n" % error)
Chris PeBenito 89ec23
        sys.stderr.flush()
Chris PeBenito 89ec23
        sys.exit(1)
Chris PeBenito 89ec23
Chris PeBenito 89ec23
def usage():
Chris PeBenito 89ec23
	sys.stdout.write("%s [-tmd] -x <xmlfile>\n\n" % sys.argv[0])
Chris PeBenito 89ec23
	sys.stdout.write("Options:\n")
Chris PeBenito 89ec23
	sys.stdout.write("-t --tunables			--	write tunable config to <file>\n")
Chris PeBenito 89ec23
	sys.stdout.write("-m --modules <file>		--	write module config to <file>\n")
Chris PeBenito 89ec23
	sys.stdout.write("-d --docs <dir>		--	write interface documentation to <dir>\n")
Chris PeBenito 89ec23
	sys.stdout.write("-x --xml <file>		--	filename to read xml data from\n")
Chris PeBenito 89ec23
Chris PeBenito 89ec23
Chris PeBenito 89ec23
try:
Chris PeBenito 89ec23
	opts, args = getopt.getopt(sys.argv[1:], "t:m:d:x:", ["tunables","modules","docs","xml"])
Chris PeBenito 89ec23
except getopt.GetoptError:
Chris PeBenito 89ec23
	usage()
Chris PeBenito 89ec23
	sys.exit(1)
Chris PeBenito 89ec23
Chris PeBenito 89ec23
tunables = modules = docs = xmlfile = None
Chris PeBenito 89ec23
Chris PeBenito 89ec23
for opt, val in opts:
Chris PeBenito 89ec23
	if opt in ("-t", "--tunables"):
Chris PeBenito 89ec23
		tunables = val
Chris PeBenito 89ec23
	if opt in ("-m", "--modules"):
Chris PeBenito 89ec23
		modules = val
Chris PeBenito 89ec23
	if opt in ("-d", "--docs"):
Chris PeBenito 89ec23
		docs = val
Chris PeBenito 89ec23
	if opt in ("-x", "--xml"):
Chris PeBenito 89ec23
		xmlfile = val
Chris PeBenito 89ec23
Chris PeBenito 89ec23
if xmlfile == None:
Chris PeBenito 89ec23
	usage()
Chris PeBenito 89ec23
	sys.exit(1)
Chris PeBenito 89ec23
Chris PeBenito 89ec23
doc = read_policy_xml(xmlfile)
Chris PeBenito 89ec23
		
Chris PeBenito 89ec23
if tunables:
Chris PeBenito 89ec23
	try:
Chris PeBenito 89ec23
		conf = open(tunables, 'w')
Chris PeBenito 89ec23
	except:
Chris PeBenito 89ec23
		error("Could not open tunables file for writing")
Chris PeBenito 89ec23
	gen_tunable_conf(doc, conf)
Chris PeBenito 89ec23
	conf.close()
Chris PeBenito 89ec23
Chris PeBenito 89ec23
Chris PeBenito 89ec23
if modules:
Chris PeBenito 89ec23
	try:
Chris PeBenito 89ec23
		conf = open(modules, 'w')
Chris PeBenito 89ec23
	except:
Chris PeBenito 89ec23
		error("Could not open modules file for writing")
Chris PeBenito 89ec23
	gen_module_conf(doc, conf)
Chris PeBenito 89ec23
	conf.close()
Chris PeBenito 89ec23
Chris PeBenito 89ec23
if docs: 
Chris PeBenito 89ec23
	gen_docs(doc, sys.stdout)