annotate Demo/scripts/pp.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 32c35f9b245c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4573
73758db2f464 /usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents: 1267
diff changeset
1 #! /usr/bin/env python
912
60e3118d3073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
934
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
3 # Emulate some Perl command line options.
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
4 # Usage: pp [-a] [-c] [-d] [-e scriptline] [-F fieldsep] [-n] [-p] [file] ...
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
5 # Where the options mean the following:
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
6 # -a : together with -n or -p, splits each line into list F
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
7 # -c : check syntax only, do not execute any code
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
8 # -d : run the script under the debugger, pdb
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
9 # -e scriptline : gives one line of the Python script; may be repeated
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
10 # -F fieldsep : sets the field separator for the -a option [not in Perl]
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
11 # -n : runs the script for each line of input
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
12 # -p : prints the line after the script has run
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
13 # When no script lines have been passed, the first file argument
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
14 # contains the script. With -n or -p, the remaining arguments are
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
15 # read as input to the script, line by line. If a file is '-'
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
16 # or missing, standard input is read.
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
17
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
18 # XXX To do:
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
19 # - add -i extension option (change files in place)
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
20 # - make a single loop over the files and lines (changes effect of 'break')?
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
21 # - add an option to specify the record separator
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
22 # - except for -n/-p, run directly from the file if at all possible
912
60e3118d3073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
60e3118d3073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 import sys
60e3118d3073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 import getopt
60e3118d3073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
60e3118d3073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 FS = ''
934
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
28 SCRIPT = []
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
29 AFLAG = 0
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
30 CFLAG = 0
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
31 DFLAG = 0
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
32 NFLAG = 0
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
33 PFLAG = 0
912
60e3118d3073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
934
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
35 try:
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
36 optlist, ARGS = getopt.getopt(sys.argv[1:], 'acde:F:np')
934
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
37 except getopt.error, msg:
56445
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
38 sys.stderr.write('%s: %s\n' % (sys.argv[0], msg))
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
39 sys.exit(2)
934
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
40
912
60e3118d3073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 for option, optarg in optlist:
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
42 if option == '-a':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
43 AFLAG = 1
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
44 elif option == '-c':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
45 CFLAG = 1
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
46 elif option == '-d':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
47 DFLAG = 1
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
48 elif option == '-e':
56445
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
49 for line in optarg.split('\n'):
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
50 SCRIPT.append(line)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
51 elif option == '-F':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
52 FS = optarg
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
53 elif option == '-n':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
54 NFLAG = 1
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
55 PFLAG = 0
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
56 elif option == '-p':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
57 NFLAG = 1
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
58 PFLAG = 1
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
59 else:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
60 print option, 'not recognized???'
912
60e3118d3073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
934
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
62 if not ARGS: ARGS.append('-')
912
60e3118d3073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
934
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
64 if not SCRIPT:
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
65 if ARGS[0] == '-':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
66 fp = sys.stdin
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
67 else:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
68 fp = open(ARGS[0], 'r')
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
69 while 1:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
70 line = fp.readline()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
71 if not line: break
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
72 SCRIPT.append(line[:-1])
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
73 del fp
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
74 del ARGS[0]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
75 if not ARGS: ARGS.append('-')
934
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
76
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
77 if CFLAG:
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
78 prologue = ['if 0:']
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
79 epilogue = []
934
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
80 elif NFLAG:
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
81 # Note that it is on purpose that AFLAG and PFLAG are
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
82 # tested dynamically each time through the loop
56445
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
83 prologue = [
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
84 'LINECOUNT = 0',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
85 'for FILE in ARGS:',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
86 ' \tif FILE == \'-\':',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
87 ' \t \tFP = sys.stdin',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
88 ' \telse:',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
89 ' \t \tFP = open(FILE, \'r\')',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
90 ' \tLINENO = 0',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
91 ' \twhile 1:',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
92 ' \t \tLINE = FP.readline()',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
93 ' \t \tif not LINE: break',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
94 ' \t \tLINENO = LINENO + 1',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
95 ' \t \tLINECOUNT = LINECOUNT + 1',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
96 ' \t \tL = LINE[:-1]',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
97 ' \t \taflag = AFLAG',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
98 ' \t \tif aflag:',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
99 ' \t \t \tif FS: F = L.split(FS)',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
100 ' \t \t \telse: F = L.split()'
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
101 ]
56445
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
102 epilogue = [
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
103 ' \t \tif not PFLAG: continue',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
104 ' \t \tif aflag:',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
105 ' \t \t \tif FS: print FS.join(F)',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
106 ' \t \t \telse: print \' \'.join(F)',
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
107 ' \t \telse: print L',
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
108 ]
934
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
109 else:
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
110 prologue = ['if 1:']
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
111 epilogue = []
912
60e3118d3073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
60e3118d3073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 # Note that we indent using tabs only, so that any indentation style
60e3118d3073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 # used in 'command' will come out right after re-indentation.
60e3118d3073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
56445
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
116 program = '\n'.join(prologue) + '\n'
934
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
117 for line in SCRIPT:
56445
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
118 program += ' \t \t' + line + '\n'
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
119 program += '\n'.join(epilogue) + '\n'
912
60e3118d3073 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
934
d383d2733176 Almost totally rewritten.
Guido van Rossum <guido@python.org>
parents: 912
diff changeset
121 import tempfile
24710
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 4573
diff changeset
122 fp = tempfile.NamedTemporaryFile()
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 4573
diff changeset
123 fp.write(program)
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 4573
diff changeset
124 fp.flush()
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 4573
diff changeset
125 if DFLAG:
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
126 import pdb
56445
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
127 pdb.run('execfile(%r)' % (fp.name,))
24710
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 4573
diff changeset
128 else:
56445
32c35f9b245c Bring old demo up-to-date.
Georg Brandl <georg@python.org>
parents: 32735
diff changeset
129 execfile(fp.name)