Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/rgrep.py @ 73323:dad02a080bbc
Improve byte-compilation in packaging to be independent of -O or -B.
The code I fixed to comply with PEP 3147 still had one bug: When run
under python -O, some paths for pyc files would be pyo, because I called
imp.cache_from_source without explicit debug_override argument in some
places, and under -O that would return .pyo (this is well explained in
the imp docs). Now all code (util.byte_compile, build_py, install_lib)
can create .pyo files according to options given by users,
without interference from the calling Python’s own optimize mode.
On a related topic, I also removed the code that prevented byte
compilation under python -B. The rationale is that packaging gives
control over the creation of pyc files to the user with its own explicit
option, and the behavior should not be changed if the calling Python
happens to run with -B for whatever reason. I will argue that this is a
bug fix and ask to be allowed to backport this change to distutils.
Finally, I moved one nugget of information about the --compile and
--optimize options from the source into the doc. It clears up a
misunderstanding that I (and maybe other people) had.
| author | Éric Araujo <merwok@netwok.org> |
|---|---|
| date | Thu, 03 Nov 2011 05:08:28 +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() |
