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 | |
| 6 | import os |
Lei Zhang | 6f3c2ff | 2015-10-09 13:49:17 -0700 | [diff] [blame^] | 7 | import subprocess |
Tom Sepez | 30762ce | 2015-04-09 13:37:02 -0700 | [diff] [blame] | 8 | import sys |
| 9 | |
| 10 | def os_name(): |
| 11 | if sys.platform.startswith('linux'): |
| 12 | return 'linux' |
| 13 | if sys.platform.startswith('win'): |
| 14 | return 'win' |
| 15 | if sys.platform.startswith('darwin'): |
| 16 | return 'mac' |
| 17 | raise Exception('Confused, can not determine OS, aborting.') |
| 18 | |
| 19 | |
Lei Zhang | 6f3c2ff | 2015-10-09 13:49:17 -0700 | [diff] [blame^] | 20 | def RunCommand(cmd, redirect_output=False): |
| 21 | try: |
| 22 | if redirect_output: |
| 23 | sys.stdout.write(subprocess.check_output(cmd, stderr=subprocess.STDOUT)) |
| 24 | else: |
| 25 | subprocess.check_call(cmd) |
| 26 | return None |
| 27 | except subprocess.CalledProcessError as e: |
| 28 | return e |
| 29 | |
| 30 | |
Tom Sepez | 30762ce | 2015-04-09 13:37:02 -0700 | [diff] [blame] | 31 | class DirectoryFinder: |
| 32 | '''A class for finding directories and paths under either a standalone |
| 33 | checkout or a chromium checkout of PDFium.''' |
| 34 | |
| 35 | def __init__(self, build_location): |
| 36 | # |build_location| is typically "out/Debug" or "out/Release". |
| 37 | # Expect |my_dir| to be .../pdfium/testing/tools. |
| 38 | self.my_dir = os.path.dirname(os.path.realpath(__file__)) |
| 39 | self.testing_dir = os.path.dirname(self.my_dir) |
| 40 | if (os.path.basename(self.my_dir) != 'tools' or |
| 41 | os.path.basename(self.testing_dir) != 'testing'): |
| 42 | raise Exception('Confused, can not find pdfium root directory, aborting.') |
| 43 | self.pdfium_dir = os.path.dirname(self.testing_dir) |
| 44 | # Find path to build directory. This depends on whether this is a |
| 45 | # standalone build vs. a build as part of a chromium checkout. For |
| 46 | # standalone, we expect a path like .../pdfium/out/Debug, but for |
| 47 | # chromium, we expect a path like .../src/out/Debug two levels |
| 48 | # higher (to skip over the third_party/pdfium path component under |
| 49 | # which chromium sticks pdfium). |
| 50 | self.base_dir = self.pdfium_dir |
| 51 | one_up_dir = os.path.dirname(self.base_dir) |
| 52 | two_up_dir = os.path.dirname(one_up_dir) |
| 53 | if (os.path.basename(two_up_dir) == 'src' and |
| 54 | os.path.basename(one_up_dir) == 'third_party'): |
| 55 | self.base_dir = two_up_dir |
| 56 | self.build_dir = os.path.join(self.base_dir, build_location) |
| 57 | self.os_name = os_name() |
| 58 | |
| 59 | def ExecutablePath(self, name): |
| 60 | '''Finds compiled binaries under the build path.''' |
| 61 | result = os.path.join(self.build_dir, name) |
| 62 | if self.os_name == 'win': |
| 63 | result = result + '.exe' |
| 64 | return result |
| 65 | |
| 66 | def ScriptPath(self, name): |
| 67 | '''Finds other scripts in the same directory as this one.''' |
| 68 | return os.path.join(self.my_dir, name) |
| 69 | |
| 70 | def WorkingDir(self, other_components=''): |
| 71 | '''Places generated files under the build directory, not source dir.''' |
| 72 | result = os.path.join(self.build_dir, 'gen', 'pdfium') |
| 73 | if other_components: |
| 74 | result = os.path.join(result, other_components) |
| 75 | return result |
| 76 | |
| 77 | def TestingDir(self, other_components=''): |
| 78 | '''Finds test files somewhere under the testing directory.''' |
| 79 | result = self.testing_dir |
| 80 | if other_components: |
| 81 | result = os.path.join(result, other_components) |
| 82 | return result |