Simplified test mode reporting to only show top level renames and not any consequence backups or skips.
Implemented live file renaming.
1 parent bad7741 commit 454f7b71423b84171fefec78b68b8e72341089bf
@tundra tundra authored on 17 Feb 2010
Showing 1 changed file
View
63
tren.py
 
PROGNAME = "tren.py"
BASENAME = PROGNAME.split(".py")[0]
PROGENV = BASENAME.upper()
RCSID = "$Id: tren.py,v 1.154 2010/02/17 22:07:53 tundra Exp $"
RCSID = "$Id: tren.py,v 1.155 2010/02/18 00:49:45 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
eLINELEN = "Specified line length too short! Must be at least %s" % MINLEN
eNAMELONG = "Renaming '%s' to new name '%s' too long! (Maximum length is %s.)"
eNAMESHORT = "Renaming '%s' to new name '%s' too short! (Minimum length is %s.)"
eNULLARG = "%s cannot be empty!"
eRENAMEFAIL = "Attempt to rename '%s' to '%s' failed : %s!"
eTOOMANYINC = "Too many includes! (Max is %d) Possible circular reference?" % MAXINCLUDES
 
 
#####
#####
 
iRENFORCED = "Target '%s' Exists. Creating Backup."
iRENSKIPPED = "Target '%s' Exists. Renaming Of '%s' Skipped."
iRENAMING = "Renaming '%s' To '%s'."
iRENAMING = "Renaming '%s' " + ARROW + " '%s'."
 
#####
# Usage Prompts
#####
# Release the working data structures
 
del SeqTypes
 
# End of '__ini__()'
# End of '__init__()'
 
 
#####
# Debug Dump
#####
 
def Rename(self):
 
self.newnames = []
self.indentlevel = -1
 
# Create a list of all renaming to be done.
# This includes the renaming of any existing targets.
oldstrings.reverse()
for i in oldstrings:
newname = newname[:i] + new + newname[i + len(old):]
 
# If the new name is different from the old one, rename it.
 
# Nothing to do, if old- and new names are the same
 
if newname != oldname:
self.__RenameIt(pathname, oldname, newname)
 
# If we're in test mode, just show what we would do
 
if ProgramOptions[TESTMODE]:
 
InfoMsg(iRENAMING % (pathname+oldname, pathname+newname))
 
# Otherwise, actually do the renaming
 
else:
self.__RenameIt(pathname, oldname, newname)
# End of 'Rename()'
 
 
#####
# Actually Rename A File (Or Show What Would Happen)
# Actually Rename A File
#####
 
def __RenameIt(self, pathname, oldname, newname):
 
# requested. We such backups with a recursive call to
# ourselves so that length and backups of backups are
# enforced.
 
# NOTE: We have to keep track of every new name produced.
# When running in test mode, this is the only way to know
# what *would* end up on the disk (in case a previous
# renaming operation creates a filename that is now in
# conflict with a subseqent renaming target.
 
if (fullnew in self.newnames) or os.path.exists(fullnew):
 
if os.path.exists(fullnew):
 
if ProgramOptions[FORCERENAME]:
 
# Create the backup
 
bkuname = newname + ProgramOptions[EXISTSUFFIX]
InfoMsg(indent + iRENFORCED % fullnew)
self.__RenameIt(pathname, newname, bkuname)
self.newnames.remove(fullnew)
# Rename the original
 
self.__RenameIt(pathname, oldname, newname)
 
# No target conflict, just do the requested renaming
 
else:
 
self.newnames.append(fullnew)
try:
os.rename(fullold, fullnew)
except OSError as e:
ErrorMsg(eRENAMEFAIL % (fullold, fullnew, e.args[1]))
 
self.indentlevel -= 1
 
# End of '__RenameIt()'
if ProgramOptions[TESTMODE]:
msgtype = TESTMODE
 
if not ProgramOptions[QUIET]:
for dmsg in l:
PrintStdout(PROGNAME + " " + msgtype + ": " + imsg)
for msg in l:
PrintStdout(PROGNAME + " " + msgtype + ": " + msg)
 
# End of 'InfoMsg()'