2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
10 # | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # tails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
27 # Released to the public domain, by Tim Peters, 03 October 2000.
28 """reindent [-d][-r][-v] [ path ... ]
30 -d (--dryrun) Dry run. Analyze, but don't make any changes to, files.
31 -r (--recurse) Recurse. Search for all .py files in subdirectories too.
32 -n (--nobackup) No backup. Does not make a ".bak" file before reindenting.
33 -v (--verbose) Verbose. Print informative msgs; else no output.
34 -h (--help) Help. Print this usage information and exit.
36 Change Python (.py) files to use 4-space indents and no hard tab characters.
37 Also trim excess spaces and tabs from ends of lines, and remove empty lines
38 at the end of files. Also ensure the last line ends with a newline.
40 If no paths are given on the command line, reindent operates as a filter,
41 reading a single source file from standard input and writing the transformed
42 source to standard output. In this case, the -d, -r and -v flags are
45 You can pass one or more file and/or directory paths. When a directory
46 path, all .py files within the directory will be examined, and, if the -r
47 option is given, likewise recursively for subdirectories.
49 If output is not to standard output, reindent overwrites files in place,
50 renaming the originals with a .bak extension. If it finds nothing to
51 change, the file is left alone. If reindent does change a file, the changed
52 file is a fixed-point for future runs (i.e., running reindent on the
53 resulting .py file won't change it again).
55 The hard part of reindenting is figuring out what to do with comment
56 lines. So long as the input files get a clean bill of health from
57 tabnanny.py, reindent should do a good job.
59 The backup file is a copy of the one that is being reindented. The ".bak"
60 file is generated with shutil.copy(), but some corner cases regarding
61 user/group and permissions could leave the backup file more readable that
62 you'd prefer. You can always use the --nobackup option to prevent this.
79 print >> sys
.stderr
, msg
80 print >> sys
.stderr
, __doc__
86 sys
.stderr
.write(sep
+ str(arg
))
88 sys
.stderr
.write("\n")
93 global verbose
, recurse
, dryrun
, makebackup
95 opts
, args
= getopt
.getopt(sys
.argv
[1:], "drnvh",
96 ["dryrun", "recurse", "nobackup", "verbose", "help"])
97 except getopt
.error
as msg
:
101 if o
in ('-d', '--dryrun'):
103 elif o
in ('-r', '--recurse'):
105 elif o
in ('-n', '--nobackup'):
107 elif o
in ('-v', '--verbose'):
109 elif o
in ('-h', '--help'):
113 r
= Reindenter(sys
.stdin
)
122 if os
.path
.isdir(file) and not os
.path
.islink(file):
124 print "listing directory", file
125 names
= os
.listdir(file)
127 fullname
= os
.path
.join(file, name
)
128 if ((recurse
and os
.path
.isdir(fullname
) and not os
.path
.islink(fullname
) and
129 not os
.path
.split(fullname
)[1].startswith(".")) or name
.lower().endswith(".py")):
134 print "checking", file, "...",
137 except IOError as msg
:
138 errprint("%s: I/O Error: %s" % (file, str(msg
)))
147 print "But this is a dry run, so leaving it alone."
151 shutil
.copyfile(file, bak
)
153 print "backed up", file, "to", bak
158 print "wrote new", file
166 def _rstrip(line
, JUNK
='\n \t'):
167 """Return line stripped of trailing spaces, tabs, newlines.
169 Note that line.rstrip() instead also strips sundry control characters,
170 but at least one known Emacs user expects to keep junk like that, not
171 mentioning Barry by name or anything <wink>.
175 while i
> 0 and line
[i
- 1] in JUNK
:
181 def __init__(self
, f
):
182 self
.find_stmt
= 1 # next token begins a fresh stmt?
183 self
.level
= 0 # current indent level
186 self
.raw
= f
.readlines()
188 # File lines, rstripped & tab-expanded. Dummy at start is so
189 # that we can use tokenize's 1-based line numbering easily.
190 # Note that a line is all-blank iff it's "\n".
191 self
.lines
= [_rstrip(line
).expandtabs() + "\n" for line
in self
.raw
]
192 self
.lines
.insert(0, None)
193 self
.index
= 1 # index into self.lines of next line
195 # List of (lineno, indentlevel) pairs, one for each stmt and
196 # comment line. indentlevel is -1 for comment lines, as a
197 # signal that tokenize doesn't know what to do about them;
198 # indeed, they're our headache!
202 tokenize
.tokenize(self
.getline
, self
.tokeneater
)
203 # Remove trailing empty lines.
205 while lines
and lines
[-1] == "\n":
209 stats
.append((len(lines
), 0))
210 # Map count of leading spaces to # we want.
212 # Program after transformation.
213 after
= self
.after
= []
214 # Copy over initial empty lines -- there's nothing to do until
215 # we see a line with *something* on it.
217 after
.extend(lines
[1:i
])
218 for i
in range(len(stats
) - 1):
219 thisstmt
, thislevel
= stats
[i
]
220 nextstmt
= stats
[i
+ 1][0]
221 have
= getlspace(lines
[thisstmt
])
226 # An indented comment line. If we saw the same
227 # indentation before, reuse what it most recently
229 want
= have2want
.get(have
, -1)
231 # Then it probably belongs to the next real stmt.
232 for j
in xrange(i
+ 1, len(stats
) - 1):
233 jline
, jlevel
= stats
[j
]
235 if have
== getlspace(lines
[jline
]):
238 if want
< 0: # Maybe it's a hanging
239 # comment like this one,
240 # in which case we should shift it like its base
242 for j
in xrange(i
- 1, -1, -1):
243 jline
, jlevel
= stats
[j
]
245 want
= have
+ getlspace(after
[jline
-1]) - \
246 getlspace(lines
[jline
])
249 # Still no luck -- leave it alone.
254 have2want
[have
] = want
256 if diff
== 0 or have
== 0:
257 after
.extend(lines
[thisstmt
:nextstmt
])
259 for line
in lines
[thisstmt
:nextstmt
]:
264 after
.append(" " * diff
+ line
)
266 remove
= min(getlspace(line
), -diff
)
267 after
.append(line
[remove
:])
268 return self
.raw
!= self
.after
271 f
.writelines(self
.after
)
273 # Line-getter for tokenize.
275 if self
.index
>= len(self
.lines
):
278 line
= self
.lines
[self
.index
]
282 # Line-eater for tokenize.
289 INDENT
=tokenize
.INDENT
,
290 DEDENT
=tokenize
.DEDENT
,
291 NEWLINE
=tokenize
.NEWLINE
,
292 COMMENT
=tokenize
.COMMENT
,
295 sline
, _scol
= row_col
297 # A program statement, or ENDMARKER, will eventually follow,
298 # after some (possibly empty) run of tokens of the form
299 # (NL | COMMENT)* (INDENT | DEDENT+)?
310 elif type == COMMENT
:
312 self
.stats
.append((sline
, -1))
313 # but we're still looking for a new stmt, so leave
320 # This is the first "real token" following a NEWLINE, so it
321 # must be the first token of the next program statement, or an
324 if line
: # not endmarker
325 self
.stats
.append((sline
, self
.level
))
328 # Count number of leading blanks.
331 while i
< n
and line
[i
] == " ":
336 if __name__
== '__main__':