diff --git a/tren.py b/tren.py index 9d8a5fa..69c8367 100755 --- a/tren.py +++ b/tren.py @@ -8,7 +8,7 @@ PROGNAME = "tren.py" BASENAME = PROGNAME.split(".py")[0] PROGENV = BASENAME.upper() -RCSID = "$Id: tren.py,v 1.145 2010/02/05 18:05:53 tundra Exp $" +RCSID = "$Id: tren.py,v 1.146 2010/02/05 19:20:25 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -61,6 +61,8 @@ ##### MAXINCLUDES = 50 # Maximum number of includes allowed +MAXNAMELEN = 255 # Maximum file or directory name length +MINNAMELEN = 1 # Minimum file or directory name length ##### # Message Formatting Constants @@ -83,7 +85,7 @@ ALL = "All" # Rename target is whole filename COMMENT = "#" # Comment character in include files DEFEXT = "." # Default name/extension separator -DEFLEN = 75 # Default output line length +DEFLEN = 80 # Default output line length DEFSEP = "=" # Default rename command separator: old=new ESC = "\\" # Escape character EXT = "Ext" # Rename target is extension @@ -165,6 +167,8 @@ eERROR = "ERROR" eFILEOPEN = "Cannot open file '%s': %s!" 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.)" eNOTHINGTODO = "Nothing to do!" eTOOMANYINC = "Too many includes! (Max is %d) Possible circular reference?" % MAXINCLUDES @@ -341,8 +345,7 @@ basename = os.path.basename(fullname) stats = os.stat(fullname) except (IOError, OSError) as e: - ErrorMsg(eFILEOPEN % (fullname, e.args[1])) - sys.exit(1) + ErrorMsg(eFILEOPEN % (fullname, e.args[1]), EXIT=True) # Store fullname, basename, and stat info for this file @@ -490,7 +493,23 @@ for i in oldstrings: basename = basename[:i] + new + basename[i + len(old):] - print ColumnPad([target, pathname + basename], padwidth = 50) + # Now that we have a new name, attempt the actual renaming + # operation + + RenamingError = False + + # First make sure the new name meets length constraints + + if len(basename) < MINNAMELEN: + ErrorMsg(eNAMESHORT% (target, basename, MINNAMELEN), EXIT=not ProgramOptions[ERRORCONTINUE]) + RenamingError = True + + if len(basename) > MAXNAMELEN: + ErrorMsg(eNAMELONG % (target, basename, MAXNAMELEN), EXIT=not ProgramOptions[ERRORCONTINUE]) + RenamingError = True + + if not RenamingError: + print ColumnPad([target, pathname + basename], padwidth = 50) # End of 'Rename()' @@ -506,6 +525,7 @@ def ResolveRenameStrings(self, old, new): + return [old, new] # End of 'ReolveRenameStrings()' @@ -610,13 +630,16 @@ # Print An Error Message ##### -def ErrorMsg(emsg): +def ErrorMsg(emsg, EXIT=False): l = ConditionLine(emsg) for emsg in l: PrintStderr(PROGNAME + " " + eERROR + ": " + emsg) + if EXIT: + sys.exit(1) + # End of 'ErrorMsg()' ##### @@ -649,8 +672,7 @@ if numseps != 1: - ErrorMsg(eBADNEWOLD % arg) - sys.exit(1) + ErrorMsg(eBADNEWOLD % arg, EXIT=True) else: old, new = arg[:sepindex], arg[sepindex + len(oldnewsep):] @@ -732,9 +754,7 @@ # We have an include without a filename at the end # of the command line which is bogus. else: - ErrorMsg(eBADARG % eBADINCL) - sys.exit(1) - + ErrorMsg(eBADARG % eBADINCL, EXIT=True) else: inclfile = rhs @@ -745,8 +765,7 @@ NUMINCLUDES += 1 if NUMINCLUDES > MAXINCLUDES: - ErrorMsg(eTOOMANYINC) - sys.exit(1) + ErrorMsg(eTOOMANYINC, EXIT=True) # Read the included file, stripping out comments @@ -774,8 +793,7 @@ OPTIONS = OPTIONS[:i] + n + OPTIONS[i+1:] except IOError as e: - ErrorMsg(eFILEOPEN % (inclfile, e.args[1])) - sys.exit(1) + ErrorMsg(eFILEOPEN % (inclfile, e.args[1]), EXIT=True) i += 1 @@ -839,13 +857,12 @@ try: opts, args = getopt.getopt(OPTIONS, 'abbCcdEefGghl:qR:r:tvw:Xx]') except getopt.GetoptError as e: - ErrorMsg(eBADARG % e.args[0]) - sys.exit(1) + ErrorMsg(eBADARG % e.args[0], EXIT=True) # Create and populate an object with rename targets. This must be # done here because this object also stores the -r renaming requests # we may find in the options processing below. Also, this object must -# be fully populated before any actual renaming can take places since +# be fully populated before any actual renaming can take place since # many of the renaming tokens derive information about the file. targs = RenameTargets(args) @@ -916,11 +933,9 @@ try: l = int(val) except: - ErrorMsg(eBADLEN % val) - sys.exit(1) + ErrorMsg(eBADLEN % val, EXIT=True) if l < MINLEN: - ErrorMsg(eLINELEN) - sys.exit(1) + ErrorMsg(eLINELEN, EXIT=True) ProgramOptions[MAXLINELEN] = l if opt == "-X":