def cdr_files_to_db(years=[],auto_delete=False,remove_test=True): import castor db = DataBaseMySQL('na58pc052.cern.ch','compass','HMcheops') tables = ['run.files','run.info'] #try: # for f in db.send('SELECT COUNT(*) FROM %s' % table): # table_size=int(f[0]) #except: # table_size=0 if auto_delete==False: if 'YES'==raw_input('REMOVING TABLES? Type YES if you are sure: '): for t in tables: print 'removing the table %s' % t db.send('DROP TABLE IF EXISTS %s' % t) remove_test = False else: print 'OK! Keeping it.' if remove_test: # Remove 'test' files: db.send("delete from run.files where file like '%%/test/%%'") db.send(r"CREATE TABLE IF NOT EXISTS %s (`run` INT,`feor` INT,`size` INT,`file` VARCHAR(222), PRIMARY KEY(file)) TYPE=MyISAM;" % tables[0]) db.send(r"CREATE TABLE IF NOT EXISTS %s (`run` INT,`year` INT,`period` VARCHAR(11), PRIMARY KEY(run)) TYPE=MyISAM;" % tables[1]) for f in castor.cdr_files(years): print f['fname'] db.send(r"REPLACE INTO %s (run,feor,size,file) VALUES(%d,%d,%d,'%s');" % (tables[0],f['run'],f['feor'],f['size'],f['fname'])) db.send(r"REPLACE INTO %s (run,year,period) VALUES(%d,%d,'%s');" % (tables[1],f['run'],f['year'],f['period']))
def main(): parser = optparse.OptionParser(version='1.1.0') parser.usage = 'cs %prog <options>\n'\ 'Author: [email protected]' parser.description = 'Run different DB-related commands.' parser.add_option('', '--test',action='store_true',dest='test',default=False, help='Run the test suite') parser.add_option('', '--run',dest='run', help='Run number', type='int') parser.add_option('', '--db-access',dest='dbaccess',default='-hna58pc052.cern.ch -uanonymous', help='DB access options (ex: -hhost -ume -ppass)', type='string') parser.add_option('', '--castor-scan',dest='castor',metavar='year(s)', help='Scan castor files for given years. (Example: 2002,2004,2006)',type='string') parser.add_option('', '--castor2db',dest='castor2db',metavar='year(s)', help='Put castor files info to DB for given year(s). Example: 2002,2003,2004,2006',type='string') (options, args) = parser.parse_args() if len(sys.argv)<=1: parser.print_help() return 1 if options.test==True: unittest.main() if options.castor: import castor for y in options.castor.split(','): castor.cdr_files([y]) return 0 if options.castor2db: for run in options.castor2db.split(','): cdr_files_to_db([int(run)]) return 0 if options.run!=None: for f in get_run_files(options.run,True,options.dbaccess): print f return 0 return 0