annotate Tools/scripts/untabify.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 71a294bd7950
children 2955f337969e 6b18a095c2e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 #! /usr/bin/env python
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 "Replace tabs with spaces in argument files. Print names of changed files."
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 import os
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import sys
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 import getopt
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 def main():
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 tabsize = 8
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 try:
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 opts, args = getopt.getopt(sys.argv[1:], "t:")
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 if not args:
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 raise getopt.error, "At least one file argument required"
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 except getopt.error, msg:
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 print msg
9412
0379eb7be323 Detlef Lannert lannert@lannert.rz.uni-duesseldorf.de notices:
Guido van Rossum <guido@python.org>
parents: 9098
diff changeset
17 print "usage:", sys.argv[0], "[-t tabwidth] file ..."
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 return
9412
0379eb7be323 Detlef Lannert lannert@lannert.rz.uni-duesseldorf.de notices:
Guido van Rossum <guido@python.org>
parents: 9098
diff changeset
19 for optname, optvalue in opts:
0379eb7be323 Detlef Lannert lannert@lannert.rz.uni-duesseldorf.de notices:
Guido van Rossum <guido@python.org>
parents: 9098
diff changeset
20 if optname == '-t':
0379eb7be323 Detlef Lannert lannert@lannert.rz.uni-duesseldorf.de notices:
Guido van Rossum <guido@python.org>
parents: 9098
diff changeset
21 tabsize = int(optvalue)
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
29107
93c92f20b6bf Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
23 for filename in args:
93c92f20b6bf Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
24 process(filename, tabsize)
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
29107
93c92f20b6bf Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
26 def process(filename, tabsize):
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 try:
29107
93c92f20b6bf Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
28 f = open(filename)
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 text = f.read()
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 f.close()
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 except IOError, msg:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29107
diff changeset
32 print "%r: I/O error: %s" % (filename, msg)
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 return
25219
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 9412
diff changeset
34 newtext = text.expandtabs(tabsize)
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 if newtext == text:
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 return
29107
93c92f20b6bf Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
37 backup = filename + "~"
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 try:
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 os.unlink(backup)
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 except os.error:
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 pass
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 try:
29107
93c92f20b6bf Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
43 os.rename(filename, backup)
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 except os.error:
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 pass
29107
93c92f20b6bf Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
46 f = open(filename, "w")
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 f.write(newtext)
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 f.close()
29107
93c92f20b6bf Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
49 print filename
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 if __name__ == '__main__':
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 main()