-
Notifications
You must be signed in to change notification settings - Fork 3
/
depends.go
205 lines (179 loc) · 3.79 KB
/
depends.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package dpkg
import (
"fmt"
"sort"
"strings"
)
var __together_cache__ = make(map[string]bool)
func (a Archive) CanTogether(pkgNames []string) bool {
sort.Strings(pkgNames)
key := strings.Join(pkgNames, ",")
if v, ok := __together_cache__[key]; ok {
return v
}
for _, pkgname := range pkgNames {
bp, err := a.FindBinary(pkgname)
if err != nil {
DebugPrintf("Invalid binary package %v: %v\n", pkgname, err)
continue
}
for _, bc := range bp.deprecatedConflict() {
for _, cc := range pkgNames {
if cc == bc {
__together_cache__[key] = false
return false
}
}
}
}
__together_cache__[key] = true
return true
// return DryInstall(pkgNames...) == nil
}
func (a Archive) DryInstall(pkgs ...string) error {
panic("Not Implement")
}
func (a Archive) CheckDep(info *DepInfo) error {
return a.checkDep(info.Filter(a.Architecture, ""))
}
func (a Archive) hasPackage(name string) bool {
_, ok := a.FindControl(name)
if ok {
return ok
}
return len(a.FindProvider(name)) != 0
}
func (a Archive) checkDep(info *DepInfo) error {
if info == nil {
return nil
}
str := info.String()
if v, ok := a.cache[str]; ok {
return v
}
if a.hasPackage(info.Name) {
return a.record(str, a.checkDep(info.And))
}
if info.Or != nil {
return a.record(str, a.checkDep(info.Or))
} else {
return a.record(str, fmt.Errorf("Can't find package %q", info.Name))
}
}
func (a Archive) record(key string, v error) error {
a.cache[key] = v
return v
}
func AssertNoUseAny(arch string) {
if arch == "any" {
panic("It's wrong to query depends by architecture of any.")
}
}
func parseSourceLine(str string, defSource, defVer string) (string, string) {
// TODO: re implement by parseDepInfo
if str == "" {
return defSource, defVer
}
fs := getArrayString(str, " ")
switch len(fs) {
case 2:
return fs[0], strings.Trim(fs[1], "()")
case 1:
return fs[0], defVer
default:
DebugPrintf("Invalid source line %q (%d)\n", str, len(fs))
return defSource, defVer
}
}
type DepInfo struct {
Name string
Ver string
Arch string
Restrict struct {
Archs []string
Profiles []string
}
And *DepInfo
Or *DepInfo
}
func (info DepInfo) SimpleDeps() []string {
i := &info
var ret []string
for i != nil {
ret = append(ret, i.Name)
if i.And == nil {
break
}
i = i.And
}
return ret
}
func (info DepInfo) String() string {
r := info.Name
if info.Or != nil {
r += " | " + info.Or.String()
}
if info.And != nil {
r += ", " + info.And.String()
}
return r
}
func (info DepInfo) Filter(arch string, profile string) *DepInfo {
return depInfoFilter(&info, func(di *DepInfo) bool {
return di != nil && di.match(arch, profile)
})
}
type filterFunc func(*DepInfo) bool
func depInfoFilter(info *DepInfo, fn filterFunc) *DepInfo {
if info == nil {
return info
}
info = depInfoFilterOr(info, fn)
if fn(info) {
info.And = depInfoFilter(info.And, fn)
return info
}
if info == nil {
return nil
}
return depInfoFilter(info.And, fn)
}
func depInfoFilterOr(info *DepInfo, fn filterFunc) *DepInfo {
if info == nil {
return info
}
if fn(info) {
info.Or = depInfoFilterOr(info.Or, fn)
return info
}
if info.Or == nil {
return depInfoFilterOr(info.And, fn)
}
info.Or.And = info.And
return depInfoFilterOr(info.Or, fn)
}
func (info DepInfo) match(arch string, profile string) bool {
return info.Name != "" && info.matchArch(arch) && info.matchProfile(profile)
}
func (info DepInfo) matchProfile(profile string) bool {
if len(info.Restrict.Profiles) == 0 {
return true
}
for _, i := range info.Restrict.Profiles {
if i == profile {
return true
}
}
return false
}
func (info DepInfo) matchArch(arch string) bool {
if len(info.Restrict.Archs) == 0 {
return true
}
for _, i := range info.Restrict.Archs {
if i == arch {
return true
}
}
return false
}