Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/Other/tab2spc.py @ 1

Last change on this file since 1 was 1, checked in by landauf, 17 years ago
File size: 1.7 KB
Line 
1#! /usr/bin/env python
2
3import sys
4import os
5import getopt
6import string
7import re
8
9def 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
29def fnHelp():
30    helpstr = """
31This program transforms all the TAB characters in all
32the files with the given extension from the directory
33tree into the given number of spaces.
34
35Syntax:
36    tab2spc [num_spaces] [ext1] [ext2] [...]
37
38Extensions are given in the form:
39    .cpp .h .txt (no asterisk at the beginning)
40
41Copyright (c) 2002 by Adrian Cearnau (cearny@cearny.ro)
42Any use, commercial or not, is allowed
43"""
44    print helpstr
45
46if len(sys.argv) != 3:
47    fnHelp()
48else:
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"
Note: See TracBrowser for help on using the repository browser.