Tom Sepez | 30762ce | 2015-04-09 13:37:02 -0700 | [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 | |
Qin Zhao | fba46da | 2015-11-23 16:50:49 -0500 | [diff] [blame] | 6 | import glob |
Tom Sepez | 30762ce | 2015-04-09 13:37:02 -0700 | [diff] [blame] | 7 | import os |
Lei Zhang | 6f3c2ff | 2015-10-09 13:49:17 -0700 | [diff] [blame] | 8 | import subprocess |
Tom Sepez | 30762ce | 2015-04-09 13:37:02 -0700 | [diff] [blame] | 9 | import sys |
| 10 | |
| 11 | def os_name(): |
| 12 | if sys.platform.startswith('linux'): |
| 13 | return 'linux' |
| 14 | if sys.platform.startswith('win'): |
| 15 | return 'win' |
| 16 | if sys.platform.startswith('darwin'): |
| 17 | return 'mac' |
| 18 | raise Exception('Confused, can not determine OS, aborting.') |
| 19 | |
| 20 | |
dsinclair | 3b5cb78 | 2016-04-28 06:17:40 -0700 | [diff] [blame] | 21 | def RunCommand(cmd): |
Lei Zhang | 6f3c2ff | 2015-10-09 13:49:17 -0700 | [diff] [blame] | 22 | try: |
dsinclair | 3b5cb78 | 2016-04-28 06:17:40 -0700 | [diff] [blame] | 23 | subprocess.check_call(cmd) |
Lei Zhang | 6f3c2ff | 2015-10-09 13:49:17 -0700 | [diff] [blame] | 24 | return None |
| 25 | except subprocess.CalledProcessError as e: |
| 26 | return e |
| 27 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame^] | 28 | # RunCommandExtractHashedFiles returns a tuple: (raised_exception, hashed_files) |
| 29 | # It runs the given command. If it fails it will return an exception and None. |
| 30 | # If it succeeds it will return None and the list of processed files extracted |
| 31 | # from the output of the command. It expects lines in this format: |
| 32 | # MD5:<path_to_image_file>:<md5_hash_in_hex> |
| 33 | # The returned hashed_files is a list of (file_path, MD5-hash) pairs. |
| 34 | def RunCommandExtractHashedFiles(cmd): |
| 35 | try: |
| 36 | output = subprocess.check_output(cmd, universal_newlines=True) |
| 37 | ret = [] |
| 38 | for line in output.split('\n'): |
| 39 | line = line.strip() |
| 40 | if line.startswith("MD5:"): |
| 41 | ret.append([x.strip() for x in line.lstrip("MD5:").rsplit(":", 1)]) |
| 42 | return None, ret |
| 43 | except subprocess.CalledProcessError as e: |
| 44 | return e, None |
| 45 | |
Qin Zhao | fba46da | 2015-11-23 16:50:49 -0500 | [diff] [blame] | 46 | # Adjust Dr. Memory wrapper to have separate log directory for each test |
| 47 | # for better error reporting. |
| 48 | def DrMemoryWrapper(wrapper, pdf_name): |
| 49 | if not wrapper: |
| 50 | return [] |
| 51 | # convert string to list |
| 52 | cmd_to_run = wrapper.split() |
| 53 | |
| 54 | # Do nothing if using default log directory. |
| 55 | if cmd_to_run.count("-logdir") == 0: |
| 56 | return cmd_to_run |
| 57 | # Usually, we pass "-logdir" "foo\bar\spam path" args to Dr. Memory. |
| 58 | # To group reports per test, we want to put the reports for each test into a |
| 59 | # separate directory. This code can be simplified when we have |
| 60 | # https://github.com/DynamoRIO/drmemory/issues/684 fixed. |
| 61 | logdir_idx = cmd_to_run.index("-logdir") |
| 62 | old_logdir = cmd_to_run[logdir_idx + 1] |
| 63 | wrapper_pid = str(os.getpid()) |
| 64 | |
| 65 | # We are using the same pid of the same python process, so append the number |
| 66 | # of entries in the logdir at the end of wrapper_pid to avoid conflict. |
| 67 | wrapper_pid += "_%d" % len(glob.glob(old_logdir + "\\*")) |
| 68 | |
| 69 | cmd_to_run[logdir_idx + 1] += "\\testcase.%s.logs" % wrapper_pid |
| 70 | os.makedirs(cmd_to_run[logdir_idx + 1]) |
| 71 | |
| 72 | f = open(old_logdir + "\\testcase.%s.name" % wrapper_pid, "w") |
| 73 | print >>f, pdf_name + ".pdf" |
| 74 | f.close() |
| 75 | |
| 76 | return cmd_to_run |
| 77 | |
Lei Zhang | 6f3c2ff | 2015-10-09 13:49:17 -0700 | [diff] [blame] | 78 | |
Tom Sepez | 30762ce | 2015-04-09 13:37:02 -0700 | [diff] [blame] | 79 | class DirectoryFinder: |
| 80 | '''A class for finding directories and paths under either a standalone |
| 81 | checkout or a chromium checkout of PDFium.''' |
| 82 | |
| 83 | def __init__(self, build_location): |
| 84 | # |build_location| is typically "out/Debug" or "out/Release". |
| 85 | # Expect |my_dir| to be .../pdfium/testing/tools. |
| 86 | self.my_dir = os.path.dirname(os.path.realpath(__file__)) |
| 87 | self.testing_dir = os.path.dirname(self.my_dir) |
| 88 | if (os.path.basename(self.my_dir) != 'tools' or |
| 89 | os.path.basename(self.testing_dir) != 'testing'): |
| 90 | raise Exception('Confused, can not find pdfium root directory, aborting.') |
| 91 | self.pdfium_dir = os.path.dirname(self.testing_dir) |
| 92 | # Find path to build directory. This depends on whether this is a |
| 93 | # standalone build vs. a build as part of a chromium checkout. For |
| 94 | # standalone, we expect a path like .../pdfium/out/Debug, but for |
| 95 | # chromium, we expect a path like .../src/out/Debug two levels |
| 96 | # higher (to skip over the third_party/pdfium path component under |
| 97 | # which chromium sticks pdfium). |
| 98 | self.base_dir = self.pdfium_dir |
| 99 | one_up_dir = os.path.dirname(self.base_dir) |
| 100 | two_up_dir = os.path.dirname(one_up_dir) |
| 101 | if (os.path.basename(two_up_dir) == 'src' and |
| 102 | os.path.basename(one_up_dir) == 'third_party'): |
| 103 | self.base_dir = two_up_dir |
| 104 | self.build_dir = os.path.join(self.base_dir, build_location) |
| 105 | self.os_name = os_name() |
| 106 | |
| 107 | def ExecutablePath(self, name): |
| 108 | '''Finds compiled binaries under the build path.''' |
| 109 | result = os.path.join(self.build_dir, name) |
| 110 | if self.os_name == 'win': |
| 111 | result = result + '.exe' |
| 112 | return result |
| 113 | |
| 114 | def ScriptPath(self, name): |
| 115 | '''Finds other scripts in the same directory as this one.''' |
| 116 | return os.path.join(self.my_dir, name) |
| 117 | |
| 118 | def WorkingDir(self, other_components=''): |
| 119 | '''Places generated files under the build directory, not source dir.''' |
| 120 | result = os.path.join(self.build_dir, 'gen', 'pdfium') |
| 121 | if other_components: |
| 122 | result = os.path.join(result, other_components) |
| 123 | return result |
| 124 | |
| 125 | def TestingDir(self, other_components=''): |
| 126 | '''Finds test files somewhere under the testing directory.''' |
| 127 | result = self.testing_dir |
| 128 | if other_components: |
| 129 | result = os.path.join(result, other_components) |
| 130 | return result |