blob: 24960466e475bf3ad53b1311f65e5cd323526229 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossumca7b2131992-07-07 09:11:53 +00002
Guido van Rossumff535a11992-08-10 10:42:36 +00003# 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 Rossumca7b2131992-07-07 09:11:53 +000023
24import sys
25import string
26import getopt
27
28FS = ''
Guido van Rossumff535a11992-08-10 10:42:36 +000029SCRIPT = []
30AFLAG = 0
31CFLAG = 0
32DFLAG = 0
33NFLAG = 0
34PFLAG = 0
Guido van Rossumca7b2131992-07-07 09:11:53 +000035
Guido van Rossumff535a11992-08-10 10:42:36 +000036try:
37 optlist, ARGS = getopt.getopt(sys.argv[1:], 'acde:F:np')
38except getopt.error, msg:
39 sys.stderr.write(sys.argv[0] + ': ' + msg + '\n')
40 sys.exit(2)
41
Guido van Rossumca7b2131992-07-07 09:11:53 +000042for option, optarg in optlist:
Guido van Rossumff535a11992-08-10 10:42:36 +000043 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 Rossumca7b2131992-07-07 09:11:53 +000062
Guido van Rossumff535a11992-08-10 10:42:36 +000063if not ARGS: ARGS.append('-')
Guido van Rossumca7b2131992-07-07 09:11:53 +000064
Guido van Rossumff535a11992-08-10 10:42:36 +000065if 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 Rossumca7b2131992-07-07 09:11:53 +000077
Guido van Rossumff535a11992-08-10 10:42:36 +000078if CFLAG:
79 prologue = ['if 0:']
80 epilogue = []
81elif 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 ]
110else:
111 prologue = ['if 1:']
112 epilogue = []
Guido van Rossumca7b2131992-07-07 09:11:53 +0000113
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 Rossumff535a11992-08-10 10:42:36 +0000117program = string.joinfields(prologue, '\n') + '\n'
118for line in SCRIPT:
119 program = program + (' \t \t' + line + '\n')
120program = program + (string.joinfields(epilogue, '\n') + '\n')
Guido van Rossumca7b2131992-07-07 09:11:53 +0000121
Guido van Rossumff535a11992-08-10 10:42:36 +0000122import tempfile
123tfn = tempfile.mktemp()
124try:
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)
133finally:
134 import os
135 try:
136 os.unlink(tfn)
137 except:
138 pass