Back to index

moin  1.9.0~rc2
migutil.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - utility functions used by the migration scripts
00004 
00005     @copyright: 2005,2007 MoinMoin:ThomasWaldmann
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 import os, sys, shutil
00009 
00010 opj = os.path.join # yes, I am lazy
00011 join = os.path.join
00012 
00013 
00014 def fatalError(msg):
00015     """ Exit with error message on fatal errors """
00016     print "Fatal error:", msg
00017     print "Stoping"
00018     sys.exit(1)
00019 
00020 
00021 def error(msg):
00022     """ Report minor error and continue """
00023     print "Error:", msg
00024 
00025 
00026 def backup(src, dst):
00027     """ Create a backup of src directory in dst, create empty src
00028 
00029     @param src: source
00030     @param dst: destination
00031     """
00032     print "Create backup of '%s' in '%s'" % (src, dst)
00033 
00034     if not os.path.isdir(src):
00035         fatalError("can't find '%s'. You must run this script from the directory where '%s' is located." % src)
00036 
00037     try:
00038         shutil.move(src, dst)
00039     except:
00040         fatalError("can't move '%s' to '%s'" % (src, dst))
00041 
00042     try:
00043         os.mkdir(src)
00044     except OSError:
00045         fatalError("can't create '%s'" % src)
00046 
00047 
00048 def listdir(path):
00049     """ Return list of files in path, filtering certain files """
00050     names = [name for name in os.listdir(path)
00051              if not name.startswith('.') and
00052              not name.endswith('.pickle') and
00053              name != 'CVS']
00054     return names
00055 
00056 
00057 def makedir(newdir):
00058     """ Create a directory, if it doesn't exist """
00059     try:
00060         os.mkdir(newdir)
00061     except OSError:
00062         pass
00063 
00064 def copy_dir(dir_from, dir_to):
00065     """ Copy a complete directory """
00066     print "%s/ -> %s/" % (dir_from, dir_to)
00067     try:
00068         shutil.copytree(dir_from, dir_to)
00069     except Exception, err:
00070         error("can't copy '%s' to '%s' (%s)" % (dir_from, dir_to, str(err)))
00071 
00072 
00073 def copy_file(fname_from, fname_to):
00074     """ Copy a single file """
00075     print "%s -> %s" % (fname_from, fname_to)
00076     try:
00077         shutil.copy2(fname_from, fname_to) # copies file data, mode, atime, mtime
00078     except:
00079         error("can't copy '%s' to '%s'" % (fname_from, fname_to))
00080 
00081 
00082 def move_file(fname_from, fname_to):
00083     """ Move a single file """
00084     print "%s -> %s" % (fname_from, fname_to)
00085     try:
00086         shutil.move(fname_from, fname_to) # moves file (even to different filesystem, including mode and atime/mtime)
00087     except:
00088         error("can't move '%s' to '%s'" % (fname_from, fname_to))
00089 
00090 
00091 def copy(items, srcdir, dstdir):
00092     """ copy items from src dir into dst dir
00093 
00094     @param items: list of items to copy
00095     @param srcdir: source directory to copy items from
00096     @param dstdir: destination directory to copy into
00097     """
00098     for item in items:
00099         src = join(srcdir, item)
00100         dst = join(dstdir, item)
00101 
00102         # Copy directories
00103         if os.path.isdir(src):
00104             copy_dir(src, dst)
00105         elif os.path.isfile(src):
00106             copy_file(src, dst)
00107         else:
00108             error("can't find '%s'" % src)
00109