recursive functionality added
Showing
1 changed file
with
34 additions
and
6 deletions
| 1 | #!/usr/bin/python | 1 | #!/usr/bin/python | 
| 2 | import string, re, sys | 2 | import string, re, sys | 
| 3 | import getopt,os.path, json | 3 | import getopt,os.path, json | 
| 4 | from pprint import pprint | ||
| 5 | |||
| 4 | 6 | ||
| 5 | 7 | ||
| 6 | debug = False | 8 | debug = False | 
| 7 | ifile = '' | 9 | ifile = '' | 
| 10 | folder = '' | ||
| 8 | ofile = ifile | 11 | ofile = ifile | 
| 9 | printit = False | 12 | printit = False | 
| 10 | vars=os.environ | 13 | vars=os.environ | 
| 14 | recursive = False | ||
| 11 | 15 | ||
| 12 | 16 | ||
| 13 | class MyTemplate(string.Template): | 17 | class MyTemplate(string.Template): | 
| ... | @@ -69,17 +73,20 @@ def engine(ifile,ofile,vars): | ... | @@ -69,17 +73,20 @@ def engine(ifile,ofile,vars): | 
| 69 | 73 | ||
| 70 | def main(argv): | 74 | def main(argv): | 
| 71 | try: | 75 | try: | 
| 72 | options, remainder = getopt.getopt(sys.argv[1:], 'o:f:i:v:dph', ['ofile=','o=', | 76 | options, remainder = getopt.getopt(sys.argv[1:], 'o:f:i:v:r:dph', ['ofile=','o=', | 
| 73 | 'ifile=','i=', | 77 | 'ifile=','i=', | 
| 74 | 'vars=','v=', | 78 | 'vars=','v=', | 
| 75 | 'print','p', | 79 | 'print','p', | 
| 76 | 'help','h' | 80 | 'help','h', | 
| 81 | 'r=' | ||
| 77 | ]) | 82 | ]) | 
| 78 | except getopt.GetoptError: | 83 | except getopt.GetoptError: | 
| 79 | usage() | 84 | usage() | 
| 80 | sys.exit(2) | 85 | sys.exit(2) | 
| 81 | 86 | ||
| 82 | global debug, vars, ifile, ofile,printit | 87 | global debug, vars, ifile, ofile,printit, recursive | 
| 88 | print options | ||
| 89 | |||
| 83 | 90 | ||
| 84 | for opt, arg in options: | 91 | for opt, arg in options: | 
| 85 | if opt in ('-v','--vars','--v'): | 92 | if opt in ('-v','--vars','--v'): | 
| ... | @@ -91,6 +98,14 @@ def main(argv): | ... | @@ -91,6 +98,14 @@ def main(argv): | 
| 91 | print "using environment" | 98 | print "using environment" | 
| 92 | vars=os.environ | 99 | vars=os.environ | 
| 93 | 100 | ||
| 101 | elif opt in ('-r','--r'): | ||
| 102 | if os.path.isdir(arg): | ||
| 103 | recursive= True | ||
| 104 | folder = arg | ||
| 105 | else: | ||
| 106 | print "folder provided [%s] does not exist" %arg | ||
| 107 | usage() | ||
| 108 | |||
| 94 | elif opt in ('-f','--ifile','--i','-i'): | 109 | elif opt in ('-f','--ifile','--i','-i'): | 
| 95 | if os.path.isfile(arg): | 110 | if os.path.isfile(arg): | 
| 96 | ifile=arg | 111 | ifile=arg | 
| ... | @@ -110,7 +125,7 @@ def main(argv): | ... | @@ -110,7 +125,7 @@ def main(argv): | 
| 110 | 125 | ||
| 111 | if debug: | 126 | if debug: | 
| 112 | print 'OPTIONS :', options | 127 | print 'OPTIONS :', options | 
| 113 | print 'VARS :', pprint(vars) | 128 | print 'VARS :', pprint(str(vars)) | 
| 114 | print 'DEBUG :', debug | 129 | print 'DEBUG :', debug | 
| 115 | print 'OUTPUT :', ofile | 130 | print 'OUTPUT :', ofile | 
| 116 | print 'INPUT :', ifile | 131 | print 'INPUT :', ifile | 
| ... | @@ -118,8 +133,21 @@ def main(argv): | ... | @@ -118,8 +133,21 @@ def main(argv): | 
| 118 | print 'REMAINING :', remainder | 133 | print 'REMAINING :', remainder | 
| 119 | 134 | ||
| 120 | 135 | ||
| 121 | 136 | if recursive : | |
| 122 | engine(ifile, ofile, vars ) | 137 | print folder | 
| 138 | for path, dirs, files in os.walk(folder): | ||
| 139 | for filename in files: | ||
| 140 | fullpath = os.path.join(path, filename) | ||
| 141 | if os.path.isfile(fullpath): | ||
| 142 | engine(fullpath, fullpath, vars) | ||
| 143 | else: | ||
| 144 | print "file %s does not exists"%fullpath | ||
| 145 | |||
| 146 | elif ifile != '': | ||
| 147 | engine(ifile, ofile, vars ) | ||
| 148 | else: | ||
| 149 | print "nothing provided" | ||
| 150 | sys.exit(1) | ||
| 123 | 151 | ||
| 124 | 152 | ||
| 125 | 153 | ... | ... | 
- 
Please register or sign in to post a comment
