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