Improving text messages.
[vutg.git] / src / vut_checker.py
blob8aebf36457834c0c138ccb4e8a83b2924729e4a8
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 """
4 =======================================
5 module_name: vut_checker
6 ---------------------------------------
7 Author: Rodrigo Peixoto
8 Data: 10/02/2008
9 ---------------------------------------
10 Description:
11 - This moludes checks the integrity of
12 the vut file, based in the basics
13 rules.
14 =======================================
15 License: GPL
16 ======================================================================
18 This program is free software: you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation, either version 3 of the License, or
21 (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 =======================================================================
32 """
34 from elementtree.ElementTree import *
35 from utils import StopGeneration
36 import re
37 import sys
38 import psyco
39 psyco.full()
40 class InvalidTagException(Exception):
41 pass
44 class VUTChercker:
46 def __init__(self, xml_source):
47 try:
48 print "Openning the vut file..."
49 self.vut = ElementTree(file=xml_source)
50 except IOError:
51 print "|VUT Error| > The file %s is not exists!" % xml_source
52 raise StopGeneration()
53 self.root = self.vut.getroot()
54 mod = self.root.get("module_name")
55 #assert mod, """Module name not defined in vut tag. Usage '<vut module_name="name">...</vut>'"""
56 if not mod:
57 print """Error: Module name not defined in vut tag.\nUsage '<vut module_name="name">...</vut>'"""
58 raise StopGeneration()
59 #assert re.match(r'^[a-zA-z][a-zA-Z0-9_]*$', mod), "The module_name can't to start with number or to contain a special character!" #Verifica se deu match, retorna um obj sre se sim e None se nᅢテᅡᆪo
60 if not re.match(r'^[a-zA-z][a-zA-Z0-9_]*$', mod):
61 print "Error: invalid module_name, it can't to start with number or to contain a special character!" #Verifica se deu match, retorna um obj sre se sim e None se nᅢテᅡᆪo
62 raise StopGeneration()
63 print "Init checking %s..." % xml_source
65 def clear_comments(self, dlist):
66 for i in dlist:# removing commets
67 if "#" in i:
68 dlist[dlist.index(i)] = i[:i.index("#")]
69 return map(str.strip, dlist)
72 def check_waveform(self, *args):
73 print "checking waveform tag..."
74 waves = map(str.strip, args[0].text.splitlines())
75 waves = [x for x in waves if((x!= "") and ("--" not in x) and ("=" not in x))]
76 #print waves
77 for signal in waves:
78 regexp = re.compile(r"""
79 (^[a-zA-z])\w* #signal name
80 (\s*\[\d+\:\d+\])?\s* #bit slice
82 i\@\|\s* #input definition
84 [0-9A-Fa-f ]+| #hexa values
85 R\d+| #random value definition
86 random_alloc\(\[(R\d+)(\,R\d+)*\]\,\d+\)| # teste
87 include_memory\(\"\w*\.mem\"\)| #random randomic values allocation
88 )\|)* |
89 o\@\|\s* #output definition
91 [0-9A-Fa-f ]+| #hexa values
92 ref_function\(([a-zA-Z]\w*)(\,[a-zA-Z]\w*)*\)+| #reference function definition
93 include_memory\(\"\w*\.mem\"\)| #random randomic values allocation
94 )\|)*
96 """,re.VERBOSE)
98 #assert regexp.match(signal),"Construction error in the signal: %s" % signal
99 if not regexp.match(signal):
100 print "Error: Construction error in the signal: %s" % signal
101 raise StopGeneration()
102 #[a-zA-Z0-9_] = \w
104 waves = map(lambda a : a.replace(" ", ""),waves)
106 def check_gen_with(self, *args):
109 @param *args:
111 print "checking gen_with tag...\n checking randrange values..."
112 ran = map(str.strip,args[0].text.splitlines())
113 ran = [x for x in ran if((x!= "") and (x[0]!="#"))]
114 ran = self.clear_comments(ran)
116 for i in ran:
117 assert re.match(r'R\d+\s*\=\s*\([0-9A-Fa-f]+\,[0-9A-Fa-f]+\)',i), "Invalid Attribution in %s" % i
119 for code in args[0].getchildren():
120 var = code.get("output")
121 if var:
122 if not re.match(r'[a-zA-Z][a-zA-Z0-9_]*$',var):
123 print "Error: Invalid output name into python_code tag."
124 raise StopGeneration()
125 else:
126 print """Error: Output not defined into python_code tag.\nUsage <python_code output="port_name">...code...</python_code>!"""
127 raise StopGeneration()
128 self.python_code_parser(code.text)
129 #print ran
131 def check_time_scale(self, *args):
134 @param *args:
137 print "checking time_scale tag..."
138 t_div = args[0].get("t_div")
139 unit = args[0].get("unit")
140 if not t_div:
141 print """Error: Time division not defined in time_scale tag.\n
142 Usage <time_scale t_div='val_integer' unit="unit{(ump)s}"/>!"""
143 raise StopGeneration()
144 if not unit:
145 print "Error: Unit not defined in time_scale tag!"
146 raise StopGeneration()
148 def check_all(self):
151 root_children = self.root.getchildren()
152 switch = {"waveform": self.check_waveform,
153 "gen_with": self.check_gen_with,
154 "time_scale": self.check_time_scale}
156 for item in root_children:
157 try:
158 switch[item.tag](item)
159 except KeyError, e:
160 raise InvalidTagException(e)
161 print "Check complete successful!!!"
163 def python_code_parser(self, pycode):
164 #TODO: Ajsutar isso aqui!!!
165 print " checking python code tag..."
166 if not pycode:
167 print "Error: The python_code tag is empty."
168 raise StopGeneration()
169 code = self.clear_comments(pycode.splitlines())
170 code = [x for x in code if(x!= "")]
172 try:
173 if re.match(r"\s*include_python_file\(\"(((\/\w+)+|\.)\/)?\w+.py[oc]?\"\)\s*$",pycode):
174 print " checking external reference function's call...ok"# % pycode.strip()
176 elif re.match(r'(?!include_python_file)',pycode):
177 print " checking local reference function's call...",
178 exec("\n".join(code))
179 print "ok"
180 else: raise Exception("VUT Syntax error!")
181 except:
182 print "Python reference model code error: ", sys.exc_info()
184 if __name__ == '__main__':
185 test = VUTChercker("example5.vut")#sys.argv[1])
186 test.check_all()