Chris PeBenito 17de1b
#!/usr/bin/python
Chris PeBenito 17de1b
Chris PeBenito 17de1b
#  Author: Joshua Brindle <jbrindle@tresys.com>
Chris PeBenito 56e1b3
#          Caleb Case <ccase@tresys.com>
Chris PeBenito 17de1b
#
Chris PeBenito 17de1b
# Copyright (C) 2005 - 2006 Tresys Technology, LLC
Chris PeBenito 17de1b
#      This program is free software; you can redistribute it and/or modify
Chris PeBenito 17de1b
#      it under the terms of the GNU General Public License as published by
Chris PeBenito 17de1b
#      the Free Software Foundation, version 2.
Chris PeBenito 17de1b
Chris PeBenito 17de1b
"""
Chris PeBenito 17de1b
	This module generates configuration files and documentation from the 
Chris PeBenito 17de1b
	SELinux reference policy XML format. 
Chris PeBenito 17de1b
"""
Chris PeBenito 17de1b
Chris PeBenito 17de1b
import sys
Chris PeBenito 17de1b
import getopt
Chris PeBenito 17de1b
import pyplate
Chris PeBenito 17de1b
import os
Chris PeBenito 17de1b
import string
Chris PeBenito 17de1b
from xml.dom.minidom import parse, parseString
Chris PeBenito 17de1b
Chris PeBenito 17de1b
#modules enabled and disabled values
Chris PeBenito 17de1b
MOD_BASE = "base"
Chris PeBenito 17de1b
MOD_ENABLED = "module"
Chris PeBenito 17de1b
MOD_DISABLED = "off"
Chris PeBenito 17de1b
Chris PeBenito 17de1b
#booleans enabled and disabled values
Chris PeBenito 17de1b
BOOL_ENABLED = "true"
Chris PeBenito 17de1b
BOOL_DISABLED = "false"
Chris PeBenito 17de1b
Chris PeBenito 17de1b
#tunables enabled and disabled values
Chris PeBenito 17de1b
TUN_ENABLED = "true"
Chris PeBenito 17de1b
TUN_DISABLED = "false"
Chris PeBenito 17de1b
Chris PeBenito 17de1b
Chris PeBenito 17de1b
def read_policy_xml(filename):
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	Takes in XML from a file and returns a parsed file.
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	try:
Chris PeBenito 17de1b
		xml_fh = open(filename)
Chris PeBenito 17de1b
	except:
Chris PeBenito 17de1b
		error("error opening " + filename)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	try:
Chris PeBenito 17de1b
		doc = parseString(xml_fh.read())
Chris PeBenito 17de1b
	except: 
Chris PeBenito 17de1b
		xml_fh.close()
Chris PeBenito 17de1b
		error("Error while parsing xml")
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	xml_fh.close()	
Chris PeBenito 17de1b
	return doc
Chris PeBenito 17de1b
Chris PeBenito 17de1b
def gen_booleans_conf(doc, file_name, namevalue_list):
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	Generates the booleans configuration file using the XML provided and the
Chris PeBenito 17de1b
	previous booleans configuration.
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	for node in doc.getElementsByTagName("bool"):
Chris PeBenito 17de1b
		for desc in node.getElementsByTagName("desc"):
Chris PeBenito 17de1b
			bool_desc = format_txt_desc(desc)
Chris PeBenito 17de1b
		s = string.split(bool_desc, "\n")
Chris PeBenito 17de1b
		file_name.write("#\n")
Chris PeBenito 17de1b
		for line in s:
Chris PeBenito 17de1b
			file_name.write("# %s\n" % line)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		bool_name = bool_val = None
Chris PeBenito 17de1b
		for (name, value) in node.attributes.items():
Chris PeBenito 17de1b
			if name == "name":
Chris PeBenito 17de1b
				bool_name = value
Chris PeBenito 17de1b
			elif name == "dftval":
Chris PeBenito 17de1b
				bool_val = value
Chris PeBenito 17de1b
Chris PeBenito 17de1b
			if [bool_name,BOOL_ENABLED] in namevalue_list:
Chris PeBenito 17de1b
				bool_val = BOOL_ENABLED
Chris PeBenito 17de1b
			elif [bool_name,BOOL_DISABLED] in namevalue_list:
Chris PeBenito 17de1b
				bool_val = BOOL_DISABLED
Chris PeBenito 17de1b
Chris PeBenito 17de1b
			if bool_name and bool_val:
Chris PeBenito 17de1b
	            		file_name.write("%s = %s\n\n" % (bool_name, bool_val))
Chris PeBenito 17de1b
				bool_name = bool_val = None
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	# tunables are currently implemented as booleans
Chris PeBenito 17de1b
	for node in doc.getElementsByTagName("tunable"):
Chris PeBenito 17de1b
		for desc in node.getElementsByTagName("desc"):
Chris PeBenito 17de1b
			bool_desc = format_txt_desc(desc)
Chris PeBenito 17de1b
		s = string.split(bool_desc, "\n")
Chris PeBenito 17de1b
		file_name.write("#\n")
Chris PeBenito 17de1b
		for line in s:
Chris PeBenito 17de1b
			file_name.write("# %s\n" % line)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		bool_name = bool_val = None
Chris PeBenito 17de1b
		for (name, value) in node.attributes.items():
Chris PeBenito 17de1b
			if name == "name":
Chris PeBenito 17de1b
				bool_name = value
Chris PeBenito 17de1b
			elif name == "dftval":
Chris PeBenito 17de1b
				bool_val = value
Chris PeBenito 17de1b
Chris PeBenito 17de1b
			if [bool_name,BOOL_ENABLED] in namevalue_list:
Chris PeBenito 17de1b
				bool_val = BOOL_ENABLED
Chris PeBenito 17de1b
			elif [bool_name,BOOL_DISABLED] in namevalue_list:
Chris PeBenito 17de1b
				bool_val = BOOL_DISABLED
Chris PeBenito 17de1b
Chris PeBenito 17de1b
			if bool_name and bool_val:
Chris PeBenito 17de1b
	            		file_name.write("%s = %s\n\n" % (bool_name, bool_val))
Chris PeBenito 17de1b
				bool_name = bool_val = None
