blob: 904ebc0f5fa2df5f1d7be723f6d679968440d0c1 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
2'''
3Created on 26-Dec-2012
Jeremy Songsterae01bba2016-07-11 15:39:17 -07004Modified 2015 by ON.Lab
adminbae64d82013-08-01 10:50:15 -07005
Jeremy Songsterae01bba2016-07-11 15:39:17 -07006Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
7the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
8or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
adminbae64d82013-08-01 10:50:15 -07009
10 TestON is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
13 (at your option) any later version.
14
15 TestON is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
Jon Hallfeff3082015-05-19 10:23:26 -070021 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070022
23
24'''
25import re
26import sys
27class TestParser:
28 def __init__(self,testFile):
29 try :
30 testFileHandler = open(testFile, 'r')
Jon Hallfeff3082015-05-19 10:23:26 -070031 except IOError:
adminbae64d82013-08-01 10:50:15 -070032 print "No such file "+testFile
33 sys.exit(0)
Jon Hallfeff3082015-05-19 10:23:26 -070034
adminbae64d82013-08-01 10:50:15 -070035 testFileList = testFileHandler.readlines()
Jon Hallfeff3082015-05-19 10:23:26 -070036 self.testscript = testFileList
adminbae64d82013-08-01 10:50:15 -070037 self.caseCode = {}
38 self.caseBlock = ''
39 self.statementsList = []
Jon Hallfeff3082015-05-19 10:23:26 -070040 index = 0
adminbae64d82013-08-01 10:50:15 -070041 self.statementsList = []
42 #initialSpaces = len(line) -len(line.lstrip())
43 while index < len(testFileList):
44 testFileList[index] = re.sub("^\s{8}|^\s{4}", "", testFileList[index])
Jon Hallfeff3082015-05-19 10:23:26 -070045 # Skip multiline comments
adminbae64d82013-08-01 10:50:15 -070046 if re.match('^(\'\'\')|^(\"\"\")',testFileList[index],0) :
47 index = index + 1
48 try :
49 while not re.match('^\s*(\'\'\')|^\s*(\"\"\")',testFileList[index],0) :
50 index = index + 1
Jon Hall1306a562015-09-04 11:21:24 -070051 except IndexError:
adminbae64d82013-08-01 10:50:15 -070052 print ''
adminbae64d82013-08-01 10:50:15 -070053
Jon Hallfeff3082015-05-19 10:23:26 -070054 # skip empty lines and single line comments
adminbae64d82013-08-01 10:50:15 -070055 elif not re.match('#|^\s*$',testFileList[index],0):
56 self.statementsList.append(testFileList[index])
57 index = index + 1
Jon Hallfeff3082015-05-19 10:23:26 -070058
adminbae64d82013-08-01 10:50:15 -070059 def case_code(self):
Jon Hallfeff3082015-05-19 10:23:26 -070060 index = 0
61 statementsList = self.statementsList
adminbae64d82013-08-01 10:50:15 -070062 while index < len(statementsList):
adminbae64d82013-08-01 10:50:15 -070063 m= re.match('def\s+CASE(\d+)',statementsList[index],0)
64 self.caseBlock = []
65 if m:
adminbae64d82013-08-01 10:50:15 -070066 index = index + 1
67 try :
68 while not re.match('\s*def\s+CASE(\d+)',statementsList[index],0) :
69 self.caseBlock.append(statementsList[index])
70 if index < len(statementsList)-1:
71 index = index + 1
72 else :
73 break
74 index = index - 1
Jon Hall1306a562015-09-04 11:21:24 -070075 except IndexError:
adminbae64d82013-08-01 10:50:15 -070076 print ''
adminbae64d82013-08-01 10:50:15 -070077 self.caseCode [str(m.group(1))] = self.caseBlock
adminbae64d82013-08-01 10:50:15 -070078 index = index + 1
Jon Hallfeff3082015-05-19 10:23:26 -070079 return self.caseCode
80
adminbae64d82013-08-01 10:50:15 -070081 def step_code(self,caseStatements):
82 index = 0
Jon Hallfeff3082015-05-19 10:23:26 -070083 step = 0
84 stepCode = {}
85 step_flag = False
adminbae64d82013-08-01 10:50:15 -070086 while index < len(caseStatements):
87 m= re.match('main\.step',caseStatements[index],0)
88 stepBlock = ''
89 if m:
90 step_flag = True
91 if step == 0 :
92 i = 0
93 block = ''
Jon Hall9910c2b2015-01-21 15:55:56 -080094 while i < index :
adminbae64d82013-08-01 10:50:15 -070095 block += caseStatements[i]
96 i = i + 1
Jon Hallfeff3082015-05-19 10:23:26 -070097 stepCode[step] = block
adminbae64d82013-08-01 10:50:15 -070098 step = step + 1
Jon Hallfeff3082015-05-19 10:23:26 -070099 stepBlock = stepBlock + caseStatements[index]
adminbae64d82013-08-01 10:50:15 -0700100 index = index + 1
101 try :
102 while not re.match('main\.step',caseStatements[index],0) :
Jon Hallfeff3082015-05-19 10:23:26 -0700103 stepBlock = stepBlock + caseStatements[index]
adminbae64d82013-08-01 10:50:15 -0700104 if index < len(caseStatements)-1:
105 index = index + 1
106 else :
107 break
108 index = index - 1
Jon Hall1306a562015-09-04 11:21:24 -0700109 except IndexError:
adminbae64d82013-08-01 10:50:15 -0700110 print ''
111 stepCode[step] = stepBlock
112 step = step + 1
113 index = index + 1
114 # If there is no step defined !!
115 if not step_flag :
116 stepCode[step] = "".join(caseStatements)
117 return stepCode
Jon Hallfeff3082015-05-19 10:23:26 -0700118
adminbae64d82013-08-01 10:50:15 -0700119 def getStepCode(self):
120 case_step_code = {}
121 case_block = self.case_code()
adminbae64d82013-08-01 10:50:15 -0700122 for case in case_block :
123 case_step_code[case] = {}
124 step_block = self.step_code(case_block[case])
125 for step in step_block :
126 case_step_code[case][step] = step_block[step]
127 return case_step_code