source: watchlistplugin/0.12/tools/uninstall.py

Last change on this file was 15264, checked in by Ryan J Ollos, 8 years ago

Remove unnecessary svn:mime-type on py files

svn:mime-type was set to "plain" for many files.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 5.1 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3"""
4Uninstaller for the Trac Watchlist Plugin.
5Version 1.0 from 24th Sep 2010
6Removes all DB tables created by the watchlist plugin.
7
8Plugin website:  http://trac-hacks.org/wiki/WatchlistPlugin
9Trac website:    http://trac.edgewall.org/
10
11Copyright (c) 2008-2010 by Martin Scharrer <martin@scharrer-online.de>
12All rights reserved.
13
14This program is free software: you can redistribute it and/or modify
15it under the terms of the GNU General Public License as published by
16the Free Software Foundation, either version 3 of the License, or
17(at your option) any later version.
18
19This program is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22GNU General Public License for more details.
23
24For a copy of the GNU General Public License see
25<http://www.gnu.org/licenses/>.
26
27$Id$
28"""
29
30import sys
31import getopt
32
33def delete_watchlist_tables(envpath, tables=('watchlist','watchlist_settings','system'), user=None):
34    """Deletes all watchlist DB entries => Uninstaller"""
35    from  trac.env   import  Environment
36    try:
37        env = Environment(envpath)
38    except:
39        print "Given path '%s' seems not to be a Trac environment." % envpath
40        sys.exit(3)
41
42    db = env.get_db_cnx()
43
44    if user is not None:
45        cursor = db.cursor()
46        if 'watchlist' in tables:
47            try:
48                cursor.execute("DELETE FROM watchlist WHERE wluser=%s", (user,))
49                print "Deleted user entries from 'watchlist' table."
50            except Exception as e:
51                db.rollback()
52                print "Could not delete user entry from 'watchlist' table: "\
53                    + unicode(e)
54        cursor = db.cursor()
55        if 'watchlist_settings' in tables:
56            try:
57                cursor.execute("DELETE FROM watchlist_settings WHERE wluser=%s", (user,))
58                print "Deleted user entries from 'watchlist_settings' table."
59            except Exception as e:
60                db.rollback()
61                print "Could not delete user entry from 'watchlist_settings' table: "\
62                    + unicode(e)
63
64        db.commit()
65        print "Finished."
66        return
67
68
69    if 'watchlist' in tables:
70        cursor = db.cursor()
71        try:
72            cursor.execute("DROP TABLE watchlist")
73            print "Deleted 'watchlist' table."
74        except:
75            db.rollback()
76            print "No 'watchlist' table for deletion found."
77
78    if 'watchlist_settings' in tables:
79        cursor = db.cursor()
80        try:
81            cursor.execute("DROP TABLE watchlist_settings")
82            print "Deleted 'watchlist_settings' table."
83        except:
84            db.rollback()
85            print "No 'watchlist_settings' table for deletion found."
86
87    if 'system' in tables:
88        cursor = db.cursor()
89        try:
90            cursor.execute("DELETE FROM system WHERE name='watchlist_version'")
91            print "Deleted watchlist version entry from system table."
92        except Exception as e:
93            db.rollback()
94            print "Could not delete 'watchlist_version' from 'system' table: "\
95                  + unicode(e)
96
97    db.commit()
98    print "Finished."
99
100
101def usage():
102    """Trac Watchlist Plugin Uninstaller v1.0 from 24th Sep 2010
103     Usage: python uninstall.py [options] /path/to/trac/environment [options]
104     Options:
105       -h,--help      This help text
106       -V,--version   Prints version number and copyright statement
107       -u,--user <user>
108                      Only remove entries of given user
109       -t,--tables <tables>
110                      Only removes/uninstalls given tables (default: all).
111     Tables:
112       watchlist, watchlist_settings, system
113    """
114    print usage.__doc__
115
116
117def main(argv):
118    envpath = None
119    tables = ['watchlist','watchlist_settings','system']
120    user = None
121    try:
122        opts, args = getopt.gnu_getopt(argv, 'hVt:u:',
123                ['help', 'version', 'tables=', 'user='])
124    except getopt.GetoptError as e:
125        print unicode(e)
126        usage()
127        sys.exit(2)
128    for opt, arg in opts:
129        if opt in ('-h', '--help'):
130            usage()
131            sys.exit()
132        elif opt in ('-V', '--version'):
133            print __doc__
134            sys.exit()
135        elif opt in ('-t', '--tables'):
136            tables = arg.split(',')
137        elif opt in ('-u', '--user'):
138            user = arg
139    if len(args) < 1:
140        print "Error: No trac environment given!"
141        usage()
142        sys.exit(2)
143    else:
144        envpath = args[0]
145
146    wtables = [ t for t in tables if t != 'system' ]
147    U = ''
148    if user is not None:
149        U = "all entries of user '%s' from " % user
150    print "This will delete " + U + "the following tables: " + ', '.join(wtables or ['none'])
151    if 'system' in tables and not U:
152        print "and remove the 'watchlist_version' from the 'system' table."
153    sys.stdout.write("Are you sure? y/N: ")
154    if sys.stdin.readline().strip().lower() == 'y':
155        delete_watchlist_tables(envpath, tables, user)
156    sys.exit()
157
158
159if __name__ == "__main__":
160    main(sys.argv[1:])
Note: See TracBrowser for help on using the repository browser.