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 | |
Qin Zhao | fba46da | 2015-11-23 16:50:49 -0500 | [diff] [blame] | 28 | # Adjust Dr. Memory wrapper to have separate log directory for each test |
| 29 | # for better error reporting. |
| 30 | def DrMemoryWrapper(wrapper, pdf_name): |
| 31 | if not wrapper: |
| 32 | return [] |
| 33 | # convert string to list |
| 34 | cmd_to_run = wrapper.split() |
| 35 | |
| 36 | # Do nothing if using default log directory. |
| 37 | if cmd_to_run.count("-logdir") == 0: |
| 38 | return cmd_to_run |
| 39 | # Usually, we pass "-logdir" "foo\bar\spam path" args to Dr. Memory. |
| 40 | # To group reports per test, we want to put the reports for each test into a |
| 41 | # separate directory. This code can be simplified when we have |
| 42 | # https://github.com/DynamoRIO/drmemory/issues/684 fixed. |
| 43 | logdir_idx = cmd_to_run.index("-logdir") |
| 44 | old_logdir = cmd_to_run[logdir_idx + 1] |
| 45 | wrapper_pid = str(os.getpid()) |
| 46 | |
| 47 | # We are using the same pid of the same python process, so append the number |
| 48 | # of entries in the logdir at the end of wrapper_pid to avoid conflict. |
| 49 | wrapper_pid += "_%d" % len(glob.glob(old_logdir + "\\*")) |
| 50 | |
| 51 | cmd_to_run[logdir_idx + 1] += "\\testcase.%s.logs" % wrapper_pid |
| 52 | os.makedirs(cmd_to_run[logdir_idx + 1]) |
| 53 | |
| 54 | f = open(old_logdir + "\\testcase.%s.name" % wrapper_pid, "w") |
| 55 | print >>f, pdf_name + ".pdf" |
| 56 | f.close() |
| 57 | |
| 58 | return cmd_to_run |
| 59 | |
Lei Zhang | 6f3c2ff | 2015-10-09 13:49:17 -0700 | [diff] [blame] | 60 | |
Tom Sepez | 30762ce | 2015-04-09 13:37:02 -0700 | [diff] [blame] | 61 | class DirectoryFinder: |
| 62 | '''A class for finding directories and paths under either a standalone |
| 63 | checkout or a chromium checkout of PDFium.''' |
| 64 | |
| 65 | def __init__(self, build_location): |
| 66 | # |build_location| is typically "out/Debug" or "out/Release". |
| 67 | # Expect |my_dir| to be .../pdfium/testing/tools. |
| 68 | self.my_dir = os.path.dirname(os.path.realpath(__file__)) |
| 69 | self.testing_dir = os.path.dirname(self.my_dir) |
| 70 | if (os.path.basename(self.my_dir) != 'tools' or |
| 71 | os.path.basename(self.testing_dir) != 'testing'): |
| 72 | raise Exception('Confused, can not find pdfium root directory, aborting.') |
| 73 | self.pdfium_dir = os.path.dirname(self.testing_dir) |
| 74 | # Find path to build directory. This depends on whether this is a |
| 75 | # standalone build vs. a build as part of a chromium checkout. For |
| 76 | # standalone, we expect a path like .../pdfium/out/Debug, but for |
| 77 | # chromium, we expect a path like .../src/out/Debug two levels |
| 78 | # higher (to skip over the third_party/pdfium path component under |
| 79 | # which chromium sticks pdfium). |
| 80 | self.base_dir = self.pdfium_dir |
| 81 | one_up_dir = os.path.dirname(self.base_dir) |
| 82 | two_up_dir = os.path.dirname(one_up_dir) |
| 83 | if (os.path.basename(two_up_dir) == 'src' and |
| 84 | os.path.basename(one_up_dir) == 'third_party'): |
| 85 | self.base_dir = two_up_dir |
| 86 | self.build_dir = os.path.join(self.base_dir, build_location) |
| 87 | self.os_name = os_name() |
| 88 | |
| 89 | def ExecutablePath(self, name): |
| 90 | '''Finds compiled binaries under the build path.''' |
| 91 | result = os.path.join(self.build_dir, name) |
| 92 | if self.os_name == 'win': |
| 93 | result = result + '.exe' |
| 94 | return result |
| 95 | |
| 96 | def ScriptPath(self, name): |
| 97 | '''Finds other scripts in the same directory as this one.''' |
| 98 | return os.path.join(self.my_dir, name) |
| 99 | |
| 100 | def WorkingDir(self, other_components=''): |
| 101 | '''Places generated files under the build directory, not source dir.''' |
| 102 | result = os.path.join(self.build_dir, 'gen', 'pdfium') |
| 103 | if other_components: |
| 104 | result = os.path.join(result, other_components) |
| 105 | return result |
| 106 | |
| 107 | def TestingDir(self, other_components=''): |
| 108 | '''Finds test files somewhere under the testing directory.''' |
| 109 | result = self.testing_dir |
| 110 | if other_components: |
| 111 | result = os.path.join(result, other_components) |
| 112 | return result |