Improve local package querying

This commit is contained in:
2025-10-18 09:51:36 +03:00
parent d56005e63e
commit bee23e2f56
5 changed files with 34 additions and 51 deletions
+2 -4
View File
@@ -207,7 +207,6 @@ func showPackageInfo() {
var info *bpmlib.PackageInfo
isFile := false
showInstallationReason := false
if stat, err := os.Stat(pkg); err == nil && !stat.IsDir() {
bpmpkg, err := bpmlib.ReadPackage(pkg)
if err != nil {
@@ -223,7 +222,6 @@ func showPackageInfo() {
} else {
info = bpmlib.GetPackageInfo(pkg, rootDir)
}
showInstallationReason = true
}
if info == nil {
log.Printf("Error: package (%s) is not installed\n", pkg)
@@ -242,7 +240,7 @@ func showPackageInfo() {
}
fmt.Println("File: " + abs)
}
fmt.Println(bpmlib.CreateReadableInfo(true, true, true, showInstallationReason, info, rootDir))
fmt.Println(info.CreateReadableInfo(rootDir))
}
}
@@ -286,7 +284,7 @@ func showPackageList() {
if n != 0 {
fmt.Println()
}
fmt.Println(bpmlib.CreateReadableInfo(true, true, true, true, info, rootDir))
fmt.Println(info.CreateReadableInfo(rootDir))
}
}
}
+9 -18
View File
@@ -1,7 +1,6 @@
package bpmlib
import (
"errors"
"fmt"
"slices"
)
@@ -170,19 +169,11 @@ func resolvePackageDependenciesFromDatabase(resolved *[]pkgInstallationReason, u
}
}
func GetPackageDependants(pkgName string, rootDir string) ([]string, error) {
ret := make([]string, 0)
// Get BPM package
pkg := GetPackage(pkgName, rootDir)
if pkg == nil {
return nil, errors.New("package not found: " + pkgName)
}
func (pkgInfo *PackageInfo) GetPackageDependants(rootDir string) (dependants []string) {
// Get installed package names
pkgs, err := GetInstalledPackages(rootDir)
if err != nil {
return nil, errors.New("could not get installed packages")
return nil
}
// Loop through all installed packages
@@ -190,33 +181,33 @@ func GetPackageDependants(pkgName string, rootDir string) ([]string, error) {
// Get installed BPM package
installedPkg := GetPackage(installedPkgName, rootDir)
if installedPkg == nil {
return nil, errors.New("package not found: " + installedPkgName)
return nil
}
// Skip iteration if comparing the same packages
if installedPkg.PkgInfo.Name == pkgName {
if installedPkg.PkgInfo.Name == pkgInfo.Name {
continue
}
// Add installed package to list if its dependencies include pkgName
if slices.ContainsFunc(installedPkg.PkgInfo.Depends, func(n string) bool {
return n == pkgName
return n == pkgInfo.Name
}) {
ret = append(ret, installedPkgName)
dependants = append(dependants, installedPkgName)
continue
}
// Loop through each virtual package
for _, vpkg := range pkg.PkgInfo.Provides {
for _, vpkg := range pkgInfo.Provides {
// Add installed package to list if its dependencies contain a provided virtual package
if slices.ContainsFunc(installedPkg.PkgInfo.Depends, func(n string) bool {
return n == vpkg
}) {
ret = append(ret, installedPkgName)
dependants = append(dependants, installedPkgName)
break
}
}
}
return ret, nil
return dependants
}
+1 -5
View File
@@ -221,11 +221,7 @@ func RemovePackages(rootDir string, force, cleanupDependencies bool, packages ..
// Get packages and their dependants
packageDepndants := make(map[string][]string, 0)
for _, action := range operation.Actions {
dependants, err := GetPackageDependants(action.(*RemovePackageAction).BpmPackage.PkgInfo.Name, rootDir)
if err != nil {
return nil, fmt.Errorf("could not get package dependants: %s", err)
}
dependants := action.(*RemovePackageAction).BpmPackage.PkgInfo.GetPackageDependants(rootDir)
packageDepndants[action.(*RemovePackageAction).BpmPackage.PkgInfo.Name] = dependants
}
+1 -4
View File
@@ -176,10 +176,7 @@ func (operation *BPMOperation) RemoveNeededPackages() error {
}
for pkg, action := range removeActions {
dependants, err := GetPackageDependants(action.BpmPackage.PkgInfo.Name, operation.RootDir)
if err != nil {
return errors.New("could not get dependant packages for package (" + pkg + ")")
}
dependants := action.BpmPackage.PkgInfo.GetPackageDependants(operation.RootDir)
dependants = slices.DeleteFunc(dependants, func(d string) bool {
if _, ok := removeActions[d]; ok {
return true
+11 -10
View File
@@ -507,39 +507,39 @@ func ReadPackageInfo(contents string) (*PackageInfo, error) {
return pkgInfo, nil
}
func CreateReadableInfo(showArchitecture, showType, showPackageRelations, showInstallationReason bool, pkgInfo *PackageInfo, rootDir string) string {
func (pkgInfo *PackageInfo) CreateReadableInfo(rootDir string) string {
ret := make([]string, 0)
appendArray := func(label string, array []string) {
if len(array) == 0 {
return
}
// Sort array
slices.Sort(array)
ret = append(ret, fmt.Sprintf("%s: %s", label, strings.Join(array, ", ")))
}
ret = append(ret, "Name: "+pkgInfo.Name)
ret = append(ret, "Description: "+pkgInfo.Description)
ret = append(ret, "Version: "+pkgInfo.GetFullVersion())
ret = append(ret, "URL: "+pkgInfo.Url)
ret = append(ret, "License: "+pkgInfo.License)
if showArchitecture {
ret = append(ret, "Architecture: "+pkgInfo.Arch)
}
if showType {
ret = append(ret, "Type: "+pkgInfo.Type)
}
if showPackageRelations {
appendArray("Dependencies", pkgInfo.Depends)
if pkgInfo.Type == "source" {
appendArray("Make Dependencies", pkgInfo.MakeDepends)
}
appendArray("Optional dependencies", pkgInfo.OptionalDepends)
dependants, err := GetPackageDependants(pkgInfo.Name, rootDir)
if err == nil {
dependants := pkgInfo.GetPackageDependants(rootDir)
if len(dependants) > 0 {
appendArray("Dependant packages", dependants)
}
appendArray("Conflicting packages", pkgInfo.Conflicts)
appendArray("Provided packages", pkgInfo.Provides)
appendArray("Replaces packages", pkgInfo.Replaces)
}
if pkgInfo.Type == "source" && len(pkgInfo.SplitPackages) != 0 {
splitPkgs := make([]string, len(pkgInfo.SplitPackages))
for i, splitPkgInfo := range pkgInfo.SplitPackages {
@@ -547,7 +547,8 @@ func CreateReadableInfo(showArchitecture, showType, showPackageRelations, showIn
}
appendArray("Split Packages", splitPkgs)
}
if IsPackageInstalled(pkgInfo.Name, rootDir) && showInstallationReason {
if rootDir != "" && IsPackageInstalled(pkgInfo.Name, rootDir) {
installationReason := GetInstallationReason(pkgInfo.Name, rootDir)
var installationReasonString string
switch installationReason {