annotate Tools/scripts/untabify.py @ 83447:678564fc0962 3.3

#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 19 Apr 2013 05:45:27 +0300
parents 3c9232a684ae
children 6cfe2982de42
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59522
fcfe324663a6 convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents: 42715
diff changeset
1 #! /usr/bin/env python3
9098
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
66565
2f6d0f361ab9 Issue #9598: untabify.py will now respect encoding cookie in the files it processes
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65570
diff changeset
8 import tokenize
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 def main():
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 tabsize = 8
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 try:
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 opts, args = getopt.getopt(sys.argv[1:], "t:")
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 if not args:
42715
e4ade16a694f Convert raise statements in Tools/.
Collin Winter <collinw@gmail.com>
parents: 42427
diff changeset
15 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
16 except getopt.error as msg:
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 40587
diff changeset
17 print(msg)
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 40587
diff changeset
18 print("usage:", sys.argv[0], "[-t tabwidth] file ...")
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 return
9412
0379eb7be323 Detlef Lannert lannert@lannert.rz.uni-duesseldorf.de notices:
Guido van Rossum <guido@python.org>
parents: 9098
diff changeset
20 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
21 if optname == '-t':
0379eb7be323 Detlef Lannert lannert@lannert.rz.uni-duesseldorf.de notices:
Guido van Rossum <guido@python.org>
parents: 9098
diff changeset
22 tabsize = int(optvalue)
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
29107
93c92f20b6bf Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
24 for filename in args:
93c92f20b6bf Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
25 process(filename, tabsize)
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
63872
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
27
66473
05ee6f8c0260 Merged revisions 85728,85731,85735,85766-85771,85773,85777 via svnmerge from
Georg Brandl <georg@python.org>
parents: 42715
diff changeset
28 def process(filename, tabsize, verbose=True):
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 try:
66565
2f6d0f361ab9 Issue #9598: untabify.py will now respect encoding cookie in the files it processes
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65570
diff changeset
30 with tokenize.open(filename) as f:
63872
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
31 text = f.read()
66565
2f6d0f361ab9 Issue #9598: untabify.py will now respect encoding cookie in the files it processes
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65570
diff changeset
32 encoding = f.encoding
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 31515
diff changeset
33 except IOError as msg:
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 40587
diff changeset
34 print("%r: I/O error: %s" % (filename, msg))
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 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
36 newtext = text.expandtabs(tabsize)
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 if newtext == text:
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 return
29107
93c92f20b6bf Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
39 backup = filename + "~"
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 try:
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 os.unlink(backup)
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 except os.error:
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 pass
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 try:
29107
93c92f20b6bf Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
45 os.rename(filename, backup)
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 except os.error:
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 pass
66565
2f6d0f361ab9 Issue #9598: untabify.py will now respect encoding cookie in the files it processes
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65570
diff changeset
48 with open(filename, "w", encoding=encoding) as f:
63872
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
49 f.write(newtext)
66473
05ee6f8c0260 Merged revisions 85728,85731,85735,85766-85771,85773,85777 via svnmerge from
Georg Brandl <georg@python.org>
parents: 42715
diff changeset
50 if verbose:
05ee6f8c0260 Merged revisions 85728,85731,85735,85766-85771,85773,85777 via svnmerge from
Georg Brandl <georg@python.org>
parents: 42715
diff changeset
51 print(filename)
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
63872
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
53
9098
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 if __name__ == '__main__':
b826e50e9573 Utility to untabify stubber results.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 main()