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: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 37 | optlist, ARGS = getopt.getopt(sys.argv[1:], 'acde:F:np') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 38 | except getopt.error as msg: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 39 | sys.stderr.write(sys.argv[0] + ': ' + msg + '\n') |
| 40 | sys.exit(2) |
Guido van Rossum | ff535a1 | 1992-08-10 10:42:36 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | ca7b213 | 1992-07-07 09:11:53 +0000 | [diff] [blame] | 42 | for option, optarg in optlist: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +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: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 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: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 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: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 79 | prologue = ['if 0:'] |
| 80 | epilogue = [] |
Guido van Rossum | ff535a1 | 1992-08-10 10:42:36 +0000 | [diff] [blame] | 81 | elif NFLAG: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 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 | ] |
Guido van Rossum | ff535a1 | 1992-08-10 10:42:36 +0000 | [diff] [blame] | 110 | else: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 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: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 119 | program = program + (' \t \t' + line + '\n') |
Guido van Rossum | ff535a1 | 1992-08-10 10:42:36 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 123 | fp = tempfile.NamedTemporaryFile() |
| 124 | fp.write(program) |
| 125 | fp.flush() |
| 126 | if DFLAG: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 127 | import pdb |
| 128 | pdb.run('execfile(%r)' % (tfn,)) |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 129 | else: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 130 | execfile(tfn) |