diff --git a/tren.py b/tren.py index d949f55..77238f2 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.166 2010/03/05 21:54:36 tundra Exp $" +RCSID = "$Id: tren.py,v 1.167 2010/03/06 01:18:02 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -104,8 +104,9 @@ # Replacement Token Literals ##### -TOKENDELIM = "/" # Symbol used to delimit renaming tokens -TOKENENV = "$" # Environment variable token +TOKENCMDEXEC = "`" # Delimiter for command execution renaming tokens +TOKENDELIM = "/" # Delimiter for all renaming tokens +TOKENENV = "$" # Introducer for environment variable replacement tokens # Internal program state literals @@ -184,6 +185,7 @@ eBADRENAMETOK = "Renaming token missing delimiter: %s" eBADREGEX = "Invalid Regular Expression: %s" eERROR = "ERROR" +eEXECFAIL = "Command: '%s' Failed To Execute!" 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.)" @@ -710,11 +712,43 @@ for r in rentokens: - # Environment variable replacement token + # Environment Variable replacement token if r[2].startswith(TOKENENV): r[2] = os.getenv(r[2][1:]) + # Command Run replacement token + + elif r[2].startswith(TOKENCMDEXEC) and r[2].endswith(TOKENCMDEXEC): + + command = r[2][1:-1] + + # Handle Windows variants which act differently + + if os.name != 'posix': + pipe = os.popen(command, 'r') + + # Handle Unix variants + + else: + pipe = os.popen('{ ' + command + '; } 2>&1', 'r') + + output = pipe.read() + status = pipe.close() + + if status == None: + status = 0 + + # Nonzero status means error attempting to execute the command + + if status: + ErrorMsg(eEXECFAIL % command) + + # Otherwise swap the command with its results, stripping newlines + + else: + r[2] = output.replace("\n", "") + # Do the actual replacement renstring = renstring[:r[0]] + r[2] + renstring[r[1]+1:]