1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """cleanfuture [-d][-r][-v] path ... |
---|
4 | n/a | |
---|
5 | n/a | -d Dry run. Analyze, but don't make any changes to, files. |
---|
6 | n/a | -r Recurse. Search for all .py files in subdirectories too. |
---|
7 | n/a | -v Verbose. Print informative msgs. |
---|
8 | n/a | |
---|
9 | n/a | Search Python (.py) files for future statements, and remove the features |
---|
10 | n/a | from such statements that are already mandatory in the version of Python |
---|
11 | n/a | you're using. |
---|
12 | n/a | |
---|
13 | n/a | Pass one or more file and/or directory paths. When a directory path, all |
---|
14 | n/a | .py files within the directory will be examined, and, if the -r option is |
---|
15 | n/a | given, likewise recursively for subdirectories. |
---|
16 | n/a | |
---|
17 | n/a | Overwrites files in place, renaming the originals with a .bak extension. If |
---|
18 | n/a | cleanfuture finds nothing to change, the file is left alone. If cleanfuture |
---|
19 | n/a | does change a file, the changed file is a fixed-point (i.e., running |
---|
20 | n/a | cleanfuture on the resulting .py file won't change it again, at least not |
---|
21 | n/a | until you try it again with a later Python release). |
---|
22 | n/a | |
---|
23 | n/a | Limitations: You can do these things, but this tool won't help you then: |
---|
24 | n/a | |
---|
25 | n/a | + A future statement cannot be mixed with any other statement on the same |
---|
26 | n/a | physical line (separated by semicolon). |
---|
27 | n/a | |
---|
28 | n/a | + A future statement cannot contain an "as" clause. |
---|
29 | n/a | |
---|
30 | n/a | Example: Assuming you're using Python 2.2, if a file containing |
---|
31 | n/a | |
---|
32 | n/a | from __future__ import nested_scopes, generators |
---|
33 | n/a | |
---|
34 | n/a | is analyzed by cleanfuture, the line is rewritten to |
---|
35 | n/a | |
---|
36 | n/a | from __future__ import generators |
---|
37 | n/a | |
---|
38 | n/a | because nested_scopes is no longer optional in 2.2 but generators is. |
---|
39 | n/a | """ |
---|
40 | n/a | |
---|
41 | n/a | import __future__ |
---|
42 | n/a | import tokenize |
---|
43 | n/a | import os |
---|
44 | n/a | import sys |
---|
45 | n/a | |
---|
46 | n/a | dryrun = 0 |
---|
47 | n/a | recurse = 0 |
---|
48 | n/a | verbose = 0 |
---|
49 | n/a | |
---|
50 | n/a | def errprint(*args): |
---|
51 | n/a | strings = map(str, args) |
---|
52 | n/a | msg = ' '.join(strings) |
---|
53 | n/a | if msg[-1:] != '\n': |
---|
54 | n/a | msg += '\n' |
---|
55 | n/a | sys.stderr.write(msg) |
---|
56 | n/a | |
---|
57 | n/a | def main(): |
---|
58 | n/a | import getopt |
---|
59 | n/a | global verbose, recurse, dryrun |
---|
60 | n/a | try: |
---|
61 | n/a | opts, args = getopt.getopt(sys.argv[1:], "drv") |
---|
62 | n/a | except getopt.error as msg: |
---|
63 | n/a | errprint(msg) |
---|
64 | n/a | return |
---|
65 | n/a | for o, a in opts: |
---|
66 | n/a | if o == '-d': |
---|
67 | n/a | dryrun += 1 |
---|
68 | n/a | elif o == '-r': |
---|
69 | n/a | recurse += 1 |
---|
70 | n/a | elif o == '-v': |
---|
71 | n/a | verbose += 1 |
---|
72 | n/a | if not args: |
---|
73 | n/a | errprint("Usage:", __doc__) |
---|
74 | n/a | return |
---|
75 | n/a | for arg in args: |
---|
76 | n/a | check(arg) |
---|
77 | n/a | |
---|
78 | n/a | def check(file): |
---|
79 | n/a | if os.path.isdir(file) and not os.path.islink(file): |
---|
80 | n/a | if verbose: |
---|
81 | n/a | print("listing directory", file) |
---|
82 | n/a | names = os.listdir(file) |
---|
83 | n/a | for name in names: |
---|
84 | n/a | fullname = os.path.join(file, name) |
---|
85 | n/a | if ((recurse and os.path.isdir(fullname) and |
---|
86 | n/a | not os.path.islink(fullname)) |
---|
87 | n/a | or name.lower().endswith(".py")): |
---|
88 | n/a | check(fullname) |
---|
89 | n/a | return |
---|
90 | n/a | |
---|
91 | n/a | if verbose: |
---|
92 | n/a | print("checking", file, "...", end=' ') |
---|
93 | n/a | try: |
---|
94 | n/a | f = open(file) |
---|
95 | n/a | except IOError as msg: |
---|
96 | n/a | errprint("%r: I/O Error: %s" % (file, str(msg))) |
---|
97 | n/a | return |
---|
98 | n/a | |
---|
99 | n/a | ff = FutureFinder(f, file) |
---|
100 | n/a | changed = ff.run() |
---|
101 | n/a | if changed: |
---|
102 | n/a | ff.gettherest() |
---|
103 | n/a | f.close() |
---|
104 | n/a | if changed: |
---|
105 | n/a | if verbose: |
---|
106 | n/a | print("changed.") |
---|
107 | n/a | if dryrun: |
---|
108 | n/a | print("But this is a dry run, so leaving it alone.") |
---|
109 | n/a | for s, e, line in changed: |
---|
110 | n/a | print("%r lines %d-%d" % (file, s+1, e+1)) |
---|
111 | n/a | for i in range(s, e+1): |
---|
112 | n/a | print(ff.lines[i], end=' ') |
---|
113 | n/a | if line is None: |
---|
114 | n/a | print("-- deleted") |
---|
115 | n/a | else: |
---|
116 | n/a | print("-- change to:") |
---|
117 | n/a | print(line, end=' ') |
---|
118 | n/a | if not dryrun: |
---|
119 | n/a | bak = file + ".bak" |
---|
120 | n/a | if os.path.exists(bak): |
---|
121 | n/a | os.remove(bak) |
---|
122 | n/a | os.rename(file, bak) |
---|
123 | n/a | if verbose: |
---|
124 | n/a | print("renamed", file, "to", bak) |
---|
125 | n/a | g = open(file, "w") |
---|
126 | n/a | ff.write(g) |
---|
127 | n/a | g.close() |
---|
128 | n/a | if verbose: |
---|
129 | n/a | print("wrote new", file) |
---|
130 | n/a | else: |
---|
131 | n/a | if verbose: |
---|
132 | n/a | print("unchanged.") |
---|
133 | n/a | |
---|
134 | n/a | class FutureFinder: |
---|
135 | n/a | |
---|
136 | n/a | def __init__(self, f, fname): |
---|
137 | n/a | self.f = f |
---|
138 | n/a | self.fname = fname |
---|
139 | n/a | self.ateof = 0 |
---|
140 | n/a | self.lines = [] # raw file lines |
---|
141 | n/a | |
---|
142 | n/a | # List of (start_index, end_index, new_line) triples. |
---|
143 | n/a | self.changed = [] |
---|
144 | n/a | |
---|
145 | n/a | # Line-getter for tokenize. |
---|
146 | n/a | def getline(self): |
---|
147 | n/a | if self.ateof: |
---|
148 | n/a | return "" |
---|
149 | n/a | line = self.f.readline() |
---|
150 | n/a | if line == "": |
---|
151 | n/a | self.ateof = 1 |
---|
152 | n/a | else: |
---|
153 | n/a | self.lines.append(line) |
---|
154 | n/a | return line |
---|
155 | n/a | |
---|
156 | n/a | def run(self): |
---|
157 | n/a | STRING = tokenize.STRING |
---|
158 | n/a | NL = tokenize.NL |
---|
159 | n/a | NEWLINE = tokenize.NEWLINE |
---|
160 | n/a | COMMENT = tokenize.COMMENT |
---|
161 | n/a | NAME = tokenize.NAME |
---|
162 | n/a | OP = tokenize.OP |
---|
163 | n/a | |
---|
164 | n/a | changed = self.changed |
---|
165 | n/a | get = tokenize.generate_tokens(self.getline).__next__ |
---|
166 | n/a | type, token, (srow, scol), (erow, ecol), line = get() |
---|
167 | n/a | |
---|
168 | n/a | # Chew up initial comments and blank lines (if any). |
---|
169 | n/a | while type in (COMMENT, NL, NEWLINE): |
---|
170 | n/a | type, token, (srow, scol), (erow, ecol), line = get() |
---|
171 | n/a | |
---|
172 | n/a | # Chew up docstring (if any -- and it may be implicitly catenated!). |
---|
173 | n/a | while type is STRING: |
---|
174 | n/a | type, token, (srow, scol), (erow, ecol), line = get() |
---|
175 | n/a | |
---|
176 | n/a | # Analyze the future stmts. |
---|
177 | n/a | while 1: |
---|
178 | n/a | # Chew up comments and blank lines (if any). |
---|
179 | n/a | while type in (COMMENT, NL, NEWLINE): |
---|
180 | n/a | type, token, (srow, scol), (erow, ecol), line = get() |
---|
181 | n/a | |
---|
182 | n/a | if not (type is NAME and token == "from"): |
---|
183 | n/a | break |
---|
184 | n/a | startline = srow - 1 # tokenize is one-based |
---|
185 | n/a | type, token, (srow, scol), (erow, ecol), line = get() |
---|
186 | n/a | |
---|
187 | n/a | if not (type is NAME and token == "__future__"): |
---|
188 | n/a | break |
---|
189 | n/a | type, token, (srow, scol), (erow, ecol), line = get() |
---|
190 | n/a | |
---|
191 | n/a | if not (type is NAME and token == "import"): |
---|
192 | n/a | break |
---|
193 | n/a | type, token, (srow, scol), (erow, ecol), line = get() |
---|
194 | n/a | |
---|
195 | n/a | # Get the list of features. |
---|
196 | n/a | features = [] |
---|
197 | n/a | while type is NAME: |
---|
198 | n/a | features.append(token) |
---|
199 | n/a | type, token, (srow, scol), (erow, ecol), line = get() |
---|
200 | n/a | |
---|
201 | n/a | if not (type is OP and token == ','): |
---|
202 | n/a | break |
---|
203 | n/a | type, token, (srow, scol), (erow, ecol), line = get() |
---|
204 | n/a | |
---|
205 | n/a | # A trailing comment? |
---|
206 | n/a | comment = None |
---|
207 | n/a | if type is COMMENT: |
---|
208 | n/a | comment = token |
---|
209 | n/a | type, token, (srow, scol), (erow, ecol), line = get() |
---|
210 | n/a | |
---|
211 | n/a | if type is not NEWLINE: |
---|
212 | n/a | errprint("Skipping file %r; can't parse line %d:\n%s" % |
---|
213 | n/a | (self.fname, srow, line)) |
---|
214 | n/a | return [] |
---|
215 | n/a | |
---|
216 | n/a | endline = srow - 1 |
---|
217 | n/a | |
---|
218 | n/a | # Check for obsolete features. |
---|
219 | n/a | okfeatures = [] |
---|
220 | n/a | for f in features: |
---|
221 | n/a | object = getattr(__future__, f, None) |
---|
222 | n/a | if object is None: |
---|
223 | n/a | # A feature we don't know about yet -- leave it in. |
---|
224 | n/a | # They'll get a compile-time error when they compile |
---|
225 | n/a | # this program, but that's not our job to sort out. |
---|
226 | n/a | okfeatures.append(f) |
---|
227 | n/a | else: |
---|
228 | n/a | released = object.getMandatoryRelease() |
---|
229 | n/a | if released is None or released <= sys.version_info: |
---|
230 | n/a | # Withdrawn or obsolete. |
---|
231 | n/a | pass |
---|
232 | n/a | else: |
---|
233 | n/a | okfeatures.append(f) |
---|
234 | n/a | |
---|
235 | n/a | # Rewrite the line if at least one future-feature is obsolete. |
---|
236 | n/a | if len(okfeatures) < len(features): |
---|
237 | n/a | if len(okfeatures) == 0: |
---|
238 | n/a | line = None |
---|
239 | n/a | else: |
---|
240 | n/a | line = "from __future__ import " |
---|
241 | n/a | line += ', '.join(okfeatures) |
---|
242 | n/a | if comment is not None: |
---|
243 | n/a | line += ' ' + comment |
---|
244 | n/a | line += '\n' |
---|
245 | n/a | changed.append((startline, endline, line)) |
---|
246 | n/a | |
---|
247 | n/a | # Loop back for more future statements. |
---|
248 | n/a | |
---|
249 | n/a | return changed |
---|
250 | n/a | |
---|
251 | n/a | def gettherest(self): |
---|
252 | n/a | if self.ateof: |
---|
253 | n/a | self.therest = '' |
---|
254 | n/a | else: |
---|
255 | n/a | self.therest = self.f.read() |
---|
256 | n/a | |
---|
257 | n/a | def write(self, f): |
---|
258 | n/a | changed = self.changed |
---|
259 | n/a | assert changed |
---|
260 | n/a | # Prevent calling this again. |
---|
261 | n/a | self.changed = [] |
---|
262 | n/a | # Apply changes in reverse order. |
---|
263 | n/a | changed.reverse() |
---|
264 | n/a | for s, e, line in changed: |
---|
265 | n/a | if line is None: |
---|
266 | n/a | # pure deletion |
---|
267 | n/a | del self.lines[s:e+1] |
---|
268 | n/a | else: |
---|
269 | n/a | self.lines[s:e+1] = [line] |
---|
270 | n/a | f.writelines(self.lines) |
---|
271 | n/a | # Copy over the remainder of the file. |
---|
272 | n/a | if self.therest: |
---|
273 | n/a | f.write(self.therest) |
---|
274 | n/a | |
---|
275 | n/a | if __name__ == '__main__': |
---|
276 | n/a | main() |
---|