|
1 #! /usr/bin/env python |
|
2 |
|
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 |
|
23 |
|
24 import sys |
|
25 import string |
|
26 import getopt |
|
27 |
|
28 FS = '' |
|
29 SCRIPT = [] |
|
30 AFLAG = 0 |
|
31 CFLAG = 0 |
|
32 DFLAG = 0 |
|
33 NFLAG = 0 |
|
34 PFLAG = 0 |
|
35 |
|
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 |
|
42 for option, optarg in optlist: |
|
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???' |
|
62 |
|
63 if not ARGS: ARGS.append('-') |
|
64 |
|
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('-') |
|
77 |
|
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 = [] |
|
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 |
|
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') |
|
121 |
|
122 import tempfile |
|
123 fp = tempfile.NamedTemporaryFile() |
|
124 fp.write(program) |
|
125 fp.flush() |
|
126 if DFLAG: |
|
127 import pdb |
|
128 pdb.run('execfile(%r)' % (tfn,)) |
|
129 else: |
|
130 execfile(tfn) |