Bläddra i källkod

New dependency parsing method. Halved handle opens

Jguer 8 år sedan
förälder
incheckning
cfb0efea45
2 ändrade filer med 52 tillägg och 80 borttagningar
  1. 13 6
      aur/aur.go
  2. 39 74
      pacman/pacman.go

+ 13 - 6
aur/aur.go

@@ -394,17 +394,24 @@ func (a *Result) Dependencies() (runDeps [2][]string, makeDeps [2][]string, err
 		q = append(q, *a)
 	}
 
+	depSearch := pacman.BuildDependencies(a.Depends)
 	if len(a.Depends) != 0 {
-		runDeps[0], runDeps[1], err = pacman.DepSatisfier(q[0].Depends)
-		fmt.Println("\x1b[1;32m=>\x1b[1;33m Dependencies: \x1b[0m")
-		printDeps(runDeps[0], runDeps[1])
+		runDeps[0], runDeps[1] = depSearch(q[0].Depends, true, false)
+		if len(runDeps[0]) != 0 || len(runDeps[1]) != 0 {
+			fmt.Println("\x1b[1;32m=>\x1b[1;33m Run Dependencies: \x1b[0m")
+			printDeps(runDeps[0], runDeps[1])
+		}
 	}
 
 	if len(a.MakeDepends) != 0 {
-		makeDeps[0], makeDeps[1], err = pacman.DepSatisfier(q[0].Depends)
-		fmt.Println("\x1b[1;32m=>\x1b[1;33m Make Dependencies: \x1b[0m")
-		printDeps(makeDeps[0], makeDeps[1])
+		makeDeps[0], makeDeps[1] = depSearch(q[0].MakeDepends, false, false)
+		if len(makeDeps[0]) != 0 || len(makeDeps[1]) != 0 {
+			fmt.Println("\x1b[1;32m=>\x1b[1;33m Make Dependencies: \x1b[0m")
+			printDeps(makeDeps[0], makeDeps[1])
+		}
 	}
+	depSearch(a.MakeDepends, false, true)
+
 	err = nil
 	return
 }

+ 39 - 74
pacman/pacman.go

@@ -240,46 +240,49 @@ func PackageSlices(toCheck []string) (aur []string, repo []string, err error) {
 	return
 }
 
-// DepSatisfier receives a string slice, returns a slice of packages found in
-// repos and one of packages not found in repos. Leaves out installed packages.
-func DepSatisfier(toCheck []string) (repo []string, notFound []string, err error) {
-	h, err := conf.CreateHandle()
-	defer h.Release()
-	if err != nil {
-		return
-	}
+// BuildDependencies finds packages, on the second run
+// compares with a baselist and avoids searching those
+func BuildDependencies(baselist []string) func(toCheck []string, isBaseList bool, last bool) (repo []string, notFound []string) {
+	h, _ := conf.CreateHandle()
 
-	localDb, err := h.LocalDb()
-	if err != nil {
-		return
-	}
-	dbList, err := h.SyncDbs()
-	if err != nil {
-		return
-	}
+	localDb, _ := h.LocalDb()
+	dbList, _ := h.SyncDbs()
 
 	f := func(c rune) bool {
 		return c == '>' || c == '<' || c == '=' || c == ' '
 	}
 
-	for _, dep := range toCheck {
-		if _, erp := localDb.PkgCache().FindSatisfier(dep); erp == nil {
-			continue
-		} else if pkg, erp := dbList.FindSatisfier(dep); erp == nil {
-			repo = append(repo, pkg.Name())
-		} else {
-			field := strings.FieldsFunc(dep, f)
-			notFound = append(notFound, field[0])
+	return func(toCheck []string, isBaseList bool, close bool) (repo []string, notFound []string) {
+		if close {
+			h.Release()
+			return
 		}
-	}
 
-	err = nil
-	return
+	Loop:
+		for _, dep := range toCheck {
+			if !isBaseList {
+				for _, base := range baselist {
+					if base == dep {
+						continue Loop
+					}
+				}
+			}
+			if _, erp := localDb.PkgCache().FindSatisfier(dep); erp == nil {
+				continue
+			} else if pkg, erp := dbList.FindSatisfier(dep); erp == nil {
+				repo = append(repo, pkg.Name())
+			} else {
+				field := strings.FieldsFunc(dep, f)
+				notFound = append(notFound, field[0])
+			}
+		}
+		return
+	}
 }
 
-// OutofRepo returns a list of packages not installed and not resolvable
-// Accepts inputs like 'gtk2', 'java-environment=8', 'linux >= 4.20'
-func OutofRepo(toCheck []string) (aur []string, repo []string, err error) {
+// DepSatisfier receives a string slice, returns a slice of packages found in
+// repos and one of packages not found in repos. Leaves out installed packages.
+func DepSatisfier(toCheck []string) (repo []string, notFound []string, err error) {
 	h, err := conf.CreateHandle()
 	defer h.Release()
 	if err != nil {
@@ -299,52 +302,14 @@ func OutofRepo(toCheck []string) (aur []string, repo []string, err error) {
 		return c == '>' || c == '<' || c == '=' || c == ' '
 	}
 
-toCheckLoop:
 	for _, dep := range toCheck {
-		field := strings.FieldsFunc(dep, f)
-
-		for _, checkR := range repo {
-			if field[0] == checkR {
-				continue toCheckLoop
-			}
-		}
-
-		for _, checkA := range aur {
-			if field[0] == checkA {
-				continue toCheckLoop
-			}
-		}
-
-		// Check if dep is installed
-		_, err = localDb.PkgByName(field[0])
-		if err == nil {
+		if _, erp := localDb.PkgCache().FindSatisfier(dep); erp == nil {
 			continue
-		}
-
-		found := false
-	Loop:
-		for _, db := range dbList.Slice() {
-			// First, Check if they're provided by package name.
-			_, err = db.PkgByName(field[0])
-			if err == nil {
-				found = true
-				repo = append(repo, field[0])
-				break Loop
-			}
-
-			for _, pkg := range db.PkgCache().Slice() {
-				for _, p := range pkg.Provides().Slice() {
-					if p.String() == dep {
-						found = true
-						repo = append(repo, pkg.Name())
-						break Loop
-					}
-				}
-			}
-		}
-
-		if !found {
-			aur = append(aur, field[0])
+		} else if pkg, erp := dbList.FindSatisfier(dep); erp == nil {
+			repo = append(repo, pkg.Name())
+		} else {
+			field := strings.FieldsFunc(dep, f)
+			notFound = append(notFound, field[0])
 		}
 	}