bugs.python.org / scripts / adjust_user

# This script changes all references to one user to point to a
# different. Useful if the user has several accounts which he 
# doesn't need anymore.

import sys
sys.path.insert(1,'/home/roundup/roundup/lib/python2.4/site-packages')
import roundup.instance
from roundup.hyperdb import Link, Multilink

if len(sys.argv) != 3:
    print "Usage: adjust_user old new"
    raise SystemExit
old, new = sys.argv[1:]

tracker = roundup.instance.open('.')
db = tracker.open('admin')

old = db.user.lookup(old)
new = db.user.lookup(new)

references = [] # class, prop
for klass in db.getclasses():
    klass = db.getclass(klass)
    klass.disableJournalling()
    for name, typ in klass.getprops().items():
        if isinstance(typ, (Link, Multilink)) and typ.classname=='user':
            references.append((klass, name))
for klass, name in references:
    for id in klass.find(**{name:old}):
        v = klass.get(id, name)
        if isinstance(v, list):
            # Multilink
            for i in range(len(v)):
                if v[i] == old:
                    v[i] = new
            # col:(add, remove)
            multilink_changes = {name:([new],[old])}
        else:
            # Link
            v = new
            multilink_changes = {}
        db.setnode(klass.classname, id, {name: v}, multilink_changes)
db.user.enableJournalling()
db.user.retire(old)
db.commit()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.