Skip to content

Commit

Permalink
fix(install): remove alpmHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
Jguer committed Aug 16, 2020
1 parent 9fd6917 commit 005b293
Show file tree
Hide file tree
Showing 15 changed files with 346 additions and 399 deletions.
5 changes: 1 addition & 4 deletions clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ func cleanAUR(keepInstalled, keepCurrent, removeAll bool, alpmHandle *alpm.Handl
installedBases := make(stringset.StringSet)
inAURBases := make(stringset.StringSet)

remotePackages, _, err := query.GetRemotePackages(alpmHandle)
if err != nil {
return err
}
remotePackages, _ := query.GetRemotePackages(config.Runtime.DBExecutor)

files, err := ioutil.ReadDir(config.BuildDir)
if err != nil {
Expand Down
15 changes: 8 additions & 7 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/leonelquinteros/gotext"

"github.com/Jguer/yay/v10/pkg/completion"
"github.com/Jguer/yay/v10/pkg/db"
"github.com/Jguer/yay/v10/pkg/intrange"
"github.com/Jguer/yay/v10/pkg/news"
"github.com/Jguer/yay/v10/pkg/query"
Expand Down Expand Up @@ -169,7 +170,7 @@ func handleCmd(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle) error {
case "G", "getpkgbuild":
return handleGetpkgbuild(cmdArgs, alpmHandle)
case "P", "show":
return handlePrint(cmdArgs, alpmHandle)
return handlePrint(cmdArgs, alpmHandle, config.Runtime.DBExecutor)
case "Y", "--yay":
return handleYay(cmdArgs, alpmHandle)
}
Expand Down Expand Up @@ -217,7 +218,7 @@ func lastBuildTime(alpmHandle *alpm.Handle) time.Time {
return lastTime
}

func handlePrint(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle) (err error) {
func handlePrint(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle, dbExecutor *db.AlpmExecutor) (err error) {
switch {
case cmdArgs.ExistsArg("d", "defaultconfig"):
tmpConfig := settings.MakeConfig()
Expand All @@ -232,9 +233,9 @@ func handlePrint(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle) (err erro
quiet := cmdArgs.ExistsArg("q", "quiet")
err = news.PrintNewsFeed(lastBuildTime(alpmHandle), config.SortMode, double, quiet)
case cmdArgs.ExistsDouble("c", "complete"):
err = completion.Show(alpmHandle, config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, true)
err = completion.Show(dbExecutor, config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, true)
case cmdArgs.ExistsArg("c", "complete"):
err = completion.Show(alpmHandle, config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, false)
err = completion.Show(dbExecutor, config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, false)
case cmdArgs.ExistsArg("s", "stats"):
err = localStatistics(alpmHandle)
default:
Expand Down Expand Up @@ -295,10 +296,10 @@ func handleSync(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle) error {
return syncInfo(cmdArgs, targets, alpmHandle)
}
if cmdArgs.ExistsArg("u", "sysupgrade") {
return install(cmdArgs, alpmHandle, false)
return install(cmdArgs, config.Runtime.DBExecutor, false)
}
if len(cmdArgs.Targets) > 0 {
return install(cmdArgs, alpmHandle, false)
return install(cmdArgs, config.Runtime.DBExecutor, false)
}
if cmdArgs.ExistsArg("y", "refresh") {
return show(passToPacman(cmdArgs))
Expand Down Expand Up @@ -426,7 +427,7 @@ func displayNumberMenu(pkgS []string, alpmHandle *alpm.Handle, cmdArgs *settings
sudoLoopBackground()
}

return install(arguments, alpmHandle, true)
return install(arguments, config.Runtime.DBExecutor, true)
}

func syncList(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle) error {
Expand Down
54 changes: 21 additions & 33 deletions install.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/leonelquinteros/gotext"

"github.com/Jguer/yay/v10/pkg/completion"
"github.com/Jguer/yay/v10/pkg/db"
"github.com/Jguer/yay/v10/pkg/dep"
"github.com/Jguer/yay/v10/pkg/intrange"
"github.com/Jguer/yay/v10/pkg/multierror"
Expand All @@ -23,6 +24,7 @@ import (
"github.com/Jguer/yay/v10/pkg/settings"
"github.com/Jguer/yay/v10/pkg/stringset"
"github.com/Jguer/yay/v10/pkg/text"
"github.com/Jguer/yay/v10/pkg/upgrade"
)

const gitEmptyTree = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
Expand Down Expand Up @@ -60,12 +62,12 @@ func asexp(cmdArgs *settings.Arguments, pkgs []string) error {
}

// Install handles package installs
func install(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle, ignoreProviders bool) (err error) {
func install(cmdArgs *settings.Arguments, dbExecutor *db.AlpmExecutor, ignoreProviders bool) (err error) {
var incompatible stringset.StringSet
var do *dep.Order

var aurUp upSlice
var repoUp upSlice
var aurUp upgrade.UpSlice
var repoUp upgrade.UpSlice

var srcinfos map[string]*gosrc.Srcinfo

Expand All @@ -80,7 +82,7 @@ func install(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle, ignoreProvide
}
}
} else if cmdArgs.ExistsArg("y", "refresh") || cmdArgs.ExistsArg("u", "sysupgrade") || len(cmdArgs.Targets) > 0 {
err = earlyPacmanCall(cmdArgs, alpmHandle)
err = earlyPacmanCall(cmdArgs, config.Runtime.DBExecutor)
if err != nil {
return err
}
Expand All @@ -89,17 +91,12 @@ func install(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle, ignoreProvide

// we may have done -Sy, our handle now has an old
// database.
alpmHandle, err = initAlpmHandle(config.Runtime.PacmanConf, alpmHandle)
if err != nil {
return err
}
config.Runtime.AlpmHandle = alpmHandle
err = config.Runtime.DBExecutor.RefreshHandle()
if err != nil {
return err
}

localNames, remoteNames, err := query.GetPackageNamesBySource(alpmHandle)
localNames, remoteNames, err := query.GetPackageNamesBySource(dbExecutor)
if err != nil {
return err
}
Expand All @@ -122,7 +119,7 @@ func install(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle, ignoreProvide

// if we are doing -u also request all packages needing update
if cmdArgs.ExistsArg("u", "sysupgrade") {
aurUp, repoUp, err = upList(warnings, alpmHandle, cmdArgs.ExistsDouble("u", "sysupgrade"))
aurUp, repoUp, err = upList(warnings, dbExecutor, cmdArgs.ExistsDouble("u", "sysupgrade"))
if err != nil {
return err
}
Expand Down Expand Up @@ -324,7 +321,7 @@ func install(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle, ignoreProvide
config.NoConfirm = oldValue
}

incompatible, err = getIncompatible(do.Aur, srcinfos, alpmHandle)
incompatible, err = getIncompatible(do.Aur, srcinfos, dbExecutor)
if err != nil {
return err
}
Expand Down Expand Up @@ -369,14 +366,14 @@ func install(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle, ignoreProvide
}
}

go exitOnError(completion.Update(alpmHandle, config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, false))
go exitOnError(completion.Update(dbExecutor, config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, false))

err = downloadPkgbuildsSources(do.Aur, incompatible)
if err != nil {
return err
}

err = buildInstallPkgbuilds(cmdArgs, alpmHandle, dp, do, srcinfos, incompatible, conflicts)
err = buildInstallPkgbuilds(cmdArgs, dbExecutor, dp, do, srcinfos, incompatible, conflicts)
if err != nil {
return err
}
Expand All @@ -403,7 +400,7 @@ func removeMake(do *dep.Order) error {
return err
}

func inRepos(syncDB alpm.DBList, pkg string) bool {
func inRepos(dbExecutor *db.AlpmExecutor, pkg string) bool {
target := dep.ToTarget(pkg)

if target.DB == "aur" {
Expand All @@ -414,33 +411,25 @@ func inRepos(syncDB alpm.DBList, pkg string) bool {

previousHideMenus := settings.HideMenus
settings.HideMenus = false
_, err := syncDB.FindSatisfier(target.DepString())
exists := dbExecutor.SyncSatisfierExists(target.DepString())
settings.HideMenus = previousHideMenus
if err == nil {
return true
}

return !syncDB.FindGroupPkgs(target.Name).Empty()
return exists || len(dbExecutor.PackagesFromGroup(target.Name)) > 0
}

func earlyPacmanCall(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle) error {
func earlyPacmanCall(cmdArgs *settings.Arguments, dbExecutor *db.AlpmExecutor) error {
arguments := cmdArgs.Copy()
arguments.Op = "S"
targets := cmdArgs.Targets
cmdArgs.ClearTargets()
arguments.ClearTargets()

syncDB, err := alpmHandle.SyncDBs()
if err != nil {
return err
}

if config.Runtime.Mode == settings.ModeRepo {
arguments.Targets = targets
} else {
// separate aur and repo targets
for _, target := range targets {
if inRepos(syncDB, target) {
if inRepos(dbExecutor, target) {
arguments.AddTarget(target)
} else {
cmdArgs.AddTarget(target)
Expand All @@ -449,8 +438,7 @@ func earlyPacmanCall(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle) error
}

if cmdArgs.ExistsArg("y", "refresh") || cmdArgs.ExistsArg("u", "sysupgrade") || len(arguments.Targets) > 0 {
err = show(passToPacman(arguments))
if err != nil {
if err := show(passToPacman(arguments)); err != nil {
return errors.New(gotext.Get("error installing repo packages"))
}
}
Expand All @@ -469,10 +457,10 @@ func earlyRefresh(cmdArgs *settings.Arguments) error {
return show(passToPacman(arguments))
}

func getIncompatible(bases []dep.Base, srcinfos map[string]*gosrc.Srcinfo, alpmHandle *alpm.Handle) (stringset.StringSet, error) {
func getIncompatible(bases []dep.Base, srcinfos map[string]*gosrc.Srcinfo, dbExecutor *db.AlpmExecutor) (stringset.StringSet, error) {
incompatible := make(stringset.StringSet)
basesMap := make(map[string]dep.Base)
alpmArch, err := alpmHandle.Arch()
alpmArch, err := dbExecutor.AlpmArch()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -943,7 +931,7 @@ func downloadPkgbuildsSources(bases []dep.Base, incompatible stringset.StringSet

func buildInstallPkgbuilds(
cmdArgs *settings.Arguments,
alpmHandle *alpm.Handle,
dbExecutor *db.AlpmExecutor,
dp *dep.Pool,
do *dep.Order,
srcinfos map[string]*gosrc.Srcinfo,
Expand All @@ -968,7 +956,7 @@ func buildInstallPkgbuilds(
config.NoConfirm = true

//remotenames: names of all non repo packages on the system
localNames, remoteNames, err := query.GetPackageNamesBySource(alpmHandle)
localNames, remoteNames, err := query.GetPackageNamesBySource(dbExecutor)
if err != nil {
return err
}
Expand Down
27 changes: 11 additions & 16 deletions pkg/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"strings"
"time"

alpm "github.com/Jguer/go-alpm"
"github.com/Jguer/yay/v10/pkg/db"
)

// Show provides completion info for shells
func Show(alpmHandle *alpm.Handle, aurURL, completionPath string, interval int, force bool) error {
err := Update(alpmHandle, aurURL, completionPath, interval, force)
func Show(dbExecutor *db.AlpmExecutor, aurURL, completionPath string, interval int, force bool) error {
err := Update(dbExecutor, aurURL, completionPath, interval, force)
if err != nil {
return err
}
Expand All @@ -33,7 +33,7 @@ func Show(alpmHandle *alpm.Handle, aurURL, completionPath string, interval int,
}

// Update updates completion cache to be used by Complete
func Update(alpmHandle *alpm.Handle, aurURL, completionPath string, interval int, force bool) error {
func Update(dbExecutor *db.AlpmExecutor, aurURL, completionPath string, interval int, force bool) error {
info, err := os.Stat(completionPath)

if os.IsNotExist(err) || (interval != -1 && time.Since(info.ModTime()).Hours() >= float64(interval*24)) || force {
Expand All @@ -50,11 +50,7 @@ func Update(alpmHandle *alpm.Handle, aurURL, completionPath string, interval int
defer os.Remove(completionPath)
}

dbList, err := alpmHandle.SyncDBs()
if err != nil {
return err
}
erra := createRepoList(&dbList, out)
erra := createRepoList(dbExecutor, out)

out.Close()
return erra
Expand Down Expand Up @@ -97,13 +93,12 @@ func createAURList(aurURL string, out io.Writer) error {
}

// CreatePackageList appends Repo packages to completion cache
func createRepoList(dbList *alpm.DBList, out io.Writer) error {
_ = dbList.ForEach(func(db alpm.DB) error {
_ = db.PkgCache().ForEach(func(pkg alpm.Package) error {
_, err := io.WriteString(out, pkg.Name()+"\t"+pkg.DB().Name()+"\n")
func createRepoList(dbExecutor *db.AlpmExecutor, out io.Writer) error {
for _, pkg := range dbExecutor.SyncPackages() {
_, err := io.WriteString(out, pkg.Name()+"\t"+pkg.DB().Name()+"\n")
if err != nil {
return err
})
return nil
})
}
}
return nil
}
54 changes: 54 additions & 0 deletions pkg/db/alpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/leonelquinteros/gotext"

"github.com/Jguer/yay/v10/pkg/text"
"github.com/Jguer/yay/v10/pkg/upgrade"
)

type AlpmExecutor struct {
Expand Down Expand Up @@ -176,6 +177,13 @@ func (ae *AlpmExecutor) LocalSatisfierExists(pkgName string) bool {
return true
}

func (ae *AlpmExecutor) SyncSatisfierExists(pkgName string) bool {
if _, err := ae.syncDB.FindSatisfier(pkgName); err != nil {
return false
}
return true
}

func (ae *AlpmExecutor) IsCorrectVersionInstalled(pkgName, versionRequired string) bool {
alpmPackage := ae.localDB.Pkg(pkgName)
if alpmPackage == nil {
Expand Down Expand Up @@ -266,3 +274,49 @@ func (ae *AlpmExecutor) PackageGroups(pkg RepoPackage) []string {
alpmPackage := pkg.(*alpm.Package)
return alpmPackage.Groups().Slice()
}

// upRepo gathers local packages and checks if they have new versions.
// Output: Upgrade type package list.
func (ae *AlpmExecutor) RepoUpgrades(enableDowngrade bool) (upgrade.UpSlice, error) {
slice := upgrade.UpSlice{}

localDB, err := ae.handle.LocalDB()
if err != nil {
return slice, err
}

err = ae.handle.TransInit(alpm.TransFlagNoLock)
if err != nil {
return slice, err
}

defer func() {
err = ae.handle.TransRelease()
}()

err = ae.handle.SyncSysupgrade(enableDowngrade)
if err != nil {
return slice, err
}
_ = ae.handle.TransGetAdd().ForEach(func(pkg alpm.Package) error {
localVer := "-"

if localPkg := localDB.Pkg(pkg.Name()); localPkg != nil {
localVer = localPkg.Version()
}

slice = append(slice, upgrade.Upgrade{
Name: pkg.Name(),
Repository: pkg.DB().Name(),
LocalVersion: localVer,
RemoteVersion: pkg.Version(),
})
return nil
})

return slice, nil
}

func (ae *AlpmExecutor) AlpmArch() (string, error) {
return ae.handle.Arch()
}
Loading

0 comments on commit 005b293

Please sign in to comment.