#!c:\Program Files\Python39\python.exe
# -*- coding: utf-8 -*-

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

# import pydevd; pydevd.settrace();
import subprocess
import biocase.configuration
from biocase.tools.various_functions import folder_not_writable

cfg = biocase.configuration.Cfg()
import graphviz

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>''' % ".".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 only if required

<!-- 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>Hint</strong></th>
        </tr>''')

# biocase libs
print('''
        <tr >
          <td>BioCASe</td>
          <td>''')
try:
    import biocase.tools.update
    print('<span class="goodstatus">' + biocase.__version__ + '</span>')
except Exception as err:
    print('<span class="dependstatus" title="%s">not installed</span>' % err)

print('</td><td>%s</td></tr></table>' % biocase.tools.update.update_hint())


# QueryTool
print('''<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>Hint</strong></th>
        </tr>
''')
# libxml2/libxslt
print('''
        <tr >
          <td>lxml</td>
          <td>''')
try:
    import lxml
    try:
        v = lxml.__version__
    except:
        v = 'installed'
    print('<span class="goodstatus">%s</span>' % v)
except Exception as err:
    print('<span class="dependstatus" title="%s">not installed</span>' % err)

print('''</td>
            <td>Use PIP to install from <a href="https://pypi.org/project/lxml/">PyPI</a>: <i>pip install lxml</i>.</td>
        </tr>
    </table>''')

# HTML
print('''

<!-- 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>''')

# pyODBC
print('''        <tr >
          <td>pyodbc</td>
          <td>''')
try:
    import pyodbc
    print('<span class="goodstatus">' + pyodbc.version + '</span>')
except Exception as err:
    print('<span class="dependstatus" title="%s">not installed</span>' % err)


print('''</td>
            <td>Use PIP to install from <a href="https://pypi.org/project/pyodbc/">PyPI</a>: <i>pip install pyodbc</i>.</td>
        </tr>''')

# pymssql
print('''        <tr >
          <td>pymssql</td>
          <td>''')
try:
    import pymssql
    print('<span class="goodstatus">' + pymssql.__version__ + '</span>')
except Exception as err:
    print('<span class="dependstatus" title="%s">not installed</span>' % err)

print('''</td>
            <td>Use PIP to install from <a href="https://pypi.org/project/pymssql/">PyPI</a>: <i>pip install pymssql</i>.</td>
        </tr>''')

# psycopg2
print('''
        <tr >
          <td>psycopg2</td>
          <td>''')
try:
    import psycopg2
    print('<span class="goodstatus">' + psycopg2.__version__ + '</span>')
except Exception as err:
    print('<span class="dependstatus" title="%s">not installed</span>' % err)

print('''</td><td>Use PIP to install from <a href="https://pypi.org/project/psycopg2/">PyPI</a>: <i>pip install psycopg2</i>.</td>
        </tr>''')

# MySQLdb
print('''
        <tr >
          <td>MySQLdb</td>
          <td>''')
try:
    import MySQLdb
    v = '.'.join([str(i) for i in MySQLdb.version_info])
    print('<span class="goodstatus">' + v + '</span>')
except Exception as err:
    print('<span class="dependstatus" title="%s">not installed</span>' % err)

print('''</td>
            <td>Use PIP to install from <a href="https://pypi.org/project/mysqlclient/">PyPI</a>: <i>pip install mysqlclient</i>.</td>
        </tr>''')

# kinterbasdb
print('''
        <tr >
          <td>fdb</td>
          <td>''')
try:
    import fdb
    print('<span class="goodstatus">' + fdb.__version__ + '</span>')
except Exception as err:
    print('<span class="dependstatus" title="%s">not installed</span>' % err)

print('''</td>
            <td>Use PIP to install from <a href="https://pypi.org/project/fdb/">PyPI</a>: <i>pip install fdb</i>.</td>
        </tr>''')

# SQLlite
print('''
        <tr >
          <td>SQLite3</td>
          <td>''')