Chris PeBenito 17de1b
Chris PeBenito 17de1b
def gen_module_conf(doc, file_name, namevalue_list):
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	Generates the module configuration file using the XML provided and the
Chris PeBenito 17de1b
	previous module configuration.
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	# If file exists, preserve settings and modify if needed.
Chris PeBenito 17de1b
	# Otherwise, create it.
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	file_name.write("#\n# This file contains a listing of available modules.\n")
Chris PeBenito 17de1b
	file_name.write("# To prevent a module from  being used in policy\n")
Chris PeBenito 17de1b
	file_name.write("# creation, set the module name to \"%s\".\n#\n" % MOD_DISABLED)
Chris PeBenito 17de1b
	file_name.write("# For monolithic policies, modules set to \"%s\" and \"%s\"\n" % (MOD_BASE, MOD_ENABLED))
Chris PeBenito 17de1b
	file_name.write("# will be built into the policy.\n#\n")
Chris PeBenito 17de1b
	file_name.write("# For modular policies, modules set to \"%s\" will be\n" % MOD_BASE)
Chris PeBenito 17de1b
	file_name.write("# included in the base module.  \"%s\" will be compiled\n" % MOD_ENABLED)
Chris PeBenito 17de1b
	file_name.write("# as individual loadable modules.\n#\n\n")
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	# For required in [True,False] is present so that the requiered modules
Chris PeBenito 17de1b
	# are at the top of the config file.
Chris PeBenito 17de1b
	for required in [True,False]:
Chris PeBenito 17de1b
		for node in doc.getElementsByTagName("module"):
Chris PeBenito 17de1b
			mod_req = False
Chris PeBenito 17de1b
			for req in node.getElementsByTagName("required"):
Chris PeBenito 17de1b
				if req.getAttribute("val") == "true":
Chris PeBenito 17de1b
					mod_req = True
Chris PeBenito 17de1b
Chris PeBenito 17de1b
			# Skip if we arnt working on the right set of modules.
Chris PeBenito 17de1b
			if mod_req and not required or not mod_req and required:
Chris PeBenito 17de1b
				continue
Chris PeBenito 17de1b
Chris PeBenito 17de1b
Chris PeBenito 17de1b
			mod_name = mod_layer = None
Chris PeBenito 17de1b
Chris PeBenito 17de1b
			mod_name = node.getAttribute("name")	
Chris PeBenito 17de1b
			mod_layer = node.parentNode.getAttribute("name")
Chris PeBenito 17de1b
Chris PeBenito 17de1b
			if mod_name and mod_layer:
Chris PeBenito 17de1b
				file_name.write("# Layer: %s\n# Module: %s\n" % (mod_layer,mod_name))
Chris PeBenito 17de1b
				if required:
Chris PeBenito 17de1b
					file_name.write("# Required in base\n")
Chris PeBenito 17de1b
				file_name.write("#\n")
Chris PeBenito 17de1b
Chris PeBenito 17de1b
			for desc in node.getElementsByTagName("summary"):
Chris PeBenito 17de1b
				if not desc.parentNode == node:
Chris PeBenito 17de1b
					continue
Chris PeBenito 17de1b
				s = string.split(format_txt_desc(desc), "\n")
Chris PeBenito 17de1b
				for line in s:
Chris PeBenito 17de1b
					file_name.write("# %s\n" % line)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
				# If the module is set as disabled.
Chris PeBenito 17de1b
				if [mod_name, MOD_DISABLED] in namevalue_list:
Chris PeBenito 17de1b
					file_name.write("%s = %s\n\n" % (mod_name, MOD_DISABLED))
Chris PeBenito 17de1b
				# If the module is set as enabled.
Chris PeBenito 17de1b
				elif [mod_name, MOD_ENABLED] in namevalue_list:
Chris PeBenito 17de1b
					file_name.write("%s = %s\n\n" % (mod_name, MOD_ENABLED))
Chris PeBenito 17de1b
				# If the module is set as base.
Chris PeBenito 17de1b
				elif [mod_name, MOD_BASE] in namevalue_list:
Chris PeBenito 17de1b
					file_name.write("%s = %s\n\n" % (mod_name, MOD_BASE))
Chris PeBenito 17de1b
				# If the module is a new module.
Chris PeBenito 17de1b
				else:
Chris PeBenito 17de1b
					# Set the module to base if it is marked as required.
Chris PeBenito 17de1b
					if mod_req:
Chris PeBenito 17de1b
						file_name.write("%s = %s\n\n" % (mod_name, MOD_BASE))
Chris PeBenito 17de1b
					# Set the module to enabled if it is not required. 
Chris PeBenito 17de1b
					else:
Chris PeBenito 17de1b
						file_name.write("%s = %s\n\n" % (mod_name, MOD_ENABLED))
Chris PeBenito 17de1b
Chris PeBenito 17de1b
def get_conf(conf):
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	Returns a list of [name, value] pairs from a config file with the format
Chris PeBenito 17de1b
	name = value
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	conf_lines = conf.readlines()
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	namevalue_list = []
Chris PeBenito 17de1b
	for i in range(0,len(conf_lines)):
Chris PeBenito 17de1b
		line = conf_lines[i]
Chris PeBenito 17de1b
		if line.strip() != '' and line.strip()[0] != "#":
Chris PeBenito 17de1b
			namevalue = line.strip().split("=")
Chris PeBenito 17de1b
			if len(namevalue) != 2:
Chris PeBenito 17de1b
				warning("line %d: \"%s\" is not a valid line, skipping"\
Chris PeBenito 17de1b
					 % (i, line.strip()))
Chris PeBenito 17de1b
				continue
Chris PeBenito 17de1b
Chris PeBenito 17de1b
			namevalue[0] = namevalue[0].strip()
Chris PeBenito 17de1b
			if len(namevalue[0].split()) > 1:
Chris PeBenito 17de1b
				warning("line %d: \"%s\" is not a valid line, skipping"\
Chris PeBenito 17de1b
					 % (i, line.strip()))
Chris PeBenito 17de1b
				continue
Chris PeBenito 17de1b
Chris PeBenito 17de1b
			namevalue[1] = namevalue[1].strip()
Chris PeBenito 17de1b
			if len(namevalue[1].split()) > 1:
Chris PeBenito 17de1b
				warning("line %d: \"%s\" is not a valid line, skipping"\
Chris PeBenito 17de1b
					 % (i, line.strip()))
Chris PeBenito 17de1b
				continue
Chris PeBenito 17de1b
Chris PeBenito 17de1b
			namevalue_list.append(namevalue)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	return namevalue_list
