Guido van Rossum | ce85827 | 1998-09-14 15:43:38 +0000 | [diff] [blame] | 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 |
Guido van Rossum | ce85827 | 1998-09-14 15:43:38 +0000 | [diff] [blame] | 7 | import getopt |
| 8 | |
| 9 | def main(): |
| 10 | tabsize = 8 |
| 11 | try: |
| 12 | opts, args = getopt.getopt(sys.argv[1:], "t:") |
| 13 | if not args: |
| 14 | raise getopt.error, "At least one file argument required" |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 15 | except getopt.error as msg: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame^] | 16 | print(msg) |
| 17 | print("usage:", sys.argv[0], "[-t tabwidth] file ...") |
Guido van Rossum | ce85827 | 1998-09-14 15:43:38 +0000 | [diff] [blame] | 18 | return |
Guido van Rossum | 8fd0f14 | 1998-10-20 15:33:33 +0000 | [diff] [blame] | 19 | for optname, optvalue in opts: |
| 20 | if optname == '-t': |
| 21 | tabsize = int(optvalue) |
Guido van Rossum | ce85827 | 1998-09-14 15:43:38 +0000 | [diff] [blame] | 22 | |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 23 | for filename in args: |
| 24 | process(filename, tabsize) |
Guido van Rossum | ce85827 | 1998-09-14 15:43:38 +0000 | [diff] [blame] | 25 | |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 26 | def process(filename, tabsize): |
Guido van Rossum | ce85827 | 1998-09-14 15:43:38 +0000 | [diff] [blame] | 27 | try: |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 28 | f = open(filename) |
Guido van Rossum | ce85827 | 1998-09-14 15:43:38 +0000 | [diff] [blame] | 29 | text = f.read() |
| 30 | f.close() |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 31 | except IOError as msg: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame^] | 32 | print("%r: I/O error: %s" % (filename, msg)) |
Guido van Rossum | ce85827 | 1998-09-14 15:43:38 +0000 | [diff] [blame] | 33 | return |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 34 | newtext = text.expandtabs(tabsize) |
Guido van Rossum | ce85827 | 1998-09-14 15:43:38 +0000 | [diff] [blame] | 35 | if newtext == text: |
| 36 | return |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 37 | backup = filename + "~" |
Guido van Rossum | ce85827 | 1998-09-14 15:43:38 +0000 | [diff] [blame] | 38 | try: |
| 39 | os.unlink(backup) |
| 40 | except os.error: |
| 41 | pass |
| 42 | try: |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 43 | os.rename(filename, backup) |
Guido van Rossum | ce85827 | 1998-09-14 15:43:38 +0000 | [diff] [blame] | 44 | except os.error: |
| 45 | pass |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 46 | f = open(filename, "w") |
Guido van Rossum | ce85827 | 1998-09-14 15:43:38 +0000 | [diff] [blame] | 47 | f.write(newtext) |
| 48 | f.close() |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame^] | 49 | print(filename) |
Guido van Rossum | ce85827 | 1998-09-14 15:43:38 +0000 | [diff] [blame] | 50 | |
| 51 | if __name__ == '__main__': |
| 52 | main() |