#!/usr/bin/env python
#
# This script is used to generate:
#
# o A HTML document containing every test-case related file 
#  in the RDF Core WG's Test Case Repository:
#
#   http://www.w3.org/2000/10/rdf-tests/rdfcore/
#
#  Each file is placed in a table of Approved, Not Approved or
#  Obsolete test case depending on the status of the file.
#
# o A HTML table of the Approved tests. This table is used in 
#  the RDF Test Cases document:
#
#   http://www.w3.org/TR/rdf-testcases/ 
#
# o A Makefile that can be used to generate a ZIP file of only
#  the Approved test cases and a ZIP file of the Approved plus
#  Not Approved test cases.  Obsolete files are not added to
#  either ZIP file.
# 
#####################################################################
#
# Usage: $0 [-doc] [-table] [-make] file_name
#
# -doc - generate the HTML document (with every test file) described 
#   above
#
# -table - generate a table of Approved test cases
#
# -make - generate the Makefile for creating ZIP files
#
# file_name - a file containing all of the test file data. See the
#   following file for more information (including the syntax of the 
#   data file that is assumed by this script):
#
#    http://www.w3.org/2000/10/rdf-tests/rdfcore/all_files
#
#####################################################################
#
# Copyright World Wide Web Consortium, (Massachusetts Institute of
# Technology, Institut National de Recherche en Informatique et en
# Automatique, Keio University).
#
# All Rights Reserved.
#
# Please see the full Copyright clause at
# <http://www.w3.org/Consortium/Legal/copyright-software.html>
#
# $Id: repo_mgmt.py,v 1.6 2001/10/31 14:31:29 barstow Exp $
# 
#####################################################################

import sys;
import string;

#
# Constants
#
REPOSITORY_TOP = "http://www.w3.org/2000/10/rdf-tests/rdfcore"
ISSUE_TOP = "http://www.w3.org/2000/03/rdf-tracking"
APPROVED = "APPROVED"
NOT_APPROVED = "NOT_APPROVED"
OBSOLETE = "OBSOLETE"

#
# Global vars for the command line options
#
doc = ""
table = ""
make = ""
input_file = ""

def process_args():
	"Process the input arguments."
	global input_file, doc, table, make

	usage = "USAGE: $0 [-doc] [-table] [-make] input_file"

	if len(sys.argv) == 1:
		print usage
		sys.exit(1)

	for i in range(len(sys.argv)):
		if i == 0:
			continue;
		if sys.argv[i] == "-doc":
			doc = "yes"
		elif sys.argv[i] == "-table":
			table = "yes"
		elif sys.argv[i] == "-make":
			make = "yes"
		else:
			input_file = sys.argv[i]

	if input_file == "":
		print usage
		sys.exit(1)

def print_heading(tag, name, heading):
	"Print a HTML headline element (e.g. h1, h2)."
	print "<" + tag + "><a name=\"" + name + "\"></a>" + heading + "</" + tag + ">"

def print_doc_start():
	"Print the boilerplate for a HTML document."
	print "<?xml version=\"1.0\" encoding=\"us-ascii\"?>"
	print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\""
	print "    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
	print "<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">"
	print "<head>"
	print "<title>RDF Test Case Repository</title>"
	print "</head>"
	print "<body bgcolor=\"#FFFFFF\">"
	print "<a href=\"http://www.w3.org/\"><img src=\"http://www.w3.org/Icons/WWW/w3c_home\" alt=\"W3C\" class=\"W3CIcon\" border=\"0\"/></a>"

def print_doc_end():
	"Print the end elements for a HTML document."
	print "<hr />"
	print "$Id: repo_mgmt.py,v 1.6 2001/10/31 14:31:29 barstow Exp $"
	print "</body>"
	print "</html>"

def print_table_start():
	"Print a HTML table header."
	print "<table border='1' width='100%'>"
	print " <tbody>"
	print "  <tr>"
	print "   <th>Issue</th>"
	print "   <th>Test Case</th>"
	print "   <th>Status</th>"
	print "  </tr>"

def print_table_end():
	"Print a HTML table footer."
	print "</tbody>"
	print "</table>"

def print_table_row(dir_file, issue, status, uri):
	"Print one row of a HTML table."
	tmp = string.split(dir_file, '/')
	dir = tmp[0]
	file = tmp[1]

	global REPOSITORY_TOP, ISSUE_TOP

	print "  <tr>"

	#
	# column 1 - issue name
	#
	if issue == "NOT_ISSUE":
		print "   <td>" + dir + " [No Issue]</td>"
	else:
		print "   <td><a href='" + ISSUE_TOP + "/#" + dir + "'>" + dir + "</a></td>"

	#
	# column 2 - file in the repository
	#
	print "   <td><a href='" + REPOSITORY_TOP + "/" + dir + "/" + file + "'>" + file + "</a></td>"

	#
	# column 3 - the approved URI or a label
	#
	if status == APPROVED:
		tmp_status = "Approved"
	elif status == NOT_APPROVED:
		tmp_status = "Not Approved"
	elif status == OBSOLETE:
		tmp_status = "Obsolete"
	else:
		tmp_status = "Status Unknown"

	if uri == "":
		print "   <td>" + tmp_status + "</td>"
	else:
		print "   <td><a href='" + uri + "'>" + tmp_status + "</a></td>"

	print "  </tr>"