Chris PeBenito 17de1b
Chris PeBenito 17de1b
def first_cmp(a, b):
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	Compares the two first elements of a list instead of the entire list.
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	return cmp(a[0], b[0])
Chris PeBenito 17de1b
Chris PeBenito 17de1b
def int_cmp(a, b):
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	Compares two interfaces.
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	return cmp(a["interface_name"], b["interface_name"])
Chris PeBenito 17de1b
		
Chris PeBenito 17de1b
def temp_cmp(a, b):
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	Compares two templates.
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	return cmp(a["template_name"], b["template_name"])
Chris PeBenito 17de1b
Chris PeBenito 17de1b
def tun_cmp(a, b):
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	Compares two tunables.
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	return cmp(a["tun_name"], b["tun_name"])
Chris PeBenito 17de1b
def bool_cmp(a, b):
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	Compares two booleans.
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	return cmp(a["bool_name"], b["bool_name"])
Chris PeBenito 17de1b
Chris PeBenito 17de1b
def gen_doc_menu(mod_layer, module_list):
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	Generates the HTML document menu.
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	menu = []
Chris PeBenito 17de1b
	for layer, value in module_list.iteritems():
Chris PeBenito 17de1b
		cur_menu = (layer, [])
Chris PeBenito 17de1b
		menu.append(cur_menu)
Chris PeBenito 17de1b
		if layer != mod_layer and mod_layer != None:
Chris PeBenito 17de1b
			continue
Chris PeBenito 17de1b
		#we are in our layer so fill in the other modules or we want them all
Chris PeBenito 17de1b
		for mod, desc in value.iteritems():
Chris PeBenito 17de1b
			cur_menu[1].append((mod, desc))
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	menu.sort(first_cmp)
Chris PeBenito 17de1b
	for x in menu:
Chris PeBenito 17de1b
		x[1].sort(first_cmp)
Chris PeBenito 17de1b
	return menu
Chris PeBenito 17de1b
Chris PeBenito 17de1b
def format_html_desc(node):
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	Formats a XML node into a HTML format.
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	desc_buf = ''
Chris PeBenito 17de1b
	for desc in node.childNodes:
Chris PeBenito 17de1b
		if desc.nodeName == "#text":
Chris PeBenito 17de1b
			if desc.data is not '':
Chris PeBenito 17de1b
				if desc.parentNode.nodeName != "p":
Chris PeBenito 17de1b
					desc_buf += "

" + desc.data + "

"
Chris PeBenito 17de1b
				else:
Chris PeBenito 17de1b
					desc_buf += desc.data
Chris PeBenito 17de1b
		else:
Chris PeBenito 17de1b
			desc_buf += "<" + desc.nodeName + ">" \
Chris PeBenito 17de1b
				 + format_html_desc(desc) \
Chris PeBenito 17de1b
				 + "</" + desc.nodeName +">"
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	return desc_buf
Chris PeBenito 17de1b
Chris PeBenito 17de1b
def format_txt_desc(node):
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	Formats a XML node into a plain text format.
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	desc_buf = ''
Chris PeBenito 17de1b
	for desc in node.childNodes:
Chris PeBenito 17de1b
		if desc.nodeName == "#text":
Chris PeBenito 17de1b
			desc_buf += desc.data + "\n"
Chris PeBenito 17de1b
		elif desc.nodeName == "p":
Chris PeBenito 17de1b
			desc_buf += desc.firstChild.data + "\n"
Chris PeBenito 17de1b
			for chld in desc.childNodes: 
Chris PeBenito 17de1b
				if chld.nodeName == "ul":
Chris PeBenito 17de1b
					desc_buf += "\n"
Chris PeBenito 17de1b
					for li in chld.getElementsByTagName("li"):
Chris PeBenito 17de1b
						desc_buf += "\t -" + li.firstChild.data + "\n"
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	return desc_buf.strip() + "\n"
Chris PeBenito 17de1b
Chris PeBenito 17de1b
def gen_docs(doc, working_dir, templatedir):
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	Generates all the documentation.
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	try:
Chris PeBenito 17de1b
		#get the template data ahead of time so we don't reopen them over and over
Chris PeBenito 17de1b
		bodyfile = open(templatedir + "/header.html", "r")
Chris PeBenito 17de1b
		bodydata = bodyfile.read()
Chris PeBenito 17de1b
		bodyfile.close()
Chris PeBenito 17de1b
		intfile = open(templatedir + "/interface.html", "r")
Chris PeBenito 17de1b
		intdata = intfile.read()
Chris PeBenito 17de1b
		intfile.close()
Chris PeBenito 17de1b
		templatefile = open(templatedir + "/template.html", "r")
Chris PeBenito 17de1b
		templatedata = templatefile.read()
Chris PeBenito 17de1b
		templatefile.close()
Chris PeBenito 56e1b3
		tunfile = open(templatedir + "/tunable.html", "r")
Chris PeBenito 56e1b3
		tundata = tunfile.read()
Chris PeBenito 56e1b3
		tunfile.close()
Chris PeBenito 56e1b3
		boolfile = open(templatedir + "/boolean.html", "r")
Chris PeBenito 56e1b3
		booldata = boolfile.read()
Chris PeBenito 56e1b3
		boolfile.close()
Chris PeBenito 17de1b
		menufile = open(templatedir + "/menu.html", "r")
Chris PeBenito 17de1b
		menudata = menufile.read()
Chris PeBenito 17de1b
		menufile.close()
Chris PeBenito 17de1b
		indexfile = open(templatedir + "/module_list.html","r")
Chris PeBenito 17de1b
		indexdata = indexfile.read()
Chris PeBenito 17de1b
		indexfile.close()
Chris PeBenito 17de1b
		modulefile = open(templatedir + "/module.html","r")
Chris PeBenito 17de1b
		moduledata = modulefile.read()
Chris PeBenito 17de1b
		modulefile.close()
Chris PeBenito 17de1b
		intlistfile = open(templatedir + "/int_list.html", "r")
Chris PeBenito 17de1b
		intlistdata = intlistfile.read()
Chris PeBenito 17de1b
		intlistfile.close()
Chris PeBenito 17de1b
		templistfile = open(templatedir + "/temp_list.html", "r")
Chris PeBenito 17de1b
		templistdata = templistfile.read()
Chris PeBenito 17de1b
		templistfile.close()
