mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-05-05 12:43:49 +00:00
remove excess dependencies leaves
This commit is contained in:
parent
9bbbd9da2e
commit
00f133689f
@ -80,7 +80,7 @@ class Executor(PackageInfo, Cleaner):
|
|||||||
# clear changes and update commit hash
|
# clear changes and update commit hash
|
||||||
self.reporter.package_changes_update(single.base, Changes(last_commit_sha))
|
self.reporter.package_changes_update(single.base, Changes(last_commit_sha))
|
||||||
# update dependencies list
|
# update dependencies list
|
||||||
dependencies = PackageArchive(self.paths.build_directory, single).depends_on()
|
dependencies = PackageArchive(self.paths.build_directory, single, self.pacman).depends_on()
|
||||||
self.reporter.package_dependencies_update(single.base, dependencies)
|
self.reporter.package_dependencies_update(single.base, dependencies)
|
||||||
# update result set
|
# update result set
|
||||||
result.add_updated(single)
|
result.add_updated(single)
|
||||||
|
@ -57,6 +57,7 @@ class AURPackage:
|
|||||||
provides(list[str]): list of packages which this package provides
|
provides(list[str]): list of packages which this package provides
|
||||||
license(list[str]): list of package licenses
|
license(list[str]): list of package licenses
|
||||||
keywords(list[str]): list of package keywords
|
keywords(list[str]): list of package keywords
|
||||||
|
groups(list[str]): list of package groups
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
Mainly this class must be used from class methods instead of default :func:`__init__()`::
|
Mainly this class must be used from class methods instead of default :func:`__init__()`::
|
||||||
@ -100,6 +101,7 @@ class AURPackage:
|
|||||||
provides: list[str] = field(default_factory=list)
|
provides: list[str] = field(default_factory=list)
|
||||||
license: list[str] = field(default_factory=list)
|
license: list[str] = field(default_factory=list)
|
||||||
keywords: list[str] = field(default_factory=list)
|
keywords: list[str] = field(default_factory=list)
|
||||||
|
groups: list[str] = field(default_factory=list)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_json(cls, dump: dict[str, Any]) -> Self:
|
def from_json(cls, dump: dict[str, Any]) -> Self:
|
||||||
@ -153,6 +155,7 @@ class AURPackage:
|
|||||||
provides=package.provides,
|
provides=package.provides,
|
||||||
license=package.licenses,
|
license=package.licenses,
|
||||||
keywords=[],
|
keywords=[],
|
||||||
|
groups=package.groups,
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -191,6 +194,7 @@ class AURPackage:
|
|||||||
provides=dump["provides"],
|
provides=dump["provides"],
|
||||||
license=dump["licenses"],
|
license=dump["licenses"],
|
||||||
keywords=[],
|
keywords=[],
|
||||||
|
groups=dump["groups"],
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -34,6 +34,13 @@ class Dependencies:
|
|||||||
|
|
||||||
paths: dict[str, list[str]] = field(default_factory=dict)
|
paths: dict[str, list[str]] = field(default_factory=dict)
|
||||||
|
|
||||||
|
def __post_init__(self) -> None:
|
||||||
|
"""
|
||||||
|
remove empty paths
|
||||||
|
"""
|
||||||
|
paths = {path: packages for path, packages in self.paths.items() if packages}
|
||||||
|
object.__setattr__(self, "paths", paths)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_json(cls, dump: dict[str, Any]) -> Self:
|
def from_json(cls, dump: dict[str, Any]) -> Self:
|
||||||
"""
|
"""
|
||||||
|
71
src/ahriman/models/filesystem_package.py
Normal file
71
src/ahriman/models/filesystem_package.py
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
#
|
||||||
|
# Copyright (c) 2021-2024 ahriman team.
|
||||||
|
#
|
||||||
|
# This file is part of ahriman
|
||||||
|
# (see https://github.com/arcan1s/ahriman).
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from ahriman.core.util import trim_package
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, kw_only=True)
|
||||||
|
class FilesystemPackage:
|
||||||
|
"""
|
||||||
|
class representing a simplified model for the package installed to filesystem
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
package_name(str): package name
|
||||||
|
depends(set[str]): list of package dependencies
|
||||||
|
directories(set[Path]): list of directories this package contains
|
||||||
|
files(list[Path]): list of files this package contains
|
||||||
|
opt_depends(set[str]): list of package optional dependencies
|
||||||
|
"""
|
||||||
|
|
||||||
|
package_name: str
|
||||||
|
depends: set[str]
|
||||||
|
opt_depends: set[str]
|
||||||
|
directories: list[Path] = field(default_factory=list)
|
||||||
|
files: list[Path] = field(default_factory=list)
|
||||||
|
|
||||||
|
def __post_init__(self) -> None:
|
||||||
|
"""
|
||||||
|
update dependencies list accordingly
|
||||||
|
"""
|
||||||
|
object.__setattr__(self, "depends", {trim_package(package) for package in self.depends})
|
||||||
|
object.__setattr__(self, "opt_depends", {trim_package(package) for package in self.opt_depends})
|
||||||
|
|
||||||
|
def depends_on(self, package_name: str) -> bool:
|
||||||
|
"""
|
||||||
|
check if package depends on given package name
|
||||||
|
|
||||||
|
Args:
|
||||||
|
package_name(str): package name to check dependencies
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True in case if the given package in the dependencies or optional dependencies lists
|
||||||
|
"""
|
||||||
|
return package_name in self.depends or package_name in self.opt_depends
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
"""
|
||||||
|
generate string representation of object
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: unique string representation
|
||||||
|
"""
|
||||||
|
return f'FilesystemPackage(package_name={self.package_name}, depends={self.depends})'
|
@ -23,8 +23,12 @@ from elftools.elf.elffile import ELFFile
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import IO
|
from typing import IO
|
||||||
|
|
||||||
|
from ahriman.core.alpm.pacman import Pacman
|
||||||
|
from ahriman.core.alpm.remote import OfficialSyncdb
|
||||||
|
from ahriman.core.exceptions import UnknownPackageError
|
||||||
from ahriman.core.util import walk
|
from ahriman.core.util import walk
|
||||||
from ahriman.models.dependencies import Dependencies
|
from ahriman.models.dependencies import Dependencies
|
||||||
|
from ahriman.models.filesystem_package import FilesystemPackage
|
||||||
from ahriman.models.package import Package
|
from ahriman.models.package import Package
|
||||||
|
|
||||||
|
|
||||||
@ -40,6 +44,7 @@ class PackageArchive:
|
|||||||
|
|
||||||
root: Path
|
root: Path
|
||||||
package: Package
|
package: Package
|
||||||
|
pacman: Pacman
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def dynamic_needed(binary_path: Path) -> list[str]:
|
def dynamic_needed(binary_path: Path) -> list[str]:
|
||||||
@ -90,6 +95,27 @@ class PackageArchive:
|
|||||||
|
|
||||||
return magic_bytes == expected
|
return magic_bytes == expected
|
||||||
|
|
||||||
|
def _load_pacman_package(self, path: Path) -> FilesystemPackage:
|
||||||
|
"""
|
||||||
|
load pacman package model from path
|
||||||
|
|
||||||
|
Args:
|
||||||
|
path(Path): path to package files database
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
FilesystemPackage: generated pacman package model with empty paths
|
||||||
|
"""
|
||||||
|
package_name, *_ = path.parent.name.rsplit("-", 2)
|
||||||
|
try:
|
||||||
|
pacman_package = OfficialSyncdb.info(package_name, pacman=self.pacman)
|
||||||
|
return FilesystemPackage(
|
||||||
|
package_name=package_name,
|
||||||
|
depends=set(pacman_package.depends),
|
||||||
|
opt_depends=set(pacman_package.opt_depends),
|
||||||
|
)
|
||||||
|
except UnknownPackageError:
|
||||||
|
return FilesystemPackage(package_name=package_name, depends=set(), opt_depends=set())
|
||||||
|
|
||||||
def depends_on(self) -> Dependencies:
|
def depends_on(self) -> Dependencies:
|
||||||
"""
|
"""
|
||||||
extract packages and paths which are required for this package
|
extract packages and paths which are required for this package
|
||||||
@ -98,17 +124,55 @@ class PackageArchive:
|
|||||||
Dependencies: map of the package name to set of paths used by this package
|
Dependencies: map of the package name to set of paths used by this package
|
||||||
"""
|
"""
|
||||||
dependencies, roots = self.depends_on_paths()
|
dependencies, roots = self.depends_on_paths()
|
||||||
|
installed_packages = self.installed_packages()
|
||||||
|
|
||||||
result: dict[str, list[str]] = {}
|
# build initial map of file path -> packages containing this path
|
||||||
for package, (directories, files) in self.installed_packages().items():
|
# in fact, keys will contain all libraries the package linked to and all directories it contains
|
||||||
if package in self.package.packages:
|
dependencies_per_path: dict[Path, list[FilesystemPackage]] = {}
|
||||||
|
for package_base, package in installed_packages.items():
|
||||||
|
if package_base in self.package.packages:
|
||||||
continue # skip package itself
|
continue # skip package itself
|
||||||
|
|
||||||
required_by = [directory for directory in directories if directory in roots]
|
required_by = [directory for directory in package.directories if directory in roots]
|
||||||
required_by.extend(library for library in files if library.name in dependencies)
|
required_by.extend(library for library in package.files if library.name in dependencies)
|
||||||
|
|
||||||
for path in required_by:
|
for path in required_by:
|
||||||
result.setdefault(str(path), []).append(package)
|
dependencies_per_path.setdefault(path, []).append(package)
|
||||||
|
|
||||||
|
# reduce trees
|
||||||
|
result = {}
|
||||||
|
base_packages = OfficialSyncdb.info("base", pacman=self.pacman).depends
|
||||||
|
# sort items from children directories to root
|
||||||
|
for path, packages_list in reversed(sorted(dependencies_per_path.items())):
|
||||||
|
packages = {package.package_name: package for package in packages_list}
|
||||||
|
# skip if this path belongs to the one of the base packages
|
||||||
|
if any(package_name in packages for package_name in base_packages):
|
||||||
|
continue
|
||||||
|
|
||||||
|
reduced_packages_list = []
|
||||||
|
for package_name, package in packages.items():
|
||||||
|
# if there is any package which is dependency of this package, we can skip it here
|
||||||
|
# in case if cyclic dependencies have been found, we keep both
|
||||||
|
has_dependencies = any(
|
||||||
|
dependency_package
|
||||||
|
for dependency_package in package.depends.intersection(packages)
|
||||||
|
if not packages[dependency_package].depends_on(package_name)
|
||||||
|
)
|
||||||
|
if has_dependencies:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# check if there is already parent of current path in the result and has the same packages
|
||||||
|
already_satisfied = False
|
||||||
|
for children_path, children_packages in result.items():
|
||||||
|
if not Path(children_path).is_relative_to(path):
|
||||||
|
continue
|
||||||
|
already_satisfied |= package_name in children_packages
|
||||||
|
if already_satisfied:
|
||||||
|
continue
|
||||||
|
|
||||||
|
reduced_packages_list.append(package_name)
|
||||||
|
|
||||||
|
result[str(path)] = reduced_packages_list
|
||||||
|
|
||||||
return Dependencies(result)
|
return Dependencies(result)
|
||||||
|
|
||||||
@ -130,7 +194,7 @@ class PackageArchive:
|
|||||||
|
|
||||||
return dependencies, roots
|
return dependencies, roots
|
||||||
|
|
||||||
def installed_packages(self) -> dict[str, tuple[list[Path], list[Path]]]:
|
def installed_packages(self) -> dict[str, FilesystemPackage]:
|
||||||
"""
|
"""
|
||||||
extract list of the installed packages and their content
|
extract list of the installed packages and their content
|
||||||
|
|
||||||
@ -142,24 +206,23 @@ class PackageArchive:
|
|||||||
|
|
||||||
pacman_local_files = self.root / "var" / "lib" / "pacman" / "local"
|
pacman_local_files = self.root / "var" / "lib" / "pacman" / "local"
|
||||||
for path in filter(lambda fn: fn.name == "files", walk(pacman_local_files)):
|
for path in filter(lambda fn: fn.name == "files", walk(pacman_local_files)):
|
||||||
package, *_ = path.parent.name.rsplit("-", 2)
|
package = self._load_pacman_package(path)
|
||||||
|
|
||||||
directories, files = [], []
|
is_files_section = False
|
||||||
is_files = False
|
|
||||||
for line in path.read_text(encoding="utf8").splitlines():
|
for line in path.read_text(encoding="utf8").splitlines():
|
||||||
if not line: # skip empty lines
|
if not line: # skip empty lines
|
||||||
continue
|
continue
|
||||||
if line.startswith("%") and line.endswith("%"): # directive started
|
if line.startswith("%") and line.endswith("%"): # directive started
|
||||||
is_files = line == "%FILES%"
|
is_files_section = line == "%FILES%"
|
||||||
if not is_files: # not a files directive
|
if not is_files_section: # not a files directive
|
||||||
continue
|
continue
|
||||||
|
|
||||||
entry = Path(line)
|
entry = Path(line)
|
||||||
if line.endswith("/"): # simple check if it is directory
|
if line.endswith("/"): # simple check if it is directory
|
||||||
directories.append(entry)
|
package.directories.append(entry)
|
||||||
else:
|
else:
|
||||||
files.append(entry)
|
package.files.append(entry)
|
||||||
|
|
||||||
result[package] = directories, files
|
result[package.package_name] = package
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
@ -30,7 +30,7 @@ def test_package_logger_set_reset(database: SQLite) -> None:
|
|||||||
database._package_logger_reset()
|
database._package_logger_reset()
|
||||||
record = logging.makeLogRecord({})
|
record = logging.makeLogRecord({})
|
||||||
with pytest.raises(AttributeError):
|
with pytest.raises(AttributeError):
|
||||||
record.package_id
|
assert record.package_id
|
||||||
|
|
||||||
|
|
||||||
def test_in_package_context(database: SQLite, package_ahriman: Package, mocker: MockerFixture) -> None:
|
def test_in_package_context(database: SQLite, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user