Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | ca7b213 | 1992-07-07 09:11:53 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | ff535a1 | 1992-08-10 10:42:36 +0000 | [diff] [blame] | 3 | # Emulate some Perl command line options. |
| 4 | # Usage: pp [-a] [-c] [-d] [-e scriptline] [-F fieldsep] [-n] [-p] [file] ... |
| 5 | # Where the options mean the following: |
| 6 | # -a : together with -n or -p, splits each line into list F |
| 7 | # -c : check syntax only, do not execute any code |
| 8 | # -d : run the script under the debugger, pdb |
| 9 | # -e scriptline : gives one line of the Python script; may be repeated |
| 10 | # -F fieldsep : sets the field separator for the -a option [not in Perl] |
| 11 | # -n : runs the script for each line of input |
| 12 | # -p : prints the line after the script has run |
| 13 | # When no script lines have been passed, the first file argument |
| 14 | # contains the script. With -n or -p, the remaining arguments are |
| 15 | # read as input to the script, line by line. If a file is '-' |
| 16 | # or missing, standard input is read. |
| 17 | |
| 18 | # XXX To do: |
| 19 | # - add -i extension option (change files in place) |
| 20 | # - make a single loop over the files and lines (changes effect of 'break')? |
| 21 | # - add an option to specify the record separator |
| 22 | # - except for -n/-p, run directly from the file if at all possible |
Guido van Rossum | ca7b213 | 1992-07-07 09:11:53 +0000 | [diff] [blame] | 23 | |
| 24 | import sys |
| 25 | import string |
| 26 | import getopt |
| 27 | |
| 28 | FS = '' |
Guido van Rossum | ff535a1 | 1992-08-10 10:42:36 +0000 | [diff] [blame] | 29 | SCRIPT = [] |
| 30 | AFLAG = 0 |
| 31 | CFLAG = 0 |
| 32 | DFLAG = 0 |
| 33 | NFLAG = 0 |
| 34 | PFLAG = 0 |
Guido van Rossum | ca7b213 | 1992-07-07 09:11:53 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | ff535a1 | 1992-08-10 10:42:36 +0000 | [diff] [blame] | 36 | try: |
| 37 | optlist, ARGS = getopt.getopt(sys.argv[1:], 'acde:F:np') |
| 38 | except getopt.error, msg: |
| 39 | sys.stderr.write(sys.argv[0] + ': ' + msg + '\n') |
| 40 | sys.exit(2) |
| 41 | |
Guido van Rossum | ca7b213 | 1992-07-07 09:11:53 +0000 | [diff] [blame] | 42 | for option, optarg in optlist: |
Guido van Rossum | ff535a1 | 1992-08-10 10:42:36 +0000 | [diff] [blame] | 43 | if option == '-a': |
| 44 | AFLAG = 1 |
| 45 | elif option == '-c': |
| 46 | CFLAG = 1 |
| 47 | elif option == '-d': |
| 48 | DFLAG = 1 |
| 49 | elif option == '-e': |
| 50 | for line in string.splitfields(optarg, '\n'): |
| 51 | SCRIPT.append(line) |
| 52 | elif option == '-F': |
| 53 | FS = optarg |
| 54 | elif option == '-n': |
| 55 | NFLAG = 1 |
| 56 | PFLAG = 0 |
| 57 | elif option == '-p': |
| 58 | NFLAG = 1 |
| 59 | PFLAG = 1 |
| 60 | else: |
| 61 | print option, 'not recognized???' |
Guido van Rossum | ca7b213 | 1992-07-07 09:11:53 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | ff535a1 | 1992-08-10 10:42:36 +0000 | [diff] [blame] | 63 | if not ARGS: ARGS.append('-') |
Guido van Rossum | ca7b213 | 1992-07-07 09:11:53 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | ff535a1 | 1992-08-10 10:42:36 +0000 | [diff] [blame] | 65 | if not SCRIPT: |
| 66 | if ARGS[0] == '-': |
| 67 | fp = sys.stdin |
| 68 | else: |
| 69 | fp = open(ARGS[0], 'r') |
| 70 | while 1: |
| 71 | line = fp.readline() |
| 72 | if not line: break |
| 73 | SCRIPT.append(line[:-1]) |
| 74 | del fp |
| 75 | del ARGS[0] |
| 76 | if not ARGS: ARGS.append('-') |
Guido van Rossum | ca7b213 | 1992-07-07 09:11:53 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | ff535a1 | 1992-08-10 10:42:36 +0000 | [diff] [blame] | 78 | if CFLAG: |
| 79 | prologue = ['if 0:'] |
| 80 | epilogue = [] |
| 81 | elif NFLAG: |
| 82 | # Note that it is on purpose that AFLAG and PFLAG are |
| 83 | # tested dynamically each time through the loop |
| 84 | prologue = [ \ |
| 85 | 'LINECOUNT = 0', \ |
| 86 | 'for FILE in ARGS:', \ |
| 87 | ' \tif FILE == \'-\':', \ |
| 88 | ' \t \tFP = sys.stdin', \ |
| 89 | ' \telse:', \ |
| 90 | ' \t \tFP = open(FILE, \'r\')', \ |
| 91 | ' \tLINENO = 0', \ |
| 92 | ' \twhile 1:', \ |
| 93 | ' \t \tLINE = FP.readline()', \ |
| 94 | ' \t \tif not LINE: break', \ |
| 95 | ' \t \tLINENO = LINENO + 1', \ |
| 96 | ' \t \tLINECOUNT = LINECOUNT + 1', \ |
| 97 | ' \t \tL = LINE[:-1]', \ |
| 98 | ' \t \taflag = AFLAG', \ |
| 99 | ' \t \tif aflag:', \ |
| 100 | ' \t \t \tif FS: F = string.splitfields(L, FS)', \ |
| 101 | ' \t \t \telse: F = string.split(L)' \ |
| 102 | ] |
| 103 | epilogue = [ \ |
| 104 | ' \t \tif not PFLAG: continue', \ |
| 105 | ' \t \tif aflag:', \ |
| 106 | ' \t \t \tif FS: print string.joinfields(F, FS)', \ |
| 107 | ' \t \t \telse: print string.join(F)', \ |
| 108 | ' \t \telse: print L', \ |
| 109 | ] |
| 110 | else: |
| 111 | prologue = ['if 1:'] |
| 112 | epilogue = [] |
Guido van Rossum | ca7b213 | 1992-07-07 09:11:53 +0000 | [diff] [blame] | 113 | |
| 114 | # Note that we indent using tabs only, so that any indentation style |
| 115 | # used in 'command' will come out right after re-indentation. |
| 116 | |
Guido van Rossum | ff535a1 | 1992-08-10 10:42:36 +0000 | [diff] [blame] | 117 | program = string.joinfields(prologue, '\n') + '\n' |
| 118 | for line in SCRIPT: |
| 119 | program = program + (' \t \t' + line + '\n') |
| 120 | program = program + (string.joinfields(epilogue, '\n') + '\n') |
Guido van Rossum | ca7b213 | 1992-07-07 09:11:53 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | ff535a1 | 1992-08-10 10:42:36 +0000 | [diff] [blame] | 122 | import tempfile |
| 123 | tfn = tempfile.mktemp() |
| 124 | try: |
| 125 | fp = open(tfn, 'w') |
| 126 | fp.write(program) |
| 127 | fp.close() |
| 128 | if DFLAG: |
| 129 | import pdb |
| 130 | pdb.run('execfile(' + `tfn` + ')') |
| 131 | else: |
| 132 | execfile(tfn) |
| 133 | finally: |
| 134 | import os |
| 135 | try: |
| 136 | os.unlink(tfn) |
| 137 | except: |
| 138 | pass |