Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:19

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