#! /usr/local/bin/python
### SimCleanPages.py, by Ilya A. Zemskov(zemskov@univer.omsk.su)

"""
 About this module: clean some parameters of pages in db

"""

__version__ = '$ Revision: 0.1.0 $ Date: 2003/05/31 16:40:00 $'

from __future__ import generators
from time import localtime, strftime
import MySQLdb
from ConfigParser import *
from os import getcwd
import sys
#---------------------------------------------------------------
comini = "..\\ini\\common.ini"
dbini = "..\\ini\\db.ini"

#--[ Special variables ]----------------------------------------
try:
    taskdir = sys.argv[1]
except IndexError, msg:
    sys.exit("Error: missing parameter - name of task directory")

try:
    taskinifile = sys.argv[2]
except IndexError, msg:
    sys.exit("Error: missing parameter - name of ini file")

comcfg = ConfigParser()
comcfg.read(comini)
wdir = comcfg.get('Options','WorkDirectory')

dbcfg = ConfigParser()
dbcfg.read(dbini)

cfg = ConfigParser()
cfg.read(wdir+'\\'+taskdir+'\\ini\\'+taskinifile)

sercfg = ConfigParser()
sercfg.read(wdir+'\\'+taskdir+'\\ini\\'+cfg.get('serOptions','CommonIni'))

flog = file(sercfg.get('logOptions','LogDirectory')+
          sercfg.get('logOptions','CleanLogFileName'),
          "a+")

dbconn = MySQLdb.Connect(dbcfg.get('dbOptions','Host'),
                         dbcfg.get('dbOptions','Login'),
                         dbcfg.get('dbOptions','Password'),
                         cfg.get('dbOptions','DBName'))

TRACETOFILE = sercfg.getint('logOptions','LogTraceToFile')

#---------------------------------------------------------------
def putfile(st):
    if TRACETOFILE:
        flog.write(st+"\n")
        flog.flush()
    else:
        print st

def cleanpages(conn, iter):
    curs = conn.cursor()
    curs.execute("update %s set lastch=%d, changed=%d, crawled=%d, wtime=%d where iter=%d"%
                 (dbcfg.get('dbOptions','PagesTableName'),
                  sercfg.getint('pageOptions','DefaultPageChange'),
                  sercfg.getint('pageOptions','DefaultPageChanged'),
                  sercfg.getint('pageOptions','DefaultPageCrawled'),
                  sercfg.getint('simOptions','ModelTime'),
                  iter))

    curs.execute("update %s set nsize=size"%
                 dbcfg.get('dbOptions','PagesTableName'))
    curs.close()

#-----------------------------------------------------------------
def main():
    global dbconn
    putfile("\n")
    putfile("-=[ Simulation Log, started %s ]=-"%strftime("%d.%m.%Y %H:%M:%S", localtime()))
    putfile("---[ SimCleanPages "+__version__+" ]--")
    putfile("-< Used %s configuration file"%(wdir+taskdir+'\\ini\\'+taskinifile))

    for j in range(sercfg.getint('simOptions','Iterations')):
        jj = j+1
        putfile("----[ Run: "+`jj`+" ]------------------------------------------------")

        cleanpages(dbconn, jj)
        putfile("-< Pages cleaned")

        putfile("----[ Run: "+`jj`+" ]------------------------------------------------")
        print "\n"

    putfile("---[ SimCleanPages "+__version__+" ]--")
    putfile("-=[ Simulation Log, finished %s ]=-"%strftime("%d.%m.%Y %H:%M:%S", localtime()))
    putfile("\n")
    print "\n"
    flog.close()    
    dbconn.close()

#--[ Main program :) ]------------------------------------------
if __name__ == '__main__':
    main()
