Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:43

0001 #!/usr/bin/env python
0002 
0003 from __future__ import print_function
0004 import os, sys, re
0005 import string
0006 import math
0007 import cx_Oracle
0008 import time
0009 
0010 ## fetch tail of popcon cronjobs and insert into  PopConAccount LOGTAILS table
0011 ## \author Michele de Gruttola (degrutto) - INFN Naples / CERN (Sep-20-2008)
0012 
0013 usage = 'usage: %s <auth_file> <cronlog_file> <number_of_lines>'% os.path.basename(sys.argv[0])
0014 if len(sys.argv)<4:
0015    print(usage)
0016    sys.exit(4)
0017 else:
0018    argv = sys.argv
0019    authfile = argv[1]
0020    logfile= argv[2]
0021    numberoflines= int(argv[3])
0022 
0023 def readConnection(fileName):
0024   f= open(fileName, 'r')
0025   n=1
0026   nl=1
0027   pasw=""
0028   db=""
0029   account=""
0030   connection=""
0031   while True:
0032       line= f.readline()
0033       line=line.strip()
0034       if line =="":
0035           break
0036       if ((re.search("connection name", line)) and (re.search("XXXXX", line)) and (re.search("XXX",line))):
0037           newline = re.sub('">', '', line)
0038           sep = newline.split('/')
0039           db= sep[2]
0040           account= sep[3]
0041           nl=n
0042       if (n==nl+2):
0043           newline = re.sub('/>', '', line)
0044           newline=newline.strip()
0045           sep = newline.split('"')
0046           pasw= sep[-2]
0047       n=n+1  
0048   f.close()
0049   connection=''.join(account + "/" + pasw + "@" + db)
0050   return connection
0051          
0052 conn=readConnection(authfile)
0053 print(conn)
0054 
0055 def readTail(fileName, numberOfLine):
0056    lines = std.vector(string)()
0057    f= open(fileName, 'r')
0058    for line in f.readlines()[-numberOfLine:]:
0059       #print line
0060       lines.push_back(line.replace("'", "''"))
0061    li="".join(lines)
0062    return li     
0063 
0064 
0065 lines= readTail(logfile,numberoflines)
0066 print(lines)
0067 crontime = time.ctime()
0068 print(crontime)
0069 
0070 orcl= cx_Oracle.connect(conn)
0071 curs=orcl.cursor()
0072 sql="""select payloadtoken from cond_log_table where payloadname='RunNumber'"""
0073 curs.execute(sql)
0074 row=curs.fetchone()
0075 while row:
0076    value=row[0]
0077   # print value
0078    row=curs.fetchone()
0079 curs.close()
0080 
0081 
0082 curs=orcl.cursor()
0083 ## adding check if exist table 
0084 bindVars={'logtails':"LOGTAILS"}
0085 sql="""select count(*) as ntab from user_tables where table_name=:logtails"""
0086 curs.execute(sql,bindVars)
0087 print(sql)
0088 row=curs.fetchone()
0089 while row:
0090   ntab=row[0]
0091   print(ntab)
0092   row=curs.fetchone()
0093 curs.close()
0094 
0095 if (ntab==0):
0096    sql="""create table logtails(
0097    filename varchar2(100),
0098    crontime timestamp with time zone,
0099    tail varchar2(4000),
0100    constraint tail_integr check (filename is not null and crontime is not null and filename is not null),
0101    constraint pk_logtails primary key (filename)
0102    )"""
0103    curs.execute(sql)
0104 
0105 ### merging log tail info
0106 
0107 curs=orcl.cursor()
0108 sql="""merge into logtails a
0109 using (select '"""+logfile+"""' as filename,
0110   to_date('"""+str(crontime)+"""', 'FMDY MON DD HH24:MI:SS YYYY' ) as crontime,
0111   '"""+lines+"""' as tail from dual) b
0112 on (a.filename = b.filename) 
0113 when matched then update set
0114    a.crontime = b.crontime,
0115    a.tail = b.tail  
0116 when not matched then
0117    insert (a.filename, a.crontime, a.tail) values
0118     (b.filename, b.crontime, b.tail)
0119 """
0120 print(sql)
0121 curs.execute(sql)
0122 curs.close()
0123 orcl.commit()