summaryrefslogtreecommitdiff
path: root/testing/tools/common.py
blob: ce292bf41fae47ce586dc2258cd4e8b6d716c619 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
# Copyright 2015 The PDFium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import glob
import os
import subprocess
import sys

def os_name():
  if sys.platform.startswith('linux'):
    return 'linux'
  if sys.platform.startswith('win'):
    return 'win'
  if sys.platform.startswith('darwin'):
    return 'mac'
  raise Exception('Confused, can not determine OS, aborting.')


def RunCommandToFile(cmd, file):
  try:
    subprocess.check_call(cmd, stdout=file)
    return None
  except subprocess.CalledProcessError as e:
    return e

def RunCommand(cmd, redirect_output=False):
  try:
    if redirect_output:
      sys.stdout.write(subprocess.check_output(cmd, stderr=subprocess.STDOUT))
    else:
      subprocess.check_call(cmd)
    return None
  except subprocess.CalledProcessError as e:
    return e


# Adjust Dr. Memory wrapper to have separate log directory for each test
# for better error reporting.
def DrMemoryWrapper(wrapper, pdf_name):
  if not wrapper:
    return []
  # convert string to list
  cmd_to_run = wrapper.split()

  # Do nothing if using default log directory.
  if cmd_to_run.count("-logdir") == 0:
    return cmd_to_run
  # Usually, we pass "-logdir" "foo\bar\spam path" args to Dr. Memory.
  # To group reports per test, we want to put the reports for each test into a
  # separate directory. This code can be simplified when we have
  # https://github.com/DynamoRIO/drmemory/issues/684 fixed.
  logdir_idx = cmd_to_run.index("-logdir")
  old_logdir = cmd_to_run[logdir_idx + 1]
  wrapper_pid = str(os.getpid())

  # We are using the same pid of the same python process, so append the number
  # of entries in the logdir at the end of wrapper_pid to avoid conflict.
  wrapper_pid += "_%d" % len(glob.glob(old_logdir + "\\*"))

  cmd_to_run[logdir_idx + 1] += "\\testcase.%s.logs" % wrapper_pid
  os.makedirs(cmd_to_run[logdir_idx + 1])

  f = open(old_logdir + "\\testcase.%s.name" % wrapper_pid, "w")
  print >>f, pdf_name + ".pdf"
  f.close()

  return cmd_to_run


class DirectoryFinder:
  '''A class for finding directories and paths under either a standalone
  checkout or a chromium checkout of PDFium.'''

  def __init__(self, build_location):
    # |build_location| is typically "out/Debug" or "out/Release".
    # Expect |my_dir| to be .../pdfium/testing/tools.
    self.my_dir = os.path.dirname(os.path.realpath(__file__))
    self.testing_dir = os.path.dirname(self.my_dir)
    if (os.path.basename(self.my_dir) != 'tools' or
        os.path.basename(self.testing_dir) != 'testing'):
      raise Exception('Confused, can not find pdfium root directory, aborting.')
    self.pdfium_dir = os.path.dirname(self.testing_dir)
    # Find path to build directory.  This depends on whether this is a
    # standalone build vs. a build as part of a chromium checkout. For
    # standalone, we expect a path like .../pdfium/out/Debug, but for
    # chromium, we expect a path like .../src/out/Debug two levels
    # higher (to skip over the third_party/pdfium path component under
    # which chromium sticks pdfium).
    self.base_dir = self.pdfium_dir
    one_up_dir = os.path.dirname(self.base_dir)
    two_up_dir = os.path.dirname(one_up_dir)
    if (os.path.basename(two_up_dir) == 'src' and
        os.path.basename(one_up_dir) == 'third_party'):
      self.base_dir = two_up_dir
    self.build_dir = os.path.join(self.base_dir, build_location)
    self.os_name = os_name()

  def ExecutablePath(self, name):
    '''Finds compiled binaries under the build path.'''
    result = os.path.join(self.build_dir, name)
    if self.os_name == 'win':
      result = result + '.exe'
    return result

  def ScriptPath(self, name):
    '''Finds other scripts in the same directory as this one.'''
    return os.path.join(self.my_dir, name)

  def WorkingDir(self, other_components=''):
    '''Places generated files under the build directory, not source dir.'''
    result = os.path.join(self.build_dir, 'gen', 'pdfium')
    if other_components:
      result = os.path.join(result, other_components)
    return result

  def TestingDir(self, other_components=''):
    '''Finds test files somewhere under the testing directory.'''
    result = self.testing_dir
    if other_components:
      result = os.path.join(result, other_components)
    return result