package dose3

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file packages.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
(******************************************************************************)
(*  This file is part of the Dose library http://www.irill.org/software/dose  *)
(*                                                                            *)
(*  Copyright (C) 2009-2011 Pietro Abate <pietro.abate@pps.jussieu.fr>        *)
(*                                                                            *)
(*  This library is free software: you can redistribute it and/or modify      *)
(*  it under the terms of the GNU Lesser General Public License as            *)
(*  published by the Free Software Foundation, either version 3 of the        *)
(*  License, or (at your option) any later version.  A special linking        *)
(*  exception to the GNU Lesser General Public License applies to this        *)
(*  library, see the COPYING file for more information.                       *)
(*                                                                            *)
(*  Work developed with the support of the Mancoosi Project                   *)
(*  http://www.mancoosi.org                                                   *)
(*                                                                            *)
(******************************************************************************)

(** Representation of a debian package description item. *)

open ExtLib
open Dose_common

include Util.Logging (struct
  let label = "dose_deb.packages"
end)

let parse_multiarch (label, (_, s)) =
  match s with
  | "None" | "none" | "No" | "no" -> `No
  | "Allowed" | "allowed" -> `Allowed
  | "Foreign" | "foreign" -> `Foreign
  | "Same" | "same" -> `Same
  | _ ->
      raise
        (Format822.ParseError ([], label, Printf.sprintf "Wrong value : %s" s))

let parse_source v =
  Dose_pef.Packages.lexbuf_wrapper Dose_pef.Packages_parser.source_top v

let parse_binarylist v =
  Dose_pef.Packages.lexbuf_wrapper Dose_pef.Packages_parser.vpkglist_top v

class package ?(name = ("Package", None)) ?(version = ("Version", None))
  ?(depends = ("Depends", None)) ?(conflicts = ("Conflicts", None))
  ?(provides = ("Provides", None)) ?(recommends = ("Recommends", None))
  ?(architecture = ("Architecture", None)) ?(multiarch = ("Multi-Arch", None))
  ?(source = ("Source", None)) ?(essential = ("Essential", None))
  ?(extra_source_only = ("Extra-Source-Only", None))
  ?(priority = ("Priority", None)) ?(pre_depends = ("Pre-Depends", None))
  ?(suggests = ("Suggests", None)) ?(enhances = ("Enhances", None))
  ?(breaks = ("Breaks", None)) ?(replaces = ("Replaces", None))
  ?(extras = ([], None)) par =
  object
    inherit
      Dose_pef.Packages.package
        ~name
        ~version
        ~depends
        ~conflicts
        ~provides
        ~recommends
        ~extras
        par

    val architecture : string * Dose_pef.Packages_types.architecture =
      let parse =
        Dose_pef.Packages.parse_s ~required:true Dose_pef.Packages.parse_string
      in
      Dose_pef.Packages.get_field_value ~parse ~par ~field:architecture

    val multiarch : string * Dose_pef.Packages_types.multiarch =
      let parse = Dose_pef.Packages.parse_s ~default:`No parse_multiarch in
      Dose_pef.Packages.get_field_value ~parse ~par ~field:multiarch

    val source
        : string
          * (Dose_pef.Packages_types.name
            * Dose_pef.Packages_types.version option) =
      let parse = Dose_pef.Packages.parse_s ~default:("", None) parse_source in
      Dose_pef.Packages.get_field_value ~parse ~par ~field:source

    val essential : string * bool =
      let parse =
        Dose_pef.Packages.parse_s ~default:false Dose_pef.Packages.parse_bool
      in
      Dose_pef.Packages.get_field_value ~parse ~par ~field:essential

    val extra_source_only : string * bool =
      let parse =
        Dose_pef.Packages.parse_s ~default:false Dose_pef.Packages.parse_bool
      in
      Dose_pef.Packages.get_field_value ~parse ~par ~field:extra_source_only

    val priority : string * string =
      let parse =
        Dose_pef.Packages.parse_s ~default:"" Dose_pef.Packages.parse_string
      in
      Dose_pef.Packages.get_field_value ~parse ~par ~field:priority

    val pre_depends : string * Dose_pef.Packages_types.vpkgformula =
      let parse =
        Dose_pef.Packages.parse_s
          ~default:[]
          Dose_pef.Packages.parse_vpkgformula
      in
      Dose_pef.Packages.get_field_value ~parse ~par ~field:pre_depends

    val suggests : string * Dose_pef.Packages_types.vpkgformula =
      let parse =
        Dose_pef.Packages.parse_s
          ~default:[]
          Dose_pef.Packages.parse_vpkgformula
      in
      Dose_pef.Packages.get_field_value ~parse ~par ~field:suggests

    val enhances : string * Dose_pef.Packages_types.vpkgformula =
      let parse =
        Dose_pef.Packages.parse_s
          ~default:[]
          Dose_pef.Packages.parse_vpkgformula
      in
      Dose_pef.Packages.get_field_value ~parse ~par ~field:enhances

    val breaks : string * Dose_pef.Packages_types.vpkglist =
      let parse =
        Dose_pef.Packages.parse_s ~default:[] Dose_pef.Packages.parse_vpkglist
      in
      Dose_pef.Packages.get_field_value ~parse ~par ~field:breaks

    val replaces : string * Dose_pef.Packages_types.vpkglist =
      let parse =
        Dose_pef.Packages.parse_s ~default:[] Dose_pef.Packages.parse_vpkglist
      in
      Dose_pef.Packages.get_field_value ~parse ~par ~field:replaces

    method architecture = snd architecture

    method multiarch = snd multiarch

    method essential = snd essential

    method extra_source_only = snd extra_source_only

    method priority = snd priority

    method source = snd source

    method pre_depends = snd pre_depends

    method suggests = snd suggests

    method enhances = snd enhances

    method breaks = snd breaks

    method replaces = snd replaces

    method set_multiarch v = {<multiarch = (fst multiarch, v)>}

    method set_essential v = {<essential = (fst essential, v)>}

    (* this method override the method in the super class *)
    method! pp oc =
      let pp_multiarch = function
        | `No -> "no"
        | `Same -> "same"
        | `Foreign -> "foreign"
        | `Allowed -> "allowed"
      in
      let pp_source = function
        | (source, None) when source <> "" -> source
        | (source, Some version) -> Printf.sprintf "%s (%s)" source version
        | _ -> ""
      in
      Dose_pef.Printer.pp_string_wl oc name ;
      Dose_pef.Printer.pp_string_wl oc version ;
      Dose_pef.Printer.pp_string_wl oc architecture ;
      Dose_pef.Printer.pp_function_wl oc ~tostring:pp_multiarch multiarch ;
      Dose_pef.Printer.pp_yes_wl oc essential ;
      Dose_pef.Printer.pp_string_wl oc priority ;
      Dose_pef.Printer.pp_function_wl oc ~tostring:pp_source source ;
      Dose_pef.Printer.pp_vpkglist_wl oc provides ;
      Dose_pef.Printer.pp_vpkgformula_wl oc depends ;
      Dose_pef.Printer.pp_vpkgformula_wl oc pre_depends ;
      Dose_pef.Printer.pp_vpkglist_wl oc conflicts ;
      Dose_pef.Printer.pp_vpkglist_wl oc breaks ;
      Dose_pef.Printer.pp_vpkgformula_wl oc suggests ;
      Dose_pef.Printer.pp_vpkgformula_wl oc enhances ;
      Dose_pef.Printer.pp_vpkgformula_wl oc recommends ;
      Dose_pef.Printer.pp_vpkglist_wl oc replaces ;
      Printf.fprintf oc "\n"
  end

let parse_package_stanza filter archs extras par =
  let p () =
    let pkg = new package ~extras:(extras, Some [("Type", "bin")]) par in
    (* make sure that the "all" arch is always considered *)
    if archs = [] then pkg
    else if List.mem pkg#architecture ("all" :: archs) then pkg
    else
      raise
        (Dose_pef.Packages.IgnorePackage
           (Printf.sprintf
              "architecture: %s is not included in %s"
              pkg#architecture
              (ExtString.String.join "," ("all" :: archs))))
  in
  try
    if Option.is_none filter then Some (p ())
    else if (Option.get filter) par then Some (p ())
    else None
  with
  | Dose_pef.Packages.IgnorePackage s ->
      let n =
        Dose_pef.Packages.parse_s
          ~default:"?"
          Dose_pef.Packages.parse_name
          "Package"
          par
      in
      let v =
        Dose_pef.Packages.parse_s
          ~default:"?"
          Dose_pef.Packages.parse_version
          "Version"
          par
      in
      let a =
        Dose_pef.Packages.parse_s
          ~default:"?"
          Dose_pef.Packages.parse_string
          "Architecture"
          par
      in
      warning "Ignoring Package (%s,%s,%s) : %s" n v a s ;
      None
  | Format822.ParseError (cl, f, err) ->
      let n =
        Dose_pef.Packages.parse_s
          ~default:"?"
          Dose_pef.Packages.parse_name
          "Package"
          par
      in
      let v =
        Dose_pef.Packages.parse_s
          ~default:"?"
          Dose_pef.Packages.parse_version
          "Version"
          par
      in
      let a =
        Dose_pef.Packages.parse_s
          ~default:"?"
          Dose_pef.Packages.parse_string
          "Architecture"
          par
      in
      let c = Printf.sprintf "Parser Error in Package (%s,%s,%s)" n v a in
      raise (Format822.ParseError (c :: cl, f, err))

(*
let status_filter par =
  try
    let (_,s) = (Dose_pef.Packages.assoc "Status" par) in
    match String.nsplit s " " with
    |[_;_;"installed"] -> true
    |_ -> false
  with Not_found -> false

let arch_filter archlist par =
  try
    let (_,s) = (Dose_pef.Packages.assoc "Architecture" par) in
    List.mem s archlist
  with Not_found -> false
*)

let parse_packages_in ?filter ?(archs = []) ?(extras = []) fname ic =
  info "Parsing Packages file %s..." fname ;
  try
    let stanza_parser = parse_package_stanza filter archs extras in
    Format822.parse_from_ch
      (Dose_pef.Packages.packages_parser fname stanza_parser)
      ic
  with Format822.ParseError (cl, field, errmsg) ->
    fatal
      "Filename %s\n %s\n %s : %s"
      fname
      (String.concat "\n " cl)
      field
      errmsg

let id p = (p#name, p#version, p#architecture)

let ( >% ) p1 p2 = Stdlib.compare (id p1) (id p2)

module Set = struct
  include Set.Make (struct
    type t = package

    let compare x y =
      let c = x >% y in
      if c = 0 && x#architecture <> "all" then
        debug
          "the input contains two packages with the same name, version and \
           architecture (%s,%s,%s). Only the latter will be considered."
          x#name
          x#version
          x#architecture ;
      c
  end)
end

let merge status packages =
  if List.length status > 0 then (
    info "Merging status file" ;
    let merge_aux p1 p2 =
      if p1 >% p2 = 0 then
        let paux = p1#set_essential (p1#essential || p2#essential) in
        paux#set_extras (List.unique (p1#extras @ p2#extras))
      else fatal "Something went wrong while merging status+packages"
    in
    let h = Hashtbl.create (List.length status) in
    List.iter (fun p -> Hashtbl.add h (id p) p) status ;
    let ps =
      List.fold_left
        (fun acc p ->
          try Set.add (merge_aux p (Hashtbl.find h (id p))) acc
          with Not_found -> Set.add p acc)
        Set.empty
        (status @ packages)
    in
    Set.elements ps)
  else packages

let installed_re = Re.Pcre.regexp "[a-z]+[ \t]+[a-z]+[ \t]+installed"

let is_installed pkg =
  try Re.Pcre.pmatch ~rex:installed_re (pkg#get_extra "Status")
  with Not_found -> false

let is_on_hold pkg =
  try
    match String.split (pkg#get_extra "Status") " " with
    | ("hold", _) -> true
    | _ -> false
  with Not_found -> false

let default_extras =
  [ ("Status", None);
    ("Size", None);
    ("Installed-Size", None);
    ("Multi-Arch", None);
    ("Filename", None) ]

(** input_raw [file] : parse a debian Packages file from [file]
    [~archs] determines which which architectures should be considered while
    parsing the Packages file. if ~arch is [] then all archs are cosidered
*)
let input_raw ?filter ?(archs = []) ?(extras = []) =
  let module M = Format822.RawInput (Set) in
  let extras = default_extras @ extras in
  M.input_raw (parse_packages_in ?filter ~archs ~extras)

(** input_raw_in ch : parse a debian Packages file from channel [ch] *)
let input_raw_in ?filter ?(archs = []) ?(extras = []) =
  let module M = Format822.RawInput (Set) in
  let extras = default_extras @ extras in
  M.input_raw_in (parse_packages_in ?filter ~archs ~extras)
OCaml

Innovation. Community. Security.