Chris PeBenito 56e1b3
		tunlistfile = open(templatedir + "/tun_list.html", "r")
Chris PeBenito 17de1b
		tunlistdata = tunlistfile.read()
Chris PeBenito 17de1b
		tunlistfile.close()
Chris PeBenito 56e1b3
		boollistfile = open(templatedir + "/bool_list.html", "r")
Chris PeBenito 56e1b3
		boollistdata = boollistfile.read()
Chris PeBenito 56e1b3
		boollistfile.close()
Chris PeBenito 56e1b3
		gboollistfile = open(templatedir + "/global_bool_list.html", "r")
Chris PeBenito 56e1b3
		gboollistdata = gboollistfile.read()
Chris PeBenito 56e1b3
		gboollistfile.close()
Chris PeBenito 56e1b3
		gtunlistfile = open(templatedir + "/global_tun_list.html", "r")
Chris PeBenito 56e1b3
		gtunlistdata = gtunlistfile.read()
Chris PeBenito 56e1b3
		gtunlistfile.close()
Chris PeBenito 17de1b
	except:
Chris PeBenito 17de1b
		error("Could not open templates")
Chris PeBenito 17de1b
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	try:
Chris PeBenito 17de1b
		os.chdir(working_dir)
Chris PeBenito 17de1b
	except:
Chris PeBenito 17de1b
		error("Could not chdir to target directory")	
Chris PeBenito 17de1b
Chris PeBenito 17de1b
Chris PeBenito 17de1b
#arg, i have to go through this dom tree ahead of time to build up the menus
Chris PeBenito 17de1b
	module_list = {}
Chris PeBenito 17de1b
	for node in doc.getElementsByTagName("module"):
Chris PeBenito 17de1b
                mod_name = mod_layer = interface_buf = ''
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		mod_name = node.getAttribute("name")
Chris PeBenito 17de1b
		mod_layer = node.parentNode.getAttribute("name")
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		for desc in node.getElementsByTagName("summary"):
Chris PeBenito 17de1b
			if desc.parentNode == node and desc:
Chris PeBenito 17de1b
				mod_summary = format_html_desc(desc)
Chris PeBenito 17de1b
		if not module_list.has_key(mod_layer):
Chris PeBenito 17de1b
			module_list[mod_layer] = {}
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		module_list[mod_layer][mod_name] = mod_summary
Chris PeBenito 17de1b
Chris PeBenito 17de1b
#generate index pages
Chris PeBenito 17de1b
	main_content_buf = ''
Chris PeBenito 17de1b
	for mod_layer,modules in module_list.iteritems():
Chris PeBenito 17de1b
		menu = gen_doc_menu(mod_layer, module_list)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		layer_summary = None
Chris PeBenito 17de1b
		for desc in doc.getElementsByTagName("summary"):
Chris PeBenito 17de1b
			if desc.parentNode.getAttribute("name") == mod_layer:
Chris PeBenito 17de1b
				layer_summary = format_html_desc(desc)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		menu_args = { "menulist" : menu,
Chris PeBenito 17de1b
			      "mod_layer" : mod_layer,
Chris PeBenito 17de1b
			      "layer_summary" : layer_summary }
Chris PeBenito 17de1b
		menu_tpl = pyplate.Template(menudata)
Chris PeBenito 17de1b
		menu_buf = menu_tpl.execute_string(menu_args)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		content_tpl = pyplate.Template(indexdata)
Chris PeBenito 17de1b
		content_buf = content_tpl.execute_string(menu_args)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		main_content_buf += content_buf
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		body_args = { "menu" : menu_buf,
Chris PeBenito 17de1b
			      "content" : content_buf }
Chris PeBenito 17de1b
	
Chris PeBenito 17de1b
		index_file = mod_layer + ".html"
Chris PeBenito 17de1b
		index_fh = open(index_file, "w")
Chris PeBenito 17de1b
		body_tpl = pyplate.Template(bodydata)
Chris PeBenito 17de1b
		body_tpl.execute(index_fh, body_args)
Chris PeBenito 17de1b
		index_fh.close()	
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	menu = gen_doc_menu(None, module_list)
Chris PeBenito 17de1b
	menu_args = { "menulist" : menu,
Chris PeBenito 17de1b
		      "mod_layer" : None }
Chris PeBenito 17de1b
	menu_tpl = pyplate.Template(menudata)
Chris PeBenito 17de1b
	menu_buf = menu_tpl.execute_string(menu_args)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	body_args = { "menu" : menu_buf,
Chris PeBenito 17de1b
		      "content" : main_content_buf }
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	index_file = "index.html"
Chris PeBenito 17de1b
	index_fh = open(index_file, "w")
Chris PeBenito 17de1b
	body_tpl = pyplate.Template(bodydata)
Chris PeBenito 17de1b
	body_tpl.execute(index_fh, body_args)
Chris PeBenito 17de1b
	index_fh.close()
Chris PeBenito 17de1b
#now generate the individual module pages
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	all_interfaces = []
Chris PeBenito 17de1b
	all_templates = []
Chris PeBenito 56e1b3
	all_tunables = []
Chris PeBenito 56e1b3
	all_booleans = []
Chris PeBenito 17de1b
	for node in doc.getElementsByTagName("module"):
Chris PeBenito 17de1b
                mod_name = mod_layer = mod_desc = interface_buf = ''
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		mod_name = node.getAttribute("name")
Chris PeBenito 17de1b
		mod_layer = node.parentNode.getAttribute("name")
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		mod_req = None
Chris PeBenito 17de1b
		for req in node.getElementsByTagName("required"):
Chris PeBenito 17de1b
			if req.getAttribute("val") == "true":
Chris PeBenito 17de1b
				mod_req = True
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		for desc in node.getElementsByTagName("summary"):
Chris PeBenito 17de1b
			if desc.parentNode == node:
Chris PeBenito 17de1b
				mod_summary = format_html_desc(desc)
Chris PeBenito 17de1b
		for desc in node.getElementsByTagName("desc"):
Chris PeBenito 17de1b
			if desc.parentNode == node:
Chris PeBenito 17de1b
				mod_desc = format_html_desc(desc)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		interfaces = []
Chris PeBenito 17de1b
		for interface in node.getElementsByTagName("interface"):
Chris PeBenito 17de1b
			interface_parameters = []
Chris PeBenito 17de1b
			interface_desc = interface_summary = None
