Mercurial > cpython-lru_cache-weakref
annotate Demo/scripts/eqfix.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 4727f260f6f8 |
| children | 6b18a095c2e7 |
| rev | line source |
|---|---|
|
4573
73758db2f464
/usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents:
1506
diff
changeset
|
1 #! /usr/bin/env python |
| 553 | 2 |
|
555
0780b95a660e
Tokenizing version (see lib/tokenize.py).
Guido van Rossum <guido@python.org>
parents:
553
diff
changeset
|
3 # Fix Python source files to use the new equality test operator, i.e., |
|
32735
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
4 # if x = y: ... |
|
555
0780b95a660e
Tokenizing version (see lib/tokenize.py).
Guido van Rossum <guido@python.org>
parents:
553
diff
changeset
|
5 # is changed to |
|
32735
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
6 # if x == y: ... |
|
555
0780b95a660e
Tokenizing version (see lib/tokenize.py).
Guido van Rossum <guido@python.org>
parents:
553
diff
changeset
|
7 # The script correctly tokenizes the Python program to reliably |
|
0780b95a660e
Tokenizing version (see lib/tokenize.py).
Guido van Rossum <guido@python.org>
parents:
553
diff
changeset
|
8 # distinguish between assignments and equality tests. |
| 553 | 9 # |
| 10 # Command line arguments are files or directories to be processed. | |
| 11 # Directories are searched recursively for files whose name looks | |
| 12 # like a python module. | |
| 13 # Symbolic links are always ignored (except as explicit directory | |
| 14 # arguments). Of course, the original file is kept as a back-up | |
| 15 # (with a "~" attached to its name). | |
|
686
d244ee03c576
Check for binaries and non-python scripts.
Guido van Rossum <guido@python.org>
parents:
555
diff
changeset
|
16 # It complains about binaries (files containing null bytes) |
|
d244ee03c576
Check for binaries and non-python scripts.
Guido van Rossum <guido@python.org>
parents:
555
diff
changeset
|
17 # and about files that are ostensibly not Python files: if the first |
|
d244ee03c576
Check for binaries and non-python scripts.
Guido van Rossum <guido@python.org>
parents:
555
diff
changeset
|
18 # line starts with '#!' and does not contain the string 'python'. |
| 553 | 19 # |
| 20 # Changes made are reported to stdout in a diff-like format. | |
| 21 # | |
| 22 # Undoubtedly you can do this using find and sed or perl, but this is | |
| 23 # a nice example of Python code that recurses down a directory tree | |
| 24 # and uses regular expressions. Also note several subtleties like | |
| 25 # preserving the file's mode and avoiding to even write a temp file | |
| 26 # when no changes are needed for a file. | |
| 27 # | |
| 28 # NB: by changing only the function fixline() you can turn this | |
| 29 # into a program for a different change to Python programs... | |
| 30 | |
| 31 import sys | |
|
37021
4727f260f6f8
Remove regsub, reconvert, regex, regex_syntax and everything under lib-old.
Neal Norwitz <nnorwitz@gmail.com>
parents:
33456
diff
changeset
|
32 import re |
|
754
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
686
diff
changeset
|
33 import os |
| 553 | 34 from stat import * |
|
686
d244ee03c576
Check for binaries and non-python scripts.
Guido van Rossum <guido@python.org>
parents:
555
diff
changeset
|
35 import string |
| 553 | 36 |
| 37 err = sys.stderr.write | |
| 38 dbg = err | |
| 39 rep = sys.stdout.write | |
| 40 | |
| 41 def main(): | |
|
32735
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
42 bad = 0 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
43 if not sys.argv[1:]: # No arguments |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
44 err('usage: ' + sys.argv[0] + ' file-or-directory ...\n') |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
45 sys.exit(2) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
46 for arg in sys.argv[1:]: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
47 if os.path.isdir(arg): |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
48 if recursedown(arg): bad = 1 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
49 elif os.path.islink(arg): |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
50 err(arg + ': will not process symbolic links\n') |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
51 bad = 1 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
52 else: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
53 if fix(arg): bad = 1 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
54 sys.exit(bad) |
| 553 | 55 |
|
37021
4727f260f6f8
Remove regsub, reconvert, regex, regex_syntax and everything under lib-old.
Neal Norwitz <nnorwitz@gmail.com>
parents:
33456
diff
changeset
|
56 ispythonprog = re.compile('^[a-zA-Z0-9_]+\.py$') |
| 553 | 57 def ispython(name): |
|
32735
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
58 return ispythonprog.match(name) >= 0 |
| 553 | 59 |
| 60 def recursedown(dirname): | |
|
32735
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
61 dbg('recursedown(%r)\n' % (dirname,)) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
62 bad = 0 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
63 try: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
64 names = os.listdir(dirname) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
65 except os.error, msg: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
66 err('%s: cannot list directory: %r\n' % (dirname, msg)) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
67 return 1 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
68 names.sort() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
69 subdirs = [] |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
70 for name in names: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
71 if name in (os.curdir, os.pardir): continue |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
72 fullname = os.path.join(dirname, name) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
73 if os.path.islink(fullname): pass |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
74 elif os.path.isdir(fullname): |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
75 subdirs.append(fullname) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
76 elif ispython(name): |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
77 if fix(fullname): bad = 1 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
78 for fullname in subdirs: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
79 if recursedown(fullname): bad = 1 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
80 return bad |
| 553 | 81 |
| 82 def fix(filename): | |
|
32735
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
83 ## dbg('fix(%r)\n' % (dirname,)) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
84 try: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
85 f = open(filename, 'r') |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
86 except IOError, msg: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
87 err('%s: cannot open: %r\n' % (filename, msg)) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
88 return 1 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
89 head, tail = os.path.split(filename) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
90 tempname = os.path.join(head, '@' + tail) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
91 g = None |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
92 # If we find a match, we rewind the file and start over but |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
93 # now copy everything to a temp file. |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
94 lineno = 0 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
95 while 1: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
96 line = f.readline() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
97 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
|
98 lineno = lineno + 1 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
99 if g is None and '\0' in line: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
100 # Check for binary files |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
101 err(filename + ': contains null bytes; not fixed\n') |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
102 f.close() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
103 return 1 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
104 if lineno == 1 and g is None and line[:2] == '#!': |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
105 # Check for non-Python scripts |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
106 words = string.split(line[2:]) |
|
37021
4727f260f6f8
Remove regsub, reconvert, regex, regex_syntax and everything under lib-old.
Neal Norwitz <nnorwitz@gmail.com>
parents:
33456
diff
changeset
|
107 if words and re.search('[pP]ython', words[0]) < 0: |
|
32735
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
108 msg = filename + ': ' + words[0] |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
109 msg = msg + ' script; not fixed\n' |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
110 err(msg) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
111 f.close() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
112 return 1 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
113 while line[-2:] == '\\\n': |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
114 nextline = f.readline() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
115 if not nextline: break |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
116 line = line + nextline |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
117 lineno = lineno + 1 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
118 newline = fixline(line) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
119 if newline != line: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
120 if g is None: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
121 try: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
122 g = open(tempname, 'w') |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
123 except IOError, msg: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
124 f.close() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
125 err('%s: cannot create: %r\n' % (tempname, msg)) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
126 return 1 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
127 f.seek(0) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
128 lineno = 0 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
129 rep(filename + ':\n') |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
130 continue # restart from the beginning |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
131 rep(repr(lineno) + '\n') |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
132 rep('< ' + line) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
133 rep('> ' + newline) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
134 if g is not None: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
135 g.write(newline) |
| 553 | 136 |
|
32735
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
137 # End of file |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
138 f.close() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
139 if not g: return 0 # No changes |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
140 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
141 # Finishing touch -- move files |
| 553 | 142 |
|
32735
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
143 # First copy the file's mode to the temp file |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
144 try: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
145 statbuf = os.stat(filename) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
146 os.chmod(tempname, statbuf[ST_MODE] & 07777) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
147 except os.error, msg: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
148 err('%s: warning: chmod failed (%r)\n' % (tempname, msg)) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
149 # Then make a backup of the original file as filename~ |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
150 try: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
151 os.rename(filename, filename + '~') |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
152 except os.error, msg: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
153 err('%s: warning: backup failed (%r)\n' % (filename, msg)) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
154 # Now move the temp file to the original file |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
155 try: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
156 os.rename(tempname, filename) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
157 except os.error, msg: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
158 err('%s: rename failed (%r)\n' % (filename, msg)) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
159 return 1 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
160 # Return succes |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
161 return 0 |
| 553 | 162 |
|
555
0780b95a660e
Tokenizing version (see lib/tokenize.py).
Guido van Rossum <guido@python.org>
parents:
553
diff
changeset
|
163 |
|
0780b95a660e
Tokenizing version (see lib/tokenize.py).
Guido van Rossum <guido@python.org>
parents:
553
diff
changeset
|
164 from tokenize import tokenprog |
|
0780b95a660e
Tokenizing version (see lib/tokenize.py).
Guido van Rossum <guido@python.org>
parents:
553
diff
changeset
|
165 |
|
0780b95a660e
Tokenizing version (see lib/tokenize.py).
Guido van Rossum <guido@python.org>
parents:
553
diff
changeset
|
166 match = {'if':':', 'elif':':', 'while':':', 'return':'\n', \ |
|
32735
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
167 '(':')', '[':']', '{':'}', '`':'`'} |
| 553 | 168 |
| 169 def fixline(line): | |
|
32735
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
170 # Quick check for easy case |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
171 if '=' not in line: return line |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
172 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
173 i, n = 0, len(line) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
174 stack = [] |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
175 while i < n: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
176 j = tokenprog.match(line, i) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
177 if j < 0: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
178 # A bad token; forget about the rest of this line |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
179 print '(Syntax error:)' |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
180 print line, |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
181 return line |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
182 a, b = tokenprog.regs[3] # Location of the token proper |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
183 token = line[a:b] |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
184 i = i+j |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
185 if stack and token == stack[-1]: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
186 del stack[-1] |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
187 elif match.has_key(token): |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
188 stack.append(match[token]) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
189 elif token == '=' and stack: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
190 line = line[:a] + '==' + line[b:] |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
191 i, n = a + len('=='), len(line) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
192 elif token == '==' and not stack: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
193 print '(Warning: \'==\' at top level:)' |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
194 print line, |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
195 return line |
| 553 | 196 |
|
33456
487280cf71b9
Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents:
32735
diff
changeset
|
197 if __name__ == "__main__": |
|
487280cf71b9
Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents:
32735
diff
changeset
|
198 main() |
