1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
# -*- coding: iso-8859-1 -*-
"""
MoinMoin - utility functions used by the migration scripts
@copyright: 2005,2007 MoinMoin:ThomasWaldmann
@license: GNU GPL, see COPYING for details.
"""
import os, sys, shutil
opj = os.path.join # yes, I am lazy
join = os.path.join
def fatalError(msg):
""" Exit with error message on fatal errors """
print "Fatal error:", msg
print "Stoping"
sys.exit(1)
def error(msg):
""" Report minor error and continue """
print "Error:", msg
def backup(src, dst):
""" Create a backup of src directory in dst, create empty src
@param src: source
@param dst: destination
"""
print "Create backup of '%s' in '%s'" % (src, dst)
if not os.path.isdir(src):
fatalError("can't find '%s'. You must run this script from the directory where '%s' is located." % src)
try:
shutil.move(src, dst)
except:
fatalError("can't move '%s' to '%s'" % (src, dst))
try:
os.mkdir(src)
except OSError:
fatalError("can't create '%s'" % src)
def listdir(path):
""" Return list of files in path, filtering certain files """
names = [name for name in os.listdir(path)
if not name.startswith('.') and
not name.endswith('.pickle') and
name != 'CVS']
return names
def makedir(newdir):
""" Create a directory, if it doesn't exist """
try:
os.mkdir(newdir)
except OSError:
pass
def copy_dir(dir_from, dir_to):
""" Copy a complete directory """
print "%s/ -> %s/" % (dir_from, dir_to)
try:
shutil.copytree(dir_from, dir_to)
except Exception, err:
error("can't copy '%s' to '%s' (%s)" % (dir_from, dir_to, str(err)))
def copy_file(fname_from, fname_to):
""" Copy a single file """
print "%s -> %s" % (fname_from, fname_to)
try:
shutil.copy2(fname_from, fname_to) # copies file data, mode, atime, mtime
except:
error("can't copy '%s' to '%s'" % (fname_from, fname_to))
def move_file(fname_from, fname_to):
""" Move a single file """
print "%s -> %s" % (fname_from, fname_to)
try:
shutil.move(fname_from, fname_to) # moves file (even to different filesystem, including mode and atime/mtime)
except:
error("can't move '%s' to '%s'" % (fname_from, fname_to))
def copy(items, srcdir, dstdir):
""" copy items from src dir into dst dir
@param items: list of items to copy
@param srcdir: source directory to copy items from
@param dstdir: destination directory to copy into
"""
for item in items:
src = join(srcdir, item)
dst = join(dstdir, item)
# Copy directories
if os.path.isdir(src):
copy_dir(src, dst)
elif os.path.isfile(src):
copy_file(src, dst)
else:
error("can't find '%s'" % src)
|