Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:46:15

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