From 123697e1dc14ead88f7edf0779d2428db64d5eb5 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Wed, 28 Aug 2024 10:34:27 +0300 Subject: [PATCH 1/9] Added basic remote repository functionality --- config/bpm.conf | 6 +- main.go | 97 +++++++++++++++++++++++-- utils/config.go | 23 ++++-- utils/package_utils.go | 9 +++ utils/repo_utils.go | 161 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 280 insertions(+), 16 deletions(-) create mode 100644 utils/repo_utils.go diff --git a/config/bpm.conf b/config/bpm.conf index d2649af..6b92f57 100644 --- a/config/bpm.conf +++ b/config/bpm.conf @@ -1,4 +1,8 @@ compilation_env: [] silent_compilation: false compilation_dir: "/var/tmp/" -binary_output_dir: "/var/lib/bpm/compiled/" \ No newline at end of file +binary_output_dir: "/var/lib/bpm/compiled/" +repositories: + - name: example-repository + source: https://my-repo.xyz/ + disabled: true \ No newline at end of file diff --git a/main.go b/main.go index f16b05f..8676752 100644 --- a/main.go +++ b/main.go @@ -29,10 +29,12 @@ var yesAll = false var buildSource = false var skipCheck = false var keepTempDir = false -var forceInstall = false +var force = false var showInstalled = false var pkgListNumbers = false var pkgListNames = false +var reinstall = false +var nosync = true func main() { utils.ReadConfig() @@ -48,6 +50,8 @@ const ( info list install + update + sync remove file ) @@ -62,6 +66,10 @@ func getCommandType() commandType { return list case "install": return install + case "update": + return update + case "sync": + return sync case "remove": return remove case "file": @@ -91,16 +99,23 @@ func resolveCommand() { continue } - } else { + } else if showInstalled { info = utils.GetPackageInfo(pkg, rootDir, false) if info == nil { - fmt.Printf("Package (%s) could not be found\n", pkg) + fmt.Printf("Package (%s) is not installed\n", pkg) continue } + } else { + entry, err := utils.GetRepositoryEntry(pkg) + if err != nil { + fmt.Printf("Package (%s) could not be found in any repository\n", pkg) + continue + } + info = &entry.Info } fmt.Println("----------------") if showInstalled { - fmt.Println(utils.CreateReadableInfo(false, false, true, false, true, info, rootDir)) + fmt.Println(utils.CreateReadableInfo(true, true, true, false, true, info, rootDir)) } else { fmt.Println(utils.CreateReadableInfo(true, true, true, true, true, info, rootDir)) } @@ -164,7 +179,7 @@ func resolveCommand() { } verb = "build" } - if !forceInstall { + if !force { if pkgInfo.Arch != "any" && pkgInfo.Arch != utils.GetArch() { fmt.Printf("skipping... cannot %s a package with a different architecture\n", verb) continue @@ -226,7 +241,7 @@ func resolveCommand() { } } - err = utils.InstallPackage(file, rootDir, verbose, forceInstall, buildSource, skipCheck, keepTempDir) + err = utils.InstallPackage(file, rootDir, verbose, force, buildSource, skipCheck, keepTempDir) if err != nil { if pkgInfo.Type == "source" && keepTempDir { fmt.Println("BPM temp directory was created at /var/tmp/bpm_source-" + pkgInfo.Name) @@ -238,6 +253,34 @@ func resolveCommand() { fmt.Println("** It is recommended you delete the temporary bpm folder in /var/tmp **") } } + case sync: + if os.Getuid() != 0 { + fmt.Println("This subcommand needs to be run with superuser permissions") + os.Exit(0) + } + if !yesAll { + fmt.Printf("Are you sure you wish to sync all databases? [y\\N] ") + reader := bufio.NewReader(os.Stdin) + text, _ := reader.ReadString('\n') + if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" { + fmt.Println("Cancelling sync...") + os.Exit(0) + } + } + for _, repo := range utils.BPMConfig.Repositories { + if repo.Disabled { + if verbose { + fmt.Printf("Skipping repository (%s) because it is disabled\n", repo.Name) + } + continue + } + fmt.Printf("Fetching package database for repository (%s)...\n", repo.Name) + err := repo.SyncLocalDatabase() + if err != nil { + log.Fatal(err) + } + } + fmt.Println("All package databases synced successfully!") case remove: if os.Getuid() != 0 { fmt.Println("This subcommand needs to be run with superuser permissions") @@ -344,11 +387,22 @@ func printHelp() { fmt.Println(" -R= lets you define the root path which will be used") fmt.Println(" -v Show additional information about what BPM is doing") fmt.Println(" -y skips the confirmation prompt") - fmt.Println(" -f skips dependency and architecture checking") + fmt.Println(" -f skips dependency, conflict and architecture checking") fmt.Println(" -o= set the binary package output directory (defaults to /var/lib/bpm/compiled)") fmt.Println(" -c= set the compilation directory (defaults to /var/tmp)") fmt.Println(" -b creates a binary package from a source package after compilation and saves it in the binary package output directory") fmt.Println(" -k keeps the compilation directory created by BPM after source package installation") + fmt.Println("-> bpm update [-R, -v, -y, -f, --reinstall, --nosync] | updates all packages that are available in the repositories") + fmt.Println(" -R= lets you define the root path which will be used") + fmt.Println(" -v Show additional information about what BPM is doing") + fmt.Println(" -y skips the confirmation prompt") + fmt.Println(" -f skips dependency, conflict and architecture checking") + fmt.Println(" --reinstall Fetches and reinstalls all packages even if they do not have a newer version available") + fmt.Println(" --nosync Skips package database syncing") + fmt.Println("-> bpm sync [-R, -v] | Syncs package databases without updating packages") + fmt.Println(" -R= lets you define the root path which will be used") + fmt.Println(" -v Show additional information about what BPM is doing") + fmt.Println(" -y skips the confirmation prompt") fmt.Println("-> bpm remove [-R, -v, -y] | removes the following packages") fmt.Println(" -v Show additional information about what BPM is doing") fmt.Println(" -R= lets you define the root path which will be used") @@ -380,8 +434,23 @@ func resolveFlags() { installFlagSet.BoolVar(&buildSource, "b", false, "Build binary package from source package") installFlagSet.BoolVar(&skipCheck, "s", false, "Skip check function during source compilation") installFlagSet.BoolVar(&keepTempDir, "k", false, "Keep temporary directory after source compilation") - installFlagSet.BoolVar(&forceInstall, "f", false, "Force installation by skipping architecture and dependency resolution") + installFlagSet.BoolVar(&force, "f", false, "Force installation by skipping architecture and dependency resolution") installFlagSet.Usage = printHelp + // Update flags + updateFlagSet := flag.NewFlagSet("Update flags", flag.ExitOnError) + updateFlagSet.StringVar(&rootDir, "R", "/", "Set the destination root") + updateFlagSet.BoolVar(&verbose, "v", false, "Show additional information about what BPM is doing") + updateFlagSet.BoolVar(&yesAll, "y", false, "Skip confirmation prompts") + updateFlagSet.BoolVar(&force, "f", false, "Force update by skipping architecture and dependency resolution") + updateFlagSet.BoolVar(&reinstall, "reinstall", false, "Fetches and reinstalls all packages even if they do not have a newer version available") + updateFlagSet.BoolVar(&nosync, "nosync", false, "Skips package database syncing") + updateFlagSet.Usage = printHelp + // Sync flags + syncFlagSet := flag.NewFlagSet("Sync flags", flag.ExitOnError) + syncFlagSet.StringVar(&rootDir, "R", "/", "Set the destination root") + syncFlagSet.BoolVar(&verbose, "v", false, "Show additional information about what BPM is doing") + syncFlagSet.BoolVar(&yesAll, "y", false, "Skip confirmation prompts") + syncFlagSet.Usage = printHelp // Remove flags removeFlagSet := flag.NewFlagSet("Remove flags", flag.ExitOnError) removeFlagSet.StringVar(&rootDir, "R", "/", "Set the destination root") @@ -415,6 +484,18 @@ func resolveFlags() { return } subcommandArgs = installFlagSet.Args() + } else if getCommandType() == update { + err := updateFlagSet.Parse(subcommandArgs) + if err != nil { + return + } + subcommandArgs = updateFlagSet.Args() + } else if getCommandType() == sync { + err := syncFlagSet.Parse(subcommandArgs) + if err != nil { + return + } + subcommandArgs = syncFlagSet.Args() } else if getCommandType() == remove { err := removeFlagSet.Parse(subcommandArgs) if err != nil { diff --git a/utils/config.go b/utils/config.go index 9fd0a81..833a063 100644 --- a/utils/config.go +++ b/utils/config.go @@ -2,14 +2,16 @@ package utils import ( "gopkg.in/yaml.v3" + "log" "os" ) type BPMConfigStruct struct { - CompilationEnv []string `yaml:"compilation_env"` - SilentCompilation bool `yaml:"silent_compilation"` - BinaryOutputDir string `yaml:"binary_output_dir"` - CompilationDir string `yaml:"compilation_dir"` + CompilationEnv []string `yaml:"compilation_env"` + SilentCompilation bool `yaml:"silent_compilation"` + BinaryOutputDir string `yaml:"binary_output_dir"` + CompilationDir string `yaml:"compilation_dir"` + Repositories []*Repository `yaml:"repositories"` } var BPMConfig BPMConfigStruct = BPMConfigStruct{ @@ -21,14 +23,21 @@ var BPMConfig BPMConfigStruct = BPMConfigStruct{ func ReadConfig() { if _, err := os.Stat("/etc/bpm.conf"); os.IsNotExist(err) { - return + log.Fatal(err) } bytes, err := os.ReadFile("/etc/bpm.conf") if err != nil { - return + log.Fatal(err) } err = yaml.Unmarshal(bytes, &BPMConfig) if err != nil { - return + log.Fatal(err) + } + for _, repo := range BPMConfig.Repositories { + repo.Entries = make(map[string]*RepositoryEntry) + err := repo.ReadLocalDatabase() + if err != nil { + log.Fatal(err) + } } } diff --git a/utils/package_utils.go b/utils/package_utils.go index ddec923..2916824 100644 --- a/utils/package_utils.go +++ b/utils/package_utils.go @@ -403,6 +403,15 @@ func CreateReadableInfo(showArchitecture, showType, showPackageRelations, showRe appendMap("Conditional conflicting packages", pkgInfo.ConditionalConflicts) appendMap("Conditional optional dependencies", pkgInfo.ConditionalOptional) } + if showRemoteInfo { + arr := make([]string, 0) + for _, repo := range BPMConfig.Repositories { + if repo.ContainsPackage(pkgInfo.Name) { + arr = append(arr, repo.Name) + } + } + appendArray("Repositories", arr) + } if showInstallationReason { if IsPackageInstalled(pkgInfo.Name, rootDir) { ret = append(ret, "Installed: yes") diff --git a/utils/repo_utils.go b/utils/repo_utils.go new file mode 100644 index 0000000..28b4583 --- /dev/null +++ b/utils/repo_utils.go @@ -0,0 +1,161 @@ +package utils + +import ( + "errors" + "gopkg.in/yaml.v3" + "io" + "net/http" + "net/url" + "os" + "path" + "strings" +) + +type Repository struct { + Name string `yaml:"name"` + Source string `yaml:"source"` + Disabled bool `yaml:"disabled"` + Entries map[string]*RepositoryEntry +} + +type RepositoryEntry struct { + Info PackageInfo `yaml:"info"` + Download string `yaml:"download"` +} + +func (repo *Repository) ContainsPackage(pkg string) bool { + _, ok := repo.Entries[pkg] + return ok +} + +func (repo *Repository) ReadLocalDatabase() error { + repoFile := "/var/lib/bpm/repositories/" + repo.Name + ".bpmdb" + if _, err := os.Stat(repoFile); err != nil { + return nil + } + + bytes, err := os.ReadFile(repoFile) + if err != nil { + return err + } + + data := string(bytes) + for _, b := range strings.Split(data, "---") { + entry := RepositoryEntry{ + Info: PackageInfo{ + Name: "", + Description: "", + Version: "", + Url: "", + License: "", + Arch: "", + Type: "", + Keep: make([]string, 0), + Depends: make([]string, 0), + ConditionalDepends: make(map[string][]string), + MakeDepends: make([]string, 0), + ConditionalMakeDepends: make(map[string][]string), + Conflicts: make([]string, 0), + ConditionalConflicts: make(map[string][]string), + Optional: make([]string, 0), + ConditionalOptional: make(map[string][]string), + Provides: make([]string, 0), + }, + Download: "", + } + err := yaml.Unmarshal([]byte(b), &entry) + if err != nil { + return err + } + repo.Entries[entry.Info.Name] = &entry + } + return nil +} + +func (repo *Repository) SyncLocalDatabase() error { + repoFile := "/var/lib/bpm/repositories/" + repo.Name + ".bpmdb" + err := os.MkdirAll(path.Dir(repoFile), 0755) + if err != nil { + return err + } + + u, err := url.JoinPath(repo.Source, "database.bpmdb") + if err != nil { + return err + } + + resp, err := http.Get(u) + if err != nil { + return err + } + defer resp.Body.Close() + + out, err := os.Create(repoFile) + if err != nil { + return err + } + defer out.Close() + + _, err = io.Copy(out, resp.Body) + + return nil +} + +func GetRepository(name string) *Repository { + for _, repo := range BPMConfig.Repositories { + if repo.Name == name { + return repo + } + } + return nil +} + +func GetRepositoryEntry(str string) (*RepositoryEntry, error) { + split := strings.Split(str, "/") + if len(split) == 1 { + pkgName := strings.TrimSpace(split[0]) + if pkgName == "" { + return nil, errors.New("could not find repository entry for this package") + } + for _, repo := range BPMConfig.Repositories { + if repo.ContainsPackage(pkgName) { + return repo.Entries[pkgName], nil + } + } + return nil, errors.New("could not find repository entry for this package") + } else if len(split) == 2 { + repoName := strings.TrimSpace(split[0]) + pkgName := strings.TrimSpace(split[1]) + if repoName == "" || pkgName == "" { + return nil, errors.New("could not find repository entry for this package") + } + repo := GetRepository(repoName) + if repo == nil || !repo.ContainsPackage(pkgName) { + return nil, errors.New("could not find repository entry for this package") + } + return repo.Entries[pkgName], nil + } else { + return nil, errors.New("could not find repository entry for this package") + } +} + +func (repo *Repository) FetchPackage(pkg, savePath string) (string, error) { + if !repo.ContainsPackage(pkg) { + return "", errors.New("Could not fetch package '" + pkg + "'") + } + entry := repo.Entries[pkg] + resp, err := http.Get(entry.Download) + if err != nil { + return "", err + } + defer resp.Body.Close() + + out, err := os.Create(savePath) + if err != nil { + return "", err + } + defer out.Close() + + _, err = io.Copy(out, resp.Body) + return savePath, nil +} -- 2.47.2 From 12d5e7580edc61a4a0c47744e85db244e63c9cff Mon Sep 17 00:00:00 2001 From: EnumDev Date: Wed, 28 Aug 2024 10:58:49 +0300 Subject: [PATCH 2/9] Disabled repositories will now be removed from the Repositories slice immediately --- main.go | 6 ------ utils/config.go | 5 +++++ utils/repo_utils.go | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 8676752..cd0fa18 100644 --- a/main.go +++ b/main.go @@ -268,12 +268,6 @@ func resolveCommand() { } } for _, repo := range utils.BPMConfig.Repositories { - if repo.Disabled { - if verbose { - fmt.Printf("Skipping repository (%s) because it is disabled\n", repo.Name) - } - continue - } fmt.Printf("Fetching package database for repository (%s)...\n", repo.Name) err := repo.SyncLocalDatabase() if err != nil { diff --git a/utils/config.go b/utils/config.go index 833a063..8f62a9d 100644 --- a/utils/config.go +++ b/utils/config.go @@ -33,6 +33,11 @@ func ReadConfig() { if err != nil { log.Fatal(err) } + for i := len(BPMConfig.Repositories) - 1; i >= 0; i-- { + if BPMConfig.Repositories[i].Disabled != nil && *BPMConfig.Repositories[i].Disabled { + BPMConfig.Repositories = append(BPMConfig.Repositories[:i], BPMConfig.Repositories[i+1:]...) + } + } for _, repo := range BPMConfig.Repositories { repo.Entries = make(map[string]*RepositoryEntry) err := repo.ReadLocalDatabase() diff --git a/utils/repo_utils.go b/utils/repo_utils.go index 28b4583..145d6ba 100644 --- a/utils/repo_utils.go +++ b/utils/repo_utils.go @@ -14,7 +14,7 @@ import ( type Repository struct { Name string `yaml:"name"` Source string `yaml:"source"` - Disabled bool `yaml:"disabled"` + Disabled *bool `yaml:"disabled"` Entries map[string]*RepositoryEntry } -- 2.47.2 From 59df2324e635dbf16a4dd0446b5a577c141a5a5c Mon Sep 17 00:00:00 2001 From: EnumDev Date: Thu, 29 Aug 2024 16:52:29 +0300 Subject: [PATCH 3/9] Added basic remote repository functionality to the install subcommand --- go.mod | 5 +- go.sum | 2 + main.go | 196 ++++++++++++++++++++++++----------------- utils/package_utils.go | 55 +++++++++++- utils/repo_utils.go | 38 ++++---- 5 files changed, 197 insertions(+), 99 deletions(-) diff --git a/go.mod b/go.mod index a287598..017c769 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module gitlab.com/bubble-package-manager/bpm go 1.22 -require gopkg.in/yaml.v3 v3.0.1 // indirect +require ( + github.com/elliotchance/orderedmap/v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index 4bc0337..e437a0b 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/elliotchance/orderedmap/v2 v2.4.0 h1:6tUmMwD9F998FNpwFxA5E6NQvSpk2PVw7RKsVq3+2Cw= +github.com/elliotchance/orderedmap/v2 v2.4.0/go.mod h1:85lZyVbpGaGvHvnKa7Qhx7zncAdBIBq6u56Hb1PRU5Q= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index cd0fa18..9ba2c99 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "bufio" "flag" "fmt" + "github.com/elliotchance/orderedmap/v2" "gitlab.com/bubble-package-manager/bpm/utils" "log" "os" @@ -106,12 +107,12 @@ func resolveCommand() { continue } } else { - entry, err := utils.GetRepositoryEntry(pkg) + entry, _, err := utils.GetRepositoryEntry(pkg) if err != nil { fmt.Printf("Package (%s) could not be found in any repository\n", pkg) continue } - info = &entry.Info + info = entry.Info } fmt.Println("----------------") if showInstalled { @@ -157,91 +158,115 @@ func resolveCommand() { fmt.Println("This subcommand needs to be run with superuser permissions") os.Exit(0) } - files := subcommandArgs - if len(files) == 0 { - fmt.Println("No files were given to install") + pkgs := subcommandArgs + if len(pkgs) == 0 { + fmt.Println("No packages or files were given to install") return } - for _, file := range files { - pkgInfo, err := utils.ReadPackage(file) + + pkgsToInstall := orderedmap.NewOrderedMap[string, *struct { + isDependency bool + pkgInfo *utils.PackageInfo + }]() + pkgsToFetch := orderedmap.NewOrderedMap[string, *struct { + isDependency bool + pkgInfo *utils.PackageInfo + }]() + unresolvedDepends := make([]string, 0) + + // Search for packages + for _, pkg := range pkgs { + if stat, err := os.Stat(pkg); err == nil && !stat.IsDir() { + pkgInfo, err := utils.ReadPackage(pkg) + if err != nil { + log.Fatalf("Could not read package. Error: %s\n", err) + } + if !reinstall && utils.IsPackageInstalled(pkgInfo.Name, rootDir) { + continue + } + pkgsToInstall.Set(pkg, &struct { + isDependency bool + pkgInfo *utils.PackageInfo + }{isDependency: false, pkgInfo: pkgInfo}) + } else { + entry, _, err := utils.GetRepositoryEntry(pkg) + if err != nil { + log.Fatalf("Could not find package (%s) in any repository\n", pkg) + } + if !reinstall && utils.IsPackageInstalled(entry.Info.Name, rootDir) { + continue + } + pkgsToFetch.Set(entry.Info.Name, &struct { + isDependency bool + pkgInfo *utils.PackageInfo + }{isDependency: false, pkgInfo: entry.Info}) + } + } + + // Check for dependencies and conflicts + for _, pkg := range pkgsToFetch.Keys() { + entry, _, err := utils.GetRepositoryEntry(pkg) if err != nil { - log.Fatalf("Could not read package\nError: %s\n", err) - } - if !yesAll { - fmt.Println("----------------\n" + utils.CreateReadableInfo(true, true, true, true, false, pkgInfo, rootDir)) - fmt.Println("----------------") - } - verb := "install" - if pkgInfo.Type == "source" { - if _, err := os.Stat("/bin/fakeroot"); os.IsNotExist(err) { - fmt.Printf("Skipping... cannot %s package (%s) due to fakeroot not being installed", verb, pkgInfo.Name) - continue - } - verb = "build" - } - if !force { - if pkgInfo.Arch != "any" && pkgInfo.Arch != utils.GetArch() { - fmt.Printf("skipping... cannot %s a package with a different architecture\n", verb) - continue - } - if unresolved := utils.CheckDependencies(pkgInfo, true, true, rootDir); len(unresolved) != 0 { - fmt.Printf("skipping... cannot %s package (%s) due to missing dependencies: %s\n", verb, pkgInfo.Name, strings.Join(unresolved, ", ")) - continue - } - if conflicts := utils.CheckConflicts(pkgInfo, true, rootDir); len(conflicts) != 0 { - fmt.Printf("skipping... cannot %s package (%s) due to conflicting packages: %s\n", verb, pkgInfo.Name, strings.Join(conflicts, ", ")) - continue - } - } - if rootDir != "/" { - fmt.Println("Warning: Operating in " + rootDir) - } - if !yesAll { - reader := bufio.NewReader(os.Stdin) - if pkgInfo.Type == "source" { - fmt.Print("Would you like to view the source.sh file of this package? [Y\\n] ") - text, _ := reader.ReadString('\n') - if strings.TrimSpace(strings.ToLower(text)) != "n" && strings.TrimSpace(strings.ToLower(text)) != "no" { - script, err := utils.GetSourceScript(file) - if err != nil { - log.Fatalf("Could not read source script\nError: %s\n", err) - } - fmt.Println(script) - fmt.Println("-------EOF-------") - } - } - } - if utils.IsPackageInstalled(pkgInfo.Name, rootDir) { - if !yesAll { - installedInfo := utils.GetPackageInfo(pkgInfo.Name, rootDir, false) - if strings.Compare(pkgInfo.Version, installedInfo.Version) > 0 { - fmt.Println("This file contains a newer version of this package (" + installedInfo.Version + " -> " + pkgInfo.Version + ")") - fmt.Print("Do you wish to update this package? [y\\N] ") - } else if strings.Compare(pkgInfo.Version, installedInfo.Version) < 0 { - fmt.Println("This file contains an older version of this package (" + installedInfo.Version + " -> " + pkgInfo.Version + ")") - fmt.Print("Do you wish to downgrade this package? (Not recommended) [y\\N] ") - } else if strings.Compare(pkgInfo.Version, installedInfo.Version) == 0 { - fmt.Println("This package is already installed on the system and is up to date") - fmt.Printf("Do you wish to re%s this package? [y\\N] ", verb) - } - reader := bufio.NewReader(os.Stdin) - text, _ := reader.ReadString('\n') - if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" { - fmt.Printf("Skipping package (%s)...\n", pkgInfo.Name) - continue - } - } - } else if !yesAll { - reader := bufio.NewReader(os.Stdin) - fmt.Printf("Do you wish to %s this package? [y\\N] ", verb) - text, _ := reader.ReadString('\n') - if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" { - fmt.Printf("Skipping package (%s)...\n", pkgInfo.Name) - continue - } + log.Fatalf("Could not read package. Error: %s\n", err) } - err = utils.InstallPackage(file, rootDir, verbose, force, buildSource, skipCheck, keepTempDir) + resolved, u, err := utils.ResolveAll(entry.Info, false, !reinstall, rootDir) + unresolvedDepends = append(unresolvedDepends, u...) + for _, depend := range resolved { + if _, ok := pkgsToFetch.Get(depend); !ok { + pkgsToFetch.GetElement(depend).Value.isDependency = true + } + } + } + + // Fetch packages from repositories + for _, pkg := range pkgsToFetch.Keys() { + isDependency, _ := pkgsToFetch.Get(pkg) + entry, repo, err := utils.GetRepositoryEntry(pkg) + if err != nil { + log.Fatalf("Could not find package (%s) in any repository\n", pkg) + } + fetchedPackage, err := repo.FetchPackage(entry.Info.Name) + if err != nil { + log.Fatalf("Could not fetch package (%s). Error: %s\n", pkg, err) + } + pkgsToInstall.Set(fetchedPackage, isDependency) + } + + // Install Packages + if rootDir != "/" { + fmt.Println("Warning: Operating in " + rootDir) + } + if pkgsToInstall.Len() == 0 { + fmt.Println("All packages are up to date!") + os.Exit(0) + } + for _, pkg := range pkgsToInstall.Keys() { + pkgInfo := pkgsToInstall.GetElement(pkg).Value.pkgInfo + installedInfo := utils.GetPackageInfo(pkgInfo.Name, rootDir, false) + if installedInfo == nil { + fmt.Printf("%s: %s (Install)\n", pkgInfo.Name, pkgInfo.Version) + } else if strings.Compare(pkgInfo.Version, installedInfo.Version) < 0 { + fmt.Printf("%s: %s -> %s (Downgrade)\n", pkgInfo.Name, installedInfo.Version, pkgInfo.Version) + } else if strings.Compare(pkgInfo.Version, installedInfo.Version) > 0 { + fmt.Printf("%s: %s -> %s (Upgrade)\n", pkgInfo.Name, installedInfo.Version, pkgInfo.Version) + } else { + fmt.Printf("%s: %s (Reinstall)\n", pkgInfo.Name, pkgInfo.Version) + } + } + if !yesAll { + reader := bufio.NewReader(os.Stdin) + fmt.Print("Do you wish to install these packages? [y\\N] ") + text, _ := reader.ReadString('\n') + if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" { + fmt.Println("Cancelling...") + os.Exit(1) + } + } + for _, pkg := range pkgsToInstall.Keys() { + value, _ := pkgsToInstall.Get(pkg) + pkgInfo := value.pkgInfo + err := utils.InstallPackage(pkg, rootDir, verbose, force, buildSource, skipCheck, keepTempDir) if err != nil { if pkgInfo.Type == "source" && keepTempDir { fmt.Println("BPM temp directory was created at /var/tmp/bpm_source-" + pkgInfo.Name) @@ -249,6 +274,12 @@ func resolveCommand() { log.Fatalf("Could not install package\nError: %s\n", err) } fmt.Printf("Package (%s) was successfully installed!\n", pkgInfo.Name) + if value.isDependency { + err := utils.SetInstallationReason(pkgInfo.Name, utils.Dependency, rootDir) + if err != nil { + log.Fatalf("Could not set installation reason for package\nError: %s\n", err) + } + } if pkgInfo.Type == "source" && keepTempDir { fmt.Println("** It is recommended you delete the temporary bpm folder in /var/tmp **") } @@ -429,6 +460,7 @@ func resolveFlags() { installFlagSet.BoolVar(&skipCheck, "s", false, "Skip check function during source compilation") installFlagSet.BoolVar(&keepTempDir, "k", false, "Keep temporary directory after source compilation") installFlagSet.BoolVar(&force, "f", false, "Force installation by skipping architecture and dependency resolution") + installFlagSet.BoolVar(&reinstall, "reinstall", false, "Reinstalls packages even if they do not have a newer version available") installFlagSet.Usage = printHelp // Update flags updateFlagSet := flag.NewFlagSet("Update flags", flag.ExitOnError) diff --git a/utils/package_utils.go b/utils/package_utils.go index 2916824..53fbf5d 100644 --- a/utils/package_utils.go +++ b/utils/package_utils.go @@ -1311,6 +1311,55 @@ func CheckConflicts(pkgInfo *PackageInfo, checkConditional bool, rootDir string) return ret } +func ResolveAll(pkgInfo *PackageInfo, checkMake, ignoreInstalled bool, rootDir string) ([]string, []string, error) { + resolved := make([]string, 0) + unresolved := make([]string, 0) + toResolve := []string{pkgInfo.Name} + allDepends := make([]string, 0) + allDepends = append(allDepends, pkgInfo.Depends...) + for condition, depends := range pkgInfo.ConditionalDepends { + if IsPackageInstalled(condition, rootDir) { + allDepends = append(allDepends, depends...) + } + } + if checkMake { + allDepends = append(allDepends, pkgInfo.MakeDepends...) + for condition, depends := range pkgInfo.ConditionalMakeDepends { + if IsPackageInstalled(condition, rootDir) { + allDepends = append(allDepends, depends...) + } + } + } + + resolve := func(depend string) { + if slices.Contains(resolved, depend) { + return + } + if ignoreInstalled && IsPackageInstalled(depend, rootDir) { + return + } + entry, _, err := GetRepositoryEntry(depend) + if err != nil { + if !slices.Contains(unresolved, depend) { + unresolved = append(unresolved, depend) + } + return + } + resolved = append(resolved, depend) + toResolve = append(toResolve, entry.Info.Depends...) + } + + for len(toResolve) > 0 { + clone := slices.Clone(toResolve) + toResolve = make([]string, 0) + for _, depend := range clone { + resolve(depend) + } + } + + return resolved, unresolved, nil +} + func IsPackageInstalled(pkg, rootDir string) bool { installedDir := path.Join(rootDir, "var/lib/bpm/installed/") pkgDir := path.Join(installedDir, pkg) @@ -1329,7 +1378,11 @@ func IsPackageProvided(pkg, rootDir string) bool { if p == pkg { return true } - if slices.Contains(GetPackageInfo(p, rootDir, true).Provides, pkg) { + i := GetPackageInfo(p, rootDir, true) + if i == nil { + continue + } + if slices.Contains(i.Provides, pkg) { return true } } diff --git a/utils/repo_utils.go b/utils/repo_utils.go index 145d6ba..ec2c702 100644 --- a/utils/repo_utils.go +++ b/utils/repo_utils.go @@ -19,8 +19,8 @@ type Repository struct { } type RepositoryEntry struct { - Info PackageInfo `yaml:"info"` - Download string `yaml:"download"` + Info *PackageInfo `yaml:"info"` + Download string `yaml:"download"` } func (repo *Repository) ContainsPackage(pkg string) bool { @@ -42,7 +42,7 @@ func (repo *Repository) ReadLocalDatabase() error { data := string(bytes) for _, b := range strings.Split(data, "---") { entry := RepositoryEntry{ - Info: PackageInfo{ + Info: &PackageInfo{ Name: "", Description: "", Version: "", @@ -110,52 +110,60 @@ func GetRepository(name string) *Repository { return nil } -func GetRepositoryEntry(str string) (*RepositoryEntry, error) { +func GetRepositoryEntry(str string) (*RepositoryEntry, *Repository, error) { split := strings.Split(str, "/") if len(split) == 1 { pkgName := strings.TrimSpace(split[0]) if pkgName == "" { - return nil, errors.New("could not find repository entry for this package") + return nil, nil, errors.New("could not find repository entry for this package") } for _, repo := range BPMConfig.Repositories { if repo.ContainsPackage(pkgName) { - return repo.Entries[pkgName], nil + return repo.Entries[pkgName], repo, nil } } - return nil, errors.New("could not find repository entry for this package") + return nil, nil, errors.New("could not find repository entry for this package") } else if len(split) == 2 { repoName := strings.TrimSpace(split[0]) pkgName := strings.TrimSpace(split[1]) if repoName == "" || pkgName == "" { - return nil, errors.New("could not find repository entry for this package") + return nil, nil, errors.New("could not find repository entry for this package") } repo := GetRepository(repoName) if repo == nil || !repo.ContainsPackage(pkgName) { - return nil, errors.New("could not find repository entry for this package") + return nil, nil, errors.New("could not find repository entry for this package") } - return repo.Entries[pkgName], nil + return repo.Entries[pkgName], repo, nil } else { - return nil, errors.New("could not find repository entry for this package") + return nil, nil, errors.New("could not find repository entry for this package") } } -func (repo *Repository) FetchPackage(pkg, savePath string) (string, error) { +func (repo *Repository) FetchPackage(pkg string) (string, error) { if !repo.ContainsPackage(pkg) { return "", errors.New("Could not fetch package '" + pkg + "'") } entry := repo.Entries[pkg] - resp, err := http.Get(entry.Download) + URL, err := url.JoinPath(repo.Source, entry.Download) + if err != nil { + return "", err + } + resp, err := http.Get(URL) if err != nil { return "", err } defer resp.Body.Close() - out, err := os.Create(savePath) + err = os.MkdirAll("/var/cache/bpm/packages/", 0755) + if err != nil { + return "", err + } + out, err := os.Create("/var/cache/bpm/packages/" + path.Base(entry.Download)) if err != nil { return "", err } defer out.Close() _, err = io.Copy(out, resp.Body) - return savePath, nil + return "/var/cache/bpm/packages/" + path.Base(entry.Download), nil } -- 2.47.2 From 26500d670dbd086867c0ad4672cb557ba7ca3f41 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Thu, 29 Aug 2024 18:29:46 +0300 Subject: [PATCH 4/9] 'bpm info' will now exit with exit code 1 when package can't be found --- main.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 9ba2c99..3e588c5 100644 --- a/main.go +++ b/main.go @@ -96,21 +96,17 @@ func resolveCommand() { if _, err := os.Stat(pkg); err == nil && !showInstalled { info, err = utils.ReadPackage(pkg) if err != nil { - fmt.Printf("File (%s) could not be read\n", pkg) - continue + log.Fatalf("File (%s) could not be read\n", pkg) } - } else if showInstalled { info = utils.GetPackageInfo(pkg, rootDir, false) if info == nil { - fmt.Printf("Package (%s) is not installed\n", pkg) - continue + log.Fatalf("Package (%s) is not installed\n", pkg) } } else { entry, _, err := utils.GetRepositoryEntry(pkg) if err != nil { - fmt.Printf("Package (%s) could not be found in any repository\n", pkg) - continue + log.Fatalf("Package (%s) could not be found in any repository\n", pkg) } info = entry.Info } -- 2.47.2 From 747c77049971d1ae70de019b502eb322be3ac5d4 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Sat, 31 Aug 2024 09:11:19 +0300 Subject: [PATCH 5/9] minor improvements to dependency resolution --- main.go | 57 ++++++++++++++++++++++++++++++++++++------ utils/package_utils.go | 6 +++-- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 3e588c5..2499b17 100644 --- a/main.go +++ b/main.go @@ -177,7 +177,7 @@ func resolveCommand() { if err != nil { log.Fatalf("Could not read package. Error: %s\n", err) } - if !reinstall && utils.IsPackageInstalled(pkgInfo.Name, rootDir) { + if !reinstall && utils.IsPackageInstalled(pkgInfo.Name, rootDir) && utils.GetPackageInfo(pkgInfo.Name, rootDir, true).Version == pkgInfo.Version { continue } pkgsToInstall.Set(pkg, &struct { @@ -189,7 +189,7 @@ func resolveCommand() { if err != nil { log.Fatalf("Could not find package (%s) in any repository\n", pkg) } - if !reinstall && utils.IsPackageInstalled(entry.Info.Name, rootDir) { + if !reinstall && utils.IsPackageInstalled(entry.Info.Name, rootDir) && utils.GetPackageInfo(entry.Info.Name, rootDir, true).Version == entry.Info.Version { continue } pkgsToFetch.Set(entry.Info.Name, &struct { @@ -200,19 +200,62 @@ func resolveCommand() { } // Check for dependencies and conflicts - for _, pkg := range pkgsToFetch.Keys() { + clone := pkgsToFetch.Copy() + pkgsToFetch = orderedmap.NewOrderedMap[string, *struct { + isDependency bool + pkgInfo *utils.PackageInfo + }]() + for _, pkg := range clone.Keys() { + value := clone.GetElement(pkg).Value + resolved, u, err := utils.ResolveAll(value.pkgInfo, false, !reinstall, rootDir) + if err != nil { + log.Fatalf("Could not resolve dependencies for package (%s). Error: %s\n", pkg, err) + } + unresolvedDepends = append(unresolvedDepends, u...) + for _, depend := range resolved { + if _, ok := pkgsToFetch.Get(depend); !ok && depend != value.pkgInfo.Name { + entry, _, err := utils.GetRepositoryEntry(depend) + if err != nil { + log.Fatalf("Could not find package (%s) in any repository\n", pkg) + } + pkgsToFetch.Set(depend, &struct { + isDependency bool + pkgInfo *utils.PackageInfo + }{isDependency: true, pkgInfo: entry.Info}) + } + } + pkgsToFetch.Set(pkg, value) + } + + clone = pkgsToFetch.Copy() + pkgsToFetch = orderedmap.NewOrderedMap[string, *struct { + isDependency bool + pkgInfo *utils.PackageInfo + }]() + for _, pkg := range clone.Keys() { + value := clone.GetElement(pkg).Value entry, _, err := utils.GetRepositoryEntry(pkg) if err != nil { log.Fatalf("Could not read package. Error: %s\n", err) } - resolved, u, err := utils.ResolveAll(entry.Info, false, !reinstall, rootDir) + if err != nil { + log.Fatalf("Could not resolve dependencies for package (%s). Error: %s\n", pkg, err) + } unresolvedDepends = append(unresolvedDepends, u...) for _, depend := range resolved { - if _, ok := pkgsToFetch.Get(depend); !ok { - pkgsToFetch.GetElement(depend).Value.isDependency = true + if _, ok := clone.Get(depend); !ok { + entry, _, err := utils.GetRepositoryEntry(depend) + if err != nil { + log.Fatalf("Could not find package (%s) in any repository\n", pkg) + } + pkgsToFetch.Set(depend, &struct { + isDependency bool + pkgInfo *utils.PackageInfo + }{isDependency: true, pkgInfo: entry.Info}) } } + pkgsToFetch.Set(pkg, value) } // Fetch packages from repositories @@ -267,7 +310,7 @@ func resolveCommand() { if pkgInfo.Type == "source" && keepTempDir { fmt.Println("BPM temp directory was created at /var/tmp/bpm_source-" + pkgInfo.Name) } - log.Fatalf("Could not install package\nError: %s\n", err) + log.Fatalf("Could not install package (%s). Error: %s\n", pkg, err) } fmt.Printf("Package (%s) was successfully installed!\n", pkgInfo.Name) if value.isDependency { diff --git a/utils/package_utils.go b/utils/package_utils.go index 53fbf5d..4b3b951 100644 --- a/utils/package_utils.go +++ b/utils/package_utils.go @@ -1025,7 +1025,7 @@ func InstallPackage(filename, rootDir string, verbose, force, binaryPkgFromSrc, if pkgInfo.Arch != "any" && pkgInfo.Arch != GetArch() { return errors.New("cannot install a package with a different architecture") } - if unresolved := CheckDependencies(pkgInfo, true, true, rootDir); len(unresolved) != 0 { + if unresolved := CheckDependencies(pkgInfo, pkgInfo.Type == "source", true, rootDir); len(unresolved) != 0 { return errors.New("Could not resolve all dependencies. Missing " + strings.Join(unresolved, ", ")) } } @@ -1314,8 +1314,10 @@ func CheckConflicts(pkgInfo *PackageInfo, checkConditional bool, rootDir string) func ResolveAll(pkgInfo *PackageInfo, checkMake, ignoreInstalled bool, rootDir string) ([]string, []string, error) { resolved := make([]string, 0) unresolved := make([]string, 0) - toResolve := []string{pkgInfo.Name} + toResolve := make([]string, 0) allDepends := make([]string, 0) + + toResolve = append(toResolve, pkgInfo.Depends...) allDepends = append(allDepends, pkgInfo.Depends...) for condition, depends := range pkgInfo.ConditionalDepends { if IsPackageInstalled(condition, rootDir) { -- 2.47.2 From 2fd01a3fc2284dd27b0218bd0a3ea39a3d65f83c Mon Sep 17 00:00:00 2001 From: EnumDev Date: Sat, 31 Aug 2024 11:40:32 +0300 Subject: [PATCH 6/9] Fixed issue where ResolveAll would not resolve make dependencies and optional dependencies and removed conditional dependencies --- main.go | 10 ++- utils/package_utils.go | 146 +++++++++++++---------------------------- utils/repo_utils.go | 30 ++++----- 3 files changed, 66 insertions(+), 120 deletions(-) diff --git a/main.go b/main.go index 2499b17..5f34ceb 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,7 @@ var showInstalled = false var pkgListNumbers = false var pkgListNames = false var reinstall = false +var noOptional = false var nosync = true func main() { @@ -207,7 +208,7 @@ func resolveCommand() { }]() for _, pkg := range clone.Keys() { value := clone.GetElement(pkg).Value - resolved, u, err := utils.ResolveAll(value.pkgInfo, false, !reinstall, rootDir) + resolved, u, err := utils.ResolveAll(value.pkgInfo, false, !noOptional, !reinstall, rootDir) if err != nil { log.Fatalf("Could not resolve dependencies for package (%s). Error: %s\n", pkg, err) } @@ -238,7 +239,7 @@ func resolveCommand() { if err != nil { log.Fatalf("Could not read package. Error: %s\n", err) } - resolved, u, err := utils.ResolveAll(entry.Info, false, !reinstall, rootDir) + resolved, u, err := utils.ResolveAll(entry.Info, false, !noOptional, !reinstall, rootDir) if err != nil { log.Fatalf("Could not resolve dependencies for package (%s). Error: %s\n", pkg, err) } @@ -447,7 +448,7 @@ func printHelp() { fmt.Println(" -R= lets you define the root path which will be used") fmt.Println(" -c lists the amount of installed packages") fmt.Println(" -n lists only the names of installed packages") - fmt.Println("-> bpm install [-R, -v, -y, -f, -o, -c, -b, -k] | installs the following files") + fmt.Println("-> bpm install [-R, -v, -y, -f, -o, -c, -b, -k, --reinstall, --no-optional] | installs the following files") fmt.Println(" -R= lets you define the root path which will be used") fmt.Println(" -v Show additional information about what BPM is doing") fmt.Println(" -y skips the confirmation prompt") @@ -456,6 +457,8 @@ func printHelp() { fmt.Println(" -c= set the compilation directory (defaults to /var/tmp)") fmt.Println(" -b creates a binary package from a source package after compilation and saves it in the binary package output directory") fmt.Println(" -k keeps the compilation directory created by BPM after source package installation") + fmt.Println(" --reinstall Reinstalls packages even if they do not have a newer version available") + fmt.Println(" --no-optional Prevents installation of optional dependencies") fmt.Println("-> bpm update [-R, -v, -y, -f, --reinstall, --nosync] | updates all packages that are available in the repositories") fmt.Println(" -R= lets you define the root path which will be used") fmt.Println(" -v Show additional information about what BPM is doing") @@ -500,6 +503,7 @@ func resolveFlags() { installFlagSet.BoolVar(&keepTempDir, "k", false, "Keep temporary directory after source compilation") installFlagSet.BoolVar(&force, "f", false, "Force installation by skipping architecture and dependency resolution") installFlagSet.BoolVar(&reinstall, "reinstall", false, "Reinstalls packages even if they do not have a newer version available") + installFlagSet.BoolVar(&noOptional, "no-optional", false, "Prevents installation of optional dependencies") installFlagSet.Usage = printHelp // Update flags updateFlagSet := flag.NewFlagSet("Update flags", flag.ExitOnError) diff --git a/utils/package_utils.go b/utils/package_utils.go index 4b3b951..22286f5 100644 --- a/utils/package_utils.go +++ b/utils/package_utils.go @@ -20,23 +20,19 @@ import ( ) type PackageInfo struct { - Name string `yaml:"name,omitempty"` - Description string `yaml:"description,omitempty"` - Version string `yaml:"version,omitempty"` - Url string `yaml:"url,omitempty"` - License string `yaml:"license,omitempty"` - Arch string `yaml:"architecture,omitempty"` - Type string `yaml:"type,omitempty"` - Keep []string `yaml:"keep,omitempty"` - Depends []string `yaml:"depends,omitempty"` - ConditionalDepends map[string][]string `yaml:"conditional_depends,omitempty"` - MakeDepends []string `yaml:"make_depends,omitempty"` - ConditionalMakeDepends map[string][]string `yaml:"conditional_make_depends,omitempty"` - Conflicts []string `yaml:"conflicts,omitempty"` - ConditionalConflicts map[string][]string `yaml:"conditional_conflicts,omitempty"` - Optional []string `yaml:"optional,omitempty"` - ConditionalOptional map[string][]string `yaml:"conditional_optional,omitempty"` - Provides []string `yaml:"provides,omitempty"` + Name string `yaml:"name,omitempty"` + Description string `yaml:"description,omitempty"` + Version string `yaml:"version,omitempty"` + Url string `yaml:"url,omitempty"` + License string `yaml:"license,omitempty"` + Arch string `yaml:"architecture,omitempty"` + Type string `yaml:"type,omitempty"` + Keep []string `yaml:"keep,omitempty"` + Depends []string `yaml:"depends,omitempty"` + MakeDepends []string `yaml:"make_depends,omitempty"` + OptionalDepends []string `yaml:"optional_depends,omitempty"` + Conflicts []string `yaml:"conflicts,omitempty"` + Provides []string `yaml:"provides,omitempty"` } type InstallationReason string @@ -312,23 +308,19 @@ func ExecutePackageScripts(filename, rootDir string, operation Operation, postOp func ReadPackageInfo(contents string, defaultValues bool) (*PackageInfo, error) { pkgInfo := PackageInfo{ - Name: "", - Description: "", - Version: "", - Url: "", - License: "", - Arch: "", - Type: "", - Keep: make([]string, 0), - Depends: make([]string, 0), - ConditionalDepends: make(map[string][]string), - MakeDepends: make([]string, 0), - ConditionalMakeDepends: make(map[string][]string), - Conflicts: make([]string, 0), - ConditionalConflicts: make(map[string][]string), - Optional: make([]string, 0), - ConditionalOptional: make(map[string][]string), - Provides: make([]string, 0), + Name: "", + Description: "", + Version: "", + Url: "", + License: "", + Arch: "", + Type: "", + Keep: make([]string, 0), + Depends: make([]string, 0), + MakeDepends: make([]string, 0), + OptionalDepends: make([]string, 0), + Conflicts: make([]string, 0), + Provides: make([]string, 0), } err := yaml.Unmarshal([]byte(contents), &pkgInfo) if err != nil { @@ -369,18 +361,6 @@ func CreateReadableInfo(showArchitecture, showType, showPackageRelations, showRe } ret = append(ret, fmt.Sprintf("%s: %s", label, strings.Join(array, ", "))) } - appendMap := func(label string, m map[string][]string) { - if len(m) == 0 { - return - } - ret = append(ret, label+":") - for k, v := range m { - if len(v) == 0 { - continue - } - ret = append(ret, fmt.Sprintf(" %s: %s", k, strings.Join(v, ", "))) - } - } ret = append(ret, "Name: "+pkgInfo.Name) ret = append(ret, "Description: "+pkgInfo.Description) ret = append(ret, "Version: "+pkgInfo.Version) @@ -395,13 +375,10 @@ func CreateReadableInfo(showArchitecture, showType, showPackageRelations, showRe if showPackageRelations { appendArray("Dependencies", pkgInfo.Depends) appendArray("Make Dependencies", pkgInfo.MakeDepends) - appendArray("Provided packages", pkgInfo.Provides) + appendArray("Optional dependencies", pkgInfo.OptionalDepends) appendArray("Conflicting packages", pkgInfo.Conflicts) - appendArray("Optional dependencies", pkgInfo.Optional) - appendMap("Conditional dependencies", pkgInfo.ConditionalDepends) - appendMap("Conditional make dependencies", pkgInfo.ConditionalMakeDepends) - appendMap("Conditional conflicting packages", pkgInfo.ConditionalConflicts) - appendMap("Conditional optional dependencies", pkgInfo.ConditionalOptional) + appendArray("Provided packages", pkgInfo.Provides) + } if showRemoteInfo { arr := make([]string, 0) @@ -1248,7 +1225,7 @@ func GetSourceScript(filename string) (string, error) { return "", errors.New("package does not contain a source.sh file") } -func CheckDependencies(pkgInfo *PackageInfo, checkMake, checkConditional bool, rootDir string) []string { +func CheckDependencies(pkgInfo *PackageInfo, checkMake, checkOptional bool, rootDir string) []string { var ret []string for _, dependency := range pkgInfo.Depends { if !IsPackageProvided(dependency, rootDir) { @@ -1262,30 +1239,14 @@ func CheckDependencies(pkgInfo *PackageInfo, checkMake, checkConditional bool, r } } } - if checkConditional { - for condition, dependencies := range pkgInfo.ConditionalDepends { - if !IsPackageInstalled(condition, rootDir) { - continue - } - for _, dependency := range dependencies { - if !IsPackageProvided(dependency, rootDir) { - ret = append(ret, dependency) - } - } - } - if checkMake { - for condition, dependencies := range pkgInfo.ConditionalMakeDepends { - if !IsPackageInstalled(condition, rootDir) { - continue - } - for _, dependency := range dependencies { - if !IsPackageInstalled(dependency, rootDir) { - ret = append(ret, dependency) - } - } + if checkOptional { + for _, dependency := range pkgInfo.OptionalDepends { + if !IsPackageProvided(dependency, rootDir) { + ret = append(ret, dependency) } } } + return ret } @@ -1296,41 +1257,20 @@ func CheckConflicts(pkgInfo *PackageInfo, checkConditional bool, rootDir string) ret = append(ret, conflict) } } - if checkConditional { - for condition, conflicts := range pkgInfo.ConditionalConflicts { - if !IsPackageInstalled(condition, rootDir) { - continue - } - for _, conflict := range conflicts { - if IsPackageInstalled(conflict, rootDir) { - ret = append(ret, conflict) - } - } - } - } return ret } -func ResolveAll(pkgInfo *PackageInfo, checkMake, ignoreInstalled bool, rootDir string) ([]string, []string, error) { +func ResolveAll(pkgInfo *PackageInfo, checkMake, checkOptional, ignoreInstalled bool, rootDir string) ([]string, []string, error) { resolved := make([]string, 0) unresolved := make([]string, 0) toResolve := make([]string, 0) - allDepends := make([]string, 0) toResolve = append(toResolve, pkgInfo.Depends...) - allDepends = append(allDepends, pkgInfo.Depends...) - for condition, depends := range pkgInfo.ConditionalDepends { - if IsPackageInstalled(condition, rootDir) { - allDepends = append(allDepends, depends...) - } - } if checkMake { - allDepends = append(allDepends, pkgInfo.MakeDepends...) - for condition, depends := range pkgInfo.ConditionalMakeDepends { - if IsPackageInstalled(condition, rootDir) { - allDepends = append(allDepends, depends...) - } - } + toResolve = append(toResolve, pkgInfo.MakeDepends...) + } + if checkOptional { + toResolve = append(toResolve, pkgInfo.OptionalDepends...) } resolve := func(depend string) { @@ -1349,6 +1289,12 @@ func ResolveAll(pkgInfo *PackageInfo, checkMake, ignoreInstalled bool, rootDir s } resolved = append(resolved, depend) toResolve = append(toResolve, entry.Info.Depends...) + if checkMake { + toResolve = append(toResolve, entry.Info.MakeDepends...) + } + if checkOptional { + toResolve = append(toResolve, entry.Info.OptionalDepends...) + } } for len(toResolve) > 0 { diff --git a/utils/repo_utils.go b/utils/repo_utils.go index ec2c702..68ce00e 100644 --- a/utils/repo_utils.go +++ b/utils/repo_utils.go @@ -43,23 +43,19 @@ func (repo *Repository) ReadLocalDatabase() error { for _, b := range strings.Split(data, "---") { entry := RepositoryEntry{ Info: &PackageInfo{ - Name: "", - Description: "", - Version: "", - Url: "", - License: "", - Arch: "", - Type: "", - Keep: make([]string, 0), - Depends: make([]string, 0), - ConditionalDepends: make(map[string][]string), - MakeDepends: make([]string, 0), - ConditionalMakeDepends: make(map[string][]string), - Conflicts: make([]string, 0), - ConditionalConflicts: make(map[string][]string), - Optional: make([]string, 0), - ConditionalOptional: make(map[string][]string), - Provides: make([]string, 0), + Name: "", + Description: "", + Version: "", + Url: "", + License: "", + Arch: "", + Type: "", + Keep: make([]string, 0), + Depends: make([]string, 0), + MakeDepends: make([]string, 0), + OptionalDepends: make([]string, 0), + Conflicts: make([]string, 0), + Provides: make([]string, 0), }, Download: "", } -- 2.47.2 From c24b7c85e3b2647df5994252f979cf2ec9f14369 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Sun, 8 Sep 2024 11:49:27 +0300 Subject: [PATCH 7/9] Improved dependency resolution and improved the 'install' subcommand --- main.go | 144 +++++++++++++++++++++++++---------------- utils/package_utils.go | 78 +++++++++------------- 2 files changed, 119 insertions(+), 103 deletions(-) diff --git a/main.go b/main.go index 5f34ceb..17bdd18 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,7 @@ var showInstalled = false var pkgListNumbers = false var pkgListNames = false var reinstall = false +var reinstallAll = false var noOptional = false var nosync = true @@ -208,13 +209,13 @@ func resolveCommand() { }]() for _, pkg := range clone.Keys() { value := clone.GetElement(pkg).Value - resolved, u, err := utils.ResolveAll(value.pkgInfo, false, !noOptional, !reinstall, rootDir) - if err != nil { - log.Fatalf("Could not resolve dependencies for package (%s). Error: %s\n", pkg, err) - } - unresolvedDepends = append(unresolvedDepends, u...) + resolved, unresolved := value.pkgInfo.ResolveAll(&[]string{}, &[]string{}, false, !noOptional, !reinstall, rootDir) + unresolvedDepends = append(unresolvedDepends, unresolved...) for _, depend := range resolved { if _, ok := pkgsToFetch.Get(depend); !ok && depend != value.pkgInfo.Name { + if !reinstallAll && utils.IsPackageInstalled(depend, rootDir) { + continue + } entry, _, err := utils.GetRepositoryEntry(depend) if err != nil { log.Fatalf("Could not find package (%s) in any repository\n", pkg) @@ -228,24 +229,15 @@ func resolveCommand() { pkgsToFetch.Set(pkg, value) } - clone = pkgsToFetch.Copy() - pkgsToFetch = orderedmap.NewOrderedMap[string, *struct { - isDependency bool - pkgInfo *utils.PackageInfo - }]() - for _, pkg := range clone.Keys() { + for _, pkg := range pkgsToInstall.Keys() { value := clone.GetElement(pkg).Value - entry, _, err := utils.GetRepositoryEntry(pkg) - if err != nil { - log.Fatalf("Could not read package. Error: %s\n", err) - } - resolved, u, err := utils.ResolveAll(entry.Info, false, !noOptional, !reinstall, rootDir) - if err != nil { - log.Fatalf("Could not resolve dependencies for package (%s). Error: %s\n", pkg, err) - } - unresolvedDepends = append(unresolvedDepends, u...) + resolved, unresolved := value.pkgInfo.ResolveAll(&[]string{}, &[]string{}, false, !noOptional, !reinstall, rootDir) + unresolvedDepends = append(unresolvedDepends, unresolved...) for _, depend := range resolved { if _, ok := clone.Get(depend); !ok { + if !reinstallAll && utils.IsPackageInstalled(depend, rootDir) { + continue + } entry, _, err := utils.GetRepositoryEntry(depend) if err != nil { log.Fatalf("Could not find package (%s) in any repository\n", pkg) @@ -259,7 +251,64 @@ func resolveCommand() { pkgsToFetch.Set(pkg, value) } + // Show summary + if len(unresolvedDepends) != 0 { + if force { + log.Fatalf("The following dependencies could not be found in any repositories: %s\n", strings.Join(unresolvedDepends, ", ")) + } else { + log.Println("Warning: The following dependencies could not be found in any repositories: " + strings.Join(unresolvedDepends, ", ")) + } + } + if pkgsToInstall.Len()+pkgsToFetch.Len() == 0 { + fmt.Println("All packages are up to date!") + os.Exit(0) + } + for _, pkg := range pkgsToInstall.Keys() { + pkgInfo, err := utils.ReadPackage(pkg) + if err != nil { + if err != nil { + log.Fatalf("Could not read package. Error: %s\n", err) + } + } + installedInfo := utils.GetPackageInfo(pkgInfo.Name, rootDir, false) + if installedInfo == nil { + fmt.Printf("%s: %s (Install)\n", pkgInfo.Name, pkgInfo.Version) + } else if strings.Compare(pkgInfo.Version, installedInfo.Version) < 0 { + fmt.Printf("%s: %s -> %s (Downgrade)\n", pkgInfo.Name, installedInfo.Version, pkgInfo.Version) + } else if strings.Compare(pkgInfo.Version, installedInfo.Version) > 0 { + fmt.Printf("%s: %s -> %s (Upgrade)\n", pkgInfo.Name, installedInfo.Version, pkgInfo.Version) + } else { + fmt.Printf("%s: %s (Reinstall)\n", pkgInfo.Name, pkgInfo.Version) + } + } + for _, pkg := range pkgsToFetch.Keys() { + pkgInfo := pkgsToFetch.GetElement(pkg).Value.pkgInfo + installedInfo := utils.GetPackageInfo(pkgInfo.Name, rootDir, false) + if installedInfo == nil { + fmt.Printf("%s: %s (Install)\n", pkgInfo.Name, pkgInfo.Version) + } else if strings.Compare(pkgInfo.Version, installedInfo.Version) < 0 { + fmt.Printf("%s: %s -> %s (Downgrade)\n", pkgInfo.Name, installedInfo.Version, pkgInfo.Version) + } else if strings.Compare(pkgInfo.Version, installedInfo.Version) > 0 { + fmt.Printf("%s: %s -> %s (Upgrade)\n", pkgInfo.Name, installedInfo.Version, pkgInfo.Version) + } else { + fmt.Printf("%s: %s (Reinstall)\n", pkgInfo.Name, pkgInfo.Version) + } + } + if rootDir != "/" { + fmt.Println("Warning: Operating in " + rootDir) + } + if !yesAll { + reader := bufio.NewReader(os.Stdin) + fmt.Print("Do you wish to install these packages? [y\\N] ") + text, _ := reader.ReadString('\n') + if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" { + fmt.Println("Cancelling...") + os.Exit(1) + } + } + // Fetch packages from repositories + fmt.Println("Fetching packages from available repositories...") for _, pkg := range pkgsToFetch.Keys() { isDependency, _ := pkgsToFetch.Get(pkg) entry, repo, err := utils.GetRepositoryEntry(pkg) @@ -273,40 +322,16 @@ func resolveCommand() { pkgsToInstall.Set(fetchedPackage, isDependency) } - // Install Packages - if rootDir != "/" { - fmt.Println("Warning: Operating in " + rootDir) - } - if pkgsToInstall.Len() == 0 { - fmt.Println("All packages are up to date!") - os.Exit(0) - } - for _, pkg := range pkgsToInstall.Keys() { - pkgInfo := pkgsToInstall.GetElement(pkg).Value.pkgInfo - installedInfo := utils.GetPackageInfo(pkgInfo.Name, rootDir, false) - if installedInfo == nil { - fmt.Printf("%s: %s (Install)\n", pkgInfo.Name, pkgInfo.Version) - } else if strings.Compare(pkgInfo.Version, installedInfo.Version) < 0 { - fmt.Printf("%s: %s -> %s (Downgrade)\n", pkgInfo.Name, installedInfo.Version, pkgInfo.Version) - } else if strings.Compare(pkgInfo.Version, installedInfo.Version) > 0 { - fmt.Printf("%s: %s -> %s (Upgrade)\n", pkgInfo.Name, installedInfo.Version, pkgInfo.Version) - } else { - fmt.Printf("%s: %s (Reinstall)\n", pkgInfo.Name, pkgInfo.Version) - } - } - if !yesAll { - reader := bufio.NewReader(os.Stdin) - fmt.Print("Do you wish to install these packages? [y\\N] ") - text, _ := reader.ReadString('\n') - if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" { - fmt.Println("Cancelling...") - os.Exit(1) - } - } for _, pkg := range pkgsToInstall.Keys() { value, _ := pkgsToInstall.Get(pkg) pkgInfo := value.pkgInfo - err := utils.InstallPackage(pkg, rootDir, verbose, force, buildSource, skipCheck, keepTempDir) + var err error + if value.isDependency { + err = utils.InstallPackage(pkg, rootDir, verbose, true, buildSource, skipCheck, keepTempDir) + } else { + err = utils.InstallPackage(pkg, rootDir, verbose, force, buildSource, skipCheck, keepTempDir) + } + if err != nil { if pkgInfo.Type == "source" && keepTempDir { fmt.Println("BPM temp directory was created at /var/tmp/bpm_source-" + pkgInfo.Name) @@ -441,14 +466,14 @@ func printHelp() { fmt.Println("-> flags will be read if passed right after the subcommand otherwise they will be read as subcommand arguments") fmt.Println("\033[1m---- Command List ----\033[0m") fmt.Println("-> bpm version | shows information on the installed version of bpm") - fmt.Println("-> bpm info [-R] | shows information on an installed package") + fmt.Println("-> bpm info [-R, -i] | shows information on an installed package") fmt.Println(" -R= lets you define the root path which will be used") fmt.Println(" -i shows information about the currently installed package") fmt.Println("-> bpm list [-R, -c, -n] | lists all installed packages") fmt.Println(" -R= lets you define the root path which will be used") fmt.Println(" -c lists the amount of installed packages") fmt.Println(" -n lists only the names of installed packages") - fmt.Println("-> bpm install [-R, -v, -y, -f, -o, -c, -b, -k, --reinstall, --no-optional] | installs the following files") + fmt.Println("-> bpm install [-R, -v, -y, -f, -o, -c, -b, -k, --reinstall, --reinstall-all, --no-optional] | installs the following files") fmt.Println(" -R= lets you define the root path which will be used") fmt.Println(" -v Show additional information about what BPM is doing") fmt.Println(" -y skips the confirmation prompt") @@ -458,15 +483,16 @@ func printHelp() { fmt.Println(" -b creates a binary package from a source package after compilation and saves it in the binary package output directory") fmt.Println(" -k keeps the compilation directory created by BPM after source package installation") fmt.Println(" --reinstall Reinstalls packages even if they do not have a newer version available") + fmt.Println(" --reinstall-all Same as --reinstall but also reinstalls dependencies") fmt.Println(" --no-optional Prevents installation of optional dependencies") - fmt.Println("-> bpm update [-R, -v, -y, -f, --reinstall, --nosync] | updates all packages that are available in the repositories") + fmt.Println("-> bpm update [-R, -v, -y, -f, --reinstall, --no-sync] | updates all packages that are available in the repositories") fmt.Println(" -R= lets you define the root path which will be used") fmt.Println(" -v Show additional information about what BPM is doing") fmt.Println(" -y skips the confirmation prompt") fmt.Println(" -f skips dependency, conflict and architecture checking") fmt.Println(" --reinstall Fetches and reinstalls all packages even if they do not have a newer version available") - fmt.Println(" --nosync Skips package database syncing") - fmt.Println("-> bpm sync [-R, -v] | Syncs package databases without updating packages") + fmt.Println(" --no-sync Skips package database syncing") + fmt.Println("-> bpm sync [-R, -v, -y] | Syncs package databases without updating packages") fmt.Println(" -R= lets you define the root path which will be used") fmt.Println(" -v Show additional information about what BPM is doing") fmt.Println(" -y skips the confirmation prompt") @@ -503,6 +529,7 @@ func resolveFlags() { installFlagSet.BoolVar(&keepTempDir, "k", false, "Keep temporary directory after source compilation") installFlagSet.BoolVar(&force, "f", false, "Force installation by skipping architecture and dependency resolution") installFlagSet.BoolVar(&reinstall, "reinstall", false, "Reinstalls packages even if they do not have a newer version available") + installFlagSet.BoolVar(&reinstallAll, "reinstall-all", false, "Same as --reinstall but also reinstalls dependencies") installFlagSet.BoolVar(&noOptional, "no-optional", false, "Prevents installation of optional dependencies") installFlagSet.Usage = printHelp // Update flags @@ -512,7 +539,7 @@ func resolveFlags() { updateFlagSet.BoolVar(&yesAll, "y", false, "Skip confirmation prompts") updateFlagSet.BoolVar(&force, "f", false, "Force update by skipping architecture and dependency resolution") updateFlagSet.BoolVar(&reinstall, "reinstall", false, "Fetches and reinstalls all packages even if they do not have a newer version available") - updateFlagSet.BoolVar(&nosync, "nosync", false, "Skips package database syncing") + updateFlagSet.BoolVar(&nosync, "no-sync", false, "Skips package database syncing") updateFlagSet.Usage = printHelp // Sync flags syncFlagSet := flag.NewFlagSet("Sync flags", flag.ExitOnError) @@ -578,5 +605,8 @@ func resolveFlags() { } subcommandArgs = fileFlagSet.Args() } + if reinstallAll { + reinstall = true + } } } diff --git a/utils/package_utils.go b/utils/package_utils.go index 22286f5..fbf92c6 100644 --- a/utils/package_utils.go +++ b/utils/package_utils.go @@ -1002,7 +1002,7 @@ func InstallPackage(filename, rootDir string, verbose, force, binaryPkgFromSrc, if pkgInfo.Arch != "any" && pkgInfo.Arch != GetArch() { return errors.New("cannot install a package with a different architecture") } - if unresolved := CheckDependencies(pkgInfo, pkgInfo.Type == "source", true, rootDir); len(unresolved) != 0 { + if unresolved := pkgInfo.CheckDependencies(pkgInfo.Type == "source", true, rootDir); len(unresolved) != 0 { return errors.New("Could not resolve all dependencies. Missing " + strings.Join(unresolved, ", ")) } } @@ -1225,7 +1225,19 @@ func GetSourceScript(filename string) (string, error) { return "", errors.New("package does not contain a source.sh file") } -func CheckDependencies(pkgInfo *PackageInfo, checkMake, checkOptional bool, rootDir string) []string { +func (pkgInfo *PackageInfo) GetAllDependencies(checkMake, checkOptional bool) []string { + allDepends := make([]string, 0) + allDepends = append(allDepends, pkgInfo.Depends...) + if checkMake { + allDepends = append(allDepends, pkgInfo.MakeDepends...) + } + if checkOptional { + allDepends = append(allDepends, pkgInfo.OptionalDepends...) + } + return allDepends +} + +func (pkgInfo *PackageInfo) CheckDependencies(checkMake, checkOptional bool, rootDir string) []string { var ret []string for _, dependency := range pkgInfo.Depends { if !IsPackageProvided(dependency, rootDir) { @@ -1250,7 +1262,7 @@ func CheckDependencies(pkgInfo *PackageInfo, checkMake, checkOptional bool, root return ret } -func CheckConflicts(pkgInfo *PackageInfo, checkConditional bool, rootDir string) []string { +func (pkgInfo *PackageInfo) CheckConflicts(rootDir string) []string { var ret []string for _, conflict := range pkgInfo.Conflicts { if IsPackageInstalled(conflict, rootDir) { @@ -1260,52 +1272,26 @@ func CheckConflicts(pkgInfo *PackageInfo, checkConditional bool, rootDir string) return ret } -func ResolveAll(pkgInfo *PackageInfo, checkMake, checkOptional, ignoreInstalled bool, rootDir string) ([]string, []string, error) { - resolved := make([]string, 0) - unresolved := make([]string, 0) - toResolve := make([]string, 0) - - toResolve = append(toResolve, pkgInfo.Depends...) - if checkMake { - toResolve = append(toResolve, pkgInfo.MakeDepends...) - } - if checkOptional { - toResolve = append(toResolve, pkgInfo.OptionalDepends...) - } - - resolve := func(depend string) { - if slices.Contains(resolved, depend) { - return - } - if ignoreInstalled && IsPackageInstalled(depend, rootDir) { - return - } - entry, _, err := GetRepositoryEntry(depend) - if err != nil { - if !slices.Contains(unresolved, depend) { - unresolved = append(unresolved, depend) +func (pkgInfo *PackageInfo) ResolveAll(resolved, unresolved *[]string, checkMake, checkOptional, ignoreInstalled bool, rootDir string) ([]string, []string) { + *unresolved = append(*unresolved, pkgInfo.Name) + for _, depend := range pkgInfo.GetAllDependencies(checkMake, checkOptional) { + if !slices.Contains(*resolved, depend) { + if slices.Contains(*unresolved, depend) || (ignoreInstalled && IsPackageInstalled(depend, rootDir)) { + continue } - return - } - resolved = append(resolved, depend) - toResolve = append(toResolve, entry.Info.Depends...) - if checkMake { - toResolve = append(toResolve, entry.Info.MakeDepends...) - } - if checkOptional { - toResolve = append(toResolve, entry.Info.OptionalDepends...) + entry, _, err := GetRepositoryEntry(depend) + if err != nil { + if !slices.Contains(*unresolved, depend) { + *unresolved = append(*unresolved, depend) + } + continue + } + entry.Info.ResolveAll(resolved, unresolved, checkMake, checkOptional, ignoreInstalled, rootDir) } } - - for len(toResolve) > 0 { - clone := slices.Clone(toResolve) - toResolve = make([]string, 0) - for _, depend := range clone { - resolve(depend) - } - } - - return resolved, unresolved, nil + *resolved = append(*resolved, pkgInfo.Name) + *unresolved = stringSliceRemove(*unresolved, pkgInfo.Name) + return *resolved, *unresolved } func IsPackageInstalled(pkg, rootDir string) bool { -- 2.47.2 From 6247c6eff7558707c8cc74e4f9143c0120e2b952 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Sun, 8 Sep 2024 12:51:47 +0300 Subject: [PATCH 8/9] Added 'search' subcommand and removed repository functionality from 'info' subcommand --- main.go | 67 +++++++++++++++++++++++++----------------- utils/package_utils.go | 20 ++----------- 2 files changed, 42 insertions(+), 45 deletions(-) diff --git a/main.go b/main.go index 17bdd18..48ddb24 100644 --- a/main.go +++ b/main.go @@ -31,7 +31,6 @@ var buildSource = false var skipCheck = false var keepTempDir = false var force = false -var showInstalled = false var pkgListNumbers = false var pkgListNames = false var reinstall = false @@ -52,6 +51,7 @@ const ( version info list + search install update sync @@ -67,6 +67,8 @@ func getCommandType() commandType { return info case "list": return list + case "search": + return search case "install": return install case "update": @@ -95,29 +97,12 @@ func resolveCommand() { } for n, pkg := range packages { var info *utils.PackageInfo - if _, err := os.Stat(pkg); err == nil && !showInstalled { - info, err = utils.ReadPackage(pkg) - if err != nil { - log.Fatalf("File (%s) could not be read\n", pkg) - } - } else if showInstalled { - info = utils.GetPackageInfo(pkg, rootDir, false) - if info == nil { - log.Fatalf("Package (%s) is not installed\n", pkg) - } - } else { - entry, _, err := utils.GetRepositoryEntry(pkg) - if err != nil { - log.Fatalf("Package (%s) could not be found in any repository\n", pkg) - } - info = entry.Info + info = utils.GetPackageInfo(pkg, rootDir, false) + if info == nil { + log.Fatalf("Package (%s) is not installed\n", pkg) } fmt.Println("----------------") - if showInstalled { - fmt.Println(utils.CreateReadableInfo(true, true, true, false, true, info, rootDir)) - } else { - fmt.Println(utils.CreateReadableInfo(true, true, true, true, true, info, rootDir)) - } + fmt.Println(utils.CreateReadableInfo(true, true, true, info, rootDir)) if n == len(packages)-1 { fmt.Println("----------------") } @@ -145,12 +130,41 @@ func resolveCommand() { fmt.Printf("Package (%s) could not be found\n", pkg) continue } - fmt.Println("----------------\n" + utils.CreateReadableInfo(true, true, true, true, true, info, rootDir)) + fmt.Println("----------------\n" + utils.CreateReadableInfo(true, true, true, info, rootDir)) if n == len(packages)-1 { fmt.Println("----------------") } } } + case search: + searchTerms := subcommandArgs + if len(searchTerms) == 0 { + fmt.Println("No search terms given") + os.Exit(0) + } + + for _, term := range searchTerms { + nameResults := make([]*utils.PackageInfo, 0) + descResults := make([]*utils.PackageInfo, 0) + for _, repo := range utils.BPMConfig.Repositories { + for _, entry := range repo.Entries { + if strings.Contains(entry.Info.Name, term) { + nameResults = append(nameResults, entry.Info) + } else if strings.Contains(entry.Info.Description, term) { + descResults = append(descResults, entry.Info) + } + } + } + results := append(nameResults, descResults...) + if len(results) == 0 { + log.Fatalf("No results for term (%s) were found\n", term) + } + fmt.Printf("Results for term (%s)\n", term) + for i, result := range results { + fmt.Println("----------------") + fmt.Printf("%d) %s: %s (%s)\n", i+1, result.Name, result.Description, result.Version) + } + } case install: if os.Getuid() != 0 { fmt.Println("This subcommand needs to be run with superuser permissions") @@ -387,7 +401,7 @@ func resolveCommand() { fmt.Printf("Package (%s) could not be found\n", pkg) continue } - fmt.Println("----------------\n" + utils.CreateReadableInfo(true, true, true, true, true, pkgInfo, rootDir)) + fmt.Println("----------------\n" + utils.CreateReadableInfo(false, false, false, pkgInfo, rootDir)) fmt.Println("----------------") if rootDir != "/" { fmt.Println("Warning: Operating in " + rootDir) @@ -466,13 +480,13 @@ func printHelp() { fmt.Println("-> flags will be read if passed right after the subcommand otherwise they will be read as subcommand arguments") fmt.Println("\033[1m---- Command List ----\033[0m") fmt.Println("-> bpm version | shows information on the installed version of bpm") - fmt.Println("-> bpm info [-R, -i] | shows information on an installed package") + fmt.Println("-> bpm info [-R] | shows information on an installed package") fmt.Println(" -R= lets you define the root path which will be used") - fmt.Println(" -i shows information about the currently installed package") fmt.Println("-> bpm list [-R, -c, -n] | lists all installed packages") fmt.Println(" -R= lets you define the root path which will be used") fmt.Println(" -c lists the amount of installed packages") fmt.Println(" -n lists only the names of installed packages") + fmt.Println("-> bpm search | Searches for packages through declared repositories") fmt.Println("-> bpm install [-R, -v, -y, -f, -o, -c, -b, -k, --reinstall, --reinstall-all, --no-optional] | installs the following files") fmt.Println(" -R= lets you define the root path which will be used") fmt.Println(" -v Show additional information about what BPM is doing") @@ -515,7 +529,6 @@ func resolveFlags() { // Info flags infoFlagSet := flag.NewFlagSet("Info flags", flag.ExitOnError) infoFlagSet.StringVar(&rootDir, "R", "/", "Set the destination root") - infoFlagSet.BoolVar(&showInstalled, "i", false, "Shows information about the currently installed package") infoFlagSet.Usage = printHelp // Install flags installFlagSet := flag.NewFlagSet("Install flags", flag.ExitOnError) diff --git a/utils/package_utils.go b/utils/package_utils.go index fbf92c6..d0c34cb 100644 --- a/utils/package_utils.go +++ b/utils/package_utils.go @@ -353,7 +353,7 @@ func CreateInfoFile(pkgInfo *PackageInfo) string { return string(bytes) } -func CreateReadableInfo(showArchitecture, showType, showPackageRelations, showRemoteInfo, showInstallationReason bool, pkgInfo *PackageInfo, rootDir string) string { +func CreateReadableInfo(showArchitecture, showType, showPackageRelations bool, pkgInfo *PackageInfo, rootDir string) string { ret := make([]string, 0) appendArray := func(label string, array []string) { if len(array) == 0 { @@ -380,23 +380,7 @@ func CreateReadableInfo(showArchitecture, showType, showPackageRelations, showRe appendArray("Provided packages", pkgInfo.Provides) } - if showRemoteInfo { - arr := make([]string, 0) - for _, repo := range BPMConfig.Repositories { - if repo.ContainsPackage(pkgInfo.Name) { - arr = append(arr, repo.Name) - } - } - appendArray("Repositories", arr) - } - if showInstallationReason { - if IsPackageInstalled(pkgInfo.Name, rootDir) { - ret = append(ret, "Installed: yes") - ret = append(ret, "Installation Reason: "+string(GetInstallationReason(pkgInfo.Name, rootDir))) - } else { - ret = append(ret, "Installed: no") - } - } + ret = append(ret, "Installation Reason: "+string(GetInstallationReason(pkgInfo.Name, rootDir))) return strings.Join(ret, "\n") } -- 2.47.2 From bc489ebd237e4b849b9487152b37674234fe2fe0 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Mon, 9 Sep 2024 11:33:48 +0300 Subject: [PATCH 9/9] Added 'update' subcommand and small fix to the 'install' subcommand --- main.go | 164 ++++++++++++++++++++++++++++++++++++++++++++++-- utils/config.go | 13 ++-- 2 files changed, 167 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 48ddb24..94ae97e 100644 --- a/main.go +++ b/main.go @@ -244,11 +244,11 @@ func resolveCommand() { } for _, pkg := range pkgsToInstall.Keys() { - value := clone.GetElement(pkg).Value + value, _ := pkgsToInstall.Get(pkg) resolved, unresolved := value.pkgInfo.ResolveAll(&[]string{}, &[]string{}, false, !noOptional, !reinstall, rootDir) unresolvedDepends = append(unresolvedDepends, unresolved...) for _, depend := range resolved { - if _, ok := clone.Get(depend); !ok { + if _, ok := clone.Get(depend); !ok && depend != value.pkgInfo.Name { if !reinstallAll && utils.IsPackageInstalled(depend, rootDir) { continue } @@ -262,7 +262,6 @@ func resolveCommand() { }{isDependency: true, pkgInfo: entry.Info}) } } - pkgsToFetch.Set(pkg, value) } // Show summary @@ -313,7 +312,7 @@ func resolveCommand() { } if !yesAll { reader := bufio.NewReader(os.Stdin) - fmt.Print("Do you wish to install these packages? [y\\N] ") + fmt.Printf("Do you wish to install these %d packages? [y\\N] ", pkgsToInstall.Len()+pkgsToFetch.Len()) text, _ := reader.ReadString('\n') if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" { fmt.Println("Cancelling...") @@ -336,6 +335,7 @@ func resolveCommand() { pkgsToInstall.Set(fetchedPackage, isDependency) } + // Install fetched packages for _, pkg := range pkgsToInstall.Keys() { value, _ := pkgsToInstall.Get(pkg) pkgInfo := value.pkgInfo @@ -346,6 +346,162 @@ func resolveCommand() { err = utils.InstallPackage(pkg, rootDir, verbose, force, buildSource, skipCheck, keepTempDir) } + if err != nil { + if pkgInfo.Type == "source" && keepTempDir { + fmt.Println("BPM temp directory was created at /var/tmp/bpm_source-" + pkgInfo.Name) + } + log.Fatalf("Could not install package (%s). Error: %s\n", pkg, err) + } + fmt.Printf("Package (%s) was successfully installed!\n", pkgInfo.Name) + if value.isDependency { + err := utils.SetInstallationReason(pkgInfo.Name, utils.Dependency, rootDir) + if err != nil { + log.Fatalf("Could not set installation reason for package\nError: %s\n", err) + } + } + if pkgInfo.Type == "source" && keepTempDir { + fmt.Println("** It is recommended you delete the temporary bpm folder in /var/tmp **") + } + } + case update: + if os.Getuid() != 0 { + fmt.Println("This subcommand needs to be run with superuser permissions") + os.Exit(0) + } + + // Sync repositories + if !nosync { + for _, repo := range utils.BPMConfig.Repositories { + fmt.Printf("Fetching package database for repository (%s)...\n", repo.Name) + err := repo.SyncLocalDatabase() + if err != nil { + log.Fatal(err) + } + } + fmt.Println("All package databases synced successfully!") + } + + utils.ReadConfig() + + // Get installed packages and check for updates + pkgs, err := utils.GetInstalledPackages(rootDir) + if err != nil { + log.Fatalf("Could not get installed packages! Error: %s\n", err) + } + toUpdate := orderedmap.NewOrderedMap[string, *struct { + isDependency bool + entry *utils.RepositoryEntry + }]() + for _, pkg := range pkgs { + entry, _, err := utils.GetRepositoryEntry(pkg) + if err != nil { + continue + } + installedInfo := utils.GetPackageInfo(pkg, rootDir, true) + if installedInfo == nil { + log.Fatalf(pkg) + } + if strings.Compare(entry.Info.Version, installedInfo.Version) > 0 { + toUpdate.Set(entry.Info.Name, &struct { + isDependency bool + entry *utils.RepositoryEntry + }{isDependency: false, entry: entry}) + } else if reinstall { + toUpdate.Set(entry.Info.Name, &struct { + isDependency bool + entry *utils.RepositoryEntry + }{isDependency: false, entry: entry}) + } + } + if toUpdate.Len() == 0 { + fmt.Println("All packages are up to date!") + os.Exit(0) + } + + // Check for new dependencies in updated packages + unresolved := make([]string, 0) + clone := toUpdate.Copy() + for _, key := range clone.Keys() { + pkg, _ := clone.Get(key) + r, u := pkg.entry.Info.ResolveAll(&[]string{}, &[]string{}, false, !noOptional, true, rootDir) + unresolved = append(unresolved, u...) + for _, depend := range r { + if _, ok := toUpdate.Get(depend); !ok { + entry, _, err := utils.GetRepositoryEntry(depend) + if err != nil { + log.Fatalf("Could not find package (%s) in any repository\n", depend) + } + toUpdate.Set(depend, &struct { + isDependency bool + entry *utils.RepositoryEntry + }{isDependency: true, entry: entry}) + } + } + } + + if len(unresolved) != 0 { + if force { + log.Fatalf("The following dependencies could not be found in any repositories: %s\n", strings.Join(unresolved, ", ")) + } else { + log.Println("Warning: The following dependencies could not be found in any repositories: " + strings.Join(unresolved, ", ")) + } + } + + for _, key := range toUpdate.Keys() { + value, _ := toUpdate.Get(key) + installedInfo := utils.GetPackageInfo(value.entry.Info.Name, rootDir, true) + if installedInfo == nil { + fmt.Printf("%s: %s (Install)\n", value.entry.Info.Name, value.entry.Info.Version) + continue + } + if strings.Compare(value.entry.Info.Version, installedInfo.Version) > 0 { + fmt.Printf("%s: %s -> %s (Upgrade)\n", value.entry.Info.Name, installedInfo.Version, value.entry.Info.Version) + } else if reinstall { + fmt.Printf("%s: %s -> %s (Reinstall)\n", value.entry.Info.Name, installedInfo.Version, value.entry.Info.Version) + } + } + + // Update confirmation prompt + if !yesAll { + fmt.Printf("Are you sure you wish to update all %d packages? [y\\N] ", toUpdate.Len()) + reader := bufio.NewReader(os.Stdin) + text, _ := reader.ReadString('\n') + if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" { + fmt.Println("Cancelling update...") + os.Exit(0) + } + } + + // Fetch packages + pkgsToInstall := orderedmap.NewOrderedMap[string, *struct { + isDependency bool + entry *utils.RepositoryEntry + }]() + fmt.Println("Fetching packages from available repositories...") + for _, pkg := range toUpdate.Keys() { + isDependency, _ := toUpdate.Get(pkg) + entry, repo, err := utils.GetRepositoryEntry(pkg) + if err != nil { + log.Fatalf("Could not find package (%s) in any repository\n", pkg) + } + fetchedPackage, err := repo.FetchPackage(entry.Info.Name) + if err != nil { + log.Fatalf("Could not fetch package (%s). Error: %s\n", pkg, err) + } + pkgsToInstall.Set(fetchedPackage, isDependency) + } + + // Install fetched packages + for _, pkg := range pkgsToInstall.Keys() { + value, _ := pkgsToInstall.Get(pkg) + pkgInfo := value.entry.Info + var err error + if value.isDependency { + err = utils.InstallPackage(pkg, rootDir, verbose, true, buildSource, skipCheck, keepTempDir) + } else { + err = utils.InstallPackage(pkg, rootDir, verbose, force, buildSource, skipCheck, keepTempDir) + } + if err != nil { if pkgInfo.Type == "source" && keepTempDir { fmt.Println("BPM temp directory was created at /var/tmp/bpm_source-" + pkgInfo.Name) diff --git a/utils/config.go b/utils/config.go index 8f62a9d..97edef6 100644 --- a/utils/config.go +++ b/utils/config.go @@ -14,12 +14,7 @@ type BPMConfigStruct struct { Repositories []*Repository `yaml:"repositories"` } -var BPMConfig BPMConfigStruct = BPMConfigStruct{ - CompilationEnv: make([]string, 0), - SilentCompilation: false, - BinaryOutputDir: "/var/lib/bpm/compiled/", - CompilationDir: "/var/tmp/", -} +var BPMConfig BPMConfigStruct func ReadConfig() { if _, err := os.Stat("/etc/bpm.conf"); os.IsNotExist(err) { @@ -29,6 +24,12 @@ func ReadConfig() { if err != nil { log.Fatal(err) } + BPMConfig = BPMConfigStruct{ + CompilationEnv: make([]string, 0), + SilentCompilation: false, + BinaryOutputDir: "/var/lib/bpm/compiled/", + CompilationDir: "/var/tmp/", + } err = yaml.Unmarshal(bytes, &BPMConfig) if err != nil { log.Fatal(err) -- 2.47.2