Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/rgrep.py @ 74006:88198b93ff2f
Issue #13560: Add PyUnicode_EncodeLocale()
* Use PyUnicode_EncodeLocale() in time.strftime() if wcsftime() is not
available
* Document my last changes in Misc/NEWS
| author | Victor Stinner <victor.stinner@haypocalc.com> |
|---|---|
| date | Sat, 17 Dec 2011 04:13:41 +0100 |
| parents | 5ae390ac6c61 |
| children |
| rev | line source |
|---|---|
|
59522
fcfe324663a6
convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents:
42427
diff
changeset
|
1 #! /usr/bin/env python3 |
|
8981
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 """Reverse grep. |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 Usage: rgrep [-i] pattern file |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 """ |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 import sys |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 import re |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 import getopt |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
12 |
|
8981
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 def main(): |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
14 bufsize = 64 * 1024 |
|
8981
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 reflags = 0 |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 opts, args = getopt.getopt(sys.argv[1:], "i") |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 for o, a in opts: |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 if o == '-i': |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 reflags = reflags | re.IGNORECASE |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 if len(args) < 2: |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 usage("not enough arguments") |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 if len(args) > 2: |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 usage("exactly one file argument required") |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 pattern, filename = args |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 try: |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 prog = re.compile(pattern, reflags) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
25219
diff
changeset
|
27 except re.error as msg: |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
28 usage("error in regular expression: %s" % msg) |
|
8981
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 try: |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 f = open(filename) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
25219
diff
changeset
|
31 except IOError as msg: |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
32 usage("can't open %r: %s" % (filename, msg), 1) |
|
8981
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 f.seek(0, 2) |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 pos = f.tell() |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 leftover = None |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 while pos > 0: |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 size = min(pos, bufsize) |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 pos = pos - size |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 f.seek(pos) |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 buffer = f.read(size) |
|
25219
8a7a2f1f88f0
Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents:
8981
diff
changeset
|
41 lines = buffer.split("\n") |
|
8981
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 del buffer |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 if leftover is None: |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 if not lines[-1]: |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 del lines[-1] |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 else: |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 lines[-1] = lines[-1] + leftover |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 if pos > 0: |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 leftover = lines[0] |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 del lines[0] |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 else: |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 leftover = None |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
53 for line in reversed(lines): |
|
8981
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 if prog.search(line): |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
55 print(line) |
|
8981
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
57 |
|
8981
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 def usage(msg, code=2): |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 sys.stdout = sys.stderr |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
60 print(msg) |
|
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
61 print(__doc__) |
|
8981
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 sys.exit(code) |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 |
|
63872
5ae390ac6c61
Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
64 |
|
8981
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 if __name__ == '__main__': |
|
0398051f8ffa
New tool: reverse grep (greps from the end). Uses a fairly efficient
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 main() |
