| |
---|
| | |
---|
| | # Program Information |
---|
| | |
---|
| | PROGNAME = "tren.py" |
---|
| | RCSID = "$Id: tren.py,v 1.103 2010/01/22 00:58:04 tundra Exp $" |
---|
| | RCSID = "$Id: tren.py,v 1.104 2010/01/22 20:33:06 tundra Exp $" |
---|
| | VERSION = RCSID.split()[2] |
---|
| | |
---|
| | # Copyright Information |
---|
| | |
---|
| |
---|
| | ##### |
---|
| | # Literals |
---|
| | ##### |
---|
| | |
---|
| | ALL = "All" |
---|
| | EXT = "Ext" |
---|
| | EXTDELIM = "." |
---|
| | NAM = "Nam" |
---|
| | ALL = "All" # Rename target is whole filename |
---|
| | EXT = "Ext" # Rename target is extension |
---|
| | EXTDELIM = "." # Extension delimeter |
---|
| | INCL = "-I" # Include file command line option |
---|
| | NAM = "Nam" # Rename target is name |
---|
| | |
---|
| | |
---|
| | #----------------------------------------------------------# |
---|
| | # Prompts, & Application Strings # |
---|
| |
---|
| | ##### |
---|
| | # Debug Messages |
---|
| | ##### |
---|
| | |
---|
| | dDEBUG = "DEBUG" |
---|
| | dDEBUG = "DEBUG" |
---|
| | |
---|
| | ##### |
---|
| | # Error Messages |
---|
| | ##### |
---|
| | |
---|
| | eBADARG = "Invalid or malformed command line argument!" |
---|
| | eERROR = "ERROR" |
---|
| | eBADARG = "Invalid or malformed command line argument!" |
---|
| | eBADINCL = "%s Option Missing Filename Specification!" % INCL |
---|
| | eERROR = "ERROR" |
---|
| | eFILEOPEN = "Cannot Open File '%s' Because: %s" |
---|
| | |
---|
| | |
---|
| | ##### |
---|
| | # Informational Messages |
---|
| |
---|
| | # Usage Prompts |
---|
| | ##### |
---|
| | |
---|
| | uTable = [PROGNAME + " " + VERSION + " - %s\n" % COPYRIGHT, |
---|
| | "usage: " + PROGNAME + " [-1abCcdEefghqtvXx] [-F file] [-l string] [-r old=new]... file|dir file|dir ...", |
---|
| | "usage: " + PROGNAME + " [-1abCcdEefghqtvXx] [-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)", |
---|
| | " -C Do case-sensitive renaming (Default)", |
---|
| | " -c Collapse case when doing string substitution.", |
---|
| | " -d Dump debugging information", |
---|
| | " -e Only perform renaming within extension portion of or directory name.", |
---|
| | " -E Continue renaming even after an error is encountered", |
---|
| | " -F file Read command line arguments from file", |
---|
| | " -f Force renaming even if target file or directory name already exists.", |
---|
| | " -g Replace all instances (global rename) of the old string with the new.", |
---|
| | " -h Print help information.", |
---|
| | " -I file Include command line arguments from file", |
---|
| | " -l string File extension delimiter string. (Default: .)", |
---|
| | " -q Quiet mode, do not show progress.", |
---|
| | " -r <old=new> Replace old with new in file or directory names.", |
---|
| | " -t Test mode, don't rename, just show what the program *would* do", |
---|
| |
---|
| | def DumpState(): |
---|
| | |
---|
| | # Dump the command line |
---|
| | DebugMsg(ColumnPad(["Command Line", sys.argv])) |
---|
| | DebugMsg(ColumnPad(["$TREN", os.getenv("TREN")])) |
---|
| | DebugMsg(ColumnPad(["Resolved Options", OPTIONS])) |
---|
| | |
---|
| | # Names of all the state variables we want dumped |
---|
| | state = [ |
---|
| | "DEBUG", |
---|
| |
---|
| | #----------------------------------------------------------# |
---|
| | # Program Entry Point # |
---|
| | #----------------------------------------------------------# |
---|
| | |
---|
| | # Command line processing - Process any options set in the |
---|
| | # environment first, and then those given on the command line |
---|
| | ##### |
---|
| | # Command Line Preprocessing |
---|
| | # |
---|
| | # Some things have to be done *before* the command line |
---|
| | # options can actually be processed. This includes: |
---|
| | # |
---|
| | # 1) Prepending any options specified in the environment variable. |
---|
| | # |
---|
| | # 2) Resolving any include file references |
---|
| | # |
---|
| | # 3) Building the data structures that depend on the file/dir names |
---|
| | # specified for renaming. We have to do this first, because |
---|
| | # -r renaming operations specified on the command line will |
---|
| | # need this information if they make use of renaming tokens. |
---|
| | # |
---|
| | ##### |
---|
| | |
---|
| | # Process any options set in the environment first, and then those |
---|
| | # given on the command line |
---|
| | |
---|
| | |
---|
| | OPTIONS = sys.argv[1:] |
---|
| | envopt = os.getenv(PROGNAME.upper()) |
---|
| | |
---|
| | envopt = os.getenv(PROGNAME.split(".py")[0].upper()) |
---|
| | if envopt: |
---|
| | OPTIONS = envopt.split() + OPTIONS |
---|
| | |
---|
| | # Resolve include file references allowing for nested includes. |
---|
| | # This has to be done here separate from the command line options so |
---|
| | # that getopt() processing below will "see" the included statements. |
---|
| | |
---|
| | while " ". join(OPTIONS).find(INCL) > -1: |
---|
| | |
---|
| | # Get the index of the next include to process. |
---|
| | # It cannot be the last item because this means the filename |
---|
| | # to include is missing. |
---|
| | |
---|
| | i = OPTIONS.index(INCL) |
---|
| | if i == len(OPTIONS)-1: |
---|
| | ErrorMsg(eBADINCL) |
---|
| | sys.exit(1) |
---|
| | |
---|
| | file = OPTIONS[i+1] ; lhs = OPTIONS[:i] ; rhs = OPTIONS[i+2:] |
---|
| | |
---|
| | # Replace insert option on the command line with that file's contents |
---|
| | |
---|
| | try: |
---|
| | n = [] |
---|
| | f = open(file) |
---|
| | for l in f.readlines(): |
---|
| | n += l.split() |
---|
| | f.close() |
---|
| | |
---|
| | OPTIONS = lhs + n + rhs |
---|
| | |
---|
| | except IOError as e: |
---|
| | ErrorMsg(eFILEOPEN % (file, e.args[1])) |
---|
| | sys.exit(1) |
---|
| | |
---|
| | # Now process the command line options |
---|
| | |
---|
| | try: |
---|
| | opts, args = getopt.getopt(OPTIONS, '1abbCcdEeF:fghl:qr:tvXx]') |
---|
| | opts, args = getopt.getopt(OPTIONS, '1abbCcdEefghl:qr:tvXx]') |
---|
| | except getopt.GetoptError: |
---|
| | ErrorMsg(eBADARG) |
---|
| | Usage() |
---|
| | sys.exit(1) |
---|
| |
---|
| | if opt == "-E": |
---|
| | ERRORCONTINUE = True |
---|
| | if opt == "-e": |
---|
| | TARGET = EXT |
---|
| | if opt == "-F": |
---|
| | print opt, val |
---|
| | if opt == "-f": |
---|
| | FORCERENAM = True |
---|
| | if opt == "-g": |
---|
| | GLOBAL = True |
---|
| |
---|
| | EXTDELIM = val |
---|
| | if opt == "-q": |
---|
| | QUIET = True |
---|
| | if opt == "-r": |
---|
| | print opt, val |
---|
| | pass |
---|
| | if opt == "-t": |
---|
| | TESTMODE = True |
---|
| | if opt == "-v": |
---|
| | print RCSID |
---|
| | PrintStdout(RCSID) |
---|
| | sys.exit(0) |
---|
| | if opt == "-X": |
---|
| | REGEX = False |
---|
| | if opt == "-x": |
---|
| | |