1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
#!/usr/bin/python
#!/neo/opt/bin/python
import sys, string, os, getopt, signal, time
sys.path.append("../python")
import neo_cgi, neo_util
import cStringIO
class ClearSilverChecker:
def __init__ (self):
self.context = ""
self.data = ""
self.at = 0
self.cmd = ""
self.tokens = []
def error(self, s):
lineno = self.lineno(self.data, self.at)
print "-E- [%s:%d] %s" % (self.context, lineno, s)
if self.cmd:
print " Command is %s" % self.cmd
if self.tokens:
print " Tokens: %s" % repr(self.tokens)
def warn(self, s):
lineno = self.lineno(self.data, self.at)
print "-W- [%s:%d] %s" % (self.context, lineno, s)
if self.cmd:
print " Command is %s" % self.cmd
if self.tokens:
print " Tokens: %s" % repr(self.tokens)
def check_file(self, filename):
print "Checking file %s" % filename
self.context = filename
try:
self.run_neo_cgi(filename)
except neo_util.ParseError, reason:
print "-E- %s" % str(reason)
self.data = open(filename, "r").read()
self.parse()
def run_neo_cgi(self, filename):
stdin = cStringIO.StringIO("")
stdout = cStringIO.StringIO()
neo_cgi.cgiWrap(stdin, stdout, {})
neo_cgi.IgnoreEmptyFormVars(1)
ncgi = neo_cgi.CGI()
path = os.path.dirname(filename)
ncgi.hdf.setValue("hdf.loadpaths.path", path)
ncgi.display(filename)
return
def lineno(self, data, i):
return len(string.split(data[:i], '\n'))
def parse(self):
self.at = 0
x = string.find(self.data[self.at:], '<?cs ')
while x >= 0:
self.at = x + self.at
ce = string.find(self.data[self.at:], '?>')
if ce == -1:
self.error("Missing ?> in expression")
else:
ce = ce + self.at
self.check_command(ce)
# reset these class variables
self.cmd = ""
self.tokens = []
self.at = self.at + 1
x = string.find(self.data[self.at:], '<?cs ')
def check_command(self, end):
cmd = self.data[self.at+5:end]
self.cmd = cmd
if cmd[0] == '/':
# handle end command
cmd = cmd[1:]
self.command_end(cmd)
return
pound = string.find(cmd, '#')
colon = string.find(cmd, ':')
bang = string.find(cmd, '!')
if colon == -1 and bang == -1:
if pound != -1:
#print "Found comment: %s" % cmd
pass
else:
self.command_begin(string.strip(cmd), "")
elif pound != -1 and bang != -1 and pound < bang:
# comment
#print "Found comment: %s" % cmd
pass
elif pound != -1 and colon != -1 and pound < colon:
# comment
#print "Found comment: %s" % cmd
pass
elif bang == -1:
arg = cmd[colon+1:]
cmd = cmd[:colon]
self.command_begin(cmd, arg)
elif colon == -1:
arg = cmd[bang+1:]
cmd = cmd[:bang]
self.command_begin(cmd, arg)
def command_end(self, cmd):
pass
def command_begin(self, cmd, args):
#print "%s -> %s" % (cmd, args)
if cmd == "alt":
self.check_expression(args)
elif cmd == "if":
self.check_expression(args)
elif cmd == "elif":
self.check_expression(args)
elif cmd == "else":
pass
elif cmd == "include":
self.check_expression(args)
elif cmd == "linclude":
self.check_expression(args)
elif cmd == "name":
self.check_expression(args)
elif cmd == "var":
self.check_expression(args)
elif cmd == "evar":
self.check_expression(args)
elif cmd == "lvar":
self.check_expression(args)
elif cmd == "def":
macro, args = self.split_macro(args)
if macro: self.check_expression(macro, lvalue=1)
if args:self.check_expression(args)
elif cmd == "call":
macro, args = self.split_macro(args)
if macro: self.check_expression(macro, lvalue=1)
if args:self.check_expression(args)
elif cmd == "with":
varname, args = self.split_equals(args)
if varname: self.check_expression(varname, lvalue=1)
if args: self.check_expression(args)
elif cmd == "each":
varname, args = self.split_equals(args)
if varname: self.check_expression(varname, lvalue=1)
if args: self.check_expression(args)
elif cmd == "loop":
varname, args = self.split_equals(args)
if varname: self.check_expression(varname, lvalue=1)
if args: self.check_expression(args)
elif cmd == "set":
varname, args = self.split_equals(args)
if varname: self.check_expression(varname, lvalue=1)
if args: self.check_expression(args)
else:
self.error("Unrecognized command %s" % cmd)
def split_equals(self, args):
x = string.find(args, '=')
if x == -1:
self.error("Missing equals")
return None, None
else:
return args[:x], args[x+1:]
def split_macro(self, args):
b = string.find(args, '(')
e = string.rfind(args, ')')
if b == -1:
self.error("Missing opening parenthesis")
return None, None
if e == -1:
self.error("Missing closing parenthesis")
return None, None
macro_name = args[:b]
args = args[b+1:e]
return macro_name, args
def check_expression(self, expr, lvalue=0):
tokens = self.tokenize_expression(expr)
#print repr(tokens)
if len(tokens) == 0:
self.error("Empty Expression")
_OP = 1
_VAR = 2
_VARN = 3
_STR = 4
_NUM = 5
_TOKEN_SEP = "\"?<>=!#-+|&,)*/%[]( \t\r\n"
def tokenize_expression(self, expr):
self.tokens = []
while expr:
#print "expr: '%s'" % expr
expr = string.lstrip(expr)
len_expr = len(expr)
if len_expr == 0: break
if expr[:2] in ["<=", ">=", "==", "!=", "||", "&&"]:
self.tokens.append((ClearSilverChecker._OP, expr[:2]))
expr = expr[2:]
continue
elif expr[0] in ["!", "?", "<", ">", "+", "-", "*", "/", "%", "(", ")", "[", "]", ".", ',']:
self.tokens.append((ClearSilverChecker._OP, expr[0]))
expr = expr[1:]
continue
elif expr[0] in ["#", "$"]:
x = 1
if expr[1] in ['+', '-']: x=2
while len_expr > x and expr[x] not in ClearSilverChecker._TOKEN_SEP: x=x+1
if x == 0:
self.error("[1] Zero length token, unexpected character %s" % expr[0])
x = 1
else:
token = expr[1:x]
if expr[0] == "#":
try:
n = int(token)
t_type = ClearSilverChecker._NUM
except ValueError:
t_type = ClearSilverChecker._VARN
else:
t_type = ClearSilverChecker._VAR
self.tokens.append((t_type, token))
expr = expr[x:]
continue
elif expr[0] in ['"', "'"]:
x = string.find(expr[1:], expr[0])
if x == -1:
self.error("Missing end of string %s " % expr)
break
else:
x = x + 1
self.tokens.append((ClearSilverChecker._STR, expr[1:x]))
expr = expr[x+2:]
continue
else:
x = 0
while len_expr > x and expr[x] not in ClearSilverChecker._TOKEN_SEP: x=x+1
if x == 0:
self.error("[2] Zero length token, unexpected character %s" % expr[0])
x = 1
else:
token = expr[:x]
try:
n = int(token)
t_type = ClearSilverChecker._NUM
self.warn("This behavior changed in version 0.9: previously this was a variable name, now its a number: %s" % token)
except ValueError:
t_type = ClearSilverChecker._VAR
self.tokens.append((t_type, token))
expr = expr[x:]
continue
return self.tokens
# For version 0.9, we changed two things, we should check for them
# both
# - an all numeric expression element is now considered a number and
# not an HDF variable name
# - we now use boolean evaluation in places that used to use either a
# special case or a numeric evaluation
def usage(argv0):
print "%s: usage info!!" % argv0
def main(argv):
alist, args = getopt.getopt(argv[1:], "", ["help"])
for (field, val) in alist:
if field == "--help":
usage(argv[0])
sys.exit(-1)
for file in args:
ClearSilverChecker().check_file(file)
if __name__ == "__main__":
main(sys.argv)
|