annotate MoinMoin/script/migration/migutil.py @ 3018:5dfd26496da8

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