Add utils.go

This commit is contained in:
morganamilo 2018-03-22 15:40:33 +00:00
parent 415659d1cd
commit 0c0cd4f883
No known key found for this signature in database
GPG Key ID: 6FE9E7996B0B082E
7 changed files with 124 additions and 159 deletions

View File

@ -12,14 +12,6 @@ import (
// Checks a single conflict against every other to be installed package's
// name and its provides.
func checkInnerConflict(name string, conflict string, conflicts map[string]stringSet, dc *depCatagories) {
add := func(h map[string]stringSet, n string, v string) {
_, ok := h[n]
if !ok {
h[n] = make(stringSet)
}
h[n].set(v)
}
deps, err := gopkg.ParseDeps([]string{conflict})
if err != nil {
return
@ -36,7 +28,7 @@ func checkInnerConflict(name string, conflict string, conflicts map[string]strin
return
}
if dep.Name == pkg.Name && version.Satisfies(dep) {
add(conflicts, name, pkg.Name)
addMapStringSet(conflicts, name, pkg.Name)
continue
}
@ -65,7 +57,7 @@ func checkInnerConflict(name string, conflict string, conflicts map[string]strin
}
if version != nil && version.Satisfies(dep) {
add(conflicts, name, pkg.Name)
addMapStringSet(conflicts, name, pkg.Name)
break
}
@ -83,7 +75,7 @@ func checkInnerConflict(name string, conflict string, conflicts map[string]strin
}
if dep.Name == pkg.Name() && version.Satisfies(dep) {
add(conflicts, name, pkg.Name())
addMapStringSet(conflicts, name, pkg.Name())
continue
}
@ -100,7 +92,7 @@ func checkInnerConflict(name string, conflict string, conflicts map[string]strin
}
if provide.Mod == alpm.DepModAny {
add(conflicts, name, pkg.Name())
addMapStringSet(conflicts, name, pkg.Name())
return fmt.Errorf("")
}
@ -110,7 +102,7 @@ func checkInnerConflict(name string, conflict string, conflicts map[string]strin
}
if version.Satisfies(dep) {
add(conflicts, name, pkg.Name())
addMapStringSet(conflicts, name, pkg.Name())
return fmt.Errorf("")
}
@ -143,14 +135,6 @@ func checkForInnerConflicts(dc *depCatagories) (map[string]stringSet) {
// Checks a provide or packagename from a to be installed package
// against every already installed package's conflicts
func checkReverseConflict(name string, provide string, conflicts map[string]stringSet) error {
add := func(h map[string]stringSet, n string, v string) {
_, ok := h[n]
if !ok {
h[n] = make(stringSet)
}
h[n].set(v)
}
var version *gopkg.CompleteVersion
var err error
@ -193,7 +177,7 @@ func checkReverseConflict(name string, provide string, conflicts map[string]stri
if version == nil || version.Satisfies(dep) {
// Todo
add(conflicts, name, pkg.Name() + " (" + provide + ")")
addMapStringSet(conflicts, name, pkg.Name() + " (" + provide + ")")
return fmt.Errorf("")
}
@ -209,14 +193,6 @@ func checkReverseConflict(name string, provide string, conflicts map[string]stri
// Checks the conflict of a to be installed package against the package name and
// provides of every installed package.
func checkConflict(name string, conflict string, conflicts map[string]stringSet) error {
add := func(h map[string]stringSet, n string, v string) {
_, ok := h[n]
if !ok {
h[n] = make(stringSet)
}
h[n].set(v)
}
localDb, err := alpmHandle.LocalDb()
if err != nil {
return err
@ -240,7 +216,7 @@ func checkConflict(name string, conflict string, conflicts map[string]stringSet)
}
if dep.Name == pkg.Name() && version.Satisfies(dep) {
add(conflicts, name, pkg.Name())
addMapStringSet(conflicts, name, pkg.Name())
return nil
}
@ -257,7 +233,7 @@ func checkConflict(name string, conflict string, conflicts map[string]stringSet)
}
if provide.Mod == alpm.DepModAny {
add(conflicts, name, pkg.Name() + " (" + provide.Name + ")")
addMapStringSet(conflicts, name, pkg.Name() + " (" + provide.Name + ")")
return fmt.Errorf("")
}
@ -267,7 +243,7 @@ func checkConflict(name string, conflict string, conflicts map[string]stringSet)
}
if version.Satisfies(dep) {
add(conflicts, name, pkg.Name() + " (" + provide.Name + ")")
addMapStringSet(conflicts, name, pkg.Name() + " (" + provide.Name + ")")
return fmt.Errorf("")
}

View File

@ -471,15 +471,7 @@ func depTreeRecursive(dt *depTree, localDb *alpm.Db, syncDb alpm.DbList, isMake
func checkVersions(dt *depTree) error {
depStrings := make([]string, 0)
has := make(map[string][]string)
add := func(h map[string][]string, n string, v string) {
_, ok := h[n]
if !ok {
h[n] = make([]string, 0, 1)
}
h[n] = append(h[n], v)
}
for _, pkg := range dt.Aur {
for _, deps := range [3][]string{pkg.Depends, pkg.MakeDepends, pkg.CheckDepends} {
for _, dep := range deps {
@ -490,12 +482,12 @@ func checkVersions(dt *depTree) error {
}
}
add(has, pkg.Name, pkg.Version)
addMapStringSlice(has, pkg.Name, pkg.Version)
for _, name := range pkg.Provides {
_name, _ver := splitNameFromDep(name)
if _ver != "" {
add(has, _name, _ver)
addMapStringSlice(has, _name, _ver)
} else {
delete(has, _name)
}
@ -510,11 +502,11 @@ func checkVersions(dt *depTree) error {
return nil
})
add(has, pkg.Name(), pkg.Version())
addMapStringSlice(has, pkg.Name(), pkg.Version())
pkg.Provides().ForEach(func(dep alpm.Depend) error {
if dep.Mod != alpm.DepModAny {
add(has, dep.Name, dep.Version)
addMapStringSlice(has, dep.Name, dep.Version)
} else {
delete(has, dep.Name)
}

View File

@ -3,7 +3,6 @@ package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
@ -676,23 +675,3 @@ func clean(pkgs []*rpc.Pkg) {
os.RemoveAll(dir)
}
}
func completeFileName(dir, name string) (string, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return "", err
}
for _, file := range files {
if file.IsDir() {
continue
}
if strings.HasPrefix(file.Name(), name) {
return dir + file.Name(), nil
}
}
return "", nil
}

View File

@ -582,34 +582,6 @@ func (parser *arguments) parseCommandLine() (err error) {
return
}
type intRange struct {
min int
max int
}
func makeIntRange(min, max int) intRange {
return intRange{
min,
max,
}
}
func (r intRange) get(n int) bool {
return n >= r.min && n <= r.max
}
type intRanges []intRange
func (rs intRanges) get(n int) bool {
for _, r := range rs {
if r.get(n) {
return true
}
}
return false
}
//parses input for number menus
//supports individual selection: 1 2 3 4
//supports range selections: 1-4 10-20

View File

@ -331,20 +331,6 @@ func statistics() (info struct {
return
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a < b {
return b
}
return a
}
// Queries the aur for information about specified packages.
// All packages should be queried in a single rpc request except when the number
// of packages exceeds the number set in config.RequestSplitN.

View File

@ -7,7 +7,6 @@ import (
"strings"
"sort"
"sync"
"unicode"
alpm "github.com/jguer/go-alpm"
pkgb "github.com/mikkeloscar/gopkgbuild"
@ -39,32 +38,6 @@ func (u upSlice) Less(i, j int) bool {
}
}
func lessRunes(iRunes, jRunes []rune) bool {
max := len(iRunes)
if max > len(jRunes) {
max = len(jRunes)
}
for idx := 0; idx < max; idx++ {
ir := iRunes[idx]
jr := jRunes[idx]
lir := unicode.ToLower(ir)
ljr := unicode.ToLower(jr)
if lir != ljr {
return lir < ljr
}
// the lowercase runes are the same, so compare the original
if ir != jr {
return ir < jr
}
}
return len(iRunes) < len(jRunes)
}
func getVersionDiff(oldVersion, newversion string) (left, right string) {
old, errOld := pkgb.NewCompleteVersion(oldVersion)
new, errNew := pkgb.NewCompleteVersion(newversion)
@ -256,29 +229,6 @@ func upRepo(local []alpm.Package) (upSlice, error) {
return slice, nil
}
//Contains returns whether e is present in s
func containsInt(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
// RemoveIntListFromList removes all src's elements that are present in target
func removeIntListFromList(src, target []int) []int {
max := len(target)
for i := 0; i < max; i++ {
if containsInt(src, target[i]) {
target = append(target[:i], target[i+1:]...)
max--
i--
}
}
return target
}
// upgradePkgs handles updating the cache and installing updates.
func upgradePkgs(dt *depTree) (stringSet, stringSet, error) {
ignore := make(stringSet)

110
utils.go Normal file
View File

@ -0,0 +1,110 @@
package main
import (
"io/ioutil"
"strings"
"unicode"
)
type intRange struct {
min int
max int
}
func makeIntRange(min, max int) intRange {
return intRange{
min,
max,
}
}
func (r intRange) get(n int) bool {
return n >= r.min && n <= r.max
}
type intRanges []intRange
func (rs intRanges) get(n int) bool {
for _, r := range rs {
if r.get(n) {
return true
}
}
return false
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a < b {
return b
}
return a
}
func addMapStringSet(h map[string]stringSet, n string, v string) {
_, ok := h[n]
if !ok {
h[n] = make(stringSet)
}
h[n].set(v)
}
func addMapStringSlice(h map[string][]string, n string, v string) {
_, ok := h[n]
if !ok {
h[n] = make([]string, 0, 1)
}
h[n] = append(h[n], v)
}
func completeFileName(dir, name string) (string, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return "", err
}
for _, file := range files {
if file.IsDir() {
continue
}
if strings.HasPrefix(file.Name(), name) {
return dir + file.Name(), nil
}
}
return "", nil
}
func lessRunes(iRunes, jRunes []rune) bool {
max := len(iRunes)
if max > len(jRunes) {
max = len(jRunes)
}
for idx := 0; idx < max; idx++ {
ir := iRunes[idx]
jr := jRunes[idx]
lir := unicode.ToLower(ir)
ljr := unicode.ToLower(jr)
if lir != ljr {
return lir < ljr
}
// the lowercase runes are the same, so compare the original
if ir != jr {
return ir < jr
}
}
return len(iRunes) < len(jRunes)
}