annotate Tools/scripts/untabify.py @ 51810:c8d06dfd4c39 3.0

Block all blocking commits on the py3k branch.
author Georg Brandl <georg@python.org>
date Mon, 23 Feb 2009 10:42:25 +0000
parents e4ade16a694f
children 05ee6f8c0260 fcfe324663a6
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:
42715
e4ade16a694f Convert raise statements in Tools/.
Collin Winter <collinw@gmail.com>
parents: 42427
diff changeset
14 raise getopt.error("At least one file argument required")
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 31515
diff changeset
15 except getopt.error as msg:
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 40587
diff changeset
16 print(msg)
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 40587
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()
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 31515
diff changeset
31 except IOError as msg:
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 40587
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()
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 40587
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()