def print_list(l):
	"Given a list, print it as HTML table syntax."
	for i in range(len(l)):
		line = l[i]
		dir_file = string.split(line)[0]
		issue = string.split(line)[1]
		status = string.split(line)[2]

		# May not have a fourth column
		try:
			uri = string.split(line)[3]
		except:
			uri = ""

		print_table_row(dir_file, issue, status, uri)

def print_table():
	"Drive the creation of the Approved test case table."
	global approved

	print_table_start()
	print_list(approved)
	print_table_end()

def print_document():
	"Drive the creation of a HTML document for all test case files."
	global approved, not_approved, obsolete

	print_doc_start()
	print_heading("h1", "intro", "RDF Test Case Repository")

	print_heading("h2", "approved", "Approved Test Cases")
	print_table_start()
	print_list(approved)
	print_table_end()

	print_heading("h2", "not_approved", "Test Cases Not Approved")
	print_table_start()
	print_list(not_approved)
	print_table_end()

	print_heading("h2", "obsolete", "Obsolete Test Cases")
	print_table_start()
	print_list(obsolete)
	print_table_end()

	print_doc_end()

def print_copyright():
	"Print a W3C copyright using Makefile comment syntax."
	print "# WARNING: this file was automatically generated!"
	print "#"
	print "# This Makefile generates ZIP files of the Test Case repository."
	print "#"
	print "# Copyright World Wide Web Consortium, (Massachusetts Institute of  "
	print "# Technology, Institut National de Recherche en Informatique et en  "
	print "# Automatique, Keio University)."
	print "# "
	print "# All Rights Reserved."
	print "#"
	print "# Please see the full Copyright clause at"
	print "# <http://www.w3.org/Consortium/Legal/copyright-software.html>"
	print "#"
	print "# $Id: repo_mgmt.py,v 1.6 2001/10/31 14:31:29 barstow Exp $"
	print "#"
	print "#####################################################################"
	print "#"

def print_makefile_rules():
	"Print the Makefile target rules."
	print ""
	print "ARCHIVE_ALL = latest_All"
	print ""
	print "ARCHIVE_APPROVED = latest_Approved"
	print ""
	print "ALL_FILE = $(FILES_APPROVED) $(FILES_NOT_APPROVED)"
	print ""
	print "all:"
	print "\trm -f $(ARCHIVE_ALL).zip"
	print "\tzip -r $(ARCHIVE_ALL) $(FILES_APPROVED) $(FILES_NOT_APPROVED)"
	print ""
	print "approved:"
	print "\trm -f $(ARCHIVE_APPROVED).zip"
	print "\tzip -r $(ARCHIVE_APPROVED) $(FILES_APPROVED)"
	print ""
	print "clean:"
	print "\trm -f $(ARCHIVE_ALL).zip $(ARCHIVE_APPROVED).zip"
	print ""

def print_file_list(macro, l):
	"Print the given Makefile macro name and its list of items."
	if len(l) <= 0:
		return
	print "\n" + macro + " = \\"

	l_len = len(l)

	for i in range(len(l)):
		line = l[i]
		dir_file = string.split(line)[0]
		if (i + 1) < l_len:
			print "\t" + dir_file + " \\"
		else:
			print "\t" + dir_file

def print_makefile():
	"Drive the creation of the Makefile."
	global approved, not_approved

	print_copyright()

	print_file_list("FILES_APPROVED", approved)

	print_file_list("FILES_NOT_APPROVED", not_approved)

	print_makefile_rules()

########################################################################
#
# Main program
#
process_args()

# Open the input file
try:
	lines = open(input_file).readlines()
except:
	print "An ERROR occured trying to read the input file '", input_file, "'"
	sys.exit(0)

#
# The lists
#
approved = []
not_approved = []
obsolete = []

line_num = 0

#
# Loop over the input file and build the lists based on each file's 
# status column
#
for line in lines:
	line_num = line_num + 1

	try:
		dir_file = string.split(line)[0]
	except:
		# blank line or some other error
		continue

	if (dir_file[0] == "#") | (len(dir_file) == 0):
		continue

	status = string.split(line)[2]

	if status == APPROVED:
		approved.append(line)
	elif status == NOT_APPROVED:
		not_approved.append(line)
	elif status == OBSOLETE:
		obsolete.append(line)
	else:
		print "ERROR - line ", line_num, " : status ", status, " is NOT supported"

#
# Create a HTML doc, a table of Approved test cases or the Makefile
# (or depending on the input arguments - may do nothing)
#
if doc != "":
	print_document()
elif table != "":
	print_table()
elif make != "":
	print_makefile()

sys.exit(0)
