Source file debcudf.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
(** Debian Specific Cudf conversion routines *)
module SSet = Set.Make (struct
type t = string * Dose_pef.Packages_types.constr option
let compare = Stdlib.compare
end)
module OcamlHashtbl = Hashtbl
open ExtLib
open Dose_common
module Version = Dose_versioning.Debian
include Util.Logging (struct
let label = "dose_deb.debcudf"
end)
module SMap = Map.Make (String)
type tables =
{ virtual_table : (string, SSet.t ref) OcamlHashtbl.t;
(** all names of virtual packages *)
unit_table : unit Util.StringHashtbl.t;
(** all names of real packages *)
versions_table : int Util.StringHashtbl.t;
(** version -> int table *)
reverse_table : string SMap.t ref Util.IntHashtbl.t;
versioned_table : unit Util.StringHashtbl.t;
(** all (versions,name) tuples.
all versions mentioned of depends,
pre_depends, conflict and breaks *)
essential_table : (string, Packages.package list ref) Hashtbl.t
(** name -> packages list *)
}
let create n =
{ virtual_table = OcamlHashtbl.create (10 * n);
unit_table = Util.StringHashtbl.create (2 * n);
versions_table = Util.StringHashtbl.create (10 * n);
versioned_table = Util.StringHashtbl.create (10 * n);
reverse_table = Util.IntHashtbl.create (10 * n);
essential_table = Hashtbl.create (n / 100)
}
type options =
{ extras_opt : extramap;
native : string option;
foreign : string list;
host : string option;
ignore_essential : bool;
builds_from : bool;
drop_bd_indep : bool;
drop_bd_arch : bool;
profiles : string list
}
let default_options =
{ extras_opt = [];
native = None;
foreign = [];
host = None;
ignore_essential = false;
builds_from = false;
drop_bd_indep = false;
drop_bd_arch = false;
profiles = []
}
let add_name_arch n = function
| "" -> CudfAdd.encode n
| a -> CudfAdd.encode (Printf.sprintf "%s:%s" n a)
let add_arch native_arch name = function
| "all" -> add_name_arch name native_arch
| package_arch -> add_name_arch name package_arch
let add_arch_info ?(native_arch = "") ?(package_arch = "") = function
| (n, Some "any") -> CudfAdd.encode n
| (n, Some "native") -> add_name_arch n native_arch
| (n, Some a) -> add_name_arch n a
| (n, None) -> add_arch native_arch n package_arch
let clear tables =
OcamlHashtbl.clear tables.virtual_table ;
Util.StringHashtbl.clear tables.unit_table ;
Util.StringHashtbl.clear tables.versions_table ;
Util.StringHashtbl.clear tables.versioned_table ;
Util.IntHashtbl.clear tables.reverse_table ;
Hashtbl.clear tables.essential_table
let add_v tables k v =
if not (Util.StringHashtbl.mem tables.versions_table k) then
Util.StringHashtbl.add tables.versions_table k v
let add_p table k =
if not (Util.StringHashtbl.mem table k) then Util.StringHashtbl.add table k ()
let add_pairs table (v, p) =
if not (Util.StringPairHashtbl.mem table (v, p)) then
Util.StringPairHashtbl.add table (v, p) ()
let add_s h k v =
try
let s = OcamlHashtbl.find h k in
s := SSet.add v !s
with Not_found -> OcamlHashtbl.add h k (ref (SSet.singleton v))
let init_virtual_table tables pkg =
List.iter
(fun ((name, _), constr) ->
add_s tables.virtual_table name (pkg#name, constr))
pkg#provides
let init_unit_table tables pkg = add_p tables.unit_table pkg#name
let init_versions_table tables table pkg =
let versionedtables = tables.versioned_table in
let conj_iter l =
List.iter
(fun ((name, _), constr) ->
add_p versionedtables name ;
match constr with
| None -> ()
| Some (_, version) -> add_pairs table (version, name))
l
in
let cnf_iter ll = List.iter conj_iter ll in
let add_source pv = function
| (p, None) -> add_pairs table (pv, p)
| (p, Some v) -> add_pairs table (v, p)
in
add_pairs table (pkg#version, pkg#name) ;
conj_iter pkg#breaks ;
conj_iter pkg#provides ;
conj_iter pkg#conflicts ;
conj_iter pkg#replaces ;
cnf_iter pkg#depends ;
cnf_iter pkg#pre_depends ;
cnf_iter pkg#recommends ;
add_source pkg#version pkg#source
let init_tables ?(options = default_options) ?(step = 1) ?(versionlist = [])
pkglist =
let n = List.length pkglist in
let tables = create n in
let temp_versions_table = Util.StringPairHashtbl.create (10 * n) in
let ivrt = init_virtual_table tables in
let ivt = init_versions_table tables temp_versions_table in
let iut = init_unit_table tables in
let iet pkg =
if (not options.ignore_essential) && pkg#essential then
CudfAdd.add_to_package_list tables.essential_table pkg#name pkg
in
List.iter (fun v -> add_pairs temp_versions_table (v, "")) versionlist ;
List.iter
(fun pkg ->
iet pkg ;
ivt pkg ;
ivrt pkg ;
iut pkg)
pkglist ;
let l =
Util.StringPairHashtbl.fold (fun v _ acc -> v :: acc) temp_versions_table []
in
let add_reverse i (n, v) =
try
let m = Util.IntHashtbl.find tables.reverse_table i in
m := SMap.add n v !m
with Not_found ->
let m = SMap.add n v SMap.empty in
Util.IntHashtbl.add tables.reverse_table i (ref m)
in
let sl = List.sort ~cmp:(fun x y -> Version.compare (fst x) (fst y)) l in
let rec numbers (prec, i) = function
| [] -> ()
| (v, n) :: t ->
if Version.equal v prec then (
add_v tables v i ;
if n <> "" then add_reverse i (n, v) ;
numbers (prec, i) t)
else (
add_v tables v (i + step) ;
if n <> "" then add_reverse (i + step) (n, v) ;
numbers (v, i + step) t)
in
numbers ("", 1) sl ;
tables
let get_cudf_version tables (package, version) =
try Util.StringHashtbl.find tables.versions_table version
with Not_found ->
warning
"Package (%s,%s) does not have an associated cudf version"
package
version ;
raise Not_found
let get_real_name name =
let dn = CudfAdd.decode name in
let no_virtual =
if ExtString.String.starts_with dn "--vir" then
let maybe_versioned = ExtString.String.slice ~first:10 dn in
if ExtString.String.starts_with maybe_versioned "-ver" then
ExtString.String.slice ~first:11 maybe_versioned
else maybe_versioned
else dn
in
try
let (n, a) = ExtString.String.split no_virtual ":" in
if n = "src" then (a, None) else (n, Some a)
with Invalid_string -> (no_virtual, None)
let get_real_version tables (cudfname, cudfversion) =
let (debname, arch) = get_real_name cudfname in
try
if CudfAdd.is_nan_version cudfversion then (debname, arch, "nan")
else
let m = !(Util.IntHashtbl.find tables.reverse_table cudfversion) in
try (debname, arch, SMap.find debname m)
with Not_found ->
let known =
String.concat
","
(List.map
(fun (n, v) -> Printf.sprintf "(%s,%s)" n v)
(SMap.bindings m))
in
fatal
"Unable to get real version for %s (%i)\n\
\ All Known versions for this package are %s"
debname
cudfversion
known
with Not_found ->
fatal
"Package (%s,%d) does not have an associated debian version"
cudfname
cudfversion
let loadl ?native_arch ?package_arch tables l =
List.flatten
(List.map
(fun (((name, _) as vpkgname), constr) ->
let encname = add_arch_info ?native_arch ?package_arch vpkgname in
let (virt_prefix, constr) =
match constr with
| None ->
("--virtual-", None)
| Some (op, v) ->
let op = Dose_pef.Pefcudf.pefcudf_op op in
let constr = Some (op, get_cudf_version tables (name, v)) in
("--virtual--versioned-", constr)
in
if OcamlHashtbl.mem tables.virtual_table name then
[(encname, constr); (virt_prefix ^ encname, constr)]
else [(encname, constr)])
l)
let loadll ?native_arch ?package_arch tables ll =
List.filter_map
(fun l ->
match loadl ?native_arch ?package_arch tables l with
| [] -> None
| l -> Some l)
ll
let loadlc ?native_arch ?package_arch tables name l =
(CudfAdd.encode name, None) :: loadl ?native_arch ?package_arch tables l
let loadlp ?native_arch ?package_arch tables l =
List.flatten
(List.map
(fun (((name, _) as vpkgname), constr) ->
let encname = add_arch_info ?native_arch ?package_arch vpkgname in
let vencname = "--virtual-" ^ encname in
let vvencname = "--virtual--versioned-" ^ encname in
match constr with
| None -> [(vencname, Some (`Eq, CudfAdd.nan_version))]
| Some ("=", v) ->
let constr = Some (`Eq, get_cudf_version tables (name, v)) in
[(vvencname, constr); (vencname, Some (`Eq, CudfAdd.nan_version))]
| _ ->
fatal
"This should never happen : a provide can be either = or \
unversioned")
l)
let preamble =
let l =
[
("name", `String None);
("number", `String None);
("type", `String None);
("architecture", `String None);
("replaces", `Vpkglist (Some []));
("recommends", `Vpkgformula (Some []));
("priority", `String (Some ""));
("source", `String (Some ""));
("sourcenumber", `String (Some ""));
("sourceversion", `Int (Some 1));
("essential", `Bool (Some false));
("filename", `String (Some ""));
("installedsize", `Int (Some 0));
("multiarch", `String (Some ""));
("native", `Int (Some 0)) ]
in
CudfAdd.add_properties Cudf.default_preamble l
let ?native_arch tables pkg =
let name =
let n =
if String.starts_with pkg#name "src:" then
String.sub pkg#name 4 (String.length pkg#name - 4)
else pkg#name
in
Some ("name", `String n)
in
let number = Some ("number", `String pkg#version) in
let architecture = Some ("architecture", `String pkg#architecture) in
let priority = Some ("priority", `String pkg#priority) in
let essential =
if pkg#essential then Some ("essential", `Bool true) else None
in
let (source, sourcenumber, sourceversion) =
let (n, v) =
match pkg#source with
| ("", _) -> (pkg#name, pkg#version)
| (n, None) -> (n, pkg#version)
| (n, Some v) -> (n, v)
in
let cv = get_cudf_version tables ("", v) in
( Some ("source", `String n),
Some ("sourcenumber", `String v),
Some ("sourceversion", `Int cv) )
in
let recommends =
match loadll tables pkg#recommends with
| [] -> None
| l -> Some ("recommends", `Vpkgformula l)
in
let replaces =
match loadl tables pkg#replaces with
| [] -> None
| l -> Some ("replaces", `Vpkglist l)
in
let native =
if Option.is_some native_arch && Option.get native_arch = pkg#architecture
then Some ("native", `Int 1)
else None
in
let =
("Type", ("type", `String None))
:: ("Filename", ("filename", `String None))
:: ("Multi-Arch", ("multiarch", `String None))
:: ("Installed-Size", ("installedsize", `Int (Some 0)))
:: extras
in
let l =
List.filter_map
(fun (debprop, (cudfprop, v)) ->
try
let s = pkg#get_extra debprop 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
List.filter_map
(function
| Some (_, `Vpkglist []) -> None
| Some (_, `Vpkgformula []) -> None
| Some (_, `String "") -> None
| e -> e)
[ priority;
name;
architecture;
number;
source;
sourcenumber;
sourceversion;
recommends;
replaces;
essential;
native ]
@ l
let add_inst inst pkg = inst || Packages.is_installed pkg
let tocudf tables ?(options = default_options) ?(inst = false) pkg =
let bind m f = List.flatten (List.map f m) in
let encpkgname = CudfAdd.encode pkg#name in
if not (Option.is_none options.native) then
let native_arch = Option.get options.native in
let package_arch =
if Sources.is_source pkg then
match options.host with
| None ->
native_arch
| Some host_arch -> host_arch
else pkg#architecture
in
let _name =
if Sources.is_source pkg then encpkgname
else add_arch native_arch pkg#name package_arch
in
let _version = get_cudf_version tables (pkg#name, pkg#version) in
let _provides =
let archlessprovide = (encpkgname, None) in
let multiarchprovides =
match pkg#multiarch with
| `No | `Same ->
loadlp ~native_arch ~package_arch tables pkg#provides
| `Foreign ->
bind (native_arch :: options.foreign) (function
| arch when arch = package_arch ->
loadlp ~native_arch ~package_arch:arch tables pkg#provides
| arch ->
(add_arch native_arch pkg#name arch, Some (`Eq, _version))
:: loadlp
~native_arch
~package_arch:arch
tables
pkg#provides)
| `Allowed ->
let any = (CudfAdd.encode (pkg#name ^ ":any"), None) in
let l =
loadlp
~native_arch
~package_arch
tables
(bind pkg#provides (fun ((name, _), c) ->
[((name, Some "any"), c); ((name, None), c)]))
in
any :: l
in
archlessprovide :: multiarchprovides
in
let _conflicts =
let originalconflicts = pkg#breaks @ pkg#conflicts in
let sc = (add_arch native_arch pkg#name package_arch, None) in
let multiarchconstraints =
match pkg#multiarch with
| `No | `Foreign | `Allowed ->
let mac = (encpkgname, None) in
[sc; mac]
| `Same ->
let masc =
List.filter_map
(function
| arch when arch = package_arch -> None
| arch ->
Some
( add_arch native_arch pkg#name arch,
Some (`Neq, _version) ))
(native_arch :: options.foreign)
in
sc :: masc
in
let multiarchconflicts =
match pkg#multiarch with
| `No | `Foreign | `Allowed ->
bind (native_arch :: options.foreign) (fun arch ->
loadl ~native_arch ~package_arch:arch tables originalconflicts)
| `Same ->
let selfconflict = function
| ((n, _), None) ->
n = pkg#name
|| List.exists (fun ((p, _), _) -> p = n) pkg#provides
| ((n, _), _) -> n = pkg#name
in
let realpackage = Util.StringHashtbl.mem tables.unit_table in
bind (native_arch :: options.foreign) (fun arch ->
bind originalconflicts (fun ((n, a), c) ->
match selfconflict ((n, a), c) with
| false ->
let l =
match (realpackage n, c) with
| (false, Some _) ->
[]
| _ -> [((n, a), c)]
in
loadl ~native_arch ~package_arch:arch tables l
| true -> (
try
List.filter_map
(fun (pn, _) ->
if pn <> pkg#name then
let encname =
add_arch_info
~native_arch
~package_arch:arch
(pn, a)
in
Some (encname, None)
else None)
((if realpackage n then [(n, c)] else [])
@ SSet.elements
!(OcamlHashtbl.find tables.virtual_table n))
with Not_found -> [])
))
in
multiarchconflicts @ multiarchconstraints
in
let _depends =
loadll ~native_arch ~package_arch tables (pkg#pre_depends @ pkg#depends)
in
let _keep pkg =
if package_arch <> native_arch && package_arch <> "all" then `Keep_none
else if Packages.is_on_hold pkg && Packages.is_installed pkg then
`Keep_version
else `Keep_none
in
{ Cudf.default_package with
Cudf.package = _name;
Cudf.version = _version;
Cudf.keep = _keep pkg;
Cudf.depends = _depends;
Cudf.conflicts = _conflicts;
Cudf.provides = _provides;
Cudf.installed = add_inst inst pkg;
Cudf.pkg_extra = add_extra ~native_arch options.extras_opt tables pkg
}
else
{ Cudf.default_package with
Cudf.package = encpkgname;
Cudf.version = get_cudf_version tables (pkg#name, pkg#version);
Cudf.keep =
(if Packages.is_on_hold pkg then `Keep_version else `Keep_none);
Cudf.depends = loadll tables (pkg#pre_depends @ pkg#depends);
Cudf.conflicts = loadlc tables pkg#name (pkg#breaks @ pkg#conflicts);
Cudf.provides = loadlp tables pkg#provides;
Cudf.installed = add_inst inst pkg;
Cudf.pkg_extra = add_extra options.extras_opt tables pkg
}
let get_essential ?(options = default_options) tables =
Hashtbl.fold
(fun name { contents = l } acc ->
([(name, None)], List.map (tocudf tables ~options) l) :: acc)
tables.essential_table
[]
let load_list ?options l =
let timer = Util.Timer.create "Debian.ToCudf" in
Util.Timer.start timer ;
let tables = init_tables ?options l in
let pkglist = List.map (tocudf tables ?options) l in
clear tables ;
Util.Timer.stop timer pkglist
let load_universe ?options l =
let timer = Util.Timer.create "Debian.ToCudf" in
let pkglist = load_list ?options l in
Util.Timer.start timer ;
let univ = Cudf.load_universe pkglist in
Util.Timer.stop timer univ