• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/python
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3
4# This program generates the copy commands you should use to update
5# the reference data for tests <build-dir>/tests/runtest* that emit an
6# output that is compared against a reference output.
7#
8# It takes in argument the diff result emitted by
9# <build-dir>/tests/runtest*, and emits on standard output a series of
10# 'cp <src> <dest>' commands to execute to update reference data of
11# the test.
12
13import fileinput
14import re
15import sys
16import getopt
17
18
19def display_usage():
20    sys.stderr.write("usage: prog-name [options] <file-name|-->\n");
21    sys.stderr.write(" options:\n");
22    sys.stderr.write("  -h  display this help\n");
23    sys.stderr.write(" argument:\n");
24    sys.stderr.write("  <file-name>  the file to read from\n");
25    sys.stderr.write("  if no argument, then reads from stdin\n");
26
27    sys.exit(2)
28
29def main():
30    input_file = None
31
32    try:
33        opts, args = getopt.getopt(sys.argv[1:], "hi", ["help"])
34    except getopt.GetoptError as err:
35        print(str(err))
36        display_usage()
37
38    for opt, arg in opts:
39        if opt in ("-h", "--help"):
40            display_usage()
41        else:
42            # unknown option.
43            display_usage()
44
45    if input_file == None and len(args) and args[0] != None:
46        input_file = open(args[0], 'r')
47    elif input_file == None:
48        input_file = sys.stdin
49
50    if input_file != None:
51        process(input_file)
52    else:
53        display_usage()
54
55
56def process(input_file):
57    source = ""
58    dest = ""
59    for line in input_file:
60        m = re.match(r'^--- (.*?)\t', line)
61        if m:
62            dest = m.group(1)
63        else:
64            m = re.match(r'^\+\+\+ (.*?)\t', line)
65            if m:
66                source = m.group(1)
67                sys.stdout.write("cp " + source + " " + dest + "\n");
68
69if __name__ == "__main__":
70    main()
71