Marcus

Here are the two Python scripts for changing user names throughout a Trac
0.10.4 database and for changing the Apache digest file. If you are using
Basic authorisation instead of digest, the script would need modifying.

I have used the scripts on my Windows 2000 Trac 0.10.4 installation and it
looks OK.

Hope this is of some help.

Steve McCusker

PS I did send these to you direct earlier but maybe they didn't get through.

generate_user_change_digest.py
------------------------------

# Generate a new Apache digest file with old users names replaced by new
ones
# and new users names added. The program can replace old passwords as well,
and
# for new users it will generate a password if none is supplied.
# The program reads a text file with lines that contain:
#   a single letter code (O for Old users or N for new users)
#   the old user name if the code='O' or the digest realm for the new user
if code='N'
#   the new user name
#   a password
# These parameters are separated by white space and must not contain white
space.
#
# This is the same file format as used by generate_user_change_sql.py
#
# Entries in the old digest file for users not listed in the user_names_file
# are copied to the new digest file without change.
#
# Usage:
#   python generate_user_change_digest.py user_names_file_name
old_digest_file new_digest_file
#
# Tested using Python 2.3.5 & Apache 2.0.59
#
# (c) 2007  Steve McCusker, Granmac Pty Ltd ([EMAIL PROTECTED])
#
# This program is not guaranteed to be correct, any use is at the user's
risk.
# The author does not restrict the use of this program in any way.

import sys
from string import *
import os.path
from random import sample
import md5

if len(sys.argv) != 4:
    print('There must be three command line parameters.')
    print('Usage:')
    print('  python generate_user_change_digest.py user_names_file_name
old_digest_file new_digest_file')
    exit(2)

inputFile = open(sys.argv[1],'r')
user_names = inputFile.readlines()
inputFile.close()

new_users = []
name_mapping = {}

for user in user_names:
    words = split(user)
    code = upper(words[0])
    if (len(words) < 3) or (len(words) > 4):
        print("ERROR: Incorrrect format in " + user)
    if code == 'N':
        new_user = [rstrip(words[2]), rstrip(words[1])]
        if len(words) == 4:
            new_user.append(rstrip(words[3]))
        new_users.append(new_user)
    else:
        old_user = [rstrip(words[2])]
        if len(words) == 4:
            old_user.append(rstrip(words[3]))
        name_mapping[rstrip(words[1])] = old_user

inputFile = open(sys.argv[2],'r')
old_digest_lines = inputFile.readlines()
inputFile.close()
outputFile = open(sys.argv[3],'wb')

for line in old_digest_lines:
    digest_entry = split(line,':')
    old_user = digest_entry[0]
    if name_mapping.has_key(digest_entry[0]):
        new_user = name_mapping[old_user]
        if len(new_user) == 1:
            password = rstrip(digest_entry[2])
        else:
            password = new_user[1]
        realm = digest_entry[1]
        print('Changing old user '+old_user+' to new user '+new_user[0]+'
with password '+password)
        prefix = '%s:%s:' % (new_user[0], realm)
        unenc = prefix + password
        digest = md5.new(unenc).hexdigest()
        new_entry = prefix + digest
        outputFile.write(new_entry +'\n')
    else:
        print("Copying unchanged user '"+old_user+\
              "' to new digest file.")
        outputFile.write(line)

for new_user in new_users:
    if len(new_user) == 2:
        new_pass = ''.join(sample(lowercase,2)+sample(digits,2)+\
                   sample(uppercase,2)+sample(lowercase,2))
    else:
        new_pass = new_user[2]
    realm = new_user[1]
    print('Adding new user '+new_user[0]+' with password '+new_pass)
    prefix = '%s:%s:' % (new_user[0], realm)
    unenc = prefix + new_pass
    digest = md5.new(unenc).hexdigest()
    new_entry = prefix + digest
    outputFile.write(new_entry +'\n')

outputFile.close()


generate_user_change_sql.py
---------------------------

# Generate the SQL needed to change the Trac 0.10.4 database to change a
user name.
# The program reads a text file with lines that contain:
#   a single letter code (O for Old users or N for new users)
#   the old user name if the code='O' or the digest realm for the new user
if code='N'
#   the new user name
#   a password
# These parameters are separated by white space and must not contain white
space.
#
# This is the same file format as used by generate_user_change_digest.py
# This program does not use the password field and ignores lines starting
# with a code of 'N'.
#
# Usage:
#   python generate_user_change_sql.py user_names_file_name new_sql_file
#
# Tested using Python 2.3.5
#
# (c) 2007  Steve McCusker, Granmac Pty Ltd ([EMAIL PROTECTED])
#
# This program is not guaranteed to be correct, any use is at the user's
risk.
# The author does not restrict the use of this program in any way.

import sys
from string import *
import os.path

if len(sys.argv) != 3:
    print('There must be two command line parameters.')
    print('Usage:')
    print('  python generate_user_change_digest.py user_names_file_name
new_sql_file')
    exit(2)

inputFile = open(sys.argv[1],'r')
user_names = inputFile.readlines()
inputFile.close()

outputFile = open(sys.argv[2],'w')

for user in user_names:
    words = split(user)
    code = upper(words[0])
    if (len(words) < 3) or (len(words) > 4):
        print("ERROR: Incorrrect format in " + user)
    if code != 'N':
        old_user = rstrip(words[1])
        new_user = rstrip(words[2])
        if old_user != new_user:
            print("Generating SQl to change "+old_user+" to "+new_user)
            outputFile.writelines([
                "update session set sid='"+new_user+"' where
sid='"+old_user+"';\n",
                "update session_attribute set sid='"+new_user+"' where
sid='"+old_user+"';\n",
                "update attachment set author='"+new_user+"' where
author='"+old_user+"';\n",
                "update revision set author='"+new_user+"' where
author='"+old_user+"';\n",
                "update wiki set author='"+new_user+"' where
author='"+old_user+"';\n",
                "update component set owner='"+new_user+"' where
owner='"+old_user+"';\n",
                "update ticket set owner='"+new_user+"' where
owner='"+old_user+"';\n",
                "update ticket set reporter='"+new_user+"' where
reporter='"+old_user+"';\n",
                "update ticket set
cc=replace(cc,'"+old_user+"','"+new_user+"') where cc LIKE
'%"+old_user+"%';\n",
                "update permission set username='"+new_user+"' where
username='"+old_user+"';\n",
                "update ticket_change set author='"+new_user+"' where
author='"+old_user+"';\n",
                "update ticket_change set
oldvalue=replace(oldvalue,'"+old_user+"','"+new_user+"') where field in
('owner', 'reporter', 'cc') AND oldvalue LIKE '%"+old_user+"%';\n",
                "update ticket_change set
newvalue=replace(newvalue,'"+old_user+"','"+new_user+"') where field in
('owner', 'reporter', 'cc') AND newvalue LIKE '%"+old_user+"%';\n"])

outputFile.close()
No virus found in this outgoing message.
Checked by AVG.
Version: 7.5.488 / Virus Database: 269.14.9/1068 - Release Date: 13/10/2007
10:15


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Trac 
Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/trac-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to