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 6import glob 7import os 8import subprocess 9import 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 21def RunCommand(cmd): 22 try: 23 subprocess.check_call(cmd) 24 return None 25 except subprocess.CalledProcessError as e: 26 return e 27 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. 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 46 47class DirectoryFinder: 48 '''A class for finding directories and paths under either a standalone 49 checkout or a chromium checkout of PDFium.''' 50 51 def __init__(self, build_location): 52 # |build_location| is typically "out/Debug" or "out/Release". 53 # Expect |my_dir| to be .../pdfium/testing/tools. 54 self.my_dir = os.path.dirname(os.path.realpath(__file__)) 55 self.testing_dir = os.path.dirname(self.my_dir) 56 if (os.path.basename(self.my_dir) != 'tools' or 57 os.path.basename(self.testing_dir) != 'testing'): 58 raise Exception('Confused, can not find pdfium root directory, aborting.') 59 self.pdfium_dir = os.path.dirname(self.testing_dir) 60 # Find path to build directory. This depends on whether this is a 61 # standalone build vs. a build as part of a chromium checkout. For 62 # standalone, we expect a path like .../pdfium/out/Debug, but for 63 # chromium, we expect a path like .../src/out/Debug two levels 64 # higher (to skip over the third_party/pdfium path component under 65 # which chromium sticks pdfium). 66 self.base_dir = self.pdfium_dir 67 one_up_dir = os.path.dirname(self.base_dir) 68 two_up_dir = os.path.dirname(one_up_dir) 69 if (os.path.basename(two_up_dir) == 'src' and 70 os.path.basename(one_up_dir) == 'third_party'): 71 self.base_dir = two_up_dir 72 self.build_dir = os.path.join(self.base_dir, build_location) 73 self.os_name = os_name() 74 75 def ExecutablePath(self, name): 76 '''Finds compiled binaries under the build path.''' 77 result = os.path.join(self.build_dir, name) 78 if self.os_name == 'win': 79 result = result + '.exe' 80 return result 81 82 def ScriptPath(self, name): 83 '''Finds other scripts in the same directory as this one.''' 84 return os.path.join(self.my_dir, name) 85 86 def WorkingDir(self, other_components=''): 87 '''Places generated files under the build directory, not source dir.''' 88 result = os.path.join(self.build_dir, 'gen', 'pdfium') 89 if other_components: 90 result = os.path.join(result, other_components) 91 return result 92 93 def TestingDir(self, other_components=''): 94 '''Finds test files somewhere under the testing directory.''' 95 result = self.testing_dir 96 if other_components: 97 result = os.path.join(result, other_components) 98 return result 99