Chris PeBenito 17de1b
			interface_name = interface.getAttribute("name")
Chris PeBenito 17de1b
			interface_line = interface.getAttribute("lineno")
Chris PeBenito 17de1b
			for desc in interface.childNodes:
Chris PeBenito 17de1b
				if desc.nodeName == "desc":
Chris PeBenito 17de1b
					interface_desc = format_html_desc(desc)
Chris PeBenito 17de1b
				elif desc.nodeName == "summary":
Chris PeBenito 17de1b
					interface_summary = format_html_desc(desc)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
			for args in interface.getElementsByTagName("param"):
Chris PeBenito 17de1b
				for desc in args.getElementsByTagName("summary"):
Chris PeBenito 17de1b
					paramdesc = format_html_desc(desc)
Chris PeBenito 17de1b
				paramname = args.getAttribute("name")
Chris PeBenito 17de1b
				if args.getAttribute("optional") == "true":
Chris PeBenito 17de1b
					paramopt = "Yes"
Chris PeBenito 17de1b
				else:
Chris PeBenito 17de1b
					paramopt = "No"
Chris PeBenito c5cfd2
				if args.getAttribute("unused") == "true":
Chris PeBenito c5cfd2
					paramunused = "Yes"
Chris PeBenito c5cfd2
				else:
Chris PeBenito c5cfd2
					paramunused = "No"
Chris PeBenito 17de1b
				parameter = { "name" : paramname,
Chris PeBenito 17de1b
					      "desc" : paramdesc,
Chris PeBenito c5cfd2
					      "optional" : paramopt,
Chris PeBenito c5cfd2
					      "unused" : paramunused }
Chris PeBenito 17de1b
				interface_parameters.append(parameter)
Chris PeBenito 17de1b
			interfaces.append( { "interface_name" : interface_name,
Chris PeBenito 17de1b
					   "interface_summary" : interface_summary,
Chris PeBenito 17de1b
					   "interface_desc" : interface_desc,
Chris PeBenito 17de1b
					   "interface_parameters" : interface_parameters })
Chris PeBenito 17de1b
			#all_interfaces is for the main interface index with all interfaces
Chris PeBenito 17de1b
			all_interfaces.append( { "interface_name" : interface_name,
Chris PeBenito 17de1b
					   "interface_summary" : interface_summary,
Chris PeBenito 17de1b
					   "interface_desc" : interface_desc,
Chris PeBenito 17de1b
					   "interface_parameters" : interface_parameters,
Chris PeBenito 17de1b
					   "mod_name": mod_name,
Chris PeBenito 17de1b
					   "mod_layer" : mod_layer })
Chris PeBenito 17de1b
		interfaces.sort(int_cmp)	
Chris PeBenito 17de1b
		interface_tpl = pyplate.Template(intdata)
Chris PeBenito 17de1b
		interface_buf = interface_tpl.execute_string({"interfaces" : interfaces})
Chris PeBenito 17de1b
	
Chris PeBenito 17de1b
Chris PeBenito 17de1b
# now generate individual template pages
Chris PeBenito 17de1b
		templates = []
Chris PeBenito 17de1b
		for template in node.getElementsByTagName("template"):
Chris PeBenito 17de1b
			template_parameters = []
Chris PeBenito 17de1b
			template_desc = template_summary = None
Chris PeBenito 17de1b
			template_name = template.getAttribute("name")
Chris PeBenito 17de1b
			template_line = template.getAttribute("lineno")
Chris PeBenito 17de1b
			for desc in template.childNodes:
Chris PeBenito 17de1b
				if desc.nodeName == "desc":
Chris PeBenito 17de1b
					template_desc = format_html_desc(desc)
Chris PeBenito 17de1b
				elif desc.nodeName == "summary":
Chris PeBenito 17de1b
					template_summary = format_html_desc(desc)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
			for args in template.getElementsByTagName("param"):
Chris PeBenito 17de1b
				for desc in args.getElementsByTagName("summary"):
Chris PeBenito 17de1b
					paramdesc = format_html_desc(desc)
Chris PeBenito 17de1b
				paramname = args.getAttribute("name")
Chris PeBenito 17de1b
				if args.getAttribute("optional") == "true":
Chris PeBenito 17de1b
					paramopt = "Yes"
Chris PeBenito 17de1b
				else:
Chris PeBenito 17de1b
					paramopt = "No"
Chris PeBenito c5cfd2
				if args.getAttribute("unused") == "true":
Chris PeBenito c5cfd2
					paramunused = "Yes"
Chris PeBenito c5cfd2
				else:
Chris PeBenito c5cfd2
					paramunused = "No"
Chris PeBenito 17de1b
				parameter = { "name" : paramname,
Chris PeBenito 17de1b
					      "desc" : paramdesc,
Chris PeBenito c5cfd2
					      "optional" : paramopt,
Chris PeBenito c5cfd2
					      "unused": paramunused }
Chris PeBenito 17de1b
				template_parameters.append(parameter)
Chris PeBenito 17de1b
			templates.append( { "template_name" : template_name,
Chris PeBenito 17de1b
					   "template_summary" : template_summary,
Chris PeBenito 17de1b
					   "template_desc" : template_desc,
Chris PeBenito 17de1b
					   "template_parameters" : template_parameters })
Chris PeBenito 17de1b
			#all_templates is for the main interface index with all templates
Chris PeBenito 17de1b
			all_templates.append( { "template_name" : template_name,
Chris PeBenito 17de1b
					   "template_summary" : template_summary,
Chris PeBenito 17de1b
					   "template_desc" : template_desc,
Chris PeBenito 17de1b
					   "template_parameters" : template_parameters,
Chris PeBenito 17de1b
					   "mod_name": mod_name,
Chris PeBenito 17de1b
					   "mod_layer" : mod_layer })
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		templates.sort(temp_cmp)	
Chris PeBenito 17de1b
		template_tpl = pyplate.Template(templatedata)
Chris PeBenito 17de1b
		template_buf = template_tpl.execute_string({"templates" : templates})
Chris PeBenito 17de1b
Chris PeBenito 56e1b3
		#generate 'boolean' pages
Chris PeBenito 56e1b3
		booleans = []
Chris PeBenito 56e1b3
		for boolean in node.getElementsByTagName("bool"):
Chris PeBenito 56e1b3
			boolean_parameters = []
Chris PeBenito 56e1b3
			boolean_desc = None
