Source file opamActionGraph.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
open OpamTypes
module type ACTION = sig
type package
module Pkg : GenericPackage with type t = package
include OpamParallel.VERTEX with type t = package action
val to_string: [< t ] -> string
val to_aligned_strings:
?append:(package -> string) -> [< t ] list -> string list list
module Set: OpamStd.SET with type elt = package action
module Map: OpamStd.MAP with type key = package action
end
let name_of_action = function
| `Remove _ -> "remove"
| `Install _ -> "install"
| `Change (`Up,_,_) -> "upgrade"
| `Change (`Down,_,_) -> "downgrade"
| `Reinstall _ -> "recompile"
| `Build _ -> "build"
let symbol_of_action =
let open OpamConsole in
function
| `Remove _ ->
utf8_symbol Symbols.circled_division_slash
~alternates:[Symbols.greek_small_letter_lambda] "X"
| `Install _ ->
utf8_symbol Symbols.asterisk_operator
~alternates:[Symbols.six_pointed_black_star] "*"
| `Change (`Up,_,_) ->
utf8_symbol Symbols.north_east_arrow
~alternates:[Symbols.upwards_arrow] "U"
| `Change (`Down,_,_) ->
utf8_symbol Symbols.south_east_arrow
~alternates:[Symbols.downwards_arrow] "D"
| `Reinstall _ ->
utf8_symbol Symbols.clockwise_open_circle_arrow
~alternates:[Symbols.up_down_arrow] "R"
| `Build _ ->
utf8_symbol Symbols.greek_small_letter_lambda
~alternates:[Symbols.six_pointed_black_star] "B"
let action_strings ?utf8 a =
if utf8 = None && (OpamConsole.utf8 ()) || utf8 = Some true
then symbol_of_action a
else name_of_action a
let action_color c =
OpamConsole.colorise (match c with
| `Install _ | `Change (`Up,_,_) -> `green
| `Remove _ | `Change (`Down,_,_) -> `red
| `Reinstall _ -> `yellow
| `Build _ -> `cyan)
module MakeAction (P: GenericPackage) : ACTION with type package = P.t
= struct
module Pkg = P
type package = P.t
type t = package action
let compare t1 t2 =
match t1,t2 with
| `Remove p, `Remove q
| `Install p, `Install q
| `Reinstall p, `Reinstall q
| `Build p, `Build q
-> P.compare p q
| `Change (`Up,p0,p), `Change (`Up,q0,q)
| `Change (`Down,p0,p), `Change (`Down,q0,q)
->
let c = P.compare p q in
if c <> 0 then c else P.compare p0 q0
| `Install _, _ | _, `Remove _ -> 1
| _, `Install _ | `Remove _, _ -> -1
| `Build _, _ | _, `Change (`Down,_,_) -> 1
| `Change (`Down,_,_), _ | _, `Build _ -> -1
| `Change (`Up,_,_), `Reinstall _ -> 1
| `Reinstall _, `Change(`Up,_,_) -> -1
let hash a = Hashtbl.hash (OpamTypesBase.map_action P.hash a)
let equal t1 t2 = compare t1 t2 = 0
let to_string a = match a with
| `Remove p | `Install p | `Reinstall p | `Build p ->
Printf.sprintf "%s %s" (action_strings a) (P.to_string p)
| `Change (_,p0,p) ->
Printf.sprintf "%s.%s %s %s"
(P.name_to_string p0)
(P.version_to_string p0)
(action_strings a)
(P.version_to_string p)
let to_aligned_strings ?(append=(fun _ -> "")) l =
List.map (fun a ->
let a = (a :> package action) in
(if OpamConsole.utf8 ()
then action_color a (symbol_of_action a)
else "-")
:: name_of_action a
:: OpamConsole.colorise `bold
(P.name_to_string (OpamTypesBase.action_contents a))
:: match a with
| `Remove p | `Install p | `Reinstall p | `Build p ->
(P.version_to_string p ^ append p) :: []
| `Change (_,p0,p) ->
Printf.sprintf "%s to %s"
(P.version_to_string p0 ^ append p0)
(P.version_to_string p ^ append p)
:: [])
l
let to_json = function
| `Remove p -> `O ["remove", P.to_json p]
| `Install p -> `O ["install", P.to_json p]
| `Change (_, o, p) ->
`O ["change", `A [P.to_json o;P.to_json p]]
| `Reinstall p -> `O ["recompile", P.to_json p]
| `Build p -> `O ["build", P.to_json p]
module O = struct
type t = package action
let compare = compare
let to_string = to_string
let to_json = to_json
end
module Set = OpamStd.Set.Make(O)
module Map = OpamStd.Map.Make(O)
end
module type SIG = sig
type package
include OpamParallel.GRAPH with type V.t = package OpamTypes.action
val reduce: t -> t
val explicit: ?noop_remove:(package -> bool) -> t -> t
val fold_descendants: (V.t -> 'a -> 'a) -> 'a -> t -> V.t -> 'a
end
module Make (A: ACTION) : SIG with type package = A.package = struct
type package = A.package
include OpamParallel.MakeGraph(A)
module Map = OpamStd.Map.Make (A.Pkg)
module Set = OpamStd.Set.Make (A.Pkg)
let reduce g =
let g = copy g in
let removals =
fold_vertex (fun v acc -> match v with
| `Remove p ->
OpamStd.String.Map.add (A.Pkg.name_to_string p) p acc
| _ -> acc)
g OpamStd.String.Map.empty
in
iter_vertex (function
| `Build p as build ->
(match
fold_succ (fun v _ -> if v = `Install p then Some v else None)
g build None
with
| None -> ()
| Some inst ->
iter_pred (fun pred -> add_edge g pred inst) g build;
remove_vertex g build)
| _ -> ())
g;
let reduced = ref Map.empty in
let g =
map_vertex (function
| `Install p as act ->
(try
let p0 = OpamStd.String.Map.find (A.Pkg.name_to_string p) removals in
let act =
match A.Pkg.compare p0 p with
| 0 -> `Reinstall p
| c -> `Change ((if c < 0 then `Up else `Down), p0, p)
in
reduced := Map.add p0 act !reduced;
act
with Not_found -> act)
| act -> act)
g
in
Map.iter (fun p act ->
let rm_act = `Remove p in
iter_pred (fun v -> add_edge g v act) g rm_act;
remove_vertex g rm_act
) !reduced;
g
let compute_closed_predecessors noop_remove g =
let closed_g = copy g in
transitive_closure closed_g;
let closed_packages =
fold_vertex (fun a acc ->
match a with
| `Build p ->
let pred =
List.filter
(function
| `Remove nv -> not (noop_remove nv)
| _ -> true)
(pred closed_g a) in
if pred = [] then Set.add p acc else acc
| _ -> acc) g Set.empty
in
let dependent_base_packages =
fold_vertex (fun a acc ->
match a with
| `Install p | `Reinstall p | `Change (_,_,p) ->
let preds =
List.filter
(function
| `Build p -> Set.mem p closed_packages
| _ -> false)
(pred closed_g a) in
OpamStd.String.Map.add (A.Pkg.name_to_string p) preds acc
| _ -> acc) g OpamStd.String.Map.empty in
function p ->
match
OpamStd.String.Map.find_opt
(A.Pkg.name_to_string p)
dependent_base_packages
with
| None -> []
| Some pred -> pred
let explicit ?(noop_remove = (fun _ -> false)) g0 =
let g = copy g0 in
let same_name p1 p2 = A.Pkg.(name_to_string p1 = name_to_string p2) in
iter_vertex (fun a ->
match a with
| `Install p | `Reinstall p | `Change (_,_,p) ->
let b = `Build p in
iter_pred (function
| `Remove p1 when same_name p p1 -> ()
| pred -> remove_edge g pred a; add_edge g pred b)
g0 a;
add_edge g b a
| `Remove _ -> ()
| `Build _ -> assert false)
g0;
let closed_predecessors = compute_closed_predecessors noop_remove g in
iter_vertex (function
| `Remove p as a ->
List.iter
(fun b -> add_edge g b a)
(closed_predecessors p)
| `Install _ | `Reinstall _ | `Change _ | `Build _ -> ())
g;
g
let fold_descendants f acc t v =
let rec aux seen f acc t v =
if A.Set.mem v seen then seen, acc else
fold_succ (fun v (seen, acc) -> aux seen f acc t v)
t v (A.Set.add v seen, f v acc)
in
snd (aux A.Set.empty f acc t v)
end