Another step towards working wmlmove.

This commit is contained in:
Eric S. Raymond 2007-05-16 20:30:20 +00:00
parent 960667de7a
commit 77d442c572
2 changed files with 59 additions and 14 deletions

View file

@ -32,7 +32,7 @@ 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.
'''
import sys, os, time, re, glob, getopt, sre_constants, md5
import sys, os, time, re, getopt, sre_constants, md5
import wmltools
if __name__ == "__main__":
@ -74,10 +74,16 @@ if __name__ == "__main__":
# Locate the unit .cfgs to be moved.
srclist = []
for src in arguments[:-1]:
if src.startswith("core"):
src = os.path.join("data", src)
else:
src = os.path.join("data", "campaigns", src)
try:
(namespace, resource) = src.split("::")
except ValueError:
sys.stderr.write("wmlmove: source name must be in the form "
"namespace::resource.\n")
sys.exit(1)
if not wmltools.is_namespace(namespace):
sys.stderr.write("wmlmove: no such scope as %s.\n" % namespace)
sys.exit(1)
src = wmltools.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)
@ -85,19 +91,14 @@ if __name__ == "__main__":
print "From:", srclist
# Validate the destination.
dst = arguments[-1]
if dst == "core":
dst = os.path.join("data", "core")
elif dst in os.listdir(os.path.join("data", "campaigns")):
dst = os.path.join("data", "campaigns", dst)
dst = wmltools.namespace_directory(arguments[-1])
if dst == None:
sys.stderr.write("wmlmove: invalid destination %s\n" % dst)
sys.exit(1)
print "To:", dst
# Cross-reference all files.
# This wires in some knowledge of the shape of the tree.
cref = wmltools.CrossRef(["data/core"] + glob.glob("data/campaigns/*"))
cref = wmltools.CrossRef(wmltools.scopelist())
# Filter reference information on all files referenced in the source .cfgs
srcrefs = cref.subtract(srclist)
@ -109,5 +110,7 @@ if __name__ == "__main__":
(defloc, len(defloc.references))
defloc.dump_references()
# This tool is not finished. More logic will go here.

View file

@ -3,7 +3,7 @@ wmltools.py -- Python routines for working with a Battle For Wesnoth WMl tree
"""
import sys, os, re, sre_constants, md5
import sys, os, re, sre_constants, md5, glob
resource_extensions = ("png", "jpg", "ogg", "wav", "map")
@ -274,6 +274,48 @@ class CrossRef:
del self.fileref[filename].references[referrer]
return smallref
## Namespace management
#
# This is the only part of the code that actually knows about the
# shape of the data tree.
def scopelist():
"Return a list of (separate) package scopes, core first."
return ["data/core"] + glob.glob("data/campaigns/*")
def is_namespace(name):
"Is the name either a valid campaign name or core?"
return name in map(os.path.basename, scopelist())
def namespace_directory(name):
"Go from namespace to directory."
if name == "core":
return "data/core"
else:
return "data/campaigns/" + name + "/"
def directory_namespace(path):
"Go from directory to namespace."
if path.startswith("data/core/"):
return "core"
elif path.startswith("data/campaigns/"):
return path.split("/")[2]
else:
return None
def namespace_member(path, namespace):
"Is a path in a specified namespace?"
ns = directory_namespace(path)
return ns != None and ns == namespace
def resolve_unit_cfg(namespace, resource):
"Get the location of a specified unit in a specified scope."
return namespace_directory(namespace) + "units/" + resource + ".cfg"
def resolve_unit_image(namespace, subdir, resource):
"Construct a plausible location for given resource in specified namespace."
return os.path.join(namespace_directory(namespace), "images/units", subdir, resource)
## Version-control hooks begin here.
#
# Change these if we move away from Subversion