7ee3f73d by Marcos Cano

recursive functionality added

1 parent ea43f423
#!/usr/bin/python
import string, re, sys
import getopt,os.path, json
from pprint import pprint
debug = False
ifile = ''
folder = ''
ofile = ifile
printit = False
vars=os.environ
recursive = False
class MyTemplate(string.Template):
......@@ -69,17 +73,20 @@ def engine(ifile,ofile,vars):
def main(argv):
try:
options, remainder = getopt.getopt(sys.argv[1:], 'o:f:i:v:dph', ['ofile=','o=',
options, remainder = getopt.getopt(sys.argv[1:], 'o:f:i:v:r:dph', ['ofile=','o=',
'ifile=','i=',
'vars=','v=',
'print','p',
'help','h'
'help','h',
'r='
])
except getopt.GetoptError:
usage()
sys.exit(2)
global debug, vars, ifile, ofile,printit
global debug, vars, ifile, ofile,printit, recursive
print options
for opt, arg in options:
if opt in ('-v','--vars','--v'):
......@@ -91,6 +98,14 @@ def main(argv):
print "using environment"
vars=os.environ
elif opt in ('-r','--r'):
if os.path.isdir(arg):
recursive= True
folder = arg
else:
print "folder provided [%s] does not exist" %arg
usage()
elif opt in ('-f','--ifile','--i','-i'):
if os.path.isfile(arg):
ifile=arg
......@@ -110,7 +125,7 @@ def main(argv):
if debug:
print 'OPTIONS :', options
print 'VARS :', pprint(vars)
print 'VARS :', pprint(str(vars))
print 'DEBUG :', debug
print 'OUTPUT :', ofile
print 'INPUT :', ifile
......@@ -118,8 +133,21 @@ def main(argv):
print 'REMAINING :', remainder
engine(ifile, ofile, vars )
if recursive :
print folder
for path, dirs, files in os.walk(folder):
for filename in files:
fullpath = os.path.join(path, filename)
if os.path.isfile(fullpath):
engine(fullpath, fullpath, vars)
else:
print "file %s does not exists"%fullpath
elif ifile != '':
engine(ifile, ofile, vars )
else:
print "nothing provided"
sys.exit(1)
......