]> git.lizzy.rs Git - rust.git/blob - src/grammar/testparser.py
complete openbsd support for `std::env`
[rust.git] / src / grammar / testparser.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2015 The Rust Project Developers. See the COPYRIGHT
4 # file at the top-level directory of this distribution and at
5 # http://rust-lang.org/COPYRIGHT.
6 #
7 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 # option. This file may not be copied, modified, or distributed
11 # except according to those terms.
12 import sys
13
14 import os
15 import subprocess
16 import argparse
17
18 # usage: testparser.py [-h] [-p PARSER [PARSER ...]] -s SOURCE_DIR
19
20 # Parsers should read from stdin and return exit status 0 for a
21 # successful parse, and nonzero for an unsuccessful parse
22
23 parser = argparse.ArgumentParser()
24 parser.add_argument('-p', '--parser', nargs='+')
25 parser.add_argument('-s', '--source-dir', nargs=1, required=True)
26 args = parser.parse_args(sys.argv[1:])
27
28 total = 0
29 ok = {}
30 bad = {}
31 for parser in args.parser:
32     ok[parser] = 0
33     bad[parser] = []
34 devnull = open(os.devnull, 'w')
35 print "\n"
36
37 for base, dirs, files in os.walk(args.source_dir[0]):
38     for f in filter(lambda p: p.endswith('.rs'), files):
39         p = os.path.join(base, f)
40         compile_fail = 'compile-fail' in p
41         ignore = any('ignore-test' in line or 'ignore-lexer-test' in line
42                      for line in open(p).readlines())
43         if compile_fail or ignore:
44             continue
45         total += 1
46         for parser in args.parser:
47             if subprocess.call(parser, stdin=open(p), stderr=subprocess.STDOUT, stdout=devnull) == 0:
48                 ok[parser] += 1
49             else:
50                 bad[parser].append(p)
51         parser_stats = ', '.join(['{}: {}'.format(parser, ok[parser]) for parser in args.parser])
52         sys.stdout.write("\033[K\r total: {}, {}, scanned {}"
53                          .format(total, os.path.relpath(parser_stats), os.path.relpath(p)))
54
55 devnull.close()
56
57 print "\n"
58
59 for parser in args.parser:
60     filename = os.path.basename(parser) + '.bad'
61     print("writing {} files that failed to parse with {} to {}".format(len(bad[parser]), parser, filename))
62     with open(filename, "w") as f:
63           for p in bad[parser]:
64               f.write(p)
65               f.write("\n")