From 9a5756805f2fe89fdadd03ee135106cb2ee4ae06 Mon Sep 17 00:00:00 2001 From: Alexis Lopez Zubieta Date: Wed, 22 Jun 2022 12:57:14 -0500 Subject: [PATCH] Remove unused libraries_patcher --- .../modules/setup/libraries_patcher.py | 73 ------------------- tests/modules/setup/test_libraries_patcher.py | 71 ------------------ 2 files changed, 144 deletions(-) delete mode 100644 appimagebuilder/modules/setup/libraries_patcher.py delete mode 100644 tests/modules/setup/test_libraries_patcher.py diff --git a/appimagebuilder/modules/setup/libraries_patcher.py b/appimagebuilder/modules/setup/libraries_patcher.py deleted file mode 100644 index ab83d330..00000000 --- a/appimagebuilder/modules/setup/libraries_patcher.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright 2022 Alexis Lopez Zubieta -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the "Software"), -# to deal in the Software without restriction, including without limitation the -# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -import logging -import pathlib - -from appimagebuilder.modules.setup.environment import Environment -from appimagebuilder.utils.finder import Finder -from appimagebuilder.utils.patchelf import PatchElf, PatchElfError - - -class LibrariesPatcher: - def __init__(self, appdir: pathlib.Path, env: Environment, finder: Finder = None): - self.appdir = appdir - self.env = env - self.finder = finder or Finder(appdir) - self.patchelf = PatchElf() - self.logger = logging.getLogger("BinaryPatching") - self.runtime_compat_dir = self.appdir / "runtime/compat" - - def patch_rpaths(self): - libraries = self.finder.find( - "*", - check_true=[Finder.is_elf], - check_false=[Finder.is_symlink], - ) - - for lib_path in libraries: - rpaths = self._resolve_rpaths(lib_path) - if rpaths: - self.logger.info(lib_path.relative_to(self.appdir)) - self.logger.info(" DT_RUNPATH: %s" % ":".join(rpaths)) - self.patchelf.set_rpath(lib_path, rpaths) - - def _resolve_rpaths(self, lib_path): - try: - needed = self.patchelf.get_needed(lib_path) - soname = self.patchelf.get_soname(lib_path) - if needed and not soname: - # libapprun_hooks.so is a runtime dependency of executables - needed.append("libapprun_hooks.so") - - rpaths = set() - for soname in needed: - needed_libs = self.finder.find("**/%s" % soname) - needed_libs_dirs = set([file.parent for file in needed_libs]) - for dir_path in needed_libs_dirs: - if "runtime" in dir_path.parts and "compat" in dir_path.parts: - rpath = dir_path.relative_to(self.runtime_compat_dir) - else: - rpath = self._rewrite_rpath_relative_to_origin( - dir_path, lib_path - ) - rpaths.add(str(rpath)) - return rpaths - except PatchElfError: - pass - - def _rewrite_rpath_relative_to_origin(self, rpath, origin): - rel_rpath = rpath.relative_to(self.appdir) - origin_rel_path = origin.relative_to(self.appdir) - nesting_deep = len(origin_rel_path.parts) - 1 - - new_rpath = "$ORIGIN/" + "../" * nesting_deep + str(rel_rpath) - return new_rpath diff --git a/tests/modules/setup/test_libraries_patcher.py b/tests/modules/setup/test_libraries_patcher.py deleted file mode 100644 index a9f7af43..00000000 --- a/tests/modules/setup/test_libraries_patcher.py +++ /dev/null @@ -1,71 +0,0 @@ -import pathlib -import re -import shutil -import subprocess -import tempfile -import unittest - -from appimagebuilder.modules.setup.environment import Environment -from appimagebuilder.modules.setup.libraries_patcher import LibrariesPatcher -from appimagebuilder.utils.patchelf import PatchElf - - -class TestLibrariesPatcher(unittest.TestCase): - def setUp(self) -> None: - self.test_appdir = tempfile.TemporaryDirectory(prefix="AppDir-") - self.test_appdir_path = pathlib.Path(self.test_appdir.name) - self.patcher = LibrariesPatcher(self.test_appdir_path, Environment()) - self._copy_sh() - - def _copy_sh(self): - bash_path = shutil.which("bash") - self.test_bin_path = pathlib.Path(self.test_appdir.name + bash_path) - self.test_bin_path.parent.mkdir(exist_ok=True, parents=True) - shutil.copy(bash_path, self.test_bin_path) - - process = subprocess.run(["ldd", bash_path], capture_output=True) - output = process.stdout.decode() - match_lib_paths_from_ldd_output = re.compile(r"=> (/.*) \(.*") - lib_paths = match_lib_paths_from_ldd_output.findall(output) - for path in lib_paths: - path = pathlib.Path(path) - if path.match("libc.*"): - target_path = pathlib.Path( - self.test_appdir.name + "/runtime/compat" + str(path) - ) - self.libc_dir_prefix = path.parent - - # create fake libapprun_hooks - fake_libapprun_hooks = pathlib.Path( - self.test_appdir.name + "/lib/libapprun_hooks.so" - ) - fake_libapprun_hooks.parent.mkdir(exist_ok=True, parents=True) - shutil.copy(path, fake_libapprun_hooks) - else: - target_path = pathlib.Path(self.test_appdir.name + str(path)) - - target_path.parent.mkdir(exist_ok=True, parents=True) - shutil.copy(path, target_path) - - def tearDown(self) -> None: - self.test_appdir.cleanup() - - def test_patch_rpaths(self): - self.patcher.patch_rpaths() - - patchelf = PatchElf() - rpath = patchelf.get_rpath(self.test_bin_path) - - bin_depth = len(self.test_bin_path.relative_to(self.test_appdir_path).parts) - 1 - # assert libs paths were included - self.assertIn("$ORIGIN" + "/.." * bin_depth + str(self.libc_dir_prefix), rpath) - - # assert libapprun_hooks.so path was included - self.assertIn("$ORIGIN" + "/.." * bin_depth + "/lib", rpath) - - # assert libc path was included relative to the current working dir - self.assertIn(str(self.libc_dir_prefix)[1:], rpath) - - -if __name__ == "__main__": - unittest.main()