Merge pull request #582 from Elvish-Hunter/python_vc_removal

wmltools: removal of git wrapper functions
This commit is contained in:
Elvish-Hunter 2016-01-22 20:38:05 +01:00
commit 48459baff4
4 changed files with 2 additions and 334 deletions

View file

@ -933,47 +933,4 @@ def resolve_unit_image(namespace, subdir, resource):
# And this is for code that does syntax transformation
baseindent = " "
## Version-control hooks begin here.
#
# Not tested since the git transition
vcdir = ".git"
def vcmove(src, dst):
"Move a file under version control. Only applied to unmodified files."
(path, base) = os.path.split(src)
if os.path.exists(os.path.join(path, ".git")):
return "git mv %s %s" % (src, dst)
else:
return "echo 'cannot move %s to %s, .git is missing'" % (src, dst)
def vcunmove(src, dst):
"Revert the result of a previous move (before commit)."
(path, base) = os.path.split(src)
if os.path.exists(os.path.join(path, ".git")):
return "git checkout %s" % dst # Revert the add at the destination
return "git rm " + dst # Remove the moved copy
return "git checkout %s" % src # Revert the deletion
else:
return "echo 'cannot unmove %s from %s, .git is missing'" % (src, dst)
def vcdelete(src):
"Delete a file under version control."
(path, base) = os.path.split(src)
if os.path.exists(os.path.join(path, ".git")):
return "git rm %s" % src
else:
return "echo 'cannot undelete %s, .git is missing'" % src
def vcundelete(src):
"Revert the result of a previous delete (before commit)."
(path, base) = os.path.split(src)
if os.path.exists(os.path.join(path, ".git")):
return "git checkout %s" % src # Revert the deletion
else:
return "echo 'cannot undelete %s, .git is missing'" % src
#
## Version-control hooks end here
# wmltools.py ends here

View file

@ -928,47 +928,4 @@ def resolve_unit_image(namespace, subdir, resource):
# And this is for code that does syntax transformation
baseindent = " "
## Version-control hooks begin here.
#
# Not tested since the git transition
vcdir = ".git"
def vcmove(src, dst):
"Move a file under version control. Only applied to unmodified files."
(path, base) = os.path.split(src)
if os.path.exists(os.path.join(path, ".git")):
return "git mv %s %s" % (src, dst)
else:
return "echo 'cannot move %s to %s, .git is missing'" % (src, dst)
def vcunmove(src, dst):
"Revert the result of a previous move (before commit)."
(path, base) = os.path.split(src)
if os.path.exists(os.path.join(path, ".git")):
return "git checkout %s" % dst # Revert the add at the destination
return "git rm " + dst # Remove the moved copy
return "git checkout %s" % src # Revert the deletion
else:
return "echo 'cannot unmove %s from %s, .git is missing'" % (src, dst)
def vcdelete(src):
"Delete a file under version control."
(path, base) = os.path.split(src)
if os.path.exists(os.path.join(path, ".git")):
return "git rm %s" % src
else:
return "echo 'cannot undelete %s, .git is missing'" % src
def vcundelete(src):
"Revert the result of a previous delete (before commit)."
(path, base) = os.path.split(src)
if os.path.exists(os.path.join(path, ".git")):
return "git checkout %s" % src # Revert the deletion
else:
return "echo 'cannot undelete %s, .git is missing'" % src
#
## Version-control hooks end here
# wmltools.py ends here

View file

@ -2281,10 +2281,9 @@ if __name__ == '__main__':
# FIXME: We should make some effort to rename mask files.
if not revert and not diffs:
if not fn.endswith(".map") and not fn.endswith(".mask") and is_map(fn):
mover = vcmove(fn, fn + ".map")
print(mover)
print('wmllint: renaming "%s" to "%s"' % (fn, fn + ".map"))
if not dryrun:
os.system(mover)
os.rename(fn, fn + ".map")
elif fn in is_main and os.path.isdir(fn.replace('.cfg', '')):
main = fn.replace('.cfg', '/_main.cfg')
if os.path.exists(main):

View file

