#!/usr/bin/python -tt # # remove_rtevalrun # A script intended to be run on the database server, which # removes a given rteval run, based on the 'rterid' value # # Copyright 2009 David Sommerseth # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # For the avoidance of doubt the "preferred form" of this code is one which # is in an open unpatent encumbered format. Where cryptographic key signing # forms part of the process of creating an executable the information # including keys needed to generate an equivalently functional executable # are deemed to be part of the source code. # import sys import getpass from optparse import OptionParser from database import Database def do_delete(dbc, table, rterid): print "Cleaning up %s ..." % table, try: res = dbc.DELETE(table, {"rterid": int(rterid)}) print "%i rows deleted" % res except Exception, err: print " ** FAILED **" print err dbc.ROLLBACK() sys.exit(2) if __name__ == '__main__': parser = OptionParser(version="%prog v0.1") parser.add_option("-H", "--host", action="store", dest="dbhost", default="localhost", help="Database server to connect to (default: %default)", metavar="HOST") parser.add_option("-p", "--port", action="store", dest="dbport", default="5432", help="Database server port to use (default: %default)", metavar="PORT") parser.add_option("-U", "--user", action="store", dest="dbuser", default="rtevaladmin", help="Database user to connect as (default: %default)", metavar="USERNAME") parser.add_option("-d", "--database", action="store", dest="dbname", default="rteval", help="Database to use (default: %default)", metavar="DATABASE") parser.add_option("-r", "--rterid", action="store", dest="rterid", default=None, help="rteval run id to remove from the database", metavar="INTEGER") (opts, args) = parser.parse_args() if opts.rterid is None: print "%s: Missing --rterid value" % sys.argv[0] sys.exit(1) dbpwd = getpass.getpass("Database password for '%s': " % opts.dbuser) try: dbc = Database(host=opts.dbhost, port=opts.dbport, user=opts.dbuser, password=dbpwd, database=opts.dbname) except Exception, err: print "Could not connect to the database: %s" % str(err) sys.exit(2) # Do the cleanup do_delete(dbc, "cyclic_rawdata", opts.rterid) do_delete(dbc, "cyclic_statistics", opts.rterid) do_delete(dbc, "rtevalruns_details", opts.rterid) do_delete(dbc, "rtevalruns", opts.rterid) dbc.COMMIT() else: raise Exception, "This is a standalone program, not a module to be imported"