| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | """ |
|---|
| 4 | Uninstaller for the Trac Watchlist Plugin. |
|---|
| 5 | Version 1.0 from 24th Sep 2010 |
|---|
| 6 | Removes all DB tables created by the watchlist plugin. |
|---|
| 7 | |
|---|
| 8 | Plugin website: http://trac-hacks.org/wiki/WatchlistPlugin |
|---|
| 9 | Trac website: http://trac.edgewall.org/ |
|---|
| 10 | |
|---|
| 11 | Copyright (c) 2008-2010 by Martin Scharrer <martin@scharrer-online.de> |
|---|
| 12 | All rights reserved. |
|---|
| 13 | |
|---|
| 14 | This program is free software: you can redistribute it and/or modify |
|---|
| 15 | it under the terms of the GNU General Public License as published by |
|---|
| 16 | the Free Software Foundation, either version 3 of the License, or |
|---|
| 17 | (at your option) any later version. |
|---|
| 18 | |
|---|
| 19 | This program is distributed in the hope that it will be useful, |
|---|
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 22 | GNU General Public License for more details. |
|---|
| 23 | |
|---|
| 24 | For a copy of the GNU General Public License see |
|---|
| 25 | <http://www.gnu.org/licenses/>. |
|---|
| 26 | |
|---|
| 27 | $Id$ |
|---|
| 28 | """ |
|---|
| 29 | |
|---|
| 30 | import sys |
|---|
| 31 | import getopt |
|---|
| 32 | |
|---|
| 33 | def 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 | |
|---|
| 101 | def 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 | |
|---|
| 117 | def 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 | |
|---|
| 159 | if __name__ == "__main__": |
|---|
| 160 | main(sys.argv[1:]) |
|---|