#!/usr/bin/python # # # 10/06/2014 David Lawrence # # # This script will modify a tt.db file to fix a problem with # the rocid for the two crates used by the ST. The fADC crate # should be rocid=94 and the TDC crate should be rocid=95. # Originally, these were set to rocid=2 and rocid=3 respectively). # import sqlite3 as lite import sys import os import re import datetime if len(sys.argv) != 2: print "You must supply a SQLite DB file to modify!" sys.exit(0); # Connect to SQLite DB file db_filename = sys.argv[1] print "" print "opening SQLite DB file: %s" % db_filename print "---------------------------------------------" con = lite.connect(db_filename) with con: # Specify that next cursor should be "dictionary" # (i.e. python's hash map) so columns can be indexed # by name con.row_factory = lite.Row # Create Cursor cur = con.cursor() # Check if this has already been updated cur.execute('SELECT * FROM Crate WHERE rocid=94 OR rocid=95') s_rows = cur.fetchall() if len(s_rows) != 0: print 'Entires exist for either rocid=94 or rocid=95. This file was probably' print 'already updated. Exiting now without modifying the file.' sys.exit(0); cur.execute('SELECT * FROM Crate WHERE rocid=2 OR rocid=3') s_rows = cur.fetchall() if len(s_rows) == 0: print 'No entires exist for either rocid=2 or rocid=3. This is not expected' print 'so the file may be out of the standard fix-script chain. Exiting now' print 'without modifying the file.' sys.exit(0); # Change rocid numbers cur.execute('UPDATE Crate SET rocid=94 WHERE rocid=2') cur.execute('UPDATE Crate SET rocid=95 WHERE rocid=3') print 'TT updated:' # Check results cur.execute('SELECT * FROM Crate,Module,Channel WHERE rocid=94 AND Crate.crateid=Module.crateid AND Module.moduleid=Channel.moduleid') s_rows = cur.fetchall() print ' %d channels defined for rocid=94' % len(s_rows) cur.execute('SELECT * FROM Crate,Module,Channel WHERE rocid=95 AND Crate.crateid=Module.crateid AND Module.moduleid=Channel.moduleid') s_rows = cur.fetchall() print ' %d channels defined for rocid=95' % len(s_rows) print 'Done'