Add repository functionality to BPM #4

Merged
EnumDev merged 9 commits from remote-repositories-functionality into master 2024-09-09 08:45:45 +00:00
5 changed files with 197 additions and 99 deletions
Showing only changes of commit 59df2324e6 - Show all commits

5
go.mod
View File

@ -2,4 +2,7 @@ module gitlab.com/bubble-package-manager/bpm
go 1.22 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
)

2
go.sum
View File

@ -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/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 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

180
main.go
View File

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"flag" "flag"
"fmt" "fmt"
"github.com/elliotchance/orderedmap/v2"
"gitlab.com/bubble-package-manager/bpm/utils" "gitlab.com/bubble-package-manager/bpm/utils"
"log" "log"
"os" "os"
@ -106,12 +107,12 @@ func resolveCommand() {
continue continue
} }
} else { } else {
entry, err := utils.GetRepositoryEntry(pkg) entry, _, err := utils.GetRepositoryEntry(pkg)
if err != nil { if err != nil {
fmt.Printf("Package (%s) could not be found in any repository\n", pkg) fmt.Printf("Package (%s) could not be found in any repository\n", pkg)
continue continue
} }
info = &entry.Info info = entry.Info
} }
fmt.Println("----------------") fmt.Println("----------------")
if showInstalled { if showInstalled {
@ -157,91 +158,115 @@ func resolveCommand() {
fmt.Println("This subcommand needs to be run with superuser permissions") fmt.Println("This subcommand needs to be run with superuser permissions")
os.Exit(0) os.Exit(0)
} }
files := subcommandArgs pkgs := subcommandArgs
if len(files) == 0 { if len(pkgs) == 0 {
fmt.Println("No files were given to install") fmt.Println("No packages or files were given to install")
return 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 { if err != nil {
log.Fatalf("Could not read package\nError: %s\n", err) log.Fatalf("Could not read package. Error: %s\n", err)
} }
if !yesAll { if !reinstall && utils.IsPackageInstalled(pkgInfo.Name, rootDir) {
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 continue
} }
verb = "build" pkgsToInstall.Set(pkg, &struct {
} isDependency bool
if !force { pkgInfo *utils.PackageInfo
if pkgInfo.Arch != "any" && pkgInfo.Arch != utils.GetArch() { }{isDependency: false, pkgInfo: pkgInfo})
fmt.Printf("skipping... cannot %s a package with a different architecture\n", verb) } else {
continue entry, _, err := utils.GetRepositoryEntry(pkg)
}
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 { if err != nil {
log.Fatalf("Could not read source script\nError: %s\n", err) log.Fatalf("Could not find package (%s) in any repository\n", pkg)
} }
fmt.Println(script) if !reinstall && utils.IsPackageInstalled(entry.Info.Name, rootDir) {
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 continue
} }
} pkgsToFetch.Set(entry.Info.Name, &struct {
} else if !yesAll { isDependency bool
reader := bufio.NewReader(os.Stdin) pkgInfo *utils.PackageInfo
fmt.Printf("Do you wish to %s this package? [y\\N] ", verb) }{isDependency: false, pkgInfo: entry.Info})
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
} }
} }
err = utils.InstallPackage(file, rootDir, verbose, force, buildSource, skipCheck, keepTempDir) // Check for dependencies and conflicts
for _, pkg := range pkgsToFetch.Keys() {
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)
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 err != nil {
if pkgInfo.Type == "source" && keepTempDir { if pkgInfo.Type == "source" && keepTempDir {
fmt.Println("BPM temp directory was created at /var/tmp/bpm_source-" + pkgInfo.Name) 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) log.Fatalf("Could not install package\nError: %s\n", err)
} }
fmt.Printf("Package (%s) was successfully installed!\n", pkgInfo.Name) 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 { if pkgInfo.Type == "source" && keepTempDir {
fmt.Println("** It is recommended you delete the temporary bpm folder in /var/tmp **") 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(&skipCheck, "s", false, "Skip check function during source compilation")
installFlagSet.BoolVar(&keepTempDir, "k", false, "Keep temporary directory after 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(&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 installFlagSet.Usage = printHelp
// Update flags // Update flags
updateFlagSet := flag.NewFlagSet("Update flags", flag.ExitOnError) updateFlagSet := flag.NewFlagSet("Update flags", flag.ExitOnError)

View File

@ -1311,6 +1311,55 @@ func CheckConflicts(pkgInfo *PackageInfo, checkConditional bool, rootDir string)
return ret 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 { func IsPackageInstalled(pkg, rootDir string) bool {
installedDir := path.Join(rootDir, "var/lib/bpm/installed/") installedDir := path.Join(rootDir, "var/lib/bpm/installed/")
pkgDir := path.Join(installedDir, pkg) pkgDir := path.Join(installedDir, pkg)
@ -1329,7 +1378,11 @@ func IsPackageProvided(pkg, rootDir string) bool {
if p == pkg { if p == pkg {
return true 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 return true
} }
} }

View File

@ -19,7 +19,7 @@ type Repository struct {
} }
type RepositoryEntry struct { type RepositoryEntry struct {
Info PackageInfo `yaml:"info"` Info *PackageInfo `yaml:"info"`
Download string `yaml:"download"` Download string `yaml:"download"`
} }
@ -42,7 +42,7 @@ func (repo *Repository) ReadLocalDatabase() error {
data := string(bytes) data := string(bytes)
for _, b := range strings.Split(data, "---") { for _, b := range strings.Split(data, "---") {
entry := RepositoryEntry{ entry := RepositoryEntry{
Info: PackageInfo{ Info: &PackageInfo{
Name: "", Name: "",
Description: "", Description: "",
Version: "", Version: "",
@ -110,52 +110,60 @@ func GetRepository(name string) *Repository {
return nil return nil
} }
func GetRepositoryEntry(str string) (*RepositoryEntry, error) { func GetRepositoryEntry(str string) (*RepositoryEntry, *Repository, error) {
split := strings.Split(str, "/") split := strings.Split(str, "/")
if len(split) == 1 { if len(split) == 1 {
pkgName := strings.TrimSpace(split[0]) pkgName := strings.TrimSpace(split[0])
if pkgName == "" { 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 { for _, repo := range BPMConfig.Repositories {
if repo.ContainsPackage(pkgName) { 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 { } else if len(split) == 2 {
repoName := strings.TrimSpace(split[0]) repoName := strings.TrimSpace(split[0])
pkgName := strings.TrimSpace(split[1]) pkgName := strings.TrimSpace(split[1])
if repoName == "" || pkgName == "" { 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) repo := GetRepository(repoName)
if repo == nil || !repo.ContainsPackage(pkgName) { 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 { } 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) { if !repo.ContainsPackage(pkg) {
return "", errors.New("Could not fetch package '" + pkg + "'") return "", errors.New("Could not fetch package '" + pkg + "'")
} }
entry := repo.Entries[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 { if err != nil {
return "", err return "", err
} }
defer resp.Body.Close() 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 { if err != nil {
return "", err return "", err
} }
defer out.Close() defer out.Close()
_, err = io.Copy(out, resp.Body) _, err = io.Copy(out, resp.Body)
return savePath, nil return "/var/cache/bpm/packages/" + path.Base(entry.Download), nil
} }