2011-09-30 14:24:38 +02:00
|
|
|
#!/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.
|
|
|
|
|
2019-02-28 15:39:59 +01:00
|
|
|
from __future__ import print_function
|
2011-09-30 14:24:38 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
2014-10-24 18:44:18 +02:00
|
|
|
import imp
|
2011-09-30 14:24:38 +02:00
|
|
|
|
2019-02-28 15:39:59 +01:00
|
|
|
# Matches trailing whitespace and any following blank lines.
|
|
|
|
_BLANK_LINES = r"([ \t]*[\r\n]{1,2})*"
|
|
|
|
|
|
|
|
# Matches the YAML frontmatter block.
|
|
|
|
_YAML_PATTERN = re.compile(r"/\*---(.*)---\*/" + _BLANK_LINES, re.DOTALL)
|
|
|
|
|
|
|
|
# Matches all known variants for the license block.
|
|
|
|
_LICENSE_PATTERN = re.compile(
|
2019-07-15 19:00:30 +02:00
|
|
|
r'// Copyright( \([C]\))? (\w+) .+\. {1,2}All rights reserved\.[\r\n]{1,2}' +
|
2019-02-28 15:39:59 +01:00
|
|
|
r'(' +
|
|
|
|
r'// This code is governed by the( BSD)? license found in the LICENSE file\.' +
|
|
|
|
r'|' +
|
|
|
|
r'// See LICENSE for details\.' +
|
|
|
|
r'|' +
|
|
|
|
r'// Use of this source code is governed by a BSD-style license that can be[\r\n]{1,2}' +
|
|
|
|
r'// found in the LICENSE file\.' +
|
|
|
|
r'|' +
|
2020-07-15 20:56:11 +02:00
|
|
|
r'// See LICENSE or https://github\.com/tc39/test262/blob/HEAD/LICENSE' +
|
2019-02-28 15:39:59 +01:00
|
|
|
r')[\r\n]{1,2}' + _BLANK_LINES, re.IGNORECASE)
|
2014-07-19 00:22:37 +02:00
|
|
|
|
2014-08-28 13:47:58 +02:00
|
|
|
yamlLoad = None
|
|
|
|
|
2019-02-28 15:39:59 +01:00
|
|
|
def yamlAttrParser(testRecord, attrs, name, onerror):
|
2014-08-28 13:47:58 +02:00
|
|
|
importYamlLoad()
|
2014-07-19 00:22:37 +02:00
|
|
|
|
2019-02-28 15:39:59 +01:00
|
|
|
parsed = yamlLoad(attrs)
|
|
|
|
if parsed is None:
|
|
|
|
onerror("Failed to parse yaml in name %s" % name)
|
2014-07-19 00:22:37 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
for key in parsed:
|
|
|
|
value = parsed[key]
|
|
|
|
if key == "info":
|
|
|
|
key = "commentary"
|
|
|
|
testRecord[key] = value
|
2014-12-01 21:46:34 +01:00
|
|
|
|
2014-07-19 00:22:37 +02:00
|
|
|
if 'flags' in testRecord:
|
|
|
|
for flag in testRecord['flags']:
|
|
|
|
testRecord[flag] = ""
|
|
|
|
|
2019-02-08 16:48:23 +01:00
|
|
|
def findLicense(src):
|
|
|
|
match = _LICENSE_PATTERN.search(src)
|
|
|
|
if not match:
|
|
|
|
return None
|
|
|
|
|
2019-02-28 15:39:59 +01:00
|
|
|
return match.group(0)
|
2019-02-08 16:48:23 +01:00
|
|
|
|
2019-02-28 15:39:59 +01:00
|
|
|
def findAttrs(src):
|
|
|
|
match = _YAML_PATTERN.search(src)
|
|
|
|
if not match:
|
|
|
|
return (None, None)
|
|
|
|
|
|
|
|
return (match.group(0), match.group(1).strip())
|
2014-07-19 00:22:37 +02:00
|
|
|
|
2019-02-28 15:39:59 +01:00
|
|
|
def parseTestRecord(src, name, onerror = print):
|
|
|
|
# Find the license block.
|
|
|
|
header = findLicense(src)
|
|
|
|
|
|
|
|
# Find the YAML frontmatter.
|
|
|
|
(frontmatter, attrs) = findAttrs(src)
|
|
|
|
|
|
|
|
# YAML frontmatter is required for all tests.
|
|
|
|
if frontmatter is None:
|
|
|
|
onerror("Missing frontmatter: %s" % name)
|
|
|
|
|
|
|
|
# The license should be placed before the frontmatter and there shouldn't be
|
|
|
|
# be any extra content between the license and the frontmatter.
|
|
|
|
if header is not None and frontmatter is not None:
|
|
|
|
headerIdx = src.index(header)
|
|
|
|
frontmatterIdx = src.index(frontmatter)
|
|
|
|
if headerIdx > frontmatterIdx:
|
|
|
|
onerror("Unexpected license after frontmatter: %s" % name)
|
|
|
|
|
|
|
|
# Search for any extra test content, but ignore whitespace only or comment lines.
|
|
|
|
extra = src[headerIdx + len(header) : frontmatterIdx]
|
|
|
|
if extra and any(line.strip() and not line.lstrip().startswith("//") for line in extra.split("\n")):
|
|
|
|
onerror("Unexpected test content between license and frontmatter: %s" % name)
|
|
|
|
|
|
|
|
# Remove the license and YAML parts from the actual test content.
|
|
|
|
test = src
|
|
|
|
if frontmatter is not None:
|
|
|
|
test = test.replace(frontmatter, '')
|
|
|
|
if header is not None:
|
|
|
|
test = test.replace(header, '')
|
2019-02-08 16:48:23 +01:00
|
|
|
|
2019-02-28 15:39:59 +01:00
|
|
|
testRecord = {}
|
|
|
|
testRecord['header'] = header.strip() if header else ''
|
2019-02-08 16:48:23 +01:00
|
|
|
testRecord['test'] = test
|
|
|
|
|
2014-07-19 00:22:37 +02:00
|
|
|
if attrs:
|
2019-02-28 15:39:59 +01:00
|
|
|
yamlAttrParser(testRecord, attrs, name, onerror)
|
|
|
|
|
|
|
|
# Report if the license block is missing in non-generated tests.
|
|
|
|
if header is None and "generated" not in testRecord:
|
|
|
|
onerror("No license found in: %s" % name)
|
2014-07-19 00:22:37 +02:00
|
|
|
|
2011-09-30 14:24:38 +02:00
|
|
|
return testRecord
|
2014-08-28 13:47:58 +02:00
|
|
|
|
|
|
|
def importYamlLoad():
|
|
|
|
global yamlLoad
|
|
|
|
if yamlLoad:
|
|
|
|
return
|
2015-07-01 20:35:13 +02:00
|
|
|
monkeyYaml = loadMonkeyYaml()
|
|
|
|
yamlLoad = monkeyYaml.load
|
2014-10-24 18:44:18 +02:00
|
|
|
|
|
|
|
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()
|