| |
---|
| | |
---|
| | PROGNAME = "tren.py" |
---|
| | BASENAME = PROGNAME.split(".py")[0] |
---|
| | PROGENV = BASENAME.upper() |
---|
| | RCSID = "$Id: tren.py,v 1.185 2010/03/10 23:37:27 tundra Exp $" |
---|
| | RCSID = "$Id: tren.py,v 1.186 2010/03/11 02:49:28 tundra Exp $" |
---|
| | VERSION = RCSID.split()[2] |
---|
| | |
---|
| | # Copyright Information |
---|
| | |
---|
| |
---|
| | |
---|
| | # List all legal command line options that will be processed by getopt() later. |
---|
| | # We exclude -I here because it is parsed manually before the getopt() call. |
---|
| | |
---|
| | OPTIONSLIST = "Ccdfhi:P:qR:r:S:tvw:Xx" # All legal command line options in getopt() format |
---|
| | OPTIONSLIST = "aCcdfhi:P:qR:r:S:tvw:Xx" # All legal command line options in getopt() format |
---|
| | |
---|
| | |
---|
| | ##### |
---|
| | # Literals |
---|
| | ##### |
---|
| | |
---|
| | ARROW = "--->" # Used for formatting renaming messages |
---|
| | ASKDOREST = "!" # Do rest of renaming without asking |
---|
| | ASKNO = "N" # Do not rename current file |
---|
| | ASKQUIT = "q" # Quit renaming all further files |
---|
| | ASKYES = "y" # Rename current file |
---|
| | COMMENT = "#" # Comment character in include files |
---|
| | DEFINST = 0 # Default replacement instance |
---|
| | DEFLEN = 80 # Default output line length |
---|
| | DEFSEP = "=" # Default rename command separator: old=new |
---|
| |
---|
| | ##### |
---|
| | # Internal program state literals |
---|
| | ##### |
---|
| | |
---|
| | ASK = "ASK" |
---|
| | DEBUG = "DEBUG" |
---|
| | CASESENSITIVE = "CASESENSITIVE" |
---|
| | ESCAPE = "ESCAPE" |
---|
| | EXISTSUFFIX = "EXISTSUFFIX" |
---|
| |
---|
| | ##### |
---|
| | |
---|
| | uTable = [PROGVER, |
---|
| | HOMEPAGE, |
---|
| | "usage: " + PROGNAME + " [[-CcdfhqtvwXx] [-I file] [-i instance] [-P escape] [ -R separator] [-S suffix] [-r old=new]] ... file|dir file|dir ...", |
---|
| | "usage: " + PROGNAME + " [[-aCcdfhqtvwXx] [-I file] [-i instance] [-P escape] [ -R separator] [-S suffix] [-r old=new]] ... file|dir file|dir ...", |
---|
| | " where,", |
---|
| | " -a Turn on interactive asking before each rename (Default: Off)", |
---|
| | " -C Do case-sensitive renaming (Default)", |
---|
| | " -c Collapse case when doing string substitution (Default: False)", |
---|
| | " -d Dump debugging information (Default: False)", |
---|
| | " -f Force renaming even if target file or directory name already exists (Default: False)", |
---|
| |
---|
| | |
---|
| | IncludedFiles = [] |
---|
| | |
---|
| | ProgramOptions = { |
---|
| | |
---|
| | ASK : False, # Interactively ask user before renaming each file |
---|
| | DEBUG : False, # Debugging off |
---|
| | CASESENSITIVE : True, # Search is case-sensitive |
---|
| | ESCAPE : DEFESC, # Escape string |
---|
| | EXISTSUFFIX : DEFSUFFIX, # What to tack on when renaming existing targets |
---|
| |
---|
| | ORDERBYSIZE : [fullnames in size order] |
---|
| | } |
---|
| | |
---|
| | self.RenRequests = [ |
---|
| | { OLD : old rename string, |
---|
| | { |
---|
| | ASK : interactive ask flag |
---|
| | OLD : old rename string, |
---|
| | NEW : new rename string, |
---|
| | DEBUG : debug flag, |
---|
| | CASESENSITIVE : case sensitivity flag, |
---|
| | FORCERENAME : force renaming flag, |
---|
| |
---|
| | else: |
---|
| | InfoMsg(indent + iRENSKIPPED % (fullnew, fullold)) |
---|
| | doit = False |
---|
| | |
---|
| | |
---|
| | if doit: |
---|
| | |
---|
| | self.NewNames.append(fullnew) |
---|
| | InfoMsg(indent + iRENAMING % (fullold, fullnew)) |
---|
| | |
---|
| | if not ProgramOptions[TESTMODE]: |
---|
| | |
---|
| | try: |
---|
| | os.rename(fullold, fullnew) |
---|
| | except OSError as e: |
---|
| | ErrorMsg(eRENAMEFAIL % (fullold, fullnew, e.args[1])) |
---|
| | if ProgramOptions[ASK]: |
---|
| | |
---|
| | answer = "" |
---|
| | while answer.lower() not in [ASKNO.lower(), ASKYES.lower(), ASKDOREST.lower(), ASKQUIT.lower()]: |
---|
| | |
---|
| | PrintStdout("Rename %s to %s? [%s]: " % (fullold, fullnew, ASKNO+ASKYES+ASKDOREST+ASKQUIT), TRAILING="") |
---|
| | |
---|
| | answer = sys.stdin.readline().lower().strip() |
---|
| | |
---|
| | # A blank line means take the default - do nothing. |
---|
| | |
---|
| | if not answer: |
---|
| | answer = ASKNO.lower() |
---|
| | |
---|
| | if answer == ASKNO.lower(): |
---|
| | doit = False |
---|
| | |
---|
| | if answer == ASKYES.lower(): |
---|
| | doit = True |
---|
| | |
---|
| | if answer == ASKDOREST.lower(): |
---|
| | doit = True |
---|
| | ProgramOptions[ASK] = False |
---|
| | |
---|
| | if answer == ASKQUIT.lower(): |
---|
| | sys.exit(1) |
---|
| | |
---|
| | if doit: |
---|
| | |
---|
| | self.NewNames.append(fullnew) |
---|
| | InfoMsg(indent + iRENAMING % (fullold, fullnew)) |
---|
| | |
---|
| | if not ProgramOptions[TESTMODE]: |
---|
| | |
---|
| | try: |
---|
| | os.rename(fullold, fullnew) |
---|
| | except OSError as e: |
---|
| | ErrorMsg(eRENAMEFAIL % (fullold, fullnew, e.args[1])) |
---|
| | |
---|
| | self.indentlevel -= 1 |
---|
| | |
---|
| | # End of '__RenameIt()' |
---|
| |
---|
| | # Now process the options |
---|
| | |
---|
| | for opt, val in opts: |
---|
| | |
---|
| | # Turn on interactive "ask" mode |
---|
| | |
---|
| | if opt == "-a": |
---|
| | ProgramOptions[ASK] = True |
---|
| | |
---|
| | # Select case-sensitivity for replacements (or not) |
---|
| | |
---|
| | if opt == "-C": |
---|
| | ProgramOptions[CASESENSITIVE] = True |
---|
| |
---|
| | |