How to check version of Python under CGI

Johann observer at NOSPAM.space.pl
Thu Jun 13 06:41:03 EDT 2002


On 13 Jun 2002 11:30:00 +0200, Sylvain Thenault <thenault at nerim.net>
wrote:

>I didn't see the solution on the newsgroup, could you share it ?

source: http://snakefarm.org/snakecharmer.tar.gz

#!/usr/bin/env python

#
# SnakeCharmer v1.3, Carsten Gaebler (cg at snakefarm.org)
#
# SnakeCharmer gives you the location of your Python interpreter,
# your path to Sendmail, your environment variables and a list of the
# Python modules that are installed on your web server.
#

import glob, os, string, sys, traceback
from stat import *

def is_executable(path):
    return (os.stat(path)[ST_MODE] & (S_IXUSR | S_IXGRP | S_IXOTH)) !=
0

def get_toplevel_libdirs():
    libdirs = sys.path[1:]
    libdirs.sort()

    tlds = {}
    for i in range(len(libdirs) - 1):
        cp = os.path.commonprefix(libdirs[i:i+2])
        if cp == "/":
            tlds[libdirs[i]] = None
            continue
        if cp[-1] == "/":
            cp = cp[:-1]
        if os.path.isdir(cp):
            tlds[cp] = None
        else:
            tlds[libdirs[i]] = None

    libdirs = tlds.keys()
    libdirs.sort()
    return libdirs
	

def listmodules(arg, dirname, names):	
    has_init_py = '__init__.py' in names
    if has_init_py:
	for p in sys_path:
            if dirname[:len(p)] == p:
                dirname = dirname[len(p):]
                break
        if dirname[:1] == '/':
            dirname = dirname[1:]
        if dirname[:4] == 'test':
            return
	dirname = string.replace(dirname, "/", ".")
	installedmodules.append(dirname)
        
    for mod in names:
        if (mod[0] != "_") and (mod[:4] != 'test'):
            ext = mod[-3:]
            if ext == '.py':
                mod = mod[:-3]
            elif ext == '.so':
                if mod[-9:-3] == 'module':
                    mod = mod[:-9]
                else:
                    mod = mod[:-3]
            else:
                continue
            if has_init_py:
                installedmodules.append(dirname + "." + mod)
            else:
                installedmodules.append(mod)


print "Content-Type: text/html"
print "X-Powered-By: Python %s" %
string.join(string.split(sys.version), " ")
print '''
<html><body bgcolor="#FFFFFF" alink="#FDB900" link="#BF0425"
vlink="#1200FD">
<h3><center><a
href="http://snakefarm.org/">snakefarm.org</a></center></h3>
<h1><center>Snake Charmer 1.3</center></h1>
'''

try:
    sys_path = sys.path[1:]
    sys_path.sort(lambda a, b: cmp(len(b), len(a)))

    installedmodules = list(sys.builtin_module_names)

    for dir in get_toplevel_libdirs():
        os.path.walk(dir, listmodules, None)

    installedmodules.sort()
    for i in range(len(installedmodules) % 3):
        installedmodules.append('')

    py = {}
    py["version"] = string.split(sys.version)[0]
    py["platform"]= sys.platform

    location = string.split(os.popen("whereis python", "r").read())
    location = filter(lambda p: p[0] == '/', location)
    location.sort()

    for i in range(len(location)):
        if os.path.islink(location[i]):
            location[i] = location[i] + " -> " +
os.readlink(location[i])

    py["location"] = string.join(location, "<br>\n")

    location = string.split(os.popen("whereis sendmail", "r").read())
    location = filter(lambda p: p[0] == '/', location)
    location.sort()
    location = filter(is_executable, location)
    py["sendmail"] = string.join(location, "<br>\n")

    libdirs = sys.path
    if libdirs[0] == '':
        libdirs[0] = './'

    py["libdirs"] = string.join(libdirs, "<br>\n")
    font = '<font face="Verdana, Arial, Sans Serif">'
    py["font"] = font

    print '<table width="100%">'
    print """
    <tr><td BGCOLOR='#99ccff' colspan="2"
align="center">%(font)s<b>Program Paths</b></td></tr>
    <tr><td bgcolor="GHOSTWHITE" width="35%%"
valign="top">%(font)s<b>Python version</b></td><td
width="65%%">%(font)s%(version)s</td></tr>
    <tr><td bgcolor="GHOSTWHITE" width="35%%"
valign="top">%(font)s<b>Python OS platform</b></td><td
width="65%%">%(font)s%(platform)s</td></tr>
    <tr><td bgcolor="GHOSTWHITE" width="35%%"
valign="top">%(font)s<b>Location of Python</b></td><td
width="65%%">%(font)s%(location)s</td></tr>
    <tr><td bgcolor="GHOSTWHITE"width="35%%"
valign="top">%(font)s<b>Location of Sendmail</b></td><td
width="65%%">%(font)s%(sendmail)s</td></tr>
    <tr><td bgcolor="GHOSTWHITE"width="35%%"
valign="top">%(font)s<b>Directories searched for Python
modules<b></td><td width="65%%">%(font)s%(libdirs)s</td></tr>
    """ % py
    print "</table>"

    print '''<table width="100%%">
    <tr><th BGCOLOR="#99ccff" colspan="2" align="center">%sEnvironment
Variables</td></tr>
    ''' % font

    envvars = os.environ.keys()
    envvars.sort()
    for envvar in envvars:
        value = os.environ[envvar]
        print '<tr><td bgcolor="GHOSTWHITE" width="35%%"
valign="top">%s<b>%s</b></td><td width="65%%">%s%s</td></tr>' % (font,
envvar, font, value)
    print "</table>"

    print '''<table width="100%%">
    <tr><th BGCOLOR="#99ccff" align="center">%sInstalled
Modules</th></tr>
    <tr><td><pre>
    ''' % font

    rows = len(installedmodules) / 3
    mods = [ [], [], [] ]
    maxlen = max(map(len, installedmodules))

    for i in range(rows):
            s = "%%-%ds %%-%ds %%s" % (maxlen, maxlen)
            print s % (installedmodules[i], installedmodules[rows +
i], installedmodules[2*rows + i])

    print """
    </pre></td></tr>
    </table>
    </body></html>
    """

except:
    tb = traceback.format_exception(sys.exc_type, sys.exc_value,
sys.exc_traceback)
    tb = string.join(tb, "")
    print '''
    <pre>
    %s
    </pre>
    </body>
    </html>
    ''' % tb
    




More information about the Python-list mailing list