admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 3 | Created on 26-Dec-2012 |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 4 | Modified 2015 by ON.Lab |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 5 | |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 6 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 7 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 8 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 9 | |
| 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 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 13 | (at your option) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 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 Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 22 | |
| 23 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 24 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 25 | import re |
| 26 | import sys |
| 27 | class TestParser: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 28 | def __init__( self, testFile ): |
| 29 | try: |
| 30 | testFileHandler = open( testFile, 'r' ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 31 | except IOError: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 32 | print "No such file " + testFile |
| 33 | sys.exit( 0 ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 34 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 35 | testFileList = testFileHandler.readlines() |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 36 | self.testscript = testFileList |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 37 | self.caseCode = {} |
| 38 | self.caseBlock = '' |
| 39 | self.statementsList = [] |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 40 | index = 0 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 41 | self.statementsList = [] |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 42 | # initialSpaces = len(line) -len(line.lstrip()) |
| 43 | while index < len( testFileList ): |
| 44 | testFileList[ index ] = re.sub( "^\s{8}|^\s{4}", "", testFileList[ index ] ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 45 | # Skip multiline comments |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 46 | if re.match( '^(\'\'\')|^(\"\"\")', testFileList[ index ], 0 ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 47 | index = index + 1 |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 48 | try: |
| 49 | while not re.match( '^\s*(\'\'\')|^\s*(\"\"\")', testFileList[ index ], 0 ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 50 | index = index + 1 |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 51 | except IndexError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 52 | print '' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 53 | |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 54 | # skip empty lines and single line comments |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 55 | elif not re.match( '#|^\s*$', testFileList[ index ], 0 ): |
| 56 | self.statementsList.append( testFileList[ index ] ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 57 | index = index + 1 |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 58 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 59 | def case_code( self ): |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 60 | index = 0 |
| 61 | statementsList = self.statementsList |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 62 | while index < len( statementsList ): |
| 63 | m = re.match( 'def\s+CASE(\d+)', statementsList[ index ], 0 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 64 | self.caseBlock = [] |
| 65 | if m: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 66 | index = index + 1 |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 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: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 71 | index = index + 1 |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 72 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 73 | break |
| 74 | index = index - 1 |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 75 | except IndexError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 76 | print '' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 77 | self.caseCode[ str( m.group( 1 ) ) ] = self.caseBlock |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 78 | index = index + 1 |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 79 | return self.caseCode |
| 80 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 81 | def step_code( self, caseStatements ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 82 | index = 0 |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 83 | step = 0 |
| 84 | stepCode = {} |
| 85 | step_flag = False |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 86 | while index < len( caseStatements ): |
| 87 | m = re.match( 'main\.step', caseStatements[ index ], 0 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 88 | stepBlock = '' |
| 89 | if m: |
| 90 | step_flag = True |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 91 | if step == 0: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 92 | i = 0 |
| 93 | block = '' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 94 | while i < index: |
| 95 | block += caseStatements[ i ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 96 | i = i + 1 |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 97 | stepCode[ step ] = block |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 98 | step = step + 1 |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 99 | stepBlock = stepBlock + caseStatements[ index ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 100 | index = index + 1 |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 101 | try: |
| 102 | while not re.match( 'main\.step', caseStatements[ index ], 0 ): |
| 103 | stepBlock = stepBlock + caseStatements[ index ] |
| 104 | if index < len( caseStatements )-1: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 105 | index = index + 1 |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 106 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 107 | break |
| 108 | index = index - 1 |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 109 | except IndexError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 110 | print '' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 111 | stepCode[ step ] = stepBlock |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 112 | step = step + 1 |
| 113 | index = index + 1 |
| 114 | # If there is no step defined !! |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 115 | if not step_flag: |
| 116 | stepCode[ step ] = "".join( caseStatements ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 117 | return stepCode |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 118 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 119 | def getStepCode( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 120 | case_step_code = {} |
| 121 | case_block = self.case_code() |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 122 | 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 ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 127 | return case_step_code |