Chris PeBenito 56e1b3
			boolean_name = boolean.getAttribute("name")
Chris PeBenito 56e1b3
			boolean_dftval = boolean.getAttribute("dftval")
Chris PeBenito 56e1b3
			for desc in boolean.childNodes:
Chris PeBenito 56e1b3
				if desc.nodeName == "desc":
Chris PeBenito 56e1b3
					boolean_desc = format_html_desc(desc)
Chris PeBenito 56e1b3
Chris PeBenito 56e1b3
			booleans.append({ "bool_name" : boolean_name,
Chris PeBenito 56e1b3
					  "desc" : boolean_desc,
Chris PeBenito 56e1b3
					  "def_val" : boolean_dftval })
Chris PeBenito 56e1b3
			#all_booleans is for the main boolean index with all booleans
Chris PeBenito 56e1b3
			all_booleans.append({ "bool_name" : boolean_name,
Chris PeBenito 56e1b3
					   "desc" : boolean_desc,
Chris PeBenito 56e1b3
					   "def_val" : boolean_dftval,
Chris PeBenito 56e1b3
					   "mod_name": mod_name,
Chris PeBenito 56e1b3
					   "mod_layer" : mod_layer })
Chris PeBenito 56e1b3
		booleans.sort(bool_cmp)
Chris PeBenito 56e1b3
		boolean_tpl = pyplate.Template(booldata)
Chris PeBenito 56e1b3
		boolean_buf = boolean_tpl.execute_string({"booleans" : booleans})
Chris PeBenito 56e1b3
Chris PeBenito 56e1b3
		#generate 'tunable' pages
Chris PeBenito 56e1b3
		tunables = []
Chris PeBenito 56e1b3
		for tunable in node.getElementsByTagName("tunable"):
Chris PeBenito 56e1b3
			tunable_parameters = []
Chris PeBenito 56e1b3
			tunable_desc = None
Chris PeBenito 56e1b3
			tunable_name = tunable.getAttribute("name")
Chris PeBenito 56e1b3
			tunable_dftval = tunable.getAttribute("dftval")
Chris PeBenito 56e1b3
			for desc in tunable.childNodes:
Chris PeBenito 56e1b3
				if desc.nodeName == "desc":
Chris PeBenito 56e1b3
					tunable_desc = format_html_desc(desc)
Chris PeBenito 56e1b3
Chris PeBenito 56e1b3
			tunables.append({ "tun_name" : tunable_name,
Chris PeBenito 56e1b3
					  "desc" : tunable_desc,
Chris PeBenito 56e1b3
					  "def_val" : tunable_dftval })
Chris PeBenito 56e1b3
			#all_tunables is for the main tunable index with all tunables
Chris PeBenito 56e1b3
			all_tunables.append({ "tun_name" : tunable_name,
Chris PeBenito 56e1b3
					   "desc" : tunable_desc,
Chris PeBenito 56e1b3
					   "def_val" : tunable_dftval,
Chris PeBenito 56e1b3
					   "mod_name": mod_name,
Chris PeBenito 56e1b3
					   "mod_layer" : mod_layer })
Chris PeBenito 56e1b3
		tunables.sort(tun_cmp)
Chris PeBenito 56e1b3
		tunable_tpl = pyplate.Template(tundata)
Chris PeBenito 56e1b3
		tunable_buf = tunable_tpl.execute_string({"tunables" : tunables})
Chris PeBenito 56e1b3
	
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		menu = gen_doc_menu(mod_layer, module_list)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		menu_tpl = pyplate.Template(menudata)
Chris PeBenito 17de1b
		menu_buf = menu_tpl.execute_string({ "menulist" : menu })
Chris PeBenito 17de1b
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		# pyplate's execute_string gives us a line of whitespace in
Chris PeBenito 17de1b
		# template_buf or interface_buf if there are no interfaces or
Chris PeBenito 17de1b
		# templates for this module. This is problematic because the
Chris PeBenito 17de1b
		# HTML templates use a conditional if on interface_buf or
Chris PeBenito 17de1b
		# template_buf being 'None' to decide if the "Template:" or
Chris PeBenito 17de1b
		# "Interface:" headers need to be printed in the module pages.
Chris PeBenito 17de1b
		# This detects if either of these are just whitespace, and sets
Chris PeBenito 17de1b
		# their values to 'None' so that when applying it to the
Chris PeBenito 17de1b
		# templates, they are properly recognized as not existing.
Chris PeBenito 17de1b
		if not interface_buf.strip():
Chris PeBenito 17de1b
			interface_buf = None
Chris PeBenito 17de1b
		if not template_buf.strip():
Chris PeBenito 17de1b
			template_buf = None
Chris PeBenito 56e1b3
		if not tunable_buf.strip():
Chris PeBenito 56e1b3
			tunable_buf = None
Chris PeBenito 56e1b3
		if not boolean_buf.strip():
Chris PeBenito 56e1b3
			boolean_buf = None
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		module_args = { "mod_layer" : mod_layer,
Chris PeBenito 17de1b
			      "mod_name" : mod_name,	
Chris PeBenito 17de1b
			      "mod_summary" : mod_summary,
Chris PeBenito 17de1b
			      "mod_desc" : mod_desc,
Chris PeBenito 17de1b
			      "mod_req" : mod_req,
Chris PeBenito 17de1b
			      "interfaces" : interface_buf,
Chris PeBenito 56e1b3
			      "templates" : template_buf,
Chris PeBenito 56e1b3
			      "tunables" : tunable_buf,
Chris PeBenito 56e1b3
			      "booleans" : boolean_buf }
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		module_tpl = pyplate.Template(moduledata)
Chris PeBenito 17de1b
		module_buf = module_tpl.execute_string(module_args)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		body_args = { "menu" : menu_buf,
Chris PeBenito 17de1b
			      "content" : module_buf }
Chris PeBenito 17de1b
			  
Chris PeBenito 17de1b
		module_file = mod_layer + "_" + mod_name + ".html"
Chris PeBenito 17de1b
		module_fh = open(module_file, "w")
Chris PeBenito 17de1b
		body_tpl = pyplate.Template(bodydata)
Chris PeBenito 17de1b
		body_tpl.execute(module_fh, body_args)
Chris PeBenito 17de1b
		module_fh.close()
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		
Chris PeBenito 17de1b
	menu = gen_doc_menu(None, module_list)
