Source file pefcudf.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
(** PEF (package exchange format) conversion routines *)
open ExtLib
open Dose_common
include Util.Logging (struct
let label = "dose_pef.pefcudf"
end)
type tables =
{ strings_table : string Util.StringHashtbl.t;
versions_table : (string, string list) Hashtbl.t;
reverse_table : (string * int, string) Hashtbl.t
}
let create n =
{ strings_table = Util.StringHashtbl.create n;
versions_table = Hashtbl.create n;
reverse_table = Hashtbl.create n
}
let clear tables =
Util.StringHashtbl.clear tables.strings_table ;
Hashtbl.clear tables.versions_table ;
Hashtbl.clear tables.reverse_table
let pefcudf_op = function
| "<<" | "<" -> `Lt
| ">>" | ">" -> `Gt
| "<=" -> `Leq
| ">=" -> `Geq
| "=" -> `Eq
| "!=" -> `Neq
| c -> fatal "Unknown operator: %s" c
let pefcudf_constr = function
| None -> None
| Some ("ALL", _) -> None
| Some (c, v) -> Some (pefcudf_op c, v)
(** convert a pef constraint into a cudf constraint *)
let pefvpkg to_cudf vpkgname =
let constr n = function
| None -> None
| Some (op, v) -> Some (pefcudf_op op, snd (to_cudf (n, v)))
in
match vpkgname with
| ((n, None), c) -> (CudfAdd.encode n, constr n c)
| ((n, Some ("any" | "native")), c) -> (CudfAdd.encode n, constr n c)
| ((n, Some a), c) -> (CudfAdd.encode (n ^ ":" ^ a), constr n c)
let init_versions_table tables table =
let add name version =
try
let version = Util.hashcons tables.strings_table version in
let l = Hashtbl.find table name in
Hashtbl.replace table name (version :: l)
with Not_found -> Hashtbl.add table name [version]
in
let conj_iter =
List.iter (fun ((name, _), sel) ->
match pefcudf_constr sel with
| None -> ()
| Some (_, version) -> add name version)
in
let cnf_iter =
List.iter (fun disjunction ->
List.iter
(fun ((name, _), sel) ->
match pefcudf_constr sel with
| None -> ()
| Some (_, version) -> add name version)
disjunction)
in
fun pkg ->
add pkg#name pkg#version ;
conj_iter pkg#provides ;
conj_iter pkg#conflicts ;
cnf_iter pkg#depends ;
cnf_iter pkg#recommends
let init_virtual_table table pkg =
let add name =
if not (Hashtbl.mem table name) then Hashtbl.add table name ()
in
List.iter (fun (name, _) -> add name) pkg#provides
let init_unit_table table pkg =
if not (Hashtbl.mem table pkg#name) then Hashtbl.add table pkg#name ()
let init_versioned_table table pkg =
let add name =
if not (Hashtbl.mem table name) then Hashtbl.add table name ()
in
let add_iter_cnf =
List.iter (fun disjunction ->
List.iter (fun (name, _) -> add name) disjunction)
in
List.iter (fun (name, _) -> add name) pkg#conflicts ;
add_iter_cnf pkg#depends
let init_tables compare pkglist =
let n = 2 * List.length pkglist in
let tables = create n in
let temp_versions_table = Hashtbl.create n in
let ivt = init_versions_table tables temp_versions_table in
List.iter (fun pkg -> ivt pkg) pkglist ;
Hashtbl.iter
(fun k l ->
Hashtbl.add
tables.versions_table
k
(List.unique (List.sort ~cmp:compare l)))
temp_versions_table ;
Hashtbl.clear temp_versions_table ;
tables
let get_cudf_version tables (package, version) =
try
let version = Util.hashcons tables.strings_table version in
let l = Hashtbl.find tables.versions_table package in
let i = fst (List.findi (fun _ a -> a = version) l) in
Hashtbl.replace tables.reverse_table (CudfAdd.encode package, i + 1) version ;
i + 1
with Not_found ->
warning "Cannot find cudf version for pef package %s (= %s)" package version ;
raise Not_found
let get_real_version tables (p, i) =
let n = try fst (ExtString.String.split p "%3a") with Invalid_string -> p in
if CudfAdd.is_nan_version i then (CudfAdd.decode n, None, "nan")
else
try (CudfAdd.decode n, None, Hashtbl.find tables.reverse_table (n, i))
with Not_found ->
warning "Cannot find real version for cudf package %s (= %d)" p i ;
raise Not_found
let encode_vpkgname ?arch ?(archs = []) vpkgname =
let aux name = function Some a -> name ^ ":" ^ a | None -> name in
match vpkgname with
| (name, None) -> [CudfAdd.encode (aux name arch)]
| (name, Some ("any" | "native")) ->
List.map (fun a -> CudfAdd.encode (name ^ ":" ^ a)) archs
| (name, Some a) -> [CudfAdd.encode (name ^ ":" ^ a)]
let loadl tables ?arch ?(archs = []) l =
List.flatten
(List.map
(fun (((name, _) as vpkgname), constr) ->
List.map
(fun encname ->
match pefcudf_constr constr with
| None -> (encname, None)
| Some (op, v) ->
(encname, Some (op, get_cudf_version tables (name, v))))
(encode_vpkgname ?arch ~archs vpkgname))
l)
let loadlp tables ?arch ?(archs = []) l =
List.flatten
(List.map
(fun (((name, _) as vpkgname), constr) ->
List.map
(fun encname ->
match pefcudf_constr constr with
| None -> (encname, None)
| Some (`Eq, v) ->
(encname, Some (`Eq, get_cudf_version tables (name, v)))
| _ -> assert false)
(encode_vpkgname ?arch ~archs vpkgname))
l)
let loadlc tables ?arch ?(archs = []) l = loadl tables ?arch ~archs l
let loadll tables ?arch ?(archs = []) ll =
List.map (fun l -> loadl tables ?arch ~archs l) ll
let preamble =
let l = [("recommends", `Vpkgformula (Some [])); ("number", `String None)] in
CudfAdd.add_properties Cudf.default_preamble l
let tables pkg =
let number = ("number", `String pkg#version) in
let l =
List.filter_map
(fun (debprop, (cudfprop, v)) ->
let debprop = String.lowercase debprop in
let cudfprop = String.lowercase cudfprop in
try
let s = List.assoc debprop pkg#extras in
let typ = Cudf_types.type_of_typedecl v in
try Some (cudfprop, Cudf_types_pp.parse_value typ s)
with Cudf_types_pp.Type_error _ ->
fatal
"Cudf Parsing Error while converting properties %s: %s"
debprop
s
with Not_found -> None)
extras
in
let recommends =
("recommends", `Vpkgformula (loadll tables pkg#recommends))
in
List.filter_map
(function
| (_, `Vpkglist []) -> None | (_, `Vpkgformula []) -> None | e -> Some e)
[number; recommends]
@ l
let tocudf tables ?( = []) ?( = fun _ _ -> []) pkg =
{ Cudf.default_package with
Cudf.package = CudfAdd.encode pkg#name;
Cudf.version = get_cudf_version tables (pkg#name, pkg#version);
Cudf.depends = loadll tables pkg#depends;
Cudf.conflicts = loadlc tables pkg#conflicts;
Cudf.provides = loadlp tables pkg#provides;
Cudf.pkg_extra = add_extra extras tables pkg @ extrasfun tables pkg
}
let load_list compare l =
let timer = Util.Timer.create "Pef.ToCudf" in
Util.Timer.start timer ;
let tables = init_tables compare l in
let pkglist = List.map (tocudf tables) l in
clear tables ;
Util.Timer.stop timer pkglist
let load_universe compare l =
let pkglist = load_list compare l in
let timer = Util.Timer.create "Pef.ToCudf" in
Util.Timer.start timer ;
let univ = Cudf.load_universe pkglist in
Util.Timer.stop timer univ