admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ''' |
| 3 | Created on 26-Dec-2012 |
| 4 | |
| 5 | @author: Anil Kumar (anilkumar.s@paxterrasolutions.com) |
| 6 | |
| 7 | |
| 8 | TestON is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | TestON is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 20 | |
| 21 | |
| 22 | ''' |
| 23 | import re |
| 24 | import sys |
| 25 | class TestParser: |
| 26 | def __init__(self,testFile): |
| 27 | try : |
| 28 | testFileHandler = open(testFile, 'r') |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 29 | except IOError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 30 | print "No such file "+testFile |
| 31 | sys.exit(0) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 32 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 33 | testFileList = testFileHandler.readlines() |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 34 | self.testscript = testFileList |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 35 | self.caseCode = {} |
| 36 | self.caseBlock = '' |
| 37 | self.statementsList = [] |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 38 | index = 0 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 39 | self.statementsList = [] |
| 40 | #initialSpaces = len(line) -len(line.lstrip()) |
| 41 | while index < len(testFileList): |
| 42 | testFileList[index] = re.sub("^\s{8}|^\s{4}", "", testFileList[index]) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 43 | # Skip multiline comments |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 44 | if re.match('^(\'\'\')|^(\"\"\")',testFileList[index],0) : |
| 45 | index = index + 1 |
| 46 | try : |
| 47 | while not re.match('^\s*(\'\'\')|^\s*(\"\"\")',testFileList[index],0) : |
| 48 | index = index + 1 |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 49 | except IndexError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 50 | print '' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 51 | |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 52 | # skip empty lines and single line comments |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 53 | elif not re.match('#|^\s*$',testFileList[index],0): |
| 54 | self.statementsList.append(testFileList[index]) |
| 55 | index = index + 1 |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 56 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 57 | def case_code(self): |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 58 | index = 0 |
| 59 | statementsList = self.statementsList |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 60 | while index < len(statementsList): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 61 | m= re.match('def\s+CASE(\d+)',statementsList[index],0) |
| 62 | self.caseBlock = [] |
| 63 | if m: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 64 | index = index + 1 |
| 65 | try : |
| 66 | while not re.match('\s*def\s+CASE(\d+)',statementsList[index],0) : |
| 67 | self.caseBlock.append(statementsList[index]) |
| 68 | if index < len(statementsList)-1: |
| 69 | index = index + 1 |
| 70 | else : |
| 71 | break |
| 72 | index = index - 1 |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 73 | except IndexError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 74 | print '' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 75 | self.caseCode [str(m.group(1))] = self.caseBlock |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 76 | index = index + 1 |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 77 | return self.caseCode |
| 78 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 79 | def step_code(self,caseStatements): |
| 80 | index = 0 |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 81 | step = 0 |
| 82 | stepCode = {} |
| 83 | step_flag = False |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 84 | while index < len(caseStatements): |
| 85 | m= re.match('main\.step',caseStatements[index],0) |
| 86 | stepBlock = '' |
| 87 | if m: |
| 88 | step_flag = True |
| 89 | if step == 0 : |
| 90 | i = 0 |
| 91 | block = '' |
Jon Hall | 9910c2b | 2015-01-21 15:55:56 -0800 | [diff] [blame] | 92 | while i < index : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 93 | block += caseStatements[i] |
| 94 | i = i + 1 |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 95 | stepCode[step] = block |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 96 | step = step + 1 |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 97 | stepBlock = stepBlock + caseStatements[index] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 98 | index = index + 1 |
| 99 | try : |
| 100 | while not re.match('main\.step',caseStatements[index],0) : |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 101 | stepBlock = stepBlock + caseStatements[index] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 102 | if index < len(caseStatements)-1: |
| 103 | index = index + 1 |
| 104 | else : |
| 105 | break |
| 106 | index = index - 1 |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 107 | except IndexError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 108 | print '' |
| 109 | stepCode[step] = stepBlock |
| 110 | step = step + 1 |
| 111 | index = index + 1 |
| 112 | # If there is no step defined !! |
| 113 | if not step_flag : |
| 114 | stepCode[step] = "".join(caseStatements) |
| 115 | return stepCode |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 116 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 117 | def getStepCode(self): |
| 118 | case_step_code = {} |
| 119 | case_block = self.case_code() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 120 | for case in case_block : |
| 121 | case_step_code[case] = {} |
| 122 | step_block = self.step_code(case_block[case]) |
| 123 | for step in step_block : |
| 124 | case_step_code[case][step] = step_block[step] |
| 125 | return case_step_code |