blob: a0cc946f1ad5377b4629255adaede150f57c45d1 [file] [log] [blame]
Tom Sepez30762ce2015-04-09 13:37:02 -07001#!/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 Zhaofba46da2015-11-23 16:50:49 -05006import glob
Tom Sepez30762ce2015-04-09 13:37:02 -07007import os
Lei Zhang6f3c2ff2015-10-09 13:49:17 -07008import subprocess
Tom Sepez30762ce2015-04-09 13:37:02 -07009import sys
10
11def 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
dsinclair3b5cb782016-04-28 06:17:40 -070021def RunCommand(cmd):
Lei Zhang6f3c2ff2015-10-09 13:49:17 -070022 try:
dsinclair3b5cb782016-04-28 06:17:40 -070023 subprocess.check_call(cmd)
Lei Zhang6f3c2ff2015-10-09 13:49:17 -070024 return None
25 except subprocess.CalledProcessError as e:
26 return e
27
stephanafa05e972017-01-02 06:19:41 -080028# 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.
34def 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 Zhaofba46da2015-11-23 16:50:49 -050046# Adjust Dr. Memory wrapper to have separate log directory for each test
47# for better error reporting.
48def 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 Zhang6f3c2ff2015-10-09 13:49:17 -070078
Tom Sepez30762ce2015-04-09 13:37:02 -070079class 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