diff --git a/tren.py b/tren.py index fa23f43..8749026 100755 --- a/tren.py +++ b/tren.py @@ -7,7 +7,7 @@ PROGNAME = "tren.py" PROGENV = PROGNAME.split(".py")[0].upper() -RCSID = "$Id: tren.py,v 1.122 2010/01/29 21:18:35 tundra Exp $" +RCSID = "$Id: tren.py,v 1.123 2010/01/30 18:20:44 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -68,6 +68,7 @@ PADWIDTH = 30 # Column width LSTPAD = 13 # Padding to use when dumping lists WRAPINDENT = 8 # Extra indent on wrapped lines +MINLEN = PADWIDTH + WRAPINDENT + 1 # Minimum line length ##### @@ -81,7 +82,7 @@ EXTDELIM = "." # Extension delimeter INCL = "-I" # Include file command line option NAM = "Nam" # Rename target is name -SEPARATOR = "-" *70 +SEPCHAR = "-" # Character used for debug separator lines #----------------------------------------------------------# @@ -99,7 +100,6 @@ dDEBUG = "DEBUG" dDUMPOBJ = "Dumping Object %s" dINCLUDING = "Including file '%s'" -dPAIR = "Option/Target Pair" dPROGENV = "$" + PROGENV dRENREQ = "Renaming Requests:" dRESOLVEDOPTS = "Resolved Command Line" @@ -121,11 +121,13 @@ # Error Messages ##### -eBADARG = "Invalid Command Line: %s!" +eBADARG = "Invalid command line: %s!" eBADINCL = "option %s requires argument" % INCL +eBOGUSLEN = "Bogus line length '%s'!" eERROR = "ERROR" -eFILEOPEN = "Cannot Open File '%s': %s!" -eTOOMANYINC = "Too Many Includes! (Max Is %d) Possible Circular Reference?" % MAXINCLUDES +eFILEOPEN = "Cannot open file '%s': %s!" +eLINELEN = "Specified line length too short! Must be at least %s" % MINLEN +eTOOMANYINC = "Too many includes! (Max is %d) Possible circular reference?" % MAXINCLUDES ##### @@ -138,7 +140,7 @@ ##### uTable = [PROGNAME + " " + VERSION + " - %s\n" % COPYRIGHT, - "usage: " + PROGNAME + " [-1abCcdEefghqtvXx] [-I file] [-l string] [-r old=new]... file|dir file|dir ...", + "usage: " + PROGNAME + " [-1abCcdEefghqtvwXx] [-I file] [-l string] [-r old=new]... file|dir file|dir ...", " where,", " -1 Rename only the first instance of the specified string (Default)", " -a Rename within the entire file or directory name (Default)", @@ -156,6 +158,7 @@ " -r Replace old with new in file or directory names.", " -t Test mode, don't rename, just show what the program *would* do", " -v Print detailed program version information and exit.", + " -w Line length of diagnostic and error output (Default: 75)" " -X Treat the renaming strings literally (Default)", " -x Treat the old replacement string as a Python regular expression", ] @@ -252,9 +255,14 @@ cmdorder = 0 for t in targs: - fullname = os.path.abspath(t) - basename = os.path.basename(t) - stats = os.stat(fullname) + try: + fullname = os.path.abspath(t) + basename = os.path.basename(t) + stats = os.stat(fullname) + except (IOError, OSError) as e: + ErrorMsg(eFILEOPEN % (t, e.args[1])) + sys.exit(1) + # This data structure is used to keep track of everything # we need to build the sequence renaming token support. @@ -371,18 +379,16 @@ def ConditionLine(msg, padchar=PADCHAR, \ padwidth=PADWIDTH, \ - wrapindent=WRAPINDENT, \ - maxlinelen=MAXLINELEN): + wrapindent=WRAPINDENT ): retval = [] - - retval.append(msg[:maxlinelen]) - msg = msg[maxlinelen:] + retval.append(msg[:MAXLINELEN]) + msg = msg[MAXLINELEN:] while msg: msg = padchar * (padwidth + wrapindent) + msg - retval.append(msg[:maxlinelen]) - msg = msg[maxlinelen:] + retval.append(msg[:MAXLINELEN]) + msg = msg[MAXLINELEN:] return retval @@ -394,9 +400,10 @@ ##### def DebugMsg(msg): - l = ConditionLine(msg) - for msg in l: - PrintStderr(PROGNAME + " " + VERSION + " " + dDEBUG + ": " + msg) + + l = ConditionLine(msg) + for msg in l: + PrintStderr(PROGNAME + " " + dDEBUG + ": " + msg) # End of 'DebugMsg()' @@ -420,6 +427,7 @@ def DumpRenameObj(obj): + SEPARATOR = SEPCHAR * MAXLINELEN DebugMsg("\n") DebugMsg(SEPARATOR) DebugMsg(dDUMPOBJ % str(obj)) @@ -443,6 +451,7 @@ def DumpState(): + SEPARATOR = SEPCHAR * MAXLINELEN DebugMsg("\n") DebugMsg(SEPARATOR) DebugMsg(dCURSTATE) @@ -476,9 +485,11 @@ ##### def ErrorMsg(emsg): + l = ConditionLine(emsg) + for emsg in l: - PrintStderr(PROGNAME + " " + VERSION + " " + eERROR + ": " + emsg) + PrintStderr(PROGNAME + " " + eERROR + ": " + emsg) # End of 'ErrorMsg()' @@ -579,47 +590,6 @@ ##### -# Break The Command Line Into Separate "Option... Target..." Pairs -##### - -def ProcessOptTgtPair(OPTIONS): - - DOINGOPTS = True - commandlines = [] - cmd = [] - - while OPTIONS: - - i = OPTIONS[0] - OPTIONS = OPTIONS[1:] - - # Process options - if i.startswith("-"): - if DOINGOPTS: - cmd.append(i) - - # Starting a new "options... targets ..." pair - else: - commandlines.append(cmd) - cmd = [] - DOINGOPTS = True - cmd.append(i) - - # Process targets - else: - DOINGOPTS = False - cmd.append(i) - - - if cmd: - commandlines.append(cmd) - - return commandlines - -# End of 'ProcessOptTgtPair()' - - -##### # Print Usage Information ##### @@ -682,9 +652,67 @@ if DEBUGFLAG in OPTIONS: DEBUG = True -# Break command line into "option ... targets ..." pairs. +RenRequests = [] -pairs = ProcessOptTgtPair(OPTIONS) +try: + opts, args = getopt.getopt(OPTIONS, '1abbCcdEefghl:qr:tvw:Xx]') +except getopt.GetoptError as e: + ErrorMsg(eBADARG % e.args[0]) + sys.exit(1) + +# Now process the options + +for opt, val in opts: + + if opt == "-1": + GLOBAL = False + if opt == "-a": + TARGET = ALL + if opt == "-b": + TARGET = NAM + if opt == "-C": + CASESENSITIVE = True + if opt == "-c": + CASESENSITIVE = False + if opt == "-d": + DumpState() + if opt == "-E": + ERRORCONTINUE = True + if opt == "-e": + TARGET = EXT + if opt == "-f": + FORCERENAM = True + if opt == "-g": + GLOBAL = True + if opt == "-h": + Usage() + sys.exit(0) + if opt == "-l": + EXTDELIM = val + if opt == "-q": + QUIET = True + if opt == "-r": + RenRequests.append(val) + if opt == "-t": + TESTMODE = True + if opt == "-v": + PrintStdout(RCSID) + sys.exit(0) + if opt == "-w": + try: + l = int(val) + except: + ErrorMsg(eBOGUSLEN % val) + sys.exit(1) + if l < MINLEN: + ErrorMsg(eLINELEN) + sys.exit(1) + MAXLINELEN = l + if opt == "-X": + REGEX = False + if opt == "-x": + REGEX = True + if DEBUG: @@ -694,80 +722,21 @@ DebugMsg(ColumnPad([dPROGENV, os.getenv("TREN")])) DebugMsg(ColumnPad([dRESOLVEDOPTS, OPTIONS])) +# Create and populate an object with rename targets. We have to +# do *before* we process any renaming requests because they may make +# reference to renaming tokens that only can be resolved with the +# contents of the 'targs' data structure. -# Now process the command line in "opts... targets" pairs +targs = None +if args: + targs = RenameTargets(args) -for commandline in pairs: - - RenRequests = [] +# Display outstanding renaming requests if we're debugging - if DEBUG: - DebugMsg(ColumnPad([dPAIR, " ".join(commandline)])) +if DEBUG: + DumpList(DebugMsg, dRENREQ , "", RenRequests) - try: - opts, args = getopt.getopt(commandline, '1abbCcdEefghl:qr:tvXx]') - except getopt.GetoptError as e: - ErrorMsg(eBADARG % e.args[0]) - sys.exit(1) +# Release the target container if we created one - # Create and populate an object with rename targets. We have to - # do *before* we process any renaming requests because they may make - # reference to renaming tokens that only can be resolved with the - # contents of the 'targs' data structure. - - targs = None - if args: - targs = RenameTargets(args) - - # Now process the options - - for opt, val in opts: - - if opt == "-1": - GLOBAL = False - if opt == "-a": - TARGET = ALL - if opt == "-b": - TARGET = NAM - if opt == "-C": - CASESENSITIVE = True - if opt == "-c": - CASESENSITIVE = False - if opt == "-d": - DumpState() - if opt == "-E": - ERRORCONTINUE = True - if opt == "-e": - TARGET = EXT - if opt == "-f": - FORCERENAM = True - if opt == "-g": - GLOBAL = True - if opt == "-h": - Usage() - sys.exit(0) - if opt == "-l": - EXTDELIM = val - if opt == "-q": - QUIET = True - if opt == "-r": - RenRequests.append(val) - if opt == "-t": - TESTMODE = True - if opt == "-v": - PrintStdout(RCSID) - sys.exit(0) - if opt == "-X": - REGEX = False - if opt == "-x": - REGEX = True - - - # Display outstanding renaming requests if we're debugging - - if DEBUG: - DumpList(DebugMsg, dRENREQ , "", RenRequests) - - # Release the target container if we created one - if targs: - del targs +if targs: + del targs