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