[690] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | import os |
---|
| 5 | import getopt |
---|
| 6 | import string |
---|
| 7 | import re |
---|
| 8 | |
---|
| 9 | def fnParse( arg, dirname, files ): |
---|
| 10 | for file in files: |
---|
| 11 | for ext in arg: |
---|
| 12 | if file.rfind( ext, len(file)-len(ext), len(file) ) != -1 and not os.path.isdir( dirname + "/" + file ): |
---|
| 13 | fo = open( dirname + "/" + file, "rb" ) |
---|
| 14 | lines = fo.read() |
---|
| 15 | if not '\0' in lines: |
---|
| 16 | newlines = re.sub( "\t", globals()['spaces'], lines ) |
---|
| 17 | if newlines != lines: |
---|
| 18 | backup = open( dirname + "/" + file + "~", "wb" ) |
---|
| 19 | backup.write( lines ) |
---|
| 20 | backup.close() |
---|
| 21 | |
---|
| 22 | fo.close() |
---|
| 23 | fo = open( dirname + "/" + file, "wb" ) |
---|
| 24 | fo.write(newlines) |
---|
| 25 | |
---|
| 26 | print "File " + dirname + "/" + file + " was changed." |
---|
| 27 | fo.close() |
---|
| 28 | |
---|
| 29 | def fnHelp(): |
---|
| 30 | helpstr = """ |
---|
| 31 | This program transforms all the TAB characters in all |
---|
| 32 | the files with the given extension from the directory |
---|
| 33 | tree into the given number of spaces. |
---|
| 34 | |
---|
| 35 | Syntax: |
---|
| 36 | tab2spc [num_spaces] [ext1] [ext2] [...] |
---|
| 37 | |
---|
| 38 | Extensions are given in the form: |
---|
| 39 | .cpp .h .txt (no asterisk at the beginning) |
---|
| 40 | |
---|
| 41 | Copyright (c) 2002 by Adrian Cearnau (cearny@cearny.ro) |
---|
| 42 | Any use, commercial or not, is allowed |
---|
| 43 | """ |
---|
| 44 | print helpstr |
---|
| 45 | |
---|
| 46 | if len(sys.argv) != 3: |
---|
| 47 | fnHelp() |
---|
| 48 | else: |
---|
| 49 | spaces = "" |
---|
| 50 | for i in range( 0, int(sys.argv[1]) ): |
---|
| 51 | spaces = spaces + ' ' |
---|
| 52 | exts = sys.argv[2].split() |
---|
| 53 | currdir = os.getcwd() |
---|
| 54 | os.path.walk( currdir, fnParse, exts ) |
---|
| 55 | #print "\nTotally", num_files, "files were patched\n" |
---|
| 56 | print "done" |
---|