summaryrefslogtreecommitdiffstats
path: root/server/remove_rtevalrun
blob: d1251d3819d6f98f9324ee8e81914d09ad49cc71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/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 <davids@redhat.com>
#
#   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"