#!/System/Library/Frameworks/Python.framework/Versions/2.5/Resources/Python.app/Contents/MacOS/Python
# -*- coding: utf-8 -*-

# ***** include the biocase.lib directory in the python sys path for importing *****
import os, sys
execfile( os.path.abspath(os.path.join(os.path.dirname( __file__ ), os.path.pardir, os.path.pardir, 'lib', 'biocase', 'adjustpath.py' ) ) )

import cgitb; cgitb.enable()
import string
import biocase.configuration

cfg = biocase.configuration.Cfg()

print 'Content-Type: text/html'
print # Blank line marking end of HTTP headers

print '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Python library tests</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../resources/css/biocase.css" rel="stylesheet" type="text/css">
<link rel="SHORTCUT ICON" href="../resources/images/favicon.ico">
</head>

<body>

<!-- HEADER -->
<div id="Header">
Python Library Tests
</div>
<div id="NavBar"><a href="../index.cgi">Start</a> <span class="breadcrumbSeparator">&raquo;</span> <a href="index.html">Utilities</a> <span class="breadcrumbSeparator">&raquo;</span><strong>Test libraries</strong> </div>

<div id="Content">

<h2>Python Interpreter: %s </span></h2>''' % string.join([str(i) for i in sys.version_info[:3]],".")

print '''<span class="goodstatus">green:</span> Ok! | <span class="badstatus">red:</span> you have to install it | <span class="dependstatus">orange:</span> install the one for your database 

<!-- START REQUIRED LIBS TABLES -->
<h2>Libraries required for core wrapper</h2>
      <table>
        <tr > 
          <th width="120"><strong>Library</strong></th>
          <th width="120"><strong>Installed Version</strong></th>
          <th width="360"><strong>Download Library</strong></th>
        </tr>'''

# biocase libs
print '''
        <tr > 
          <td>BioCASE lib</td>
          <td>'''
try:
    import biocase
    print '<span class="goodstatus">' + biocase.__version__ + '</span>'
except:
    print '<span class="badstatus">not installed</span>'

print '''</td>
            <td>BioCASE libs</td>
        </tr>'''


# HTML
print'''
    </table>   
<h4>&nbsp;</h4>
<h2>Libraries only needed for querytool:</h2>
      <table>
        <tr > 
          <th width="120"><strong>Library</strong></th>
          <th width="120"><strong>Installed Version</strong></th>
          <th width="360"><strong>Download Library</strong></th>
        </tr>
'''
# 4suite
print '''
        <tr > 
          <td>4Suite XML</td>
          <td>'''
try:
    import Ft
    print '<span class="goodstatus">' + Ft.__version__ + '</span>'
except:
    print '<span class="badstatus">not installed</span>'

print '''</td>
            <td><a href="http://4suite.org/?xslt=downloadproduct.xslt&show=http%3A%2F%2F4suite.org%2Fdata%2Fsoftware%2F4Suite">4Suite XML</a></td>
        </tr>'''


# HTML
print '''
    </table>

<!-- START DRIVER TABLES -->
<h4>&nbsp;</h4>
<h2>Status of additional database dependent drivers:</h2>
      <table>
        <tr > 
          <th width="120"><strong>Library</strong></th>
          <th width="120"><strong>Installed Version</strong></th>
          <th width="360"><strong>Download Library</strong></th>
        </tr>'''
        
# mxODBC
print '''        <tr > 
          <td>mxODBC</td>
          <td>'''
try:
    import mx.ODBC
    print '<span class="goodstatus">' + mx.ODBC.__version__ + '</span>'
except:
    print '<span class="dependstatus">not installed</span>'

print '''</td>
            <td><a href="http://www.egenix.com/files/python/eGenix-mx-Extensions.html#Download-mxCOMMERCIAL">ODBC module</a></td>
        </tr>'''
        
# pymssql
print '''        <tr > 
          <td>pymssql</td>
          <td>'''
try:
    import pymssql
    print '<span class="goodstatus">' + pymssql.__version__ + '</span>'
except:
    print '<span class="dependstatus">not installed</span>'

print '''</td>
            <td>
            <a href="http://pymssql.sourceforge.net/">MS SQL Server module (unix/win)</a></td>
        </tr>'''


# psycopg2
print '''
        <tr > 
          <td>psycopg2</td>
          <td>'''
try:
    import psycopg2
    print '<span class="goodstatus">' + psycopg2.__version__ + '</span>'
except:
    print '<span class="dependstatus">not installed</span>'
          
print '''</td>
            <td><a href="http://initd.org/software/initd/psycopg">PostgreSQL module</a>  <a href="http://stickpeople.com/projects/python/win-psycopg/">win binaries</a></td>
        </tr>'''


# pyPgSQL
print '''
        <tr > 
          <td>pyPgSQL</td>
          <td>'''
try:
    import pyPgSQL
    print '<span class="goodstatus">' + pyPgSQL.__version__ + '</span>'
except:
    print '<span class="dependstatus">not installed</span>'
          
print '''</td>
            <td><a href="http://www.pygresql.org/readme.html#download-pygresql-here">Alternative PostgreSQL module</a> <a href="http://pypgsql.sourceforge.net">@Sourceforge</a></td>
        </tr>'''


# MySQLdb
print '''
        <tr > 
          <td>MySQLdb</td>
          <td>'''
try:
    import MySQLdb
    print '<span class="goodstatus">' + MySQLdb.__version__ + '</span>'
except:
    print '<span class="dependstatus">not installed</span>'
          
print '''</td>
            <td><a href="http://sourceforge.net/projects/mysql-python/">mySQL module</a></td>
        </tr>'''


# cx_Oracle
print '''
        <tr > 
          <td>cx_Oracle</td>
          <td>'''
try:
    import cx_Oracle
    print '<span class="goodstatus">' + cx_Oracle.version + '</span>'
except:
    print '<span class="dependstatus">not installed</span>'
          
print '''</td>
            <td><a href="http://www.cxtools.net/default.aspx?nav=cxorlb">Oracle 8-9 module</a>, you also need the <a href="http://otn.oracle.com/software/content.html">Oracle Client</a></td>
        </tr>'''


# kinterbasdb
print '''
        <tr > 
          <td>kinterbasdb</td>
          <td>'''
try:
    import kinterbasdb
    print '<span class="goodstatus">' + string.join([str(x) for x in kinterbasdb.__version__], ".") + '</span>'
except:
    print '<span class="dependstatus">not installed</span>'
          
print '''</td>
            <td><a href="http://kinterbasdb.sourceforge.net/">Firebird/Interbase module</a></td>
        </tr>'''



# SQLlite
print '''
        <tr > 
          <td>SQLlite</td>
          <td>'''
try:
    import sqlite
    print '<span class="goodstatus">' + sqlite.version + '</span>'
except:
    print '<span class="dependstatus">not installed</span>'
          
print '''</td>
            <td><a href="http://initd.org/tracker/pysqlite">pySQLite module</a></td>
        </tr>
