|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright (C) 2003 by Intevation GmbH |
| 3 | +# Author: |
| 4 | +# Thomas Arendsen Hein <[email protected]> |
| 5 | +# |
| 6 | +# This program is free software dual licensed under the GPL (>=v2) |
| 7 | +# and the Roundup Licensing (see COPYING.txt in the roundup distribution). |
| 8 | + |
| 9 | +""" |
| 10 | +copy-user <instance-home> <instance-home> <userid> [<userid>...] |
| 11 | +
|
| 12 | +Copy one or more Roundup users from one tracker instance to another. |
| 13 | +Example: |
| 14 | + copy-user /roundup/tracker1 /roundup/tracker2 `seq 3 10` 14 16 |
| 15 | + (copies users 3, 4, 5, 6, 7, 8, 9, 10, 14 and 16) |
| 16 | +""" |
| 17 | + |
| 18 | +__version__ = "$Revision: 1.1 $" |
| 19 | +# $Source: /home/stefan/projects/roundup-migrate/roundup/scripts/copy-user.py,v $ |
| 20 | +# $Id: copy-user.py,v 1.1 2003-12-04 23:13:43 richard Exp $ |
| 21 | + |
| 22 | +import sys |
| 23 | +import roundup.instance |
| 24 | + |
| 25 | + |
| 26 | +def copy_user(home1, home2, *userids): |
| 27 | + """Copy users which are listed by userids from home1 to home2""" |
| 28 | + |
| 29 | + copyattribs = ['username', 'password', 'address', 'realname', 'phone', |
| 30 | + 'organisation', 'alternate_addresses', 'roles', 'timezone'] |
| 31 | + |
| 32 | + try: |
| 33 | + instance1 = roundup.instance.open(home1) |
| 34 | + print "Opened source instance: %s" % home1 |
| 35 | + except: |
| 36 | + print "Can't open source instance: %s" % home1 |
| 37 | + sys.exit(1) |
| 38 | + |
| 39 | + try: |
| 40 | + instance2 = roundup.instance.open(home2) |
| 41 | + print "Opened target instance: %s" % home2 |
| 42 | + except: |
| 43 | + print "Can't open target instance: %s" % home2 |
| 44 | + sys.exit(1) |
| 45 | + |
| 46 | + db1 = instance1.open('admin') |
| 47 | + db2 = instance2.open('admin') |
| 48 | + |
| 49 | + userlist = db1.user.list() |
| 50 | + for userid in userids: |
| 51 | + try: |
| 52 | + userid = str(int(userid)) |
| 53 | + except ValueError, why: |
| 54 | + print "Not a numeric user id: %s Skipping ..." % (userid,) |
| 55 | + continue |
| 56 | + if userid not in userlist: |
| 57 | + print "User %s not in source instance. Skipping ..." % userid |
| 58 | + continue |
| 59 | + |
| 60 | + user = {} |
| 61 | + for attrib in copyattribs: |
| 62 | + value = db1.user.get(userid, attrib) |
| 63 | + if value: |
| 64 | + user[attrib] = value |
| 65 | + try: |
| 66 | + db2.user.lookup(user['username']) |
| 67 | + print "User %s: Username '%s' exists in target instance. Skipping ..." % (userid, user['username']) |
| 68 | + continue |
| 69 | + except KeyError, why: |
| 70 | + pass |
| 71 | + print "Copying user %s (%s) ..." % (userid, user['username']) |
| 72 | + db2.user.create(**user) |
| 73 | + |
| 74 | + db2.commit() |
| 75 | + db2.close() |
| 76 | + print "Closed target instance." |
| 77 | + db1.close() |
| 78 | + print "Closed source instance." |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + if len(sys.argv) < 4: |
| 83 | + print __doc__ |
| 84 | + sys.exit(1) |
| 85 | + else: |
| 86 | + copy_user(*sys.argv[1:]) |
| 87 | + |
0 commit comments