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