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