From 9f7364c95589d5b6272c07824f84763d7d973af6 Mon Sep 17 00:00:00 2001 From: mcmartin Date: Tue, 17 Jun 2008 01:05:03 +0000 Subject: [PATCH] A new script for easing custom content moves. git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3025 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/doc/devel/resources | 3 ++ tools/resmap/translate.py | 96 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100755 tools/resmap/translate.py diff --git a/sc2/doc/devel/resources b/sc2/doc/devel/resources index 269f52bd9..20db1b7de 100644 --- a/sc2/doc/devel/resources +++ b/sc2/doc/devel/resources @@ -125,6 +125,9 @@ compile. Such resources will not be reflected in the RMP files, but will have their headers defined appropriately so that addon packs may provide values. +Major reorganization of files may be done with the transform.py file; +its usage string describes its script format. + LEGACY SYSTEM ------------- diff --git a/tools/resmap/translate.py b/tools/resmap/translate.py new file mode 100755 index 000000000..13884749c --- /dev/null +++ b/tools/resmap/translate.py @@ -0,0 +1,96 @@ +#!/usr/bin/python + +import sys +from gen_resfiles import * + +class FileRemap: + def __init__(self, src, dest): + self.src = src + self.dest = dest + def as_script(self): + return "%s -> %s" % (self.src, self.dest) + def svn_shell(self): + return ["svn move %s %s" % (self.src, self.dest)] + def transform(self, x): + if x.res_file == self.src: + x.res_file = self.dest + +class DirRemap: + def __init__(self, src, dest): + self.src = src + self.dest = dest + def as_script(self): + return "%s => %s" % (self.src, self.dest) + def svn_shell(self): + return ["svn move %s %s" % (self.src, self.dest)] + def transform(self, x): + if x.res_file.startswith(self.src): + x.res_file = self.dest + x.res_file[len(self.src):] + +def load_script(fname): + result = [] + linenum = 0; + for line in file(fname): + linenum += 1 + line = line.strip() + if line == '' or line[0] == '#': + continue + tokens = [x.strip() for x in line.split()] + # No spaces in file names, please. + if len(tokens) == 3: + (src, op, dest) = tokens + if op == '->': + result.append(FileRemap(src, dest)) + elif op == '=>': + result.append(DirRemap(src, dest)) + else: + print>>sys.stderr, "Line %d: Warning: Unknown operator '%s'" % (linenum, op) + else: + print>>sys.stderr, "Line %d: Warning: Unparsable line" % linenum + return result + +def usage(): + print>>sys.stderr,"""Usage: + + %s [filenames] + +Where [filenames] are the names of the resource modification scripts. Each +line in a modification script has the form + + old_filename -> new_filename + +or + + old_dirname => new_dirname + +This script will then rewrite resources.csv and translate.sh appropriately. +The translate.sh script should be run from the base content directory.""" % sys.argv[0] + +def go(filenames): + r = read_csv() + shellscript = [] + for f in sys.argv[1:]: + try: + vals = load_script(f) + for x in vals: + shellscript.extend(x.svn_shell()) + for y in r: + x.transform(y) + print + except IOError, msg: + print "%s: %s" % (f, msg.strerror) + outf = file('transform.sh', 'wt') + for x in shellscript: + print>>outf, x + outf.close() + os.chmod("transform.sh", 0755) + outf = file('resources.csv', 'wt') + for x in r: + print>>outf, x.csv_line() + outf.close() + +if __name__ == '__main__': + if len(sys.argv) == 1: + usage() + else: + go(sys.argv[1:])