Chris PeBenito 17de1b
	menu_args = { "menulist" : menu,
Chris PeBenito 17de1b
		      "mod_layer" : None }
Chris PeBenito 17de1b
	menu_tpl = pyplate.Template(menudata)
Chris PeBenito 17de1b
	menu_buf = menu_tpl.execute_string(menu_args)
Chris PeBenito 17de1b
	
Chris PeBenito 17de1b
	#build the interface index
Chris PeBenito 17de1b
	all_interfaces.sort(int_cmp)
Chris PeBenito 17de1b
	interface_tpl = pyplate.Template(intlistdata)
Chris PeBenito 17de1b
	interface_buf = interface_tpl.execute_string({"interfaces" : all_interfaces})
Chris PeBenito 17de1b
	int_file = "interfaces.html"
Chris PeBenito 17de1b
	int_fh = open(int_file, "w")
Chris PeBenito 17de1b
	body_tpl = pyplate.Template(bodydata)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	body_args = { "menu" : menu_buf, 
Chris PeBenito 17de1b
		      "content" : interface_buf }
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	body_tpl.execute(int_fh, body_args)
Chris PeBenito 17de1b
	int_fh.close()
Chris PeBenito 17de1b
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	#build the template index
Chris PeBenito 17de1b
	all_templates.sort(temp_cmp)
Chris PeBenito 17de1b
	template_tpl = pyplate.Template(templistdata)
Chris PeBenito 17de1b
	template_buf = template_tpl.execute_string({"templates" : all_templates})
Chris PeBenito 17de1b
	temp_file = "templates.html"
Chris PeBenito 17de1b
	temp_fh = open(temp_file, "w")
Chris PeBenito 17de1b
	body_tpl = pyplate.Template(bodydata)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	body_args = { "menu" : menu_buf, 
Chris PeBenito 17de1b
		      "content" : template_buf }
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	body_tpl.execute(temp_fh, body_args)
Chris PeBenito 17de1b
	temp_fh.close()
Chris PeBenito 17de1b
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	#build the global tunable index
Chris PeBenito 56e1b3
	global_tun = []
Chris PeBenito 17de1b
	for tunable in doc.getElementsByTagName("tunable"):
Chris PeBenito 17de1b
		if tunable.parentNode.nodeName == "policy":
Chris PeBenito 17de1b
			tunable_name = tunable.getAttribute("name")
Chris PeBenito 17de1b
			default_value = tunable.getAttribute("dftval")
Chris PeBenito 17de1b
			for desc in tunable.getElementsByTagName("desc"):
Chris PeBenito 17de1b
				description = format_html_desc(desc)
Chris PeBenito 56e1b3
			global_tun.append( { "tun_name" : tunable_name,
Chris PeBenito 17de1b
						"def_val" : default_value,
Chris PeBenito 17de1b
						"desc" : description } )
Chris PeBenito 56e1b3
	global_tun.sort(tun_cmp)
Chris PeBenito 56e1b3
	global_tun_tpl = pyplate.Template(gtunlistdata)
Chris PeBenito 56e1b3
	global_tun_buf = global_tun_tpl.execute_string({"tunables" : global_tun})
Chris PeBenito 17de1b
	global_tun_file = "global_tunables.html"
Chris PeBenito 17de1b
	global_tun_fh = open(global_tun_file, "w")
Chris PeBenito 17de1b
	body_tpl = pyplate.Template(bodydata)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	body_args = { "menu" : menu_buf,
Chris PeBenito 17de1b
		      "content" : global_tun_buf }
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	body_tpl.execute(global_tun_fh, body_args)
Chris PeBenito 17de1b
	global_tun_fh.close()
Chris PeBenito 17de1b
Chris PeBenito 56e1b3
	#build the tunable index
Chris PeBenito 56e1b3
	all_tunables = all_tunables + global_tun
Chris PeBenito 56e1b3
	all_tunables.sort(tun_cmp)
Chris PeBenito 56e1b3
	tunable_tpl = pyplate.Template(tunlistdata)
Chris PeBenito 56e1b3
	tunable_buf = tunable_tpl.execute_string({"tunables" : all_tunables})
Chris PeBenito 56e1b3
	temp_file = "tunables.html"
Chris PeBenito 56e1b3
	temp_fh = open(temp_file, "w")
Chris PeBenito 56e1b3
	body_tpl = pyplate.Template(bodydata)
Chris PeBenito 56e1b3
Chris PeBenito 56e1b3
	body_args = { "menu" : menu_buf, 
Chris PeBenito 56e1b3
		      "content" : tunable_buf }
Chris PeBenito 56e1b3
Chris PeBenito 56e1b3
	body_tpl.execute(temp_fh, body_args)
Chris PeBenito 56e1b3
	temp_fh.close()
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	#build the global boolean index
Chris PeBenito 56e1b3
	global_bool = []
Chris PeBenito 17de1b
	for boolean in doc.getElementsByTagName("bool"):
Chris PeBenito 17de1b
		if boolean.parentNode.nodeName == "policy":
Chris PeBenito 17de1b
			bool_name = boolean.getAttribute("name")
Chris PeBenito 17de1b
			default_value = boolean.getAttribute("dftval")
Chris PeBenito 17de1b
			for desc in boolean.getElementsByTagName("desc"):
Chris PeBenito 17de1b
				description = format_html_desc(desc)
Chris PeBenito 56e1b3
			global_bool.append( { "bool_name" : bool_name,
Chris PeBenito 17de1b
						"def_val" : default_value,
Chris PeBenito 17de1b
						"desc" : description } )
Chris PeBenito 56e1b3
	global_bool.sort(bool_cmp)
Chris PeBenito 56e1b3
	global_bool_tpl = pyplate.Template(gboollistdata)
Chris PeBenito 56e1b3
	global_bool_buf = global_bool_tpl.execute_string({"booleans" : global_bool})
Chris PeBenito 17de1b
	global_bool_file = "global_booleans.html"
Chris PeBenito 17de1b
	global_bool_fh = open(global_bool_file, "w")
Chris PeBenito 17de1b
	body_tpl = pyplate.Template(bodydata)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	body_args = { "menu" : menu_buf,
Chris PeBenito 17de1b
		      "content" : global_bool_buf }
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	body_tpl.execute(global_bool_fh, body_args)
Chris PeBenito 17de1b
	global_bool_fh.close()
