# renaming a working package. usage = """usage: python renamewp.py old new where old and new are decimal WP numbers in the range 1-99. """ import os, sys, mkxref def do_rename(old, new): oldS = "WP%02d" % old newS = "WP%02d" % new wpfiles = mkxref.get_wps_from_dir() if wpfiles.has_key(oldS): if wpfiles.has_key(newS): raise ValueError, "cannot rename because %s is in the way!" % wpfiles[newS] oldName = wpfiles[oldS] p = len(mkxref.START_STR) newName = "%s%02d%s" % (oldName[:p], new, oldName[p+2:]) cmd = 'svn move "%s" "%s"' % (oldName, newName) print ">>>", cmd err = os.system(cmd) if err: print "aborting due to the above error" sys.exit(1) del wpfiles[oldS] wpfiles[newS] = newName workfiles = os.listdir(".") workfiles = [fname for fname in workfiles if os.path.splitext(fname)[-1].lower() == ".txt"] find = oldS+"_" repl = newS+"_" for fname in workfiles: txt = file(fname).read() if txt.find(find) >= 0: print "rewriting %s" % fname txt = txt.replace(find, repl) file(fname, "w").write(txt) print "re-creating cross references" mkxref.build_xref_file() print "done." def main(): args = sys.argv[1:] try: old, new = map(int, args) if min(old, new) < 1 or max(old, new) > 99: raise ValueError except: print usage sys.exit(1) do_rename(old, new) if __name__ == "__main__": main()