-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage4_api.go
596 lines (475 loc) · 13.1 KB
/
stage4_api.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
Copyright (C) 2020-2021 Daniele Rondina <geaaru@sabayonlinux.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package converter
import (
"errors"
"fmt"
cfg "github.com/mudler/luet/pkg/config"
. "github.com/mudler/luet/pkg/logger"
luet_pkg "github.com/mudler/luet/pkg/package"
)
type Stage4Leaf struct {
Package *luet_pkg.DefaultPackage
Father []*luet_pkg.DefaultPackage
Position int
Counter int
}
type Stage4Tree struct {
Id int
Map map[string]*Stage4Leaf
Deps []*luet_pkg.DefaultPackage
}
type Stage4LeafCache struct {
MapAssign map[string]*luet_pkg.DefaultPackage
}
type Stage4Levels struct {
Name string
Levels []*Stage4Tree
Map map[string]*luet_pkg.DefaultPackage
Changed map[string]*luet_pkg.DefaultPackage
CacheLeaf map[string]*Stage4LeafCache
Quiet bool
}
func NewStage4LeafCache() *Stage4LeafCache {
return &Stage4LeafCache{
MapAssign: make(map[string]*luet_pkg.DefaultPackage, 0),
}
}
func NewStage4Levels() *Stage4Levels {
return &Stage4Levels{
Levels: []*Stage4Tree{},
Map: make(map[string]*luet_pkg.DefaultPackage, 0),
Changed: make(map[string]*luet_pkg.DefaultPackage, 0),
CacheLeaf: make(map[string]*Stage4LeafCache, 0),
Quiet: false,
}
}
func NewStage4LevelsWithSize(nLevels int) *Stage4Levels {
ans := NewStage4Levels()
for i := 0; i < nLevels; i++ {
tree := NewStage4Tree(i + 1)
ans.AddTree(tree)
}
return ans
}
func (l *Stage4Levels) PackageHasAncient(pkg, ancient *luet_pkg.DefaultPackage, startLevel int) (bool, error) {
ans := false
found := false
key := fmt.Sprintf("%s/%s", pkg.GetCategory(), pkg.GetName())
if startLevel >= len(l.Levels) {
return false, errors.New("Invalid start level")
}
lastLevel := len(l.Levels) - 1
for i := startLevel; i <= lastLevel; i++ {
// Check if the package is present on the level.
if _, ok := l.Levels[i].Map[key]; !ok {
continue
}
found = true
for _, dep := range l.Levels[i].Map[key].Package.GetRequires() {
if dep.GetName() == ancient.GetName() &&
dep.GetCategory() == ancient.GetCategory() {
ans = true
break
}
if i < lastLevel {
hasAncient, err := l.PackageHasAncient(dep, ancient, i+1)
if err != nil {
return ans, err
}
if hasAncient {
ans = true
break
}
}
}
if ans {
break
}
}
if !found {
return false, errors.New("Package " + key + " not found on level " + fmt.Sprintf("%d", startLevel))
}
return ans, nil
}
func (l *Stage4Levels) GetMap() *map[string]*luet_pkg.DefaultPackage { return &l.Map }
func (l *Stage4Levels) Dump() string {
ans := ""
for _, t := range l.Levels {
ans += t.Dump()
}
return ans
}
func (l *Stage4Levels) AddTree(t *Stage4Tree) {
l.Levels = append(l.Levels, t)
}
func (l *Stage4Levels) RegisterAssignment(p, father, newFather *luet_pkg.DefaultPackage) error {
key := fmt.Sprintf("%s/%s", p.GetCategory(), p.GetName())
fatherKey := fmt.Sprintf("%s/%s", father.GetCategory(), father.GetName())
v, ok := l.CacheLeaf[key]
if !ok {
v = NewStage4LeafCache()
}
v.MapAssign[fatherKey] = newFather
l.CacheLeaf[key] = v
return nil
}
func (l *Stage4Levels) GetAssignment(p, father *luet_pkg.DefaultPackage) *luet_pkg.DefaultPackage {
key := fmt.Sprintf("%s/%s", p.GetCategory(), p.GetName())
fatherKey := fmt.Sprintf("%s/%s", father.GetCategory(), father.GetName())
v, ok := l.CacheLeaf[key]
if !ok {
return nil
}
ans, _ := v.MapAssign[fatherKey]
return ans
}
func (l *Stage4Levels) AddDependency(p, father *luet_pkg.DefaultPackage, level int) error {
if level >= len(l.Levels) {
return errors.New("Invalid level")
}
key := fmt.Sprintf("%s/%s", p.GetCategory(), p.GetName())
v, ok := l.Map[key]
if ok {
l.Levels[level].AddDependency(v, father)
} else {
l.Map[key] = p
l.Levels[level].AddDependency(p, father)
}
return nil
}
func IsInStack(stack []string, pkg string) bool {
ans := false
for _, p := range stack {
if p == pkg {
ans = true
break
}
}
return ans
}
func (l *Stage4Levels) AddDependencyRecursive(p, father *luet_pkg.DefaultPackage, stack []string, level int) (bool, error) {
var err error
var toInsert bool = true
if level >= len(l.Levels) {
return false, errors.New("Invalid level")
}
key := fmt.Sprintf("%s/%s", p.GetCategory(), p.GetName())
if cfg.LuetCfg.GetLogging().Level == "debug" {
DebugC(fmt.Sprintf("Adding recursive %s package to level %d (%v)", key, level+1, stack))
}
_, ok := l.Map[key]
if !ok {
return false, errors.New(fmt.Sprintf("On add dependency not found package %s/%s",
p.GetCategory(), p.GetName()))
}
inStack := IsInStack(stack, key)
if inStack {
return true, nil
}
stack = append(stack, key)
if len(p.GetRequires()) > 0 {
for _, d := range p.GetRequires() {
key = fmt.Sprintf("%s/%s", d.GetCategory(), d.GetName())
v, ok := l.Map[key]
if !ok {
return false, errors.New(fmt.Sprintf("For package %s/%s not found dependency %s",
p.GetCategory(), p.GetName(), key))
}
toInsert, err = l.AddDependencyRecursive(v, p, stack, level+1)
if err != nil {
return false, err
}
if !toInsert {
break
}
}
}
if toInsert {
return toInsert, l.Levels[level].AddDependency(p, father)
} else {
DebugC(GetAurora().Bold(fmt.Sprintf("For package %s break cycle.", key)))
return false, nil
}
}
func (l *Stage4Levels) AddChangedPackage(pkg *luet_pkg.DefaultPackage) {
key := fmt.Sprintf("%s/%s", pkg.GetCategory(), pkg.GetName())
l.Changed[key] = pkg
}
func (l *Stage4Levels) analyzeLevelLeafs(pos int) (bool, error) {
tree := l.Levels[pos]
rescan := false
if cfg.LuetCfg.GetLogging().Level == "debug" {
// Call this only with debug enabled.
DebugC(fmt.Sprintf("[%d-%d] Tree:\n%s", tree.Id, pos, tree.Dump()))
}
for _, leaf := range *tree.GetMap() {
r, err := l.AnalyzeLeaf(pos, tree, leaf)
if err != nil {
return rescan, err
}
if r {
rescan = true
break
}
} // end for key map
return rescan, nil
}
func (l *Stage4Levels) Resolve() error {
// Start from bottom
pos := len(l.Levels)
// Check if the levels are sufficient for serialization
missingLevels := len(l.Levels[0].Map) - pos
// POST: we need to add levels.
for i := 1; i <= missingLevels; i++ {
tree := NewStage4Tree(pos + i)
l.AddTree(tree)
}
initialLevels := len(l.Levels)
tot_pkg := len(l.Levels[0].Map)
for pos > 0 {
pos--
pkgs := tot_pkg - len(l.Levels[0].Map)
rescan, err := l.analyzeLevelLeafs(pos)
if err != nil {
return err
}
if rescan {
if !l.Quiet {
InfoC(
fmt.Sprintf(
"%s Analyzed packages %2d/%2d ...",
l.Name, pkgs, tot_pkg,
))
}
// Restarting analysis from begin
pos = initialLevels
}
}
return nil
}
func NewStage4Leaf(pkg, father *luet_pkg.DefaultPackage, pos int) (*Stage4Leaf, error) {
if pkg == nil || pos < 0 {
return nil, errors.New("Invalid parameter on create stage4 leaf")
}
ans := &Stage4Leaf{
Package: pkg,
Father: []*luet_pkg.DefaultPackage{},
Position: pos,
Counter: 1,
}
if father != nil {
ans.Father = append(ans.Father, father)
}
return ans, nil
}
func (l *Stage4Leaf) String() string {
ans := fmt.Sprintf("%s/%s (%d, %d) ",
l.Package.GetCategory(), l.Package.GetName(),
l.Position, l.Counter)
if len(l.Father) > 0 {
ans += " father: ["
for _, f := range l.Father {
ans += fmt.Sprintf("%s/%s, ", f.GetCategory(), f.GetName())
}
ans += "]"
}
return ans
}
func (l *Stage4Leaf) AddFather(father *luet_pkg.DefaultPackage) {
// Check if the father is already present
notFound := true
for _, f := range l.Father {
if f.GetCategory() == father.GetCategory() &&
f.GetName() == father.GetName() {
notFound = false
break
}
}
if notFound {
l.Father = append(l.Father, father)
l.Counter++
}
}
func (l *Stage4Leaf) DelFather(father *luet_pkg.DefaultPackage) {
pos := -1
for idx, f := range l.Father {
if f.GetCategory() == father.GetCategory() &&
f.GetName() == father.GetName() {
pos = idx
break
}
}
if pos >= 0 {
l.Father = append(l.Father[:pos], l.Father[pos+1:]...)
l.Counter--
DebugC(fmt.Sprintf("From leaf %s/%s delete father %s/%s (%d)",
l.Package.GetCategory(), l.Package.GetName(),
father.GetCategory(), father.GetName(), l.Counter,
))
}
}
func NewStage4Tree(id int) *Stage4Tree {
return &Stage4Tree{
Id: id,
Map: make(map[string]*Stage4Leaf, 0),
Deps: []*luet_pkg.DefaultPackage{},
}
}
func (t *Stage4Tree) Dump() string {
ans := fmt.Sprintf("[%d] Map: [ ", t.Id)
for k, v := range t.Map {
ans += fmt.Sprintf("%s-%d ", k, v.Counter)
if v.Counter > 0 {
ans += "("
for _, father := range v.Father {
ans += fmt.Sprintf("%s/%s, ", father.GetCategory(), father.GetName())
}
ans += ")"
}
ans += ", "
}
ans += "]\n"
ans += fmt.Sprintf("[%d] Deps: [ ", t.Id)
for _, d := range t.Deps {
ans += fmt.Sprintf("%s/%s", d.GetCategory(), d.GetName())
if len(d.GetRequires()) > 0 {
ans += " ("
for _, d2 := range d.GetRequires() {
ans += fmt.Sprintf("%s/%s, ", d2.GetCategory(), d2.GetName())
}
ans += " )"
}
ans += ", "
}
ans += "]\n"
return ans
}
func (t *Stage4Tree) GetMap() *map[string]*Stage4Leaf {
return &t.Map
}
func (t *Stage4Tree) GetDeps() *[]*luet_pkg.DefaultPackage { return &t.Deps }
func (t *Stage4Tree) GetDependency(pos int) (*luet_pkg.DefaultPackage, error) {
if len(t.Deps) <= pos {
return nil, errors.New("Invalid position")
}
return t.Deps[pos], nil
}
func (t *Stage4Tree) DropDependency(p *luet_pkg.DefaultPackage) error {
key := fmt.Sprintf("%s/%s", p.GetCategory(), p.GetName())
leaf, ok := t.Map[key]
if !ok {
return errors.New(fmt.Sprintf("Package %s is not present on tree.", key))
}
if leaf.Counter > 1 {
leaf.Counter--
DebugC(fmt.Sprintf("For %s decrement counter.", key))
return nil
}
DebugC(fmt.Sprintf("[%d] Dropping dependency %s...", t.Id, key), len(t.Map))
delete(t.Map, key)
if len(t.Deps) == (leaf.Position - 1) {
// POST: just drop last element
t.Deps = t.Deps[:leaf.Position-1]
} else if leaf.Position == 0 {
// POST: just drop the first element
t.Deps = t.Deps[1:]
} else {
// POST: drop element between other elements
t.Deps = append(t.Deps[:leaf.Position], t.Deps[leaf.Position+1:]...)
}
for k, v := range t.Map {
if v.Position > leaf.Position {
t.Map[k].Position--
}
}
DebugC(fmt.Sprintf("[%d] Dropped dependency %s...", t.Id, key), len(t.Map))
return nil
}
func (t *Stage4Tree) AddDependency(p, father *luet_pkg.DefaultPackage) error {
fatherKey := "-"
if father != nil {
fatherKey = fmt.Sprintf("%s/%s", father.GetCategory(), father.GetName())
}
DebugC(fmt.Sprintf("[L%d] Adding dep %s/%s with father %s",
t.Id, p.GetCategory(), p.GetName(), fatherKey))
if leaf, ok := t.Map[fmt.Sprintf("%s/%s", p.GetCategory(), p.GetName())]; ok {
leaf.AddFather(father)
return nil
}
leaf, err := NewStage4Leaf(p, father, len(t.Deps))
if err != nil {
return err
}
t.Map[fmt.Sprintf("%s/%s", p.GetCategory(), p.GetName())] = leaf
t.Deps = append(t.Deps, p)
return nil
}
// TODO: Move in another place
func RemoveDependencyFromLuetPackage(pkg, dep *luet_pkg.DefaultPackage) error {
DebugC(GetAurora().Bold(fmt.Sprintf("Removing dep %s/%s from package %s/%s...",
dep.GetCategory(), dep.GetName(),
pkg.GetCategory(), pkg.GetName(),
)))
pos := -1
for idx, d := range pkg.GetRequires() {
if d.GetCategory() == dep.GetCategory() && d.GetName() == dep.GetName() {
pos = idx
break
}
}
if pos < 0 {
// Ignore error until we fix father cleanup
Warning(fmt.Sprintf("Dependency %s/%s not found on package %s/%s",
dep.GetCategory(), dep.GetName(),
pkg.GetCategory(), pkg.GetName(),
))
/*
return nil
*/
return errors.New(
fmt.Sprintf("Dependency %s/%s not found on package %s/%s",
dep.GetCategory(), dep.GetName(),
pkg.GetCategory(), pkg.GetName(),
))
}
pkg.PackageRequires = append(pkg.PackageRequires[:pos], pkg.PackageRequires[pos+1:]...)
return nil
}
func AddDependencyToLuetPackage(pkg, dep *luet_pkg.DefaultPackage) {
DebugC(fmt.Sprintf("Adding %s/%s dep to package %s/%s",
dep.GetCategory(), dep.GetName(),
pkg.GetCategory(), pkg.GetName()))
// Check if dependency is already present.
notFound := true
for _, d := range pkg.GetRequires() {
if d.GetCategory() == dep.GetCategory() && d.GetName() == dep.GetName() {
notFound = false
break
}
}
if notFound {
DebugC(GetAurora().Bold(fmt.Sprintf("Added %s/%s dep to package %s/%s",
dep.GetCategory(), dep.GetName(),
pkg.GetCategory(), pkg.GetName())))
// TODO: check if we need set PackageRequires
pkg.PackageRequires = append(pkg.PackageRequires,
&luet_pkg.DefaultPackage{
Category: dep.GetCategory(),
Name: dep.GetName(),
Version: ">=0",
},
)
}
}