Mercurial > p > roundup > code
comparison scripts/copy-user.py @ 5376:64b05e24dbd8
Python 3 preparation: convert print to a function.
Tool-assisted patch. It is possible that some "from __future__ import
print_function" are not in fact needed, if a file only uses print()
with a single string as an argument and so would work fine in Python 2
without that import.
| author | Joseph Myers <jsm@polyomino.org.uk> |
|---|---|
| date | Tue, 24 Jul 2018 09:54:52 +0000 |
| parents | 198b6e810c67 |
| children | 2a6c3eb4e059 |
comparison
equal
deleted
inserted
replaced
| 5375:1ad46057ae4a | 5376:64b05e24dbd8 |
|---|---|
| 13 Example: | 13 Example: |
| 14 copy-user /roundup/tracker1 /roundup/tracker2 `seq 3 10` 14 16 | 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) | 15 (copies users 3, 4, 5, 6, 7, 8, 9, 10, 14 and 16) |
| 16 """ | 16 """ |
| 17 | 17 |
| 18 from __future__ import print_function | |
| 18 import sys | 19 import sys |
| 19 import roundup.instance | 20 import roundup.instance |
| 20 | 21 |
| 21 | 22 |
| 22 def copy_user(home1, home2, *userids): | 23 def copy_user(home1, home2, *userids): |
| 25 copyattribs = ['username', 'password', 'address', 'realname', 'phone', | 26 copyattribs = ['username', 'password', 'address', 'realname', 'phone', |
| 26 'organisation', 'alternate_addresses', 'roles', 'timezone'] | 27 'organisation', 'alternate_addresses', 'roles', 'timezone'] |
| 27 | 28 |
| 28 try: | 29 try: |
| 29 instance1 = roundup.instance.open(home1) | 30 instance1 = roundup.instance.open(home1) |
| 30 print "Opened source instance: %s" % home1 | 31 print("Opened source instance: %s" % home1) |
| 31 except: | 32 except: |
| 32 print "Can't open source instance: %s" % home1 | 33 print("Can't open source instance: %s" % home1) |
| 33 sys.exit(1) | 34 sys.exit(1) |
| 34 | 35 |
| 35 try: | 36 try: |
| 36 instance2 = roundup.instance.open(home2) | 37 instance2 = roundup.instance.open(home2) |
| 37 print "Opened target instance: %s" % home2 | 38 print("Opened target instance: %s" % home2) |
| 38 except: | 39 except: |
| 39 print "Can't open target instance: %s" % home2 | 40 print("Can't open target instance: %s" % home2) |
| 40 sys.exit(1) | 41 sys.exit(1) |
| 41 | 42 |
| 42 db1 = instance1.open('admin') | 43 db1 = instance1.open('admin') |
| 43 db2 = instance2.open('admin') | 44 db2 = instance2.open('admin') |
| 44 | 45 |
| 48 userlist = db1.user.list() | 49 userlist = db1.user.list() |
| 49 for userid in userids: | 50 for userid in userids: |
| 50 try: | 51 try: |
| 51 userid = str(int(userid)) | 52 userid = str(int(userid)) |
| 52 except ValueError as why: | 53 except ValueError as why: |
| 53 print "Not a numeric user id: %s Skipping ..." % (userid,) | 54 print("Not a numeric user id: %s Skipping ..." % (userid,)) |
| 54 continue | 55 continue |
| 55 if userid not in userlist: | 56 if userid not in userlist: |
| 56 print "User %s not in source instance. Skipping ..." % userid | 57 print("User %s not in source instance. Skipping ..." % userid) |
| 57 continue | 58 continue |
| 58 | 59 |
| 59 user = {} | 60 user = {} |
| 60 for attrib in copyattribs: | 61 for attrib in copyattribs: |
| 61 value = db1.user.get(userid, attrib) | 62 value = db1.user.get(userid, attrib) |
| 62 if value: | 63 if value: |
| 63 user[attrib] = value | 64 user[attrib] = value |
| 64 try: | 65 try: |
| 65 db2.user.lookup(user['username']) | 66 db2.user.lookup(user['username']) |
| 66 print "User %s: Username '%s' exists in target instance. Skipping ..." % (userid, user['username']) | 67 print("User %s: Username '%s' exists in target instance. Skipping ..." % (userid, user['username'])) |
| 67 continue | 68 continue |
| 68 except KeyError as why: | 69 except KeyError as why: |
| 69 pass | 70 pass |
| 70 print "Copying user %s (%s) ..." % (userid, user['username']) | 71 print("Copying user %s (%s) ..." % (userid, user['username'])) |
| 71 db2.user.create(**user) | 72 db2.user.create(**user) |
| 72 | 73 |
| 73 db2.commit() | 74 db2.commit() |
| 74 db2.close() | 75 db2.close() |
| 75 print "Closed target instance." | 76 print("Closed target instance.") |
| 76 db1.close() | 77 db1.close() |
| 77 print "Closed source instance." | 78 print("Closed source instance.") |
| 78 | 79 |
| 79 | 80 |
| 80 if __name__ == "__main__": | 81 if __name__ == "__main__": |
| 81 if len(sys.argv) < 4: | 82 if len(sys.argv) < 4: |
| 82 print __doc__ | 83 print(__doc__) |
| 83 sys.exit(1) | 84 sys.exit(1) |
| 84 else: | 85 else: |
| 85 copy_user(*sys.argv[1:]) | 86 copy_user(*sys.argv[1:]) |
| 86 | 87 |
