Back to index

moin  1.9.0~rc2
12_to_13_mig10.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 """
00003     migration from moin 1.3 < patch-xxx to moin 1.3 >= patch-xxx
00004     We fix 2 issues here:
00005     * we forgot to handle edit-lock files. We simply delete them now.
00006     * we convert attachment names to utf-8
00007 
00008     Steps for a successful migration:
00009 
00010         1. Stop your wiki and make a backup of old data and code
00011 
00012         2. Make a copy of the wiki's "data" directory to your working dir
00013 
00014         3. make sure that from_encoding and to_encoding matches your needs (see
00015            beginning of script below and config.charset in moin_config.py) and
00016            run python 12_to_13_mig10.py from your working dir
00017 
00018         4. If there was no error, you will find:
00019             data.pre-mig10 - the script renames your data directory copy to that name
00020             data - converted data dir
00021 
00022         5. Verify conversion results (number of pages, size of logs, attachments,
00023            number of backup copies) - everything should be reasonable before
00024            you proceed.
00025 
00026         6. Copy additional files from data.pre-mig10 to data (maybe intermaps, logs,
00027            etc.). Be aware that the file contents AND file names of wiki content
00028            may have changed, so DO NOT copy the files inside the cache/ directory,
00029            let the wiki refill it.
00030 
00031         7. Replace the data directory your wiki uses with the data directory
00032            you created by previous steps. DO NOT simply copy the converted stuff
00033            into the original or you will duplicate pages and create chaos!
00034 
00035         8. Test it - if something has gone wrong, you still have your backup.
00036 
00037 
00038     @copyright: 2005 Thomas Waldmann
00039     @license: GPL, see COPYING for details
00040 """
00041 
00042 from_encoding = 'iso8859-1'
00043 #from_encoding = 'utf-8'
00044 
00045 to_encoding = 'utf-8'
00046 
00047 import os, os.path, sys, urllib
00048 
00049 # Insert THIS moin dir first into sys path, or you would run another
00050 # version of moin!
00051 sys.path.insert(0, '../../../..')
00052 from MoinMoin import wikiutil
00053 
00054 from MoinMoin.script.migration.migutil import opj, listdir, copy_file, move_file, copy_dir
00055 
00056 def migrate(dir_to):
00057     """ this removes edit-lock files from the pagedirs and
00058         converts attachment filenames
00059     """
00060     pagesdir = opj(dir_to, 'pages')
00061     pagelist = listdir(pagesdir)
00062     for pagename in pagelist:
00063         pagedir = opj(pagesdir, pagename)
00064         editlock = opj(pagedir, 'edit-lock')
00065         try:
00066             os.remove(editlock)
00067         except:
00068             pass
00069 
00070         attachdir = os.path.join(pagedir, 'attachments')
00071         for root, dirs, files in os.walk(attachdir):
00072             for f in  files:
00073                 try:
00074                     f.decode(to_encoding)
00075                 except UnicodeDecodeError:
00076                     fnew = f.decode(from_encoding).encode(to_encoding)
00077                     os.rename(os.path.join(root, f), os.path.join(root, fnew))
00078                     print 'renamed', f, '\n ->', fnew, ' in dir:', root
00079 
00080 
00081 origdir = 'data.pre-mig10'
00082 destdir = 'data'
00083 
00084 # Backup original dir and create new empty dir
00085 try:
00086     os.rename(destdir, origdir)
00087 except OSError:
00088     print "You need to be in the directory where your copy of the 'data' directory is located."
00089     sys.exit(1)
00090 
00091 copy_dir(origdir, destdir)
00092 migrate(destdir)
00093 
00094