Cleanup generation code (#3041)

* Simplify find_cases

* Improve help text

* Improve YAML-capturing regex

* Use built-in dedenting

* Fix use of built-in dedenting

* Fix use of built-in dedenting for Python 3
This commit is contained in:
Richard Gibson 2021-07-16 09:39:57 -04:00 committed by GitHub
parent a92327395c
commit d15066ec39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 22 deletions

View File

@ -4,7 +4,7 @@
from __future__ import print_function from __future__ import print_function
import argparse import argparse
import os, sys import glob, os, sys
from lib.expander import Expander from lib.expander import Expander
from lib.test import Test from lib.test import Test
@ -21,22 +21,16 @@ def is_case_dir(location):
return False return False
def find_cases(location): def find_cases(location):
# When a file is specified, return the file name and its containing # single file
# directory
if os.path.isfile(location): if os.path.isfile(location):
return location, [os.path.dirname(location)] return location, [os.path.dirname(location)]
# directory with case files
if is_case_dir(location): if is_case_dir(location):
return None, [location] return None, [location]
else:
return None, map( # other directory, return all contents other than hidden "dot files"
lambda x: os.path.join(args.cases, x), return None, glob.glob(os.path.join(location, '*'))
filter(
# skip hidden files on Unix, such as ".DS_Store" on Mac
lambda x: not x.startswith('.'),
os.listdir(args.cases)
)
)
def clean(args): def clean(args):
for (subdir, _, fileNames) in os.walk(args.directory): for (subdir, _, fileNames) in os.walk(args.directory):
@ -80,13 +74,12 @@ subparsers = parser.add_subparsers()
create_parser = subparsers.add_parser('create', create_parser = subparsers.add_parser('create',
help='''Generate test material''') help='''Generate test material''')
create_parser.add_argument('-o', '--out', help='''The directory to write the create_parser.add_argument('-o', '--out', help='''The directory in which to write
compiled tests. If unspecified, tests will be written to standard out.''') compiled tests. If unspecified, tests will be written to standard output.''')
create_parser.add_argument('-p', '--parents', action='store_true', create_parser.add_argument('-p', '--parents', action='store_true',
help='''Create non-existent directories as necessary.''') help='''Create non-existent directories as necessary.''')
create_parser.add_argument('-n', '--no-clobber', action='store_true', create_parser.add_argument('-n', '--no-clobber', action='store_true',
help='''Do not produce test if a corresponding file exists within this help='''Abort if any test file already exists.''')
directory.''')
create_parser.add_argument('cases', create_parser.add_argument('cases',
help='''Test cases to generate. May be a file or a directory.''') help='''Test cases to generate. May be a file or a directory.''')
create_parser.set_defaults(func=create) create_parser.set_defaults(func=create)

View File

@ -1,17 +1,23 @@
# Copyright (C) 2016 the V8 project authors. All rights reserved. # Copyright (C) 2016 the V8 project authors. All rights reserved.
# This code is governed by the BSD license found in the LICENSE file. # This code is governed by the BSD license found in the LICENSE file.
import yaml, re import yaml, re, textwrap
yamlPattern = re.compile(r'\---\n([\s]*)((?:\s|\S)*)[\n\s*]---', yamlPattern = re.compile(
flags=re.DOTALL|re.MULTILINE) r'^\s*---\n(.*?)(?:\n[^\n\S]*)?---\s*$',
flags=re.DOTALL)
endOfLine = re.compile(r'(^|.)$', flags=re.MULTILINE)
def parse_yaml(string): def parse_yaml(string):
match = yamlPattern.match(string) match = yamlPattern.match(string)
if not match: if not match:
return False return False
unindented = re.sub('^' + match.group(1), '', # dedent truncates only-whitespace lines,
match.group(2), flags=re.MULTILINE) # so run it against a transformed string
# in which every line is terminated by a dot
terminated = endOfLine.sub(r'\1~', match.group(1))
dedented_terminated = textwrap.dedent(terminated)
dedented = endOfLine.sub('', dedented_terminated)
return yaml.safe_load(unindented) return yaml.safe_load(dedented)