-
Notifications
You must be signed in to change notification settings - Fork 4
/
upgrade_common.ml
1890 lines (1770 loc) · 60.8 KB
/
upgrade_common.ml
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* Co-installability tools
* http://coinst.irill.org/
* Copyright (C) 2005-2011 Jérôme Vouillon
* Laboratoire PPS - CNRS Université Paris Diderot
*
* These programs are free software; you can redistribute them and/or
* modify them under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 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
* Lesser 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/>.
*)
let debug_coinst =
Debug.make "coinst" "Debug co-installability issue analyse" []
let debug_time = Debug.make "time" "Print execution times" []
let debug_cluster = Debug.make "cluster" "Debug clustering algorithm" []
let debug_problems =
Debug.make "coinst_prob"
"Debug enumeration of possible co-installability issues" []
let debug_problem_graph =
Debug.make "coinst_graph"
"Write the graph of new dependencies to /tmp/newdeps.dot" []
let debug = false
module IntSet = Util.IntSet
module StringSet = Util.StringSet
module M = Deb_lib
module Coinst = Coinst_common.F(M)
module Repository = Coinst.Repository
open Repository
module Quotient = Coinst.Quotient
module Graph = Graph.F (Coinst.Repository)
module PSetSet = Set.Make (PSet)
module Timer = Util.Timer
(****)
type ignored_sets = (StringSet.t list * bool) list ref
type ignored_sets_2 = (PSet.t list * bool) list
let ignored_set_domain l =
List.fold_left
(fun s (l, ext) ->
List.fold_left
(fun s s' ->
StringSet.fold (fun nm s -> M.PkgSet.add (M.add_name nm) s) s' s)
s l)
M.PkgSet.empty !l
let forced_packages l =
List.fold_left
(fun s (l, ext) ->
match l with
[s'] when not ext -> StringSet.union s s'
| _ -> s)
StringSet.empty !l
let intern_ignored_sets dist l =
List.fold_left
(fun r (l, ext) ->
let l =
List.map
(fun s ->
StringSet.fold
(fun nm s ->
match M.parse_package_name dist nm with
[p] -> PSet.add (Package.of_index p) s
| [] -> s
| _ :: _ -> assert false)
s PSet.empty)
l
in
if List.exists PSet.is_empty l then r else (l, ext) :: r)
[] l
let is_ignored_set l s =
List.exists
(fun (l, ext) ->
try
not
(StringSet.is_empty
(List.fold_left
(fun s s' ->
let p = StringSet.choose (StringSet.inter s s') in
StringSet.remove p s)
s l)
&&
ext)
with Not_found ->
false)
!l
let ignored_set_domain_2 l =
List.fold_left
(fun s (l, ext) -> List.fold_left PSet.union s l)
PSet.empty l
let is_ignored_set_2 l s =
List.exists
(fun (l, ext) ->
try
not
(PSet.is_empty
(List.fold_left
(fun s s' ->
let p = PSet.choose (PSet.inter s s') in
PSet.remove p s)
s l)
&&
ext)
with Not_found ->
false)
l
let print_ignore_spec dist f l =
Util.print_list
(fun f (l, ext) ->
Util.print_list
(fun f s ->
Util.print_list (Package.print_name dist) "|" f (PSet.elements s))
"," f l;
if ext then Format.fprintf f ",_")
" " f l
(****)
let new_deps pred possibly_ignored_packages deps1 dist2 deps2 =
PTbl.mapi
(fun p2 i ->
if i = -1 then
Formula._true
else begin
let p1 = Package.of_index i in
let f1 = PTbl.get deps1 p1 in
let f2 = PTbl.get deps2 p2 in
let f2 =
Formula.filter
(fun d2 ->
Disj.exists (fun p -> PSet.mem p possibly_ignored_packages) d2
||
let d1 =
Disj.fold
(fun p2 d2 ->
let i = PTbl.get pred p2 in
if i = -1 then d2 else
Disj.disj (Disj.lit (Package.of_index i)) d2)
d2 Disj._false
in
not (Formula.implies1 f1 d1))
f2
in
if debug && not (Formula.implies Formula._true f2) then begin
Format.printf "%a ==> %a@."
(Package.print_name dist2) p2
(Formula.print dist2) f2;
(*
Format.printf "%a --> %a@."
(Package.print_name dist1) p1
(Formula.print dist1) f1
*)
end;
f2
end)
pred
(****)
module ListTbl = Util.ListTbl
type st =
{ dist : M.pool; deps : Formula.t PTbl.t; confl : Conflict.t;
pieces : (int, Package.t * Disj.t) Hashtbl.t;
pieces_in_confl : (Package.t, int) ListTbl.t;
set : PSet.t;
installed : IntSet.t; not_installed : IntSet.t;
check : PSet.t -> bool }
let print_prob st =
IntSet.iter
(fun i ->
let (p, d) = Hashtbl.find st.pieces i in
Format.printf "%a => %a; "
(Package.print_name st.dist) p
(Disj.print st.dist) d)
st.installed;
Format.printf "@."
let rec add_piece st i cont =
assert (not (IntSet.mem i st.installed || IntSet.mem i st.not_installed));
let (p, d) = Hashtbl.find st.pieces i in
if
(* We do not add a dependency if it is implied by, or implies, a
dependency currently under consideration. *)
not (IntSet.exists
(fun i' ->
let (_, d') = Hashtbl.find st.pieces i' in
Disj.implies d d' || Disj.implies d' d)
st.installed)
&&
(* When adding a package in st.set, we check that d is not implied
by any of the dependencies of a package already in st.set *)
(PSet.mem p st.set ||
not (PSet.exists
(fun p -> Formula.implies1 (PTbl.get st.deps p) d)
st.set))
&&
(* If we are adding a package, we check whether the set is still
co-installable *)
(PSet.mem p st.set || st.check (PSet.add p st.set))
then begin
if debug_problems () then
Format.printf "Adding %a => %a@."
(Package.print_name st.dist) p (Disj.print st.dist) d;
let st =
{st with set = PSet.add p st.set;
installed = IntSet.add i st.installed}
in
if debug_problems () then print_prob st;
(* Make sure that there is at least one piece in conflict for all
dependencies, then consider all possible additions *)
Disj.fold
(fun p cont st ->
if
PSet.exists
(fun q ->
List.exists (fun i -> IntSet.mem i st.installed)
(ListTbl.find st.pieces_in_confl q))
(Conflict.of_package st.confl p)
then
cont st
else
ignore
(PSet.fold
(fun q st ->
List.fold_right (fun j st -> do_add_piece st j cont)
(ListTbl.find st.pieces_in_confl q) st)
(Conflict.of_package st.confl p) st))
d
(fun st ->
if debug_problems () then
Format.printf "Considering all possible additions in %d: %a...@."
i (Disj.print st.dist) d;
Disj.fold
(fun p cont ->
PSet.fold
(fun q cont ->
List.fold_right (fun j cont st -> maybe_add_piece st j cont)
(ListTbl.find st.pieces_in_confl q) cont)
(Conflict.of_package st.confl p) cont)
d cont st)
st
end else
if debug_problems () then
Format.printf "Could not add %a => %a@."
(Package.print_name st.dist) p (Disj.print st.dist) d;
and do_add_piece st i cont =
if IntSet.mem i st.installed then begin
cont st; st
end else if not (IntSet.mem i st.not_installed) then begin
add_piece st i cont;
{st with not_installed = IntSet.add i st.not_installed}
end else
st
and maybe_add_piece st i cont =
if
not (IntSet.mem i st.installed || IntSet.mem i st.not_installed)
then begin
add_piece st i cont;
cont {st with not_installed = IntSet.add i st.not_installed}
end else
cont st
let find_problems dist deps confl check =
let pieces = Hashtbl.create 101 in
let last_piece = ref (-1) in
let pieces_in_confl = ListTbl.create 101 in
PTbl.iteri
(fun p f ->
Formula.iter f
(fun d ->
incr last_piece;
let i = !last_piece in
Hashtbl.add pieces i (p, d);
Disj.iter d (fun p -> ListTbl.add pieces_in_confl p i)))
deps;
let st =
{ dist = dist; deps = deps; confl = confl;
pieces = pieces; pieces_in_confl = pieces_in_confl;
set = PSet.empty; check = check;
installed = IntSet.empty; not_installed = IntSet.empty }
in
for i = 0 to !last_piece do
add_piece st i (fun _ -> ())
done
(****)
type state =
{ dist : M.deb_pool;
deps : Formula.t PTbl.t;
confl : Conflict.t;
deps' : Formula.t PTbl.t;
confl' : Conflict.t;
st : M.Solver.state }
(****)
type pkg_ref = string * bool * bool
type reason =
R_depends of pkg_ref * string M.dep * pkg_ref list
| R_conflict of pkg_ref * string M.dep * pkg_ref
type clause = { pos : StringSet.t; neg : StringSet.t }
let print_clause ch clause =
Util.print_list (fun f -> Format.fprintf f "-%s") " | " ch
(StringSet.elements clause.neg);
if not (StringSet.is_empty clause.pos || StringSet.is_empty clause.neg) then
Format.fprintf ch " | ";
Util.print_list (fun f -> Format.fprintf f "%s") " | " ch
(StringSet.elements clause.pos)
let problematic_packages dist1 dist dist2 reasons =
let resolve_dep dist l =
List.fold_left
(fun s cstr ->
List.fold_left
(fun s p -> StringSet.add (M.name_of_id p.M.package) s)
s (M.resolve_package_dep_raw dist cstr))
StringSet.empty l
in
let extern_deps l =
List.map (fun (id, rel) -> (M.name_of_id id, rel)) l in
let (s1, s2, lst) =
List.fold_left
(fun (s1, s2, lst) r ->
match r with
M.R_depends (n, l) ->
let p = M.find_package_by_num dist n in
let id = p.M.package in
let nm = M.name_of_id id in
let d = resolve_dep dist l in
let d1 = resolve_dep dist1 l in
let d2 = resolve_dep dist2 l in
let s1 = StringSet.union (StringSet.diff d1 d) s1 in
let s2 = StringSet.union (StringSet.diff d2 d) s2 in
let unchanged_dep dist =
match M.find_packages_by_name dist id with
[] ->
false
| [q] ->
M.compare_version q.M.version p.M.version = 0
||
List.exists
(fun l' ->
let d1' = resolve_dep dist1 l' in
let d2' = resolve_dep dist2 l' in
not (StringSet.is_empty d1' && StringSet.is_empty d2')
&&
StringSet.subset d1' d1
&&
StringSet.subset d2' d2)
(q.M.pre_depends @ q.M.depends)
| _ ->
assert false
in
let u1 = unchanged_dep dist1 in
let u2 = unchanged_dep dist2 in
let s1 = if u1 then s1 else StringSet.add nm s1 in
let s2 = if u2 then s2 else StringSet.add nm s2 in
let pkgs d1 d2 =
List.map
(fun nm -> (nm, StringSet.mem nm d1, StringSet.mem nm d2))
(StringSet.elements (StringSet.union d1 d2))
in
let d12 = StringSet.union d1 d2 in
let other_deps u dist old lst =
if u then lst else
match M.find_packages_by_name dist id with
[] ->
lst
| [q] ->
List.fold_left
(fun lst l' ->
let d1' = resolve_dep dist1 l' in
let d2' = resolve_dep dist2 l' in
if
StringSet.subset d1' d12
&&
StringSet.subset d2' d12
then
R_depends ((nm, old, not old), extern_deps l',
pkgs d1' d2') :: lst
else
lst)
lst (q.M.pre_depends @ q.M.depends)
| _ ->
assert false
in
let lst = other_deps u1 dist1 true lst in
let lst = other_deps u2 dist2 false lst in
(s1, s2,
R_depends ((nm, u1, u2), extern_deps l, pkgs d1 d2) :: lst)
| M.R_conflict (j, k, Some (i, l)) ->
let i' = if i = j then k else j in
let p = M.find_package_by_num dist i in
let p' = M.find_package_by_num dist i' in
let id = p.M.package in
let nm = M.name_of_id id in
let nm' = M.name_of_id p'.M.package in
let c1 = resolve_dep dist1 l in
let c2 = resolve_dep dist2 l in
let u1' = StringSet.mem nm' c1 in
let s1 = if u1' then s1 else StringSet.add nm' s1 in
let u2' = StringSet.mem nm' c2 in
let s2 = if u2' then s2 else StringSet.add nm' s2 in
let unchanged_cfl dist =
match M.find_packages_by_name dist id with
[] ->
false
| [q] ->
(*
Format.eprintf "%s ## %s (%a): %b %b %b %b@." nm nm' M.print_package_dependency (q.M.breaks @ q.M.conflicts) u1' (StringSet.mem nm' (resolve_dep dist1 (List.flatten (q.M.breaks @ q.M.conflicts)))) u2' (StringSet.mem nm' (resolve_dep dist1 (List.flatten (q.M.breaks @ q.M.conflicts))));
*)
M.compare_version q.M.version p.M.version = 0
||
(let l' = List.flatten (q.M.breaks @ q.M.conflicts) in
(not u1' || StringSet.mem nm' (resolve_dep dist1 l'))
&&
(not u2' || StringSet.mem nm' (resolve_dep dist2 l')))
| _ ->
assert false
in
let u1 = unchanged_cfl dist1 in
let u2 = unchanged_cfl dist2 in
let s1 = if u1 then s1 else StringSet.add nm s1 in
let s2 = if u2 then s2 else StringSet.add nm s2 in
(s1, s2,
R_conflict ((nm, u1, u2), extern_deps l, (nm', u1', u2')) :: lst)
| M.R_conflict (_, _, None) ->
assert false)
(StringSet.empty, StringSet.empty, []) reasons
in
({pos = s1; neg = s2}, List.rev lst)
let compute_support dist1 dist2 reasons =
let add support (nm, _, _) = StringSet.add nm support in
let support =
List.fold_left
(fun support r ->
match r with
R_depends (p, _, pl) -> add (List.fold_left add support pl) p
| R_conflict (p1, _, p2) -> add (add support p2) p1)
StringSet.empty reasons
in
(support,
StringSet.filter
(fun nm -> M.has_package_of_name dist1 (M.add_name nm)) support,
StringSet.filter
(fun nm -> M.has_package_of_name dist2 (M.add_name nm)) support)
let problematic_packages dist1 dist dist2 s reasons =
let (clause, reasons) = problematic_packages dist1 dist dist2 reasons in
let (support_set, support1, support2) =
compute_support dist1 dist2 reasons in
let support = Array.of_list (StringSet.elements support_set) in
let package_index = Hashtbl.create 17 in
Array.iteri (fun n nm -> Hashtbl.add package_index nm n) support;
let n = Array.length support in
let conflict = Hashtbl.create 5 in
PSet.iter
(fun p ->
let p = M.find_package_by_num dist (Package.index p) in
Hashtbl.add conflict (M.name_of_id p.M.package) ())
s;
let in_testing nm =
match
M.find_packages_by_name dist1 (M.id_of_name nm),
M.find_packages_by_name dist (M.id_of_name nm)
with
[p1], [p] -> M.compare_version p1.M.version p.M.version = 0
| [], [] -> true
| _ -> false
in
let var nm t = (2 * Hashtbl.find package_index nm + if t then 0 else 1) in
let nlit nm t = M.Solver.lit_of_var (var nm t) false in
let plit nm t = M.Solver.lit_of_var (var nm t) true in
let print_var ch p =
Format.fprintf ch "%s(%s)"
support.(p / 2) (if p mod 2 = 0 then "testing" else "sid")
in
let pr = M.Solver.initialize_problem ~print_var (2 * n) in
let vars = ref [] in
Hashtbl.iter
(fun nm _ ->
vars := var nm true :: var nm false :: !vars;
M.Solver.add_rule pr [|plit nm true; plit nm false|] [])
conflict;
let lst = ref [] in
Hashtbl.iter
(fun nm _ ->
lst := var nm (not (in_testing nm)) :: !lst;
M.Solver.add_rule pr [|nlit nm true; nlit nm false|] [])
package_index;
(*
let print_ref f (nm, t, u) =
match t, u with
true, true -> Format.fprintf f "%s" nm
| true, false -> Format.fprintf f "%s (testing)" nm
| false, true -> Format.fprintf f "%s (sid)" nm
| false, false -> assert false
in
Format.eprintf "=======================@.";
*)
List.iter
(fun r ->
match r with
R_depends (p, _, pl) ->
(*
Format.eprintf "| %a => %a@." print_ref p
(Util.print_list print_ref " | ") pl;
*)
let lit = M.Solver.lit_of_var in
let l =
List.fold_right
(fun (nm, t, u) l ->
let l = if t then var nm true :: l else l in
let l = if u then var nm false :: l else l in
l)
pl []
in
let (nm, t, u) = p in
if t then begin
M.Solver.add_rule pr
(Array.of_list (lit (var nm true) false ::
List.map (fun x -> lit x true) l)) [];
M.Solver.associate_vars pr (lit (var nm true) true) l
end;
if u then begin
M.Solver.add_rule pr
(Array.of_list (lit (var nm false) false ::
List.map (fun x -> lit x true) l)) [];
M.Solver.associate_vars pr (lit (var nm false) true) l
end
| R_conflict ((nm1, t1, u1), _, (nm2, t2, u2)) ->
(*
Format.eprintf "| %a ## %a@." print_ref (nm1, t1, u1) print_ref (nm2, t2, u2);
*)
if t1 && t2 then
M.Solver.add_rule pr [|nlit nm1 true; nlit nm2 true|] [];
if t1 && u2 then
M.Solver.add_rule pr [|nlit nm1 true; nlit nm2 false|] [];
if u1 && t2 then
M.Solver.add_rule pr [|nlit nm1 false; nlit nm2 true|] [];
if u1 && u2 then
M.Solver.add_rule pr [|nlit nm1 false; nlit nm2 false|] [])
reasons;
(*
Format.eprintf ">> %a@." print_clause clause;
Format.eprintf "- "; List.iter (fun p -> Format.eprintf " %s (%s)" support.(p / 2)(if p mod 2 = 0 then "testing" else "sid")) !lst; Format.eprintf "@.";
*)
let rec minimize l l' f =
match l' with
[] ->
List.rev l
| x :: r ->
if f (List.rev_append l r) then
minimize (x :: l) r f
else
minimize l r f
in
let check lst =
(*
Format.eprintf ") - "; List.iter (fun p -> Format.eprintf " %s (%s)" support.(p / 2)(if p mod 2 = 0 then "testing" else "sid")) lst; Format.eprintf "@.";
*)
let res = M.Solver.solve_neg_list pr !vars lst in
(*
Format.eprintf ") ==> %b@." res;
*)
M.Solver.reset pr;
res
in
let lst = minimize [] !lst (fun lst -> check lst) in
(*
Format.eprintf "- "; List.iter (fun p -> Format.eprintf " %s (%s)" support.(p / 2)(if p mod 2 = 0 then "testing" else "sid")) lst; Format.eprintf "@.";
*)
let pos = ref StringSet.empty in
let neg = ref StringSet.empty in
List.iter
(fun x ->
let nm = support.(x / 2) in
if x mod 2 = 0 then
pos := StringSet.add nm !pos
else
neg := StringSet.add nm !neg)
lst;
({pos = !pos; neg = !neg}, reasons, support1, support2)
(****)
type problem =
{ p_clause : clause; p_issue : Util.StringSet.t; p_explain : reason list;
p_support1 : Util.StringSet.t; p_support2 : Util.StringSet.t }
type issue =
{ i_issue : PSet.t; i_problem : problem }
let prepare_analyze dist =
let (deps, confl) = Coinst.compute_dependencies_and_conflicts dist in
let (deps', confl') = Coinst.flatten_and_simplify dist deps confl in
let st = Coinst.generate_rules (Quotient.trivial dist) deps' confl' in
{ dist=dist; deps=deps; confl=confl; deps'=deps'; confl'=confl'; st=st }
let compute_predecessors dist1 dist2 =
PTbl.init dist2
(fun p2 ->
let nm = M.package_name dist2 (Package.index p2) in
match M.parse_package_name dist1 nm with
[] ->
if debug then Format.printf "%s is a new package@." nm;
-1
| [p1] ->
p1
| _ ->
assert false)
let analyze ?(check_new_packages = false) ignored_sets
?reference dist1_state dist2 =
let
{ dist = dist1; deps = deps1; confl = confl1;
deps' = deps1'; confl' = confl1'; st = st1 }
= dist1_state
in
let t = Timer.start () in
let t' = Timer.start () in
let (deps2, confl2) = Coinst.compute_dependencies_and_conflicts dist2 in
if debug_time () then
Format.eprintf " Deps and confls: %f@." (Timer.stop t');
let (deps2', confl2') = Coinst.flatten_and_simplify dist2 deps2 confl2 in
let t' = Timer.start () in
let st2 = Coinst.generate_rules (Quotient.trivial dist2) deps2' confl2' in
if debug_time () then begin
Format.eprintf " Rules: %f@." (Timer.stop t');
Format.eprintf " Target dist: %f@." (Timer.stop t)
end;
let t = Timer.start () in
let pred = compute_predecessors dist1 dist2 in
let new_conflicts = ref [] in
Conflict.iter confl2
(fun p2 q2 ->
let i = PTbl.get pred p2 in
let j = PTbl.get pred q2 in
if i <> -1 && j <> -1 then begin
let p1 = Package.of_index i in
let q1 = Package.of_index j in
if not (Conflict.check confl1 p1 q1) then begin
if debug_coinst () then begin
Format.eprintf "possible new conflict: %a %a@."
(Package.print_name dist1) p1
(Package.print_name dist1) q1;
end;
new_conflicts := (p2, q2) :: !new_conflicts;
end
end);
let results = ref PSetSet.empty in
let add_result s =
if not (PSetSet.mem s !results) then begin
if debug_coinst () then begin
Format.eprintf "==>";
PSet.iter
(fun p -> Format.eprintf " %a" (Package.print_name dist2) p) s;
Format.eprintf "@."
end;
results := PSetSet.add s !results
end
in
let is_installable p =
let res = M.Solver.solve st2 (Package.index p) in
M.Solver.reset st2;
res
and was_installable p =
let res = M.Solver.solve st1 (PTbl.get pred p) in
M.Solver.reset st1;
res
in
(*
(* Clearly non installable packages *)
PTbl.iteri
(fun p f ->
if
PTbl.get pred p <> -1 &&
Formula.implies f Formula._false && was_installable p
then
add_result (PSet.singleton p))
deps2';
*)
(* New conflict pairs *)
List.iter
(fun (p2, q2) ->
let pi = is_installable p2 in
let qi = is_installable q2 in
if not pi && was_installable p2 then add_result (PSet.singleton p2);
if not qi && was_installable q2 then add_result (PSet.singleton q2);
if pi && qi then begin
let i = PTbl.get pred p2 in
let j = PTbl.get pred q2 in
let p1 = Package.of_index i in
let q1 = Package.of_index j in
if M.Solver.solve_lst st1 [i; j] then begin
if debug then begin
Format.printf "new conflict: %a %a@."
(Package.print_name dist1) p1
(Package.print_name dist1) q1;
end;
add_result (PSet.add p2 (PSet.add q2 PSet.empty))
end else begin
if debug then begin
Format.printf "NOT new conflict: %a %a@."
(Package.print_name dist1) p1
(Package.print_name dist1) q1;
M.show_reasons dist1 (M.Solver.collect_reasons_lst st1 [i; j])
end
end;
M.Solver.reset st1
end)
!new_conflicts;
(* Only consider new dependencies. *)
let ignored_sets' = intern_ignored_sets dist2 !ignored_sets in
let possibly_ignored_packages =
ignored_set_domain_2 ignored_sets' in
let deps2 = new_deps pred possibly_ignored_packages deps1 dist2 deps2 in
(* Compute the corresponding flattened dependencies. *)
let deps2 =
PTbl.mapi
(fun p f ->
Formula.fold
(fun d f ->
Formula.conj
(PSet.fold
(fun p f -> Formula.disj (PTbl.get deps2' p) f)
(Disj.to_lits d) Formula._false) f)
f Formula._true)
deps2
in
(* Only keep those that are new... *)
let deps2 = new_deps pred PSet.empty deps1' dist2 deps2 in
(* ...and that are indeed in the flattened repository *)
let deps2 =
PTbl.mapi
(fun p f ->
let f' = PTbl.get deps2' p in
Formula.filter
(fun d ->
Formula.exists (fun d' -> Disj.equiv d d') f') f)
deps2
in
(* Only keep relevant conflicts. *)
let dep_targets = ref PSet.empty in
PTbl.iteri
(fun _ f ->
Formula.iter f
(fun d ->
Disj.iter d (fun p -> dep_targets := PSet.add p !dep_targets)))
deps2;
Conflict.iter confl2'
(fun p2 q2 ->
let i1 = PTbl.get pred p2 in
let j1 = PTbl.get pred q2 in
if
not ((PSet.mem p2 !dep_targets && j1 <> -1) ||
(PSet.mem q2 !dep_targets && i1 <> -1) ||
(PSet.mem p2 !dep_targets && PSet.mem q2 !dep_targets))
then
Conflict.remove confl2' p2 q2);
(*
List.iter
(fun (p2, q2) ->
if
not (PSet.mem p2 possibly_ignored_packages
||
PSet.mem q2 possibly_ignored_packages)
then
Conflict.remove confl2' p2 q2)
!new_conflicts;
*)
(* As a consequence, some new dependencies might not be relevant anymore. *)
let deps2 = Coinst.remove_clearly_irrelevant_deps confl2' deps2 in
(* Add self dependencies for packages with conflicts, as we want to
consider them as well to find possible problems. *)
let deps2 =
PTbl.mapi
(fun p f ->
if Conflict.has confl2' p && PTbl.get pred p <> -1 then
Formula.conj (Formula.lit p) f
else
f)
deps2
in
if debug_problem_graph () then
Graph.output "/tmp/newdeps.dot"
~package_weight:(fun p ->
if Formula.implies (Formula.lit p) (PTbl.get deps2 p) then
(if PTbl.get pred p = -1 then 1. else 10.)
else 1000.)
(Quotient.trivial dist2) deps2 confl2';
(*
Conflict.iter confl2' (fun p q -> Format.eprintf "%a ## %a@." (Package.print dist2) p (Package.print dist2) q);
PTbl.iteri (fun p f -> Format.eprintf "%a: %a@." (Package.print dist2) p (Formula.print dist2) f) deps2;
*)
if debug_time () then Format.eprintf " Init: %f@." (Timer.stop t);
let check s =
let now_installable s =
let res =
M.Solver.solve_lst st2 (List.map Package.index (PSet.elements s)) in
M.Solver.reset st2;
res
in
let l = PSet.elements s in
let was_coinstallable =
M.Solver.solve_lst st1 (List.map (fun p -> PTbl.get pred p) l)
in
M.Solver.reset st1;
if not was_coinstallable then begin
if debug then begin
Format.printf "Was not co-installable:";
List.iter (fun p -> Format.printf " %a" (Package.print_name dist2) p) l;
Format.printf "@.";
end;
false
end else if now_installable s then begin
if debug then begin
Format.printf "Still co-installable:";
List.iter (fun p -> Format.printf " %a" (Package.print_name dist2) p) l;
Format.printf "@.";
end;
true
end else begin
if
PSet.exists (fun p -> not (now_installable (PSet.remove p s))) s
then begin
if debug_coinst () then begin
Format.eprintf "Not minimal:";
List.iter (fun p -> Format.eprintf " %a" (Package.print_name dist2) p) l;
Format.eprintf "@.";
end;
end else begin
add_result s
end;
false
end
in
let t = Timer.start () in
find_problems dist2 deps2 confl2' check;
if debug_time () then
Format.eprintf " Enumerating problems: %f@." (Timer.stop t);
let results =
PSetSet.filter (fun s -> not (is_ignored_set_2 ignored_sets' s)) !results
in
(****)
let t = Timer.start () in
let all_pkgs = ref PSet.empty in
let all_conflicts = Conflict.create dist2 in
let dep_src = PTbl.create dist2 PSet.empty in
let dep_trg = PTbl.create dist2 PSet.empty in
let add_rel r p q = PTbl.set r p (PSet.add q (PTbl.get r p)) in
let broken_new_packages = ref PSet.empty in
if check_new_packages then begin
let forced_pkgs = forced_packages ignored_sets in
PTbl.iteri
(fun p _ ->
if
PTbl.get pred p = -1 &&
not (StringSet.mem (M.package_name dist2 (Package.index p))
forced_pkgs)
then begin
(*Format.eprintf "??? %a@." (Package.print dist2) p;*)
if not (M.Solver.solve st2 (Package.index p)) then begin
(*
M.Solver.solve st2init (Package.index p);
M.Solver.reset st2init;
*)
broken_new_packages := PSet.add p !broken_new_packages
end;
M.Solver.reset st2
end)
deps2
end;
(****)
let (graphs, broken_new_packages) =
if PSetSet.is_empty results && PSet.is_empty !broken_new_packages then
([], [])
else begin
let s = PSetSet.fold PSet.union results !broken_new_packages in
let t = Timer.start () in
let st2init = M.generate_rules_restricted dist2 (pset_indices s) in
if debug_time () then
Format.eprintf " Generating constraints: %f@." (Timer.stop t);
(List.map
(fun s ->
let l = List.map Package.index (PSet.elements s) in
let res = M.Solver.solve_lst st2init l in
assert (not res);
let r = M.Solver.collect_reasons_lst st2init l in
M.Solver.reset st2init;
let confl = Conflict.create dist2 in
let deps = PTbl.create dist2 Formula._true in
let pkgs = ref PSet.empty in
let package i =
let p = Package.of_index i in pkgs := PSet.add p !pkgs; p in
(*
if debug_coinst () then M.show_reasons dist2 r;
*)
List.iter
(fun r ->
match r with
M.R_conflict (n1, n2, _) ->
Conflict.add confl (package n1) (package n2);
Conflict.add all_conflicts (package n1) (package n2)
| M.R_depends (n, l) ->
let p = package n in
let l =
List.map package
(List.flatten
(List.map (M.resolve_package_dep dist2) l))
in
List.iter
(fun q ->
add_rel dep_src q p;
add_rel dep_trg p q)
l;
PTbl.set deps p
(Formula.conj (PTbl.get deps p)
(Formula.of_disj (Disj.lit_disj l))))
r;
all_pkgs := PSet.union !all_pkgs !pkgs;
let (clause, explanation, support1, support2) =
match reference with
Some dist2_state ->
problematic_packages
dist1_state.dist dist2 dist2_state.dist s r
| None ->
problematic_packages dist1_state.dist dist2 dist2 s r
in
(*
PSet.iter (fun p -> Format.printf " %a" (Package.print_name dist2) p) s;
Format.printf "==> %a@." (Formula.print dist1) ppkgs;
*)
{ i_issue = s;
i_problem =
{ p_clause = clause;
p_issue =
PSet.fold
(fun p s ->
StringSet.add (M.package_name dist2 (Package.index p))
s)
s StringSet.empty;
p_explain = explanation;
p_support1 = support1; p_support2 = support2 } })