@ -1,245 +0,0 @@
#!/usr/bin/env python
'''\
wmlmove -- generate commands to move a unit in the Wesnoth tree
usage: wmlmove --help
wmlmove [--revert] {src...} {dst}
wmlmove --delete src
This script facilitates moving or deleting units (referenced by their
.cfg file) along with all of their associated resources (images and
sounds). It may be run from the Wesnoth top-level directory or
anywhere beneath.
For purposes of this command, a "namespace identifier" is either the
basename of a campaign subdirectory or the special name "core",
indicating the units, images and sounds directories in data/core.
A source must be a namespace identifier, followed by "::", followed by
the name of a unit (the basename of its path without the .cfg
extension). A destination must be a namespace identifier, optionally
followed by :: and a unit class.
The script generates a sequence of shell commands. These commands
will move the unit and all its resources to appropriate places under
the target campaign or core directories. Appropriate VCS move commands
will be used on version-controlled files.
A resource is considered to belong to a unit (and moves with it) if the
resource is included in the unit .cfg and lives near the unit .cfg.
"Near" for a unit in the core data means anywhere in the core data,
but not in a campaign directory. "Near" for a unit under a
data/campaign directory means anywhere in that same campaign
directory, but not in core data or any other campaign directory.
The --revert option generates commands used to revert the result of a
specified move, undoing version-control operations as needed.
The --delete option removes a unit and its associated resources.
The --listspaces option lists all namespace identifiers.
The --imageclass option may be used to insert a directory path segment
into each destination filename just before the basename. Normally you
will not need to do this, as the image class will be deduced from the
second argument.
Note: After performing a move generated with this command, use
wmlscope to check that you have not left any references to the moved
units dangling.
Also be aware that this tool does nothing to update the unit
translation files.
Here is an example:
wmlmove Heir_To_The_Throne::Elvish_Lord Heir_To_The_Throne::Elvish_High_Lord Heir_To_The_Throne::Elvish_Lady core::elves
'''
import sys, os, time, re, getopt, sre_constants
from wesnoth.wmltools import *
if __name__ == "__main__":
# Process options.
(options, arguments) = getopt.getopt(sys.argv[1:], "dhi:lLr", ['delete', 'help', 'imageclass=', 'list', 'listspaces', 'revert'])
listem = False
listspaces = False
iclass = None
delete = False
revert = False
for (switch, val) in options:
if switch in ('-h', '--help'):
sys.stderr.write(__doc__)
sys.exit(0)
elif switch in ('-d', '--delete'):
delete = True
elif switch in ('-i', '--imageclass'):
iclass = val
elif switch in ('-l', '--list'):
listem = True
elif switch in ('-L', '--listspaces'):
listspaces = True
elif switch in ('-r', '--revert'):
listem = True
if not listspaces:
if len(arguments) == 0:
sys.stderr.write("wmlmove: at least one path to a unit is required.\n")
sys.stderr.write(__doc__)
sys.exit(1)
if not delete:
if len(arguments) == 1:
sys.stderr.write("wmlmove: a campaign name or 'core' is required.\n")
sys.stderr.write(__doc__)
sys.exit(1)
else:
dst = arguments.pop()
pop_to_top("wmlmove")
if listspaces:
print "\n".join(map(os.path.basename, scopelist()))
sys.exit(0)
# Locate the unit .cfgs to be moved.
srclist = []
for src in arguments:
try:
(namespace, resource) = src.split("::")
except ValueError:
sys.stderr.write(
"wmlmove: source name %s not in the form namespace::resource.\n" % src)
sys.exit(1)
if not is_namespace(namespace):
sys.stderr.write("wmlmove: no such scope as %s.\n" % namespace)
sys.exit(1)
src = resolve_unit_cfg(namespace, resource)
if not os.path.exists(src):
sys.stderr.write("wmlmove: can't find %s to move it.\n" % src)
sys.exit(1)
srclist.append(src)
# Validate the destination.
if not delete:
uclass = None
if "::" in dst:
(dst, uclass) = dst.split("::")
elif dst == "core":
sys.stderr.write("wmlmove: moves to core require a unit subclass.\n")
sys.exit(1)
dstdir = namespace_directory(dst)
if dstdir == None:
sys.stderr.write("wmlmove: invalid namespace %s\n" % dstdir)
sys.exit(1)
# Cross-reference all files.
cref = CrossRef(scopelist())
# Filter reference information on all files referenced in the source .cfgs
srcrefs = cref.subtract(srclist)
# List all relevant resources and their other references.
if listem:
for (name, defloc) in srcrefs.fileref.items():
print "Resource %s is used in %d files:" % \
(defloc, len(defloc.references))
defloc.dump_references()
# Generate the actual move commands
print "# Generated script from", " ".join(sys.argv[1:])
if not delete and not revert:
print '''
replace()
# Replace a specified string with another in any number of files
{
left="$1"; right="$2"; shift; shift;
for file in $*
do
if grep "$left" $file >/dev/null 2>&1
then
overwrite $file sed "s@$left@$right@g" $file
fi
done
}
overwrite()
# Replace a file with itself filtered by a command
{
opath=$PATH
PATH=/bin:/usr/bin
file=$1; shift
new=/tmp/over$$; old=/tmp/under$$
trap \'rm -f $new $old ; exit 1\' 1 2 15
if PATH=$opath "$@" >$new
then
cp $file $old # save the old file
trap "" 1 2 15 # We are committed; ignore signals
cp $new $file
else
echo "overwrite: $1 failed, $file unchanged" 1 >&2
exit 1
fi
rm -f $new $old
}
'''
print "chdir", os.getcwd()
if delete:
print "# Image deletions:"
for (name, defloc) in srcrefs.fileref.items():
for namespace in map(directory_namespace, srclist):
if namespace_member(name, namespace) and cref.refcount(name) == 0:
if revert:
print vcundelete(name)
else:
print vcdelete(name)
break
print ""
print "# .cfg deletions"
for filename in srclist:
if revert:
print vcundelete(filename)
else:
print vcdelete(filename)
else:
if iclass == None:
if not iclass:
if uclass:
iclass = uclass
else:
iclass = 'monsters'
print "# Defaulting image subclass to '%s', use -i to set it." % iclass
print "# Image moves:"
for (name, defloc) in srcrefs.fileref.items():
source = directory_namespace(name)
target = resolve_unit_image(dst, iclass, os.path.basename(name))
if revert:
if not namespace_member(name, source):
print vcunmove(name, target)
else:
if not namespace_member(name, dst):
print vcmove(name, target)
print ""
print "# .cfg moves and transformations"
for filename in srclist:
source = directory_namespace(filename)
target = resolve_unit_cfg(dst, uclass, os.path.basename(filename))
if revert:
if not namespace_member(filename, source):
print vcunmove(filename, target)
else:
if not namespace_member(filename, dst):
print vcmove(filename, target)
if iclass:
print "replace 'units/' 'units/%s/' %s" % (iclass, target)
if dst == "core":
print "replace '#textdomain wesnoth.*' '#textdomain wesnoth' %s" % target
else:
print "echo 'Warning: text domain will require manual change.'"
# wmlmove ends here