| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | """ |
|---|
| 4 | User renamer for the Trac Watchlist Plugin. |
|---|
| 5 | Version 1.0 from 24th Sep 2010 |
|---|
| 6 | Renames a users name in 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 rename_user(envpath, oldname, newname): |
|---|
| 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 | cursor = db.cursor() |
|---|
| 44 | |
|---|
| 45 | try: |
|---|
| 46 | cursor.execute(""" |
|---|
| 47 | UPDATE watchlist |
|---|
| 48 | SET wluser=%s |
|---|
| 49 | WHERE wluser=%s |
|---|
| 50 | """, (newname,oldname)) |
|---|
| 51 | cursor.execute(""" |
|---|
| 52 | UPDATE watchlist_settings |
|---|
| 53 | SET wluser=%s |
|---|
| 54 | WHERE wluser=%s |
|---|
| 55 | """, (newname,oldname)) |
|---|
| 56 | print "Renamed user '%s' to '%s'." % (oldname,newname) |
|---|
| 57 | db.commit() |
|---|
| 58 | except Exception as e: |
|---|
| 59 | db.rollback() |
|---|
| 60 | print "Could not rename user: " + unicode(e) |
|---|
| 61 | print "Does the new user already exists?" |
|---|
| 62 | sys.exit(3) |
|---|
| 63 | |
|---|
| 64 | db.commit() |
|---|
| 65 | print "Finished." |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | def usage(): |
|---|
| 69 | """Trac Watchlist Plugin User Renamer v1.0 from 24th Sep 2010 |
|---|
| 70 | Usage: python uninstall.py [options] /path/to/trac/environment oldname newname |
|---|
| 71 | Options: |
|---|
| 72 | -h,--help This help text |
|---|
| 73 | -V,--version Prints version number and copyright statement |
|---|
| 74 | """ |
|---|
| 75 | print usage.__doc__ |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | def main(argv): |
|---|
| 79 | envpath = None |
|---|
| 80 | try: |
|---|
| 81 | opts, args = getopt.gnu_getopt(argv, 'hV', |
|---|
| 82 | ['help', 'version']) |
|---|
| 83 | except getopt.GetoptError as e: |
|---|
| 84 | print unicode(e) |
|---|
| 85 | usage() |
|---|
| 86 | sys.exit(2) |
|---|
| 87 | for opt, arg in opts: |
|---|
| 88 | if opt in ('-h', '--help'): |
|---|
| 89 | usage() |
|---|
| 90 | sys.exit() |
|---|
| 91 | elif opt in ('-V', '--version'): |
|---|
| 92 | print __doc__ |
|---|
| 93 | sys.exit() |
|---|
| 94 | if len(args) != 3: |
|---|
| 95 | print "ERROR: wrong number of arguments!" |
|---|
| 96 | usage() |
|---|
| 97 | sys.exit(2) |
|---|
| 98 | else: |
|---|
| 99 | envpath = args[0] |
|---|
| 100 | oldname, newname = args[1:] |
|---|
| 101 | |
|---|
| 102 | print "This will rename watchlist user '%s' to '%s'" % (oldname,newname) |
|---|
| 103 | sys.stdout.write("Are you sure? y/N: ") |
|---|
| 104 | if sys.stdin.readline().strip().lower() == 'y': |
|---|
| 105 | rename_user(envpath, oldname, newname) |
|---|
| 106 | sys.exit() |
|---|
| 107 | |
|---|
| 108 | if __name__ == "__main__": |
|---|
| 109 | main(sys.argv[1:]) |
|---|
| 110 | |
|---|
| 111 | # EOF |
|---|