try:
    import sqlite3
    print('<span class="goodstatus">' + sqlite3.version + '</span>')
except Exception as err:
    print('<span class="dependstatus" title="%s">not installed</span>' % err)

print('''</td>
            <td>(integrated into Python)</td>
        </tr>
''')

# cx_Oracle
print('''
        <tr >
          <td>cx_Oracle</td>
          <td>''')
try:
    import cx_Oracle
    print('<span class="goodstatus">' + cx_Oracle.version + '</span>')
except Exception as err:
    print('<span class="dependstatus" title="%s">not installed</span>' % err)

print('''</td>
            <td><a href="http://cx-oracle.sourceforge.net/">Oracle module</a>, you also need the <a href="http://www.oracle.com/technetwork/database/index.html">Oracle Client</a></td>
        </tr>''')

# Sybase
print('''
        <tr >
          <td>Sybase</td>
          <td>''')
try:
    import Sybase
    print('<span class="goodstatus">' + Sybase.__version__ + '</span>')
except Exception as err:
    print('<span class="dependstatus" title="%s">not installed</span>' % err)

print('''</td>
            <td><a href="http://www.object-craft.com.au/projects/sybase/">Sybase module</a></td>
        </tr>
</table>
<br>
<p><img src="../resources/images/tooltip.png"/>
If you can't find the Windows installer package for your Python version, try your luck <a href="http://www.lfd.uci.edu/~gohlke/pythonlibs/" target="_blank">here</a>.</p>
''')

# DETECT GRAPHVIZ, external binaries
try:
    if cfg.server.graphviz_dot:
        os.environ["PATH"] += os.pathsep + cfg.server.graphviz_dot
        version = '.'.join([str(i) for i in graphviz.version()])
        graphviz_status = '<span class="goodstatus">%s</span>' % version
    else:
        graphviz_status = '<span class="dependstatus">not configured</span>'
except Exception as err:
    graphviz_status = '<span class="dependstatus" title="%s">Binary not found</span>' % err

# Java
try:
    proc = subprocess.Popen([cfg.server.java, '-version'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    version = str(proc.stderr.readline())
    java_status = '<span class="goodstatus">%s</span>' % version.replace("b'java version ", '').replace("b'openjdk version ", '').replace('"', '').replace("\\r", "").replace("\\n", "").replace("'", "")
except Exception as err:
    java_status = '<span class="dependstatus" title="%s">Binary not found</span>' % err

print('''
<h4>&nbsp;</h4>
<h2>Optional external binaries</h2>
      <table>
        <tr >
          <th width="120"><strong>Binary</strong></th>
          <th width="120"><strong>Status/Version</strong></th>
          <th width="360"><strong>Path</strong></th>
        </tr>
        <tr >
          <td>%s</td>
          <td>%s</td>
          <td>%s</td>
        </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,
      '<a href="http://www.java.com/de/download/">Java</a>', java_status, cfg.server.java))

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>'

statusArchiveWork = folder_not_writable(cfg.archiveWorkLocator)
if statusArchiveWork is None:
    awrk_folder_writable_status = '<span class="goodstatus">Writeable</span>'
else:
    awrk_folder_writable_status = '<span class="badstatus">Unwriteable</span>'

statusArchiveDownload = folder_not_writable(cfg.archiveDownloadLocator)
if statusArchiveDownload is None:
    adld_folder_writable_status = '<span class="goodstatus">Writeable</span>'
else:
    adld_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>
        <tr >
          <td>Archive temp dir</td>
          <td>%s</td>
          <td>%s</td>
        </tr>
        <tr >
          <td>Archive download Dir</td>
          <td>%s</td>
          <td>%s</td>
        </tr>
      </table>
''' % (config_folder_writable_status, str(statusCfg), log_folder_writable_status, str(statusLog), awrk_folder_writable_status, str(statusArchiveWork), adld_folder_writable_status, str(statusArchiveDownload)))

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>
''')