Added --clean and --revert so that we can safely experiment with conversions.

This commit is contained in:
Eric S. Raymond 2007-04-23 22:50:58 +00:00
parent bfaa6d0654
commit e668ce40f6

View file

@ -1,9 +1,23 @@
#!/usr/bin/env python
#
# Up-convert UMC between versions.
# Up-convert WML and maps between versions.
#
# All conversion logic for lifting WML and maps from older versions of the
# markup to newer ones should live here. This includes resource path changes
# and renames, also map format conversions.
#
# The recommended procedure is this:
# 1. Run it with --dryrun first to see what it will do.
# 2. If the messages look good, run without .dryrun; the old content
# will be left in backup files with a -bak extension.
# 3. Test the conversion.
# 4. Use either --clean to remove the -bak files or --revert to
# undo the conversion.
#
# Note about the 1.3.1 -> 1.3.2 map conversion: terrain codes will only be
# spotted and converted when preceded by space, comma, or equal sign.
# spotted and converted when preceded by space, comma, or equal sign. This
# will handle maps (either in their own files or included as a WML attribute)
# and the most common cases in WML (e.g. filters and unit declarations).
import sys, os, re, getopt
@ -122,6 +136,8 @@ Usage: upconvert [options]
-d, --dryrun List changes but don't perform them.
-o, --oldversion Specify version to begin with.
-v, --verbose List files as they are examined.
-c, --clean Clean up -bak files
-r, --revert Revert the conversion from the -bak files
""")
def mapconvert2(mapline):
@ -131,15 +147,19 @@ def mapconvert2(mapline):
return mapline
if __name__ == '__main__':
(options, arguments) = getopt.getopt(sys.argv[1:], "dho:v", [
(options, arguments) = getopt.getopt(sys.argv[1:], "cdho:rv", [
"help",
"oldversion=",
"dryrun",
"verbose",
"clean",
"revert",
])
oldversion = 'older'
dryrun = False
verbose = False
clean = False
revert = False
for (switch, val) in options:
if switch in ('-h', '--help'):
help()
@ -150,6 +170,14 @@ if __name__ == '__main__':
verbose = True
elif switch in ('-d', '--dryrun'):
dryrun = True
elif switch in ('-c', '--clean'):
clean = True
elif switch in ('-r', '--revert'):
revert = True
if clean and revert:
sys.stderr.write("upconvert: can't do clean and revert together.\n")
sys.exit(1)
# Compute the series of version upgrades to perform, and describe it.
versions = filemoves.keys()
@ -171,36 +199,50 @@ if __name__ == '__main__':
for fn in allcfgfiles("."):
if verbose:
print fn
if dryrun:
ifp = open(fn)
backup = fn + "-bak"
if clean or revert:
# Do housekeeping
if os.path.exists(backup):
if clean:
print "Removing %s" % backup
if not dryrun:
os.remove(backup)
elif revert:
print "Reverting %s" % backup
if not dryrun:
os.rename(backup, fn)
else:
os.rename(fn, fn + "-bak")
ifp = open(fn + "-bak")
ofp = open(fn, "w")
modified = False
for (i, line) in enumerate(ifp):
transformed = line
# Filename conversions
if ".cfg" in fn:
for step in fileconversions:
for (old, new) in step:
transformed = transformed.replace(old, new)
# Map-format conversions
if "1.3.1" in versions and 'message' not in transformed:
transformed = mapconvert2(transformed)
# Do file conversions
if dryrun:
ifp = open(fn)
else:
os.rename(fn, backup)
ifp = open(backup)
ofp = open(fn, "w")
modified = False
for (i, line) in enumerate(ifp):
transformed = line
# Filename conversions
if ".cfg" in fn:
for step in fileconversions:
for (old, new) in step:
transformed = transformed.replace(old, new)
# Map-format conversions
if "1.3.1" in versions and 'message' not in transformed:
transformed = mapconvert2(transformed)
if ofp:
ofp.write(transformed)
if transformed != line:
if not 'maps' in fn:
print "%s, line %d: %s -> %s" % \
(fn, i+1, line.strip(), transformed.strip())
modified = True
if ofp:
ofp.write(transformed)
if transformed != line:
if not 'maps' in fn:
print "%s, line %d: %s -> %s" % \
(fn, i+1, line.strip(), transformed.strip())
modified = True
if ofp:
ofp.close()
if not modified:
# Nothing changed, move the backup file back into place.
os.rename(fn + "-bak", fn)
if modified and 'maps' in fn:
print "%s modified." % fn
ofp.close()
if not modified:
# Nothing changed, move the backup file back into place.
os.rename(backup, fn)
if modified and 'maps' in fn:
print "%s modified." % fn
# upconvert ends here