| |
---|
| | |
---|
| | 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 |
---|
| | |
---|
| |
---|
| | # General Program Constants |
---|
| | ##### |
---|
| | |
---|
| | 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 |
---|
| | ##### |
---|
| |
---|
| | |
---|
| | 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 |
---|
| | INCL = "I" # Include file command line option |
---|
| |
---|
| | eBADLEN = "Bad line length '%s'!" |
---|
| | 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 |
---|
| | |
---|
| | |
---|
| |
---|
| | try: |
---|
| | 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 |
---|
| | |
---|
| | self.RenNames[fullname] = {BASE : basename, PATHNAME : fullname.split(basename)[0], STATS : stats} |
---|
| |
---|
| | oldstrings.reverse() |
---|
| | 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()' |
---|
| | |
---|
| | |
---|
| |
---|
| | """ |
---|
| | |
---|
| | def ResolveRenameStrings(self, old, new): |
---|
| | |
---|
| | |
---|
| | return [old, new] |
---|
| | |
---|
| | # End of 'ReolveRenameStrings()' |
---|
| | |
---|
| |
---|
| | ##### |
---|
| | # 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()' |
---|
| | |
---|
| | ##### |
---|
| |
---|
| | i += 1 |
---|
| | |
---|
| | |
---|
| | if numseps != 1: |
---|
| | ErrorMsg(eBADNEWOLD % arg) |
---|
| | sys.exit(1) |
---|
| | ErrorMsg(eBADNEWOLD % arg, EXIT=True) |
---|
| | |
---|
| | else: |
---|
| | old, new = arg[:sepindex], arg[sepindex + len(oldnewsep):] |
---|
| | old = old.replace(ESC + oldnewsep, oldnewsep) |
---|
| |
---|
| | |
---|
| | # 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 |
---|
| | |
---|
| |
---|
| | # sure we stop recursive/circular includes. |
---|
| | |
---|
| | NUMINCLUDES += 1 |
---|
| | if NUMINCLUDES > MAXINCLUDES: |
---|
| | ErrorMsg(eTOOMANYINC) |
---|
| | sys.exit(1) |
---|
| | ErrorMsg(eTOOMANYINC, EXIT=True) |
---|
| | |
---|
| | # Read the included file, stripping out comments |
---|
| | |
---|
| | try: |
---|
| |
---|
| | |
---|
| | 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 |
---|
| | |
---|
| | return OPTIONS |
---|
| |
---|
| | |
---|
| | 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) |
---|
| | |
---|
| |
---|
| | if opt == "-w": |
---|
| | 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": |
---|
| | ProgramOptions[REGEX] = False |
---|
| |
---|
| | |