+++ /dev/null
-#! /usr/bin/env python\r
-\r
-# Emulate some Perl command line options.\r
-# Usage: pp [-a] [-c] [-d] [-e scriptline] [-F fieldsep] [-n] [-p] [file] ...\r
-# Where the options mean the following:\r
-# -a : together with -n or -p, splits each line into list F\r
-# -c : check syntax only, do not execute any code\r
-# -d : run the script under the debugger, pdb\r
-# -e scriptline : gives one line of the Python script; may be repeated\r
-# -F fieldsep : sets the field separator for the -a option [not in Perl]\r
-# -n : runs the script for each line of input\r
-# -p : prints the line after the script has run\r
-# When no script lines have been passed, the first file argument\r
-# contains the script. With -n or -p, the remaining arguments are\r
-# read as input to the script, line by line. If a file is '-'\r
-# or missing, standard input is read.\r
-\r
-# XXX To do:\r
-# - add -i extension option (change files in place)\r
-# - make a single loop over the files and lines (changes effect of 'break')?\r
-# - add an option to specify the record separator\r
-# - except for -n/-p, run directly from the file if at all possible\r
-\r
-import sys\r
-import getopt\r
-\r
-FS = ''\r
-SCRIPT = []\r
-AFLAG = 0\r
-CFLAG = 0\r
-DFLAG = 0\r
-NFLAG = 0\r
-PFLAG = 0\r
-\r
-try:\r
- optlist, ARGS = getopt.getopt(sys.argv[1:], 'acde:F:np')\r
-except getopt.error, msg:\r
- sys.stderr.write('%s: %s\n' % (sys.argv[0], msg))\r
- sys.exit(2)\r
-\r
-for option, optarg in optlist:\r
- if option == '-a':\r
- AFLAG = 1\r
- elif option == '-c':\r
- CFLAG = 1\r
- elif option == '-d':\r
- DFLAG = 1\r
- elif option == '-e':\r
- for line in optarg.split('\n'):\r
- SCRIPT.append(line)\r
- elif option == '-F':\r
- FS = optarg\r
- elif option == '-n':\r
- NFLAG = 1\r
- PFLAG = 0\r
- elif option == '-p':\r
- NFLAG = 1\r
- PFLAG = 1\r
- else:\r
- print option, 'not recognized???'\r
-\r
-if not ARGS: ARGS.append('-')\r
-\r
-if not SCRIPT:\r
- if ARGS[0] == '-':\r
- fp = sys.stdin\r
- else:\r
- fp = open(ARGS[0], 'r')\r
- while 1:\r
- line = fp.readline()\r
- if not line: break\r
- SCRIPT.append(line[:-1])\r
- del fp\r
- del ARGS[0]\r
- if not ARGS: ARGS.append('-')\r
-\r
-if CFLAG:\r
- prologue = ['if 0:']\r
- epilogue = []\r
-elif NFLAG:\r
- # Note that it is on purpose that AFLAG and PFLAG are\r
- # tested dynamically each time through the loop\r
- prologue = [\r
- 'LINECOUNT = 0',\r
- 'for FILE in ARGS:',\r
- ' \tif FILE == \'-\':',\r
- ' \t \tFP = sys.stdin',\r
- ' \telse:',\r
- ' \t \tFP = open(FILE, \'r\')',\r
- ' \tLINENO = 0',\r
- ' \twhile 1:',\r
- ' \t \tLINE = FP.readline()',\r
- ' \t \tif not LINE: break',\r
- ' \t \tLINENO = LINENO + 1',\r
- ' \t \tLINECOUNT = LINECOUNT + 1',\r
- ' \t \tL = LINE[:-1]',\r
- ' \t \taflag = AFLAG',\r
- ' \t \tif aflag:',\r
- ' \t \t \tif FS: F = L.split(FS)',\r
- ' \t \t \telse: F = L.split()'\r
- ]\r
- epilogue = [\r
- ' \t \tif not PFLAG: continue',\r
- ' \t \tif aflag:',\r
- ' \t \t \tif FS: print FS.join(F)',\r
- ' \t \t \telse: print \' \'.join(F)',\r
- ' \t \telse: print L',\r
- ]\r
-else:\r
- prologue = ['if 1:']\r
- epilogue = []\r
-\r
-# Note that we indent using tabs only, so that any indentation style\r
-# used in 'command' will come out right after re-indentation.\r
-\r
-program = '\n'.join(prologue) + '\n'\r
-for line in SCRIPT:\r
- program += ' \t \t' + line + '\n'\r
-program += '\n'.join(epilogue) + '\n'\r
-\r
-import tempfile\r
-fp = tempfile.NamedTemporaryFile()\r
-fp.write(program)\r
-fp.flush()\r
-if DFLAG:\r
- import pdb\r
- pdb.run('execfile(%r)' % (fp.name,))\r
-else:\r
- execfile(fp.name)\r