File: parseTestRecord.py

package info (click to toggle)
qtdeclarative-opensource-src-gles 5.15.17%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 258,992 kB
  • sloc: javascript: 512,415; cpp: 497,385; xml: 8,892; python: 3,304; ansic: 2,764; sh: 206; makefile: 46; php: 27
file content (130 lines) | stat: -rw-r--r-- 3,438 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python

# Copyright 2011 by Google, Inc.  All rights reserved.
# This code is governed by the BSD license found in the LICENSE file.

# TODO: resolve differences with common.py and unify into one file.


import logging
import optparse
import os
from os import path
import platform
import re
import subprocess
import sys
import tempfile
import time
import imp

# from TestCasePackagerConfig import *

headerPatternStr = r"(?:(?:\s*\/\/.*)?\s*\n)*"
captureCommentPatternStr = r"\/\*\*?((?:\s|\S)*?)\*\/\s*\n"
anyPatternStr = r"(?:\s|\S)*"

headerPattern = re.compile("^" + headerPatternStr)

# Should match anything
testRecordPattern = re.compile(r"^(" + headerPatternStr +
                               r")(?:" + captureCommentPatternStr +
                               r")?(" + anyPatternStr +
                               r")$")

stars = re.compile(r"\s*\n\s*\*\s?")
atattrs = re.compile(r"\s*\n\s*\*\s*@")

yamlPattern = re.compile(r"---((?:\s|\S)*)---")
newlinePattern = re.compile(r"\n")

yamlLoad = None

def stripStars(text):
    return stars.sub('\n', text).strip()

def stripHeader(src):
    header = headerPattern.match(src).group(0)
    return src[len(header):]

def matchParts(src, name):
    match = testRecordPattern.match(src)
    if match == None:
        raise Exception('unrecognized: ' + name)
    return match

def hasYAML(text):
    match = yamlPattern.match(text)
    if match == None:
        return False
    return True

def oldAttrParser(testRecord, body, name):
    propTexts = atattrs.split(body)
    testRecord['commentary'] = stripStars(propTexts[0])
    del propTexts[0]
    for propText in propTexts:
        propMatch = re.match(r"^\w+", propText)
        if propMatch == None:
            raise Exception('Malformed "@" attribute: ' + name)
        propName = propMatch.group(0)
        propVal = stripStars(propText[len(propName):])

        if propName in testRecord:
            raise Exception('duplicate: ' + propName)
        testRecord[propName] = propVal;

def yamlAttrParser(testRecord, attrs, name):
    match = yamlPattern.match(attrs)
    body = match.group(1)
    importYamlLoad()
    parsed = yamlLoad(body)

    if (parsed is None):
        print "Failed to parse yaml in name %s"%(name)
        return

    for key in parsed:
        value = parsed[key]
        if key == "info":
            key = "commentary"
        testRecord[key] = value

    if 'flags' in testRecord:
        for flag in testRecord['flags']:
            testRecord[flag] = ""

def parseTestRecord(src, name):
    testRecord = {}
    match = matchParts(src, name)
    testRecord['header'] = match.group(1).strip()
    testRecord['test'] = match.group(3) # do not trim

    attrs = match.group(2)
    if attrs:
        if hasYAML(attrs):
            yamlAttrParser(testRecord, attrs, name)
        else:
            oldAttrParser(testRecord, attrs, name)

    return testRecord

def importYamlLoad():
    global yamlLoad
    if yamlLoad:
        return
    monkeyYaml = loadMonkeyYaml()
    yamlLoad = monkeyYaml.load

def loadMonkeyYaml():
    f = None
    try:
        p = os.path.dirname(os.path.realpath(__file__))
        (f, pathname, description) = imp.find_module("monkeyYaml", [p])
        module = imp.load_module("monkeyYaml", f, pathname, description)
        return module
    except:
        raise ImportError("Cannot load monkeyYaml")
    finally:
        if f:
            f.close()