Chris PeBenito 56e1b3
	
Chris PeBenito 56e1b3
	#build the boolean index
Chris PeBenito 56e1b3
	all_booleans = all_booleans + global_bool
Chris PeBenito 56e1b3
	all_booleans.sort(bool_cmp)
Chris PeBenito 56e1b3
	boolean_tpl = pyplate.Template(boollistdata)
Chris PeBenito 56e1b3
	boolean_buf = boolean_tpl.execute_string({"booleans" : all_booleans})
Chris PeBenito 56e1b3
	temp_file = "booleans.html"
Chris PeBenito 56e1b3
	temp_fh = open(temp_file, "w")
Chris PeBenito 56e1b3
	body_tpl = pyplate.Template(bodydata)
Chris PeBenito 56e1b3
Chris PeBenito 56e1b3
	body_args = { "menu" : menu_buf, 
Chris PeBenito 56e1b3
		      "content" : boolean_buf }
Chris PeBenito 56e1b3
Chris PeBenito 56e1b3
	body_tpl.execute(temp_fh, body_args)
Chris PeBenito 56e1b3
	temp_fh.close()
Chris PeBenito 17de1b
Chris PeBenito 17de1b
Chris PeBenito 17de1b
Chris PeBenito 17de1b
def error(error):
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	Print an error message and exit.
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
Chris PeBenito 17de1b
        sys.stderr.write("%s exiting for: " % sys.argv[0])
Chris PeBenito 17de1b
        sys.stderr.write("%s\n" % error)
Chris PeBenito 17de1b
        sys.stderr.flush()
Chris PeBenito 17de1b
        sys.exit(1)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
def warning(warn):
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	Print a warning message.
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	sys.stderr.write("%s warning: " % sys.argv[0])
Chris PeBenito 17de1b
	sys.stderr.write("%s\n" % warn)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
def usage():
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
	Describes the proper usage of this tool.
Chris PeBenito 17de1b
	"""
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	sys.stdout.write("%s [-tmdT] -x <xmlfile>\n\n" % sys.argv[0])
Chris PeBenito 17de1b
	sys.stdout.write("Options:\n")
Chris PeBenito 17de1b
	sys.stdout.write("-b --booleans	<file>		--	write boolean config to <file>\n")
Chris PeBenito 17de1b
	sys.stdout.write("-m --modules <file>		--	write module config to <file>\n")
Chris PeBenito 17de1b
	sys.stdout.write("-d --docs <dir>		--	write interface documentation to <dir>\n")
Chris PeBenito 17de1b
	sys.stdout.write("-x --xml <file>		--	filename to read xml data from\n")
Chris PeBenito 17de1b
	sys.stdout.write("-T --templates <dir>		--	template directory for documents\n")
Chris PeBenito 17de1b
Chris PeBenito 17de1b
Chris PeBenito 17de1b
# MAIN PROGRAM
Chris PeBenito 17de1b
try:
Chris PeBenito 17de1b
	opts, args = getopt.getopt(sys.argv[1:], "b:m:d:x:T:", ["booleans","modules","docs","xml", "templates"])
Chris PeBenito 17de1b
except getopt.GetoptError:
Chris PeBenito 17de1b
	usage()
Chris PeBenito 17de1b
	sys.exit(1)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
booleans = modules = docsdir = None
Chris PeBenito 17de1b
templatedir = "templates/"
Chris PeBenito 17de1b
xmlfile = "policy.xml"
Chris PeBenito 17de1b
Chris PeBenito 17de1b
for opt, val in opts:
Chris PeBenito 17de1b
	if opt in ("-b", "--booleans"):
Chris PeBenito 17de1b
		booleans = val
Chris PeBenito 17de1b
	if opt in ("-m", "--modules"):
Chris PeBenito 17de1b
		modules = val
Chris PeBenito 17de1b
	if opt in ("-d", "--docs"):
Chris PeBenito 17de1b
		docsdir = val
Chris PeBenito 17de1b
	if opt in ("-x", "--xml"):
Chris PeBenito 17de1b
		xmlfile = val
Chris PeBenito 17de1b
	if opt in ("-T", "--templates"):
Chris PeBenito 17de1b
		templatedir = val
Chris PeBenito 17de1b
Chris PeBenito 17de1b
doc = read_policy_xml(xmlfile)
Chris PeBenito 17de1b
		
Chris PeBenito 17de1b
if booleans:
Chris PeBenito 17de1b
	namevalue_list = []
Chris PeBenito 17de1b
	if os.path.exists(booleans):
Chris PeBenito 17de1b
		try:
Chris PeBenito 17de1b
			conf = open(booleans, 'r')
Chris PeBenito 17de1b
		except:
Chris PeBenito 17de1b
			error("Could not open booleans file for reading")
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		namevalue_list = get_conf(conf)
Chris PeBenito 17de1b
Chris PeBenito 17de1b
		conf.close()
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	try:
Chris PeBenito 17de1b
		conf = open(booleans, 'w')
Chris PeBenito 17de1b
	except:
Chris PeBenito 17de1b
		error("Could not open booleans file for writing")
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	gen_booleans_conf(doc, conf, namevalue_list)
Chris PeBenito 17de1b
	conf.close()
Chris PeBenito 17de1b
Chris PeBenito 17de1b
Chris PeBenito 17de1b
if modules:
Chris PeBenito 17de1b
	namevalue_list = []
Chris PeBenito 17de1b
	if os.path.exists(modules):
Chris PeBenito 17de1b
		try:
Chris PeBenito 17de1b
			conf = open(modules, 'r')
Chris PeBenito 17de1b
		except:
Chris PeBenito 17de1b
			error("Could not open modules file for reading")
Chris PeBenito 17de1b
		namevalue_list = get_conf(conf)	
Chris PeBenito 17de1b
		conf.close()
Chris PeBenito 17de1b
Chris PeBenito 17de1b
	try:
Chris PeBenito 17de1b
		conf = open(modules, 'w')
Chris PeBenito 17de1b
	except:
Chris PeBenito 17de1b
		error("Could not open modules file for writing")
Chris PeBenito 17de1b
	gen_module_conf(doc, conf, namevalue_list)
Chris PeBenito 17de1b
	conf.close()
Chris PeBenito 17de1b
Chris PeBenito 17de1b
if docsdir: 
Chris PeBenito 17de1b
	gen_docs(doc, docsdir, templatedir)