Consolidate Python code to locate PDFium root
Consolidates Python code to locate the PDFium root directory into a
single shared module named "pdfium_root".
Bug: pdfium:1916
Change-Id: I45be2663d65a85c40610751c24d0be0ee37a57bf
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/99170
Auto-Submit: K. Moon <kmoon@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/testing/tools/common.py b/testing/tools/common.py
index 0aa42df..d83b945 100755
--- a/testing/tools/common.py
+++ b/testing/tools/common.py
@@ -12,6 +12,8 @@
import subprocess
import sys
+import pdfium_root
+
def os_name():
if sys.platform.startswith('linux'):
@@ -87,26 +89,12 @@
checkout or a chromium checkout of PDFium.'''
def __init__(self, build_location):
- # |build_location| is typically "out/Debug" or "out/Release".
- # Expect |my_dir| to be .../pdfium/testing/tools.
- self.my_dir = os.path.dirname(os.path.realpath(__file__))
- self.testing_dir = os.path.dirname(self.my_dir)
- if (os.path.basename(self.my_dir) != 'tools' or
- os.path.basename(self.testing_dir) != 'testing'):
- raise Exception('Confused, can not find pdfium root directory, aborting.')
- self.pdfium_dir = os.path.dirname(self.testing_dir)
- # Find path to build directory. This depends on whether this is a
- # standalone build vs. a build as part of a chromium checkout. For
- # standalone, we expect a path like .../pdfium/out/Debug, but for
- # chromium, we expect a path like .../src/out/Debug two levels
- # higher (to skip over the third_party/pdfium path component under
- # which chromium sticks pdfium).
- self.base_dir = self.pdfium_dir
- one_up_dir = os.path.dirname(self.base_dir)
- two_up_dir = os.path.dirname(one_up_dir)
- if (os.path.basename(two_up_dir) == 'src' and
- os.path.basename(one_up_dir) == 'third_party'):
- self.base_dir = two_up_dir
+ # `build_location` is typically "out/Debug" or "out/Release".
+ root_finder = pdfium_root.RootDirectoryFinder()
+ self.testing_dir = os.path.join(root_finder.pdfium_root, 'testing')
+ self.my_dir = os.path.join(self.testing_dir, 'tools')
+ self.pdfium_dir = root_finder.pdfium_root
+ self.base_dir = root_finder.source_root
self.build_dir = os.path.join(self.base_dir, build_location)
self.os_name = os_name()
diff --git a/testing/tools/pdfium_root.py b/testing/tools/pdfium_root.py
new file mode 100644
index 0000000..c4fe60f
--- /dev/null
+++ b/testing/tools/pdfium_root.py
@@ -0,0 +1,53 @@
+# Copyright 2022 The PDFium Authors
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+"""Utilities for working with the PDFium tree's root directory."""
+
+import os
+import sys
+
+
+class RootDirectoryFinder:
+ """Finds the PDFium tree's root directories.
+
+ The implementation expects that either:
+ 1. PDFium is a standalone checkout.
+ 2. PDFium is part of another tree within a "third_party/pdfium" directory.
+
+ Attributes:
+ pdfium_root: The path to the root of the PDFium tree.
+ source_root: The path to the root of the source tree. May differ from
+ `pdfium_root` if PDFium is a third-party dependency in another tree.
+ """
+
+ def __init__(self):
+ # Expect `self_dir` to be ".../testing/tools".
+ self_dir = os.path.dirname(os.path.realpath(__file__))
+
+ self.pdfium_root = _remove_path_suffix(self_dir, ('testing', 'tools'))
+ if not self.pdfium_root:
+ raise Exception('Cannot find testing/tools within PDFium root directory')
+
+ # In a Chromium checkout, expect `self.pdfium_root` to be
+ # ".../third_party/pdfium".
+ self.source_root = _remove_path_suffix(self.pdfium_root,
+ ('third_party', 'pdfium'))
+ if not self.source_root:
+ self.source_root = self.pdfium_root
+
+
+def _remove_path_suffix(path, expected_suffix):
+ for expected_part in reversed(expected_suffix):
+ if os.path.basename(path) != expected_part:
+ return None
+ path = os.path.dirname(path)
+ return path
+
+
+def add_source_directory_to_import_path(source_directory_path):
+ """Adds a source root-relative directory to the import path."""
+ root_finder = RootDirectoryFinder()
+ path = os.path.realpath(
+ os.path.join(root_finder.source_root, source_directory_path))
+ if path not in sys.path:
+ sys.path.insert(0, path)
diff --git a/testing/tools/skia_gold/__init__.py b/testing/tools/skia_gold/__init__.py
index 1c98a89..39381b9 100644
--- a/testing/tools/skia_gold/__init__.py
+++ b/testing/tools/skia_gold/__init__.py
@@ -1,3 +1,7 @@
-from .path_util import AddDirToPathIfNeeded, GetPDFiumDir
+# Copyright 2022 The PDFium Authors
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
-AddDirToPathIfNeeded(GetPDFiumDir(), 'build')
+import pdfium_root
+
+pdfium_root.add_source_directory_to_import_path('build')
diff --git a/testing/tools/skia_gold/path_util.py b/testing/tools/skia_gold/path_util.py
deleted file mode 100644
index e9fb04a..0000000
--- a/testing/tools/skia_gold/path_util.py
+++ /dev/null
@@ -1,31 +0,0 @@
-#!/usr/bin/env python
-# Copyright 2021 The PDFium Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style license that can be
-# found in the LICENSE file.
-
-import os
-import sys
-
-
-def AddDirToPathIfNeeded(*path_parts):
- # pylint: disable=no-value-for-parameter
- path = os.path.abspath(os.path.join(*path_parts))
- if os.path.isdir(path) and path not in sys.path:
- sys.path.append(path)
-
-
-def GetPDFiumDir():
- if not GetPDFiumDir.pdfium_dir:
- # Expect |skia_gold_dir| to be .../pdfium/testing/tools/skia_gold.
- skia_gold_dir = os.path.dirname(os.path.realpath(__file__))
- tools_dir = os.path.dirname(skia_gold_dir)
- testing_dir = os.path.dirname(tools_dir)
- if (os.path.basename(tools_dir) != 'tools' or
- os.path.basename(testing_dir) != 'testing'):
- raise RuntimeError(
- 'Confused, can not find pdfium root directory, aborting.')
- GetPDFiumDir.pdfium_dir = os.path.dirname(testing_dir)
- return GetPDFiumDir.pdfium_dir
-
-
-GetPDFiumDir.pdfium_dir = None
diff --git a/testing/tools/skia_gold/pdfium_skia_gold_properties.py b/testing/tools/skia_gold/pdfium_skia_gold_properties.py
index e658368..2b639eb 100644
--- a/testing/tools/skia_gold/pdfium_skia_gold_properties.py
+++ b/testing/tools/skia_gold/pdfium_skia_gold_properties.py
@@ -7,7 +7,7 @@
import subprocess
import sys
-from . import path_util
+import pdfium_root
from skia_gold_common import skia_gold_properties
@@ -15,10 +15,11 @@
@staticmethod
def _GetGitOriginMainHeadSha1():
+ root_finder = pdfium_root.RootDirectoryFinder()
try:
return subprocess.check_output(['git', 'rev-parse', 'origin/main'],
shell=_IsWin(),
- cwd=path_util.GetPDFiumDir()).strip()
+ cwd=root_finder.pdfium_root).strip()
except subprocess.CalledProcessError:
return None