Tom Sepez | 30762ce | 2015-04-09 13:37:02 -0700 | [diff] [blame] | 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 | |
Qin Zhao | fba46da | 2015-11-23 16:50:49 -0500 | [diff] [blame] | 6 | import glob |
Tom Sepez | 30762ce | 2015-04-09 13:37:02 -0700 | [diff] [blame] | 7 | import os |
Henrique Nakashima | f24fc1e | 2017-08-03 13:29:22 -0400 | [diff] [blame^] | 8 | import re |
Lei Zhang | 6f3c2ff | 2015-10-09 13:49:17 -0700 | [diff] [blame] | 9 | import subprocess |
Tom Sepez | 30762ce | 2015-04-09 13:37:02 -0700 | [diff] [blame] | 10 | import sys |
| 11 | |
| 12 | def os_name(): |
| 13 | if sys.platform.startswith('linux'): |
| 14 | return 'linux' |
| 15 | if sys.platform.startswith('win'): |
| 16 | return 'win' |
| 17 | if sys.platform.startswith('darwin'): |
| 18 | return 'mac' |
| 19 | raise Exception('Confused, can not determine OS, aborting.') |
| 20 | |
| 21 | |
dsinclair | 3b5cb78 | 2016-04-28 06:17:40 -0700 | [diff] [blame] | 22 | def RunCommand(cmd): |
Lei Zhang | 6f3c2ff | 2015-10-09 13:49:17 -0700 | [diff] [blame] | 23 | try: |
dsinclair | 3b5cb78 | 2016-04-28 06:17:40 -0700 | [diff] [blame] | 24 | subprocess.check_call(cmd) |
Lei Zhang | 6f3c2ff | 2015-10-09 13:49:17 -0700 | [diff] [blame] | 25 | return None |
| 26 | except subprocess.CalledProcessError as e: |
| 27 | return e |
| 28 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 29 | # RunCommandExtractHashedFiles returns a tuple: (raised_exception, hashed_files) |
| 30 | # It runs the given command. If it fails it will return an exception and None. |
| 31 | # If it succeeds it will return None and the list of processed files extracted |
| 32 | # from the output of the command. It expects lines in this format: |
| 33 | # MD5:<path_to_image_file>:<md5_hash_in_hex> |
| 34 | # The returned hashed_files is a list of (file_path, MD5-hash) pairs. |
| 35 | def RunCommandExtractHashedFiles(cmd): |
| 36 | try: |
| 37 | output = subprocess.check_output(cmd, universal_newlines=True) |
| 38 | ret = [] |
| 39 | for line in output.split('\n'): |
| 40 | line = line.strip() |
| 41 | if line.startswith("MD5:"): |
| 42 | ret.append([x.strip() for x in line.lstrip("MD5:").rsplit(":", 1)]) |
| 43 | return None, ret |
| 44 | except subprocess.CalledProcessError as e: |
| 45 | return e, None |
| 46 | |
Lei Zhang | 6f3c2ff | 2015-10-09 13:49:17 -0700 | [diff] [blame] | 47 | |
Tom Sepez | 30762ce | 2015-04-09 13:37:02 -0700 | [diff] [blame] | 48 | class DirectoryFinder: |
| 49 | '''A class for finding directories and paths under either a standalone |
| 50 | checkout or a chromium checkout of PDFium.''' |
| 51 | |
| 52 | def __init__(self, build_location): |
| 53 | # |build_location| is typically "out/Debug" or "out/Release". |
| 54 | # Expect |my_dir| to be .../pdfium/testing/tools. |
| 55 | self.my_dir = os.path.dirname(os.path.realpath(__file__)) |
| 56 | self.testing_dir = os.path.dirname(self.my_dir) |
| 57 | if (os.path.basename(self.my_dir) != 'tools' or |
| 58 | os.path.basename(self.testing_dir) != 'testing'): |
| 59 | raise Exception('Confused, can not find pdfium root directory, aborting.') |
| 60 | self.pdfium_dir = os.path.dirname(self.testing_dir) |
| 61 | # Find path to build directory. This depends on whether this is a |
| 62 | # standalone build vs. a build as part of a chromium checkout. For |
| 63 | # standalone, we expect a path like .../pdfium/out/Debug, but for |
| 64 | # chromium, we expect a path like .../src/out/Debug two levels |
| 65 | # higher (to skip over the third_party/pdfium path component under |
| 66 | # which chromium sticks pdfium). |
| 67 | self.base_dir = self.pdfium_dir |
| 68 | one_up_dir = os.path.dirname(self.base_dir) |
| 69 | two_up_dir = os.path.dirname(one_up_dir) |
| 70 | if (os.path.basename(two_up_dir) == 'src' and |
| 71 | os.path.basename(one_up_dir) == 'third_party'): |
| 72 | self.base_dir = two_up_dir |
| 73 | self.build_dir = os.path.join(self.base_dir, build_location) |
| 74 | self.os_name = os_name() |
| 75 | |
| 76 | def ExecutablePath(self, name): |
| 77 | '''Finds compiled binaries under the build path.''' |
| 78 | result = os.path.join(self.build_dir, name) |
| 79 | if self.os_name == 'win': |
| 80 | result = result + '.exe' |
| 81 | return result |
| 82 | |
| 83 | def ScriptPath(self, name): |
| 84 | '''Finds other scripts in the same directory as this one.''' |
| 85 | return os.path.join(self.my_dir, name) |
| 86 | |
| 87 | def WorkingDir(self, other_components=''): |
| 88 | '''Places generated files under the build directory, not source dir.''' |
| 89 | result = os.path.join(self.build_dir, 'gen', 'pdfium') |
| 90 | if other_components: |
| 91 | result = os.path.join(result, other_components) |
| 92 | return result |
| 93 | |
| 94 | def TestingDir(self, other_components=''): |
| 95 | '''Finds test files somewhere under the testing directory.''' |
| 96 | result = self.testing_dir |
| 97 | if other_components: |
| 98 | result = os.path.join(result, other_components) |
| 99 | return result |
Henrique Nakashima | f24fc1e | 2017-08-03 13:29:22 -0400 | [diff] [blame^] | 100 | |
| 101 | |
| 102 | def GetBooleanGnArg(arg_name, build_dir): |
| 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']) |
| 108 | os.chdir(cwd) |
| 109 | arg_match_output = re.search('%s = (.*)' % arg_name, gn_args_output).group(1) |
| 110 | return arg_match_output == 'true' |