#!/usr/bin/python

'''Python CGI test script by Ben Hutchings <ben@decadentplace.org.uk>.

Use this to find out what Python facilities your web server provides.
Upload it to a directory where you can run CGI scripts.  If the web
server runs Unix, remember to make the uploaded script executable.
Then load the URL for the CGI script in a web browser.  If this
produces an error message, try changing the interpreter name on the
first line to /usr/local/bin/python and uploading the script again.
Once it is working, you should have a list of available Python
executables.  You may be able to choose an alternate version of
Python by changing the interpreter name again.'''

import os, os.path, re, string, sys

def esc(s):
    return string.replace(string.replace(s, '&', '&amp;'), '<', '&lt;')

print 'Content-Type: text/html'
print ''

print '<title>Python CGI test</title>'
print '<h1>Python CGI test</h1>'

print '<p>Python executable:', esc(sys.executable), '</p>'
print '<p>Python version:', esc(sys.version), '</p>'

os_path = string.split(os.environ['PATH'], os.pathsep)
print '<p>OS path:</p>'
print '<ul>'
for dir_name in os_path:
    print '<li>', esc(dir_name), '</li>'
print '</ul>'
print '<p>Python executables found in OS path:</p>'
print '<ul>'
for dir_name in os_path:
    try:
        for file_name in os.listdir(dir_name):
            if file_name[:6] == 'python':
                print '<li>', esc(os.path.join(dir_name, file_name)), '</li>'
    except OSError:
        pass
print '</ul>'

print '<p>Python path:</p>'
print '<ul>'
for dir_name in sys.path:
    print '<li>', esc(dir_name), '</li>'
print '</ul>'
print '<p>Python modules found in Python path or built-in:</p>'
modules = {}
for name in sys.builtin_module_names:
    modules[name] = '(built-in)'
for dir_name in sys.path:
    # Python treats an empty string in the path as the current directory
    if dir_name == '':
        dir_name = os.curdir
    # Handle directories
    if os.path.isdir(dir_name):
        try:
            for file_name in os.listdir(dir_name):
                # Match *.py and *.py?
                full_name = os.path.join(dir_name, file_name)
                match = re.match(r'(\w+)\.py\w?$', file_name)
                if match:
                    name = match.group(1)
                    if not modules.has_key(name):
                        modules[name] = full_name
                # Match directories containing __init__.py
                elif (os.path.isdir(full_name)
                      and os.path.isfile(os.path.join(full_name,
                                                      '__init__.py'))):
                    if not modules.has_key(file_name):
                        modules[file_name] = full_name
        except OSError:
            pass
    # Handle zip files
    elif dir_name[:4] == '.zip':
        try:
            list = __import__('zipfile').ZipFile(dir_name).namelist()
            for file_name in list:
                # Match *.py, *.py? and */__init__.py
                match = re.match(r'(\w+)([/\\]__init__\.py|\.py\w?$',
                                 file_name)
                if match:
                    name = match.group(1)
                    if not modules.has_key(name):
                        modules[name] = dir_name
        except (ImportError, IOError):
            pass
names = modules.keys()
names.sort()
print '<table>'
print '<tr><th>Name</th><th>Filename</th></tr>'
for name in names:
    print '<tr><td>', name, '</td><td>', modules[name], '</td></tr>'
print '</table>'
