admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [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 | 23fb216 | 2017-09-15 14:59:57 -0700 | [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 | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 24 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 25 | import re |
| 26 | import sys |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 27 | |
| 28 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 29 | class TestParser: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 30 | |
| 31 | def __init__( self, testFile ): |
| 32 | try: |
| 33 | testFileHandler = open( testFile, 'r' ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 34 | except IOError: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 35 | print "No such file " + testFile |
| 36 | sys.exit( 0 ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 37 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 38 | testFileList = testFileHandler.readlines() |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 39 | self.testscript = testFileList |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 40 | self.caseCode = {} |
| 41 | self.caseBlock = '' |
| 42 | self.statementsList = [] |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 43 | index = 0 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 44 | self.statementsList = [] |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 45 | # initialSpaces = len( line ) -len( line.lstrip() ) |
| 46 | while index < len( testFileList ): |
| 47 | testFileList[ index ] = re.sub( "^\s{8}|^\s{4}", "", testFileList[ index ] ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 48 | # Skip multiline comments |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 49 | if re.match( '^(\'\'\' )|^( \"\"\" )', testFileList[ index ], 0 ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 50 | index = index + 1 |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 51 | try: |
| 52 | while not re.match( '^\s*(\'\'\' )|^\s*( \"\"\" )', testFileList[ index ], 0 ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 53 | index = index + 1 |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 54 | except IndexError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 55 | print '' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 56 | |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 57 | # skip empty lines and single line comments |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 58 | elif not re.match( '#|^\s*$', testFileList[ index ], 0 ): |
| 59 | self.statementsList.append( testFileList[ index ] ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 60 | index = index + 1 |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 61 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 62 | def case_code( self ): |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 63 | index = 0 |
| 64 | statementsList = self.statementsList |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 65 | while index < len( statementsList ): |
| 66 | m = re.match( 'def\s+CASE(\d+)', statementsList[ index ], 0 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 67 | self.caseBlock = [] |
| 68 | if m: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 69 | index = index + 1 |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 70 | try: |
| 71 | while not re.match( '\s*def\s+CASE(\d+)', statementsList[ index ], 0 ): |
| 72 | self.caseBlock.append( statementsList[ index ] ) |
| 73 | if index < len( statementsList ) - 1: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 74 | index = index + 1 |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 75 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 76 | break |
| 77 | index = index - 1 |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 78 | except IndexError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 79 | print '' |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 80 | self.caseCode[ str( m.group( 1 ) ) ] = self.caseBlock |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 81 | index = index + 1 |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 82 | return self.caseCode |
| 83 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 84 | def step_code( self, caseStatements ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 85 | index = 0 |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 86 | step = 0 |
| 87 | stepCode = {} |
| 88 | step_flag = False |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 89 | while index < len( caseStatements ): |
| 90 | m = re.match( 'main\.step', caseStatements[ index ], 0 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 91 | stepBlock = '' |
| 92 | if m: |
| 93 | step_flag = True |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 94 | if step == 0: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 95 | i = 0 |
| 96 | block = '' |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 97 | while i < index: |
| 98 | block += caseStatements[ i ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 99 | i = i + 1 |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 100 | stepCode[ step ] = block |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 101 | step = step + 1 |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 102 | stepBlock = stepBlock + caseStatements[ index ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 103 | index = index + 1 |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 104 | try: |
| 105 | while not re.match( 'main\.step', caseStatements[ index ], 0 ): |
| 106 | stepBlock = stepBlock + caseStatements[ index ] |
| 107 | if index < len( caseStatements ) - 1: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 108 | index = index + 1 |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 109 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 110 | break |
| 111 | index = index - 1 |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 112 | except IndexError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 113 | print '' |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 114 | stepCode[ step ] = stepBlock |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 115 | step = step + 1 |
| 116 | index = index + 1 |
| 117 | # If there is no step defined !! |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 118 | if not step_flag: |
| 119 | stepCode[ step ] = "".join( caseStatements ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 120 | return stepCode |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 121 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 122 | def getStepCode( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 123 | case_step_code = {} |
| 124 | case_block = self.case_code() |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 125 | for case in case_block: |
| 126 | case_step_code[ case ] = {} |
| 127 | step_block = self.step_code( case_block[ case ] ) |
| 128 | for step in step_block: |
| 129 | case_step_code[ case ][ step ] = step_block[ step ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 130 | return case_step_code |