'''

# Sybase
print '''
        <tr > 
          <td>sybase</td>
          <td>'''
try:
    import sybase
    print '<span class="goodstatus">' + sybase.__version__ + '</span>'
except:
    print '<span class="dependstatus">not installed</span>'
          
print '''</td>
            <td><a href="http://www.object-craft.com.au/projects/sybase/">Sybase module</a></td>
        </tr>
</table>        
'''



# DETECT GRAPHVIZ, external binaries
if cfg.server.graphviz_dot is not None and os.path.isfile(cfg.server.graphviz_dot):
    graphviz_status = '<span class="goodstatus">%s</span>' % "Binary found"
else:
    graphviz_status = '<span class="badstatus">%s</span>' % "Binary not found"

print '''
<h4>&nbsp;</h4>
<h2>Optional external binaries:</h2>
      <table>
        <tr > 
          <th width="120"><strong>Binary</strong></th>
          <th width="120"><strong>Status</strong></th>
          <th width="360"><strong>Path</strong></th>
        </tr>
        <tr > 
          <td>%s</td>
          <td>%s</td>
          <td>%s</td>
        </tr>
      </table>          
''' %('<a href="http://www.graphviz.org/Download.php">Graphviz Dot</a>', graphviz_status, cfg.server.graphviz_dot)




#
#Detect if the configuration and log folders are writable rescursively.
#
def folder_not_writable(path, verbose=False):
    '''Returns "---" if folder is writable 
    or the absolute path of the directory or file that is not writable.'''
    
    for root, dirs, files in os.walk(path):
        if verbose:
            print "<br>-->walk dir: %s"%(root)
        # check directories by creating a test dummy file
        for name in dirs:
            fn = os.path.join(root, name, 'dummy1973TestomatKarambolaIggI')
            try:
                f = file(fn, 'a')
            except:
                if verbose:
                    print "<br><strong>DIR %s IS NOT WRITABLE !!!</strong>"%(fn)
                return os.path.join(root, name)
            f.close()
            os.unlink(fn)
            if verbose:
                print "<br>dir %s is writable."%( os.path.join(root, name) )
        # check if files are writable by opening them for appending
        for name in files:
            fn = os.path.join(root, name)
            try:
                f = file(fn, 'a')
            except:
                if verbose:
                    print "<br><strong>FILE %s IS NOT WRITABLE !!!</strong>"%(fn)
                return fn
            if verbose:
                print "<br> file %s is writable."%(fn)
    return None


statusCfg = folder_not_writable(cfg.configurationLocator)
if statusCfg is None:
    config_folder_writable_status = '<span class="goodstatus">Writeable</span>'
else:
    config_folder_writable_status = '<span class="badstatus">Unwriteable</span>'

statusLog = folder_not_writable(cfg.logLocator)
if statusLog is None:
    log_folder_writable_status = '<span class="goodstatus">Writeable</span>'
else:
    log_folder_writable_status = '<span class="badstatus">Unwriteable</span>'


# HTML
print '''
<h4>&nbsp;</h4>
<h2>Status of writable directories and files:</h2>
      <table>
        <tr > 
          <th width="120"><strong>Directory</strong></th>
          <th width="120"><strong>Status</strong></th>
          <th width="360"><strong>Path to problematic file</strong></th>
        </tr>
        <tr > 
          <td>Configuration</td>
          <td>%s</td>
          <td>%s</td>
        </tr>
        <tr > 
          <td>Log</td>
          <td>%s</td>
          <td>%s</td>
        </tr>
      </table>          
''' %(config_folder_writable_status, str(statusCfg), log_folder_writable_status, str(statusLog))
        
print '''
</div>

<!-- FOOTER -->

<div id="Footer"> 
  <a href="http://www.biocase.org"><img src="../resources/images/biocase_powered.gif" width="112" height="34"></a> 
</div>

</body>
</html>
'''
