Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce85827

Browse files
committed
Utility to untabify stubber results.
1 parent dcd038f commit ce85827

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

Tools/scripts/untabify.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#! /usr/bin/env python
2+
3+
"Replace tabs with spaces in argument files. Print names of changed files."
4+
5+
import os
6+
import sys
7+
import string
8+
import getopt
9+
10+
def main():
11+
tabsize = 8
12+
try:
13+
opts, args = getopt.getopt(sys.argv[1:], "t:")
14+
if not args:
15+
raise getopt.error, "At least one file argument required"
16+
except getopt.error, msg:
17+
print msg
18+
print "usage:", sys.argv[0], "file ..."
19+
return
20+
21+
for file in args:
22+
process(file, tabsize)
23+
24+
def process(file, tabsize):
25+
try:
26+
f = open(file)
27+
text = f.read()
28+
f.close()
29+
except IOError, msg:
30+
print "%s: I/O error: %s" % (`file`, str(msg))
31+
return
32+
newtext = string.expandtabs(text, tabsize)
33+
if newtext == text:
34+
return
35+
backup = file + "~"
36+
try:
37+
os.unlink(backup)
38+
except os.error:
39+
pass
40+
try:
41+
os.rename(file, backup)
42+
except os.error:
43+
pass
44+
f = open(file, "w")
45+
f.write(newtext)
46+
f.close()
47+
print file
48+
49+
if __name__ == '__main__':
50+
main()

0 commit comments

Comments
 (0)