Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2015 The PDFium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | import optparse |
| 7 | import os |
| 8 | import re |
| 9 | import subprocess |
| 10 | import sys |
| 11 | |
| 12 | # Nomenclature: |
| 13 | # x_root - "x" |
| 14 | # x_filename - "x.ext" |
| 15 | # x_path - "path/to/a/b/c/x.ext" |
| 16 | # c_dir - "path/to/a/b/c" |
| 17 | |
| 18 | def generate_and_test(input_filename, source_dir, working_dir, |
Tom Sepez | df16baf | 2015-02-24 12:47:58 -0800 | [diff] [blame] | 19 | fixup_path, pdfium_test_path, pdfium_diff_path): |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 20 | input_root, _ = os.path.splitext(input_filename) |
| 21 | input_path = os.path.join(source_dir, input_root + '.in') |
| 22 | pdf_path = os.path.join(working_dir, input_root + '.pdf') |
Tom Sepez | df16baf | 2015-02-24 12:47:58 -0800 | [diff] [blame] | 23 | actual_path_template = os.path.join(working_dir, input_root + '.pdf.%d.png') |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 24 | expected_path_template = os.path.join(source_dir, |
Tom Sepez | df16baf | 2015-02-24 12:47:58 -0800 | [diff] [blame] | 25 | input_root + '_expected.pdf.%d.png') |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 26 | try: |
| 27 | subprocess.check_call( |
| 28 | [fixup_path, '--output-dir=' + working_dir, input_path]) |
Tom Sepez | df16baf | 2015-02-24 12:47:58 -0800 | [diff] [blame] | 29 | subprocess.check_call([pdfium_test_path, '--png', pdf_path]) |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 30 | i = 0; |
| 31 | while True: |
| 32 | expected_path = expected_path_template % i; |
| 33 | actual_path = actual_path_template % i; |
| 34 | if not os.path.exists(expected_path): |
Tom Sepez | df16baf | 2015-02-24 12:47:58 -0800 | [diff] [blame] | 35 | if i == 0: |
| 36 | print "WARNING: no expected results files found for " + input_filename |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 37 | break |
Tom Sepez | df16baf | 2015-02-24 12:47:58 -0800 | [diff] [blame] | 38 | print "Checking " + actual_path |
| 39 | subprocess.check_call([pdfium_diff_path, expected_path, actual_path]) |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 40 | i += 1 |
| 41 | except subprocess.CalledProcessError as e: |
| 42 | print "FAILURE: " + input_filename + "; " + str(e) |
Tom Sepez | a8fd0e8 | 2015-03-17 13:30:11 -0700 | [diff] [blame^] | 43 | return False |
| 44 | return True |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 45 | |
| 46 | def main(): |
| 47 | parser = optparse.OptionParser() |
| 48 | parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), |
| 49 | help='relative path from the base source directory') |
| 50 | options, args = parser.parse_args() |
| 51 | |
| 52 | # Expect |my_dir| to be .../pdfium/testing/tools. |
| 53 | my_dir = os.path.dirname(os.path.realpath(__file__)) |
| 54 | testing_dir = os.path.dirname(my_dir) |
| 55 | pdfium_dir = os.path.dirname(testing_dir) |
| 56 | if (os.path.basename(my_dir) != 'tools' or |
| 57 | os.path.basename(testing_dir) != 'testing'): |
| 58 | print 'Confused, can not find pdfium root directory, aborting.' |
| 59 | return 1 |
| 60 | |
| 61 | # Other scripts are found in the same directory as this one. |
| 62 | fixup_path = os.path.join(my_dir, 'fixup_pdf_template.py') |
| 63 | |
| 64 | # test files are in .../pdfium/testing/resources/pixel. |
| 65 | source_dir = os.path.join(testing_dir, 'resources', 'pixel') |
| 66 | |
| 67 | # Find path to build directory. This depends on whether this is a |
| 68 | # standalone build vs. a build as part of a chromium checkout. For |
| 69 | # standalone, we expect a path like .../pdfium/out/Debug, but for |
| 70 | # chromium, we expect a path like .../src/out/Debug two levels |
| 71 | # higher (to skip over the third_party/pdfium path component under |
| 72 | # which chromium sticks pdfium). |
| 73 | base_dir = pdfium_dir |
| 74 | one_up_dir = os.path.dirname(base_dir) |
| 75 | two_up_dir = os.path.dirname(one_up_dir) |
| 76 | if (os.path.basename(two_up_dir) == 'src' and |
| 77 | os.path.basename(one_up_dir) == 'third_party'): |
| 78 | base_dir = two_up_dir |
| 79 | build_dir = os.path.join(base_dir, options.build_dir) |
| 80 | |
| 81 | # Compiled binaries are found under the build path. |
| 82 | pdfium_test_path = os.path.join(build_dir, 'pdfium_test') |
Tom Sepez | df16baf | 2015-02-24 12:47:58 -0800 | [diff] [blame] | 83 | pdfium_diff_path = os.path.join(build_dir, 'pdfium_diff') |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 84 | if sys.platform.startswith('win'): |
| 85 | pdfium_test_path = pdfium_test_path + '.exe' |
Tom Sepez | df16baf | 2015-02-24 12:47:58 -0800 | [diff] [blame] | 86 | pdfium_diff_path = pdfium_diff_path + '.exe' |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 87 | # TODO(tsepez): Mac may require special handling here. |
| 88 | |
| 89 | # Place generated files under the build directory, not source directory. |
| 90 | gen_dir = os.path.join(build_dir, 'gen', 'pdfium') |
| 91 | working_dir = os.path.join(gen_dir, 'testing', 'pixel') |
| 92 | if not os.path.exists(working_dir): |
| 93 | os.makedirs(working_dir) |
| 94 | |
Tom Sepez | a8fd0e8 | 2015-03-17 13:30:11 -0700 | [diff] [blame^] | 95 | os_exit_code = 0 |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 96 | input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]in$') |
| 97 | for input_filename in os.listdir(source_dir): |
| 98 | if input_file_re.match(input_filename): |
| 99 | input_path = os.path.join(source_dir, input_filename) |
| 100 | if os.path.isfile(input_path): |
Tom Sepez | a8fd0e8 | 2015-03-17 13:30:11 -0700 | [diff] [blame^] | 101 | if not generate_and_test(input_filename, source_dir, working_dir, |
| 102 | fixup_path, pdfium_test_path, pdfium_diff_path): |
| 103 | os_exit_code = 1 |
| 104 | |
| 105 | return os_exit_code |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 106 | |
| 107 | |
| 108 | if __name__ == '__main__': |
| 109 | sys.exit(main()) |