1#!/usr/bin/env python3 2# Copyright 2015 The PDFium Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import datetime 7import glob 8import os 9import re 10import subprocess 11import sys 12 13import pdfium_root 14 15 16def os_name(): 17 if sys.platform.startswith('linux'): 18 return 'linux' 19 if sys.platform.startswith('win'): 20 return 'win' 21 if sys.platform.startswith('darwin'): 22 return 'mac' 23 raise Exception('Confused, can not determine OS, aborting.') 24 25 26def RunCommandPropagateErr(cmd, 27 stdout_has_errors=False, 28 exit_status_on_error=None): 29 """Run a command as a subprocess. 30 31 Errors in that subprocess are printed out if it returns an error exit code. 32 33 Args: 34 cmd: Command to run as a list of strings. 35 stdout_has_errors: Whether to print stdout instead of stderr on an error 36 exit. 37 exit_status_on_error: If specified, upon an error in the subprocess the 38 caller script exits immediately with the given status. 39 """ 40 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 41 output, err = p.communicate() 42 43 if p.returncode: 44 PrintErr('\nError when invoking "%s"' % ' '.join(cmd)) 45 if stdout_has_errors: 46 PrintErr(output) 47 48 PrintErr(err) 49 50 if exit_status_on_error is not None: 51 sys.exit(exit_status_on_error) 52 53 return None 54 55 return output 56 57 58class DirectoryFinder: 59 '''A class for finding directories and paths under either a standalone 60 checkout or a chromium checkout of PDFium.''' 61 62 def __init__(self, build_location): 63 # `build_location` is typically "out/Debug" or "out/Release". 64 root_finder = pdfium_root.RootDirectoryFinder() 65 self.testing_dir = os.path.join(root_finder.pdfium_root, 'testing') 66 self.my_dir = os.path.join(self.testing_dir, 'tools') 67 self.pdfium_dir = root_finder.pdfium_root 68 self.base_dir = root_finder.source_root 69 self.build_dir = os.path.join(self.base_dir, build_location) 70 self.os_name = os_name() 71 72 def ExecutablePath(self, name): 73 '''Finds compiled binaries under the build path.''' 74 result = os.path.join(self.build_dir, name) 75 if self.os_name == 'win': 76 result = result + '.exe' 77 return result 78 79 def ScriptPath(self, name): 80 '''Finds other scripts in the same directory as this one.''' 81 return os.path.join(self.my_dir, name) 82 83 def WorkingDir(self, other_components=''): 84 '''Places generated files under the build directory, not source dir.''' 85 result = os.path.join(self.build_dir, 'gen', 'pdfium') 86 if other_components: 87 result = os.path.join(result, other_components) 88 return result 89 90 def TestingDir(self, other_components=''): 91 '''Finds test files somewhere under the testing directory.''' 92 result = self.testing_dir 93 if other_components: 94 result = os.path.join(result, other_components) 95 return result 96 97 def ThirdPartyFontsDir(self): 98 '''Finds directory with the test fonts.''' 99 return os.path.join(self.base_dir, 'third_party', 'test_fonts') 100 101 102def GetBooleanGnArg(arg_name, build_dir, verbose=False): 103 '''Extract the value of a boolean flag in args.gn''' 104 cwd = os.getcwd() 105 os.chdir(build_dir) 106 gn_args_output = subprocess.check_output( 107 ['gn', 'args', '.', '--list=%s' % arg_name, '--short']).decode('utf8') 108 os.chdir(cwd) 109 arg_match_output = re.search('%s = (.*)' % arg_name, gn_args_output).group(1) 110 if verbose: 111 print( 112 "Found '%s' for value of %s" % (arg_match_output, arg_name), 113 file=sys.stderr) 114 return arg_match_output == 'true' 115 116 117def PrintWithTime(s): 118 """Prints s prepended by a timestamp.""" 119 print('[%s] %s' % (datetime.datetime.now().strftime("%Y%m%d %H:%M:%S"), s)) 120 121 122def PrintErr(s): 123 """Prints s to stderr.""" 124 print(s, file=sys.stderr) 125