#!/usr/bin/python
# cfgcl.py: cleans commented and blank lines out of config files
# Copyright(c) Darren Kirby 2004. Released under the Artistic Licence:
# http://www.opensource.org/licenses/artistic-license.php
import sys
from getopt import getopt, GetoptError
import os.path
import re
def showUsage():
print '''Usage: cfgcl [-h] [-tN] file [outfile]
Options:
'-h' Print this drivel
'-tN' Token. 'N' is a code for the comment delimiter. Current tokens are:
'1' for ';' ie: php.ini
'2' for '"' ie: vimrc
The default is the hashmark: '#'
'''
def readInfile(infile):
f = open(infile, "r")
text = f.readlines()
f.close()
return text
def writeOutfile(outfile, text, token):
c = re.compile('^[\s]*' + token + '[a-zA-Z0-9_]*')
b = re.compile('^$')
if os.path.exists(outfile) == 1:
os.unlink(outfile)
f = open(outfile, "a")
for line in text:
if not c.match(line):
if not b.match(line):
f.write(line)
sys.exit(0)
def main():
try:
opts, args = getopt(sys.argv[1:], "ht:")
except GetoptError:
print("Invalid option(s)")
showUsage()
sys.exit(22)
if os.path.exists(args[0]) == 0:
print "%s: File not found" % args[0]
sys.exit(2)
token = '#'
for opt, arg in opts:
if opt in ("-h"):
showUsage()
sys.exit(0)
if opt in ("-t"):
if arg == "1":
token = ';'
elif arg == "2":
token = '"'
text = readInfile(args[0])
if len(args) == 2:
outfile = args[1]
else:
outfile = (os.path.basename(args[0]) + ".clean")
writeOutfile(outfile, text, token)
if __name__ == '__main__':
main()
syntax highlighted by Code2HTML, v. 0.9.1