File: utils.ml

package info (click to toggle)
ben 1.15
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 676 kB
  • sloc: ml: 4,125; sh: 345; javascript: 78; ansic: 39; makefile: 29; python: 18
file content (88 lines) | stat: -rw-r--r-- 3,298 bytes parent folder | download
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
(**************************************************************************)
(*  Copyright © 2009 Stéphane Glondu <steph@glondu.net>                   *)
(*                                                                        *)
(*  This program is free software: you can redistribute it and/or modify  *)
(*  it under the terms of the GNU Affero General Public License as        *)
(*  published by the Free Software Foundation, either version 3 of the    *)
(*  License, or (at your option) any later version, with the additional   *)
(*  exemption that compiling, linking, and/or using OpenSSL is allowed.   *)
(*                                                                        *)
(*  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     *)
(*  Affero General Public License for more details.                       *)
(*                                                                        *)
(*  You should have received a copy of the GNU Affero General Public      *)
(*  License along with this program.  If not, see                         *)
(*  <http://www.gnu.org/licenses/>.                                       *)
(**************************************************************************)

open Ben
open Core
open Error
open Lexing

let p = Clflags.progress

let parse_control_in_channel kind filename ic keep f accu =
  let filter k = keep (String.lowercase_ascii k) in
  p "Parsing %s...\n" filename;
  let result =
    Lexer.stanza_fold
      (fun p accu ->
        let p = Stanza.filter filter p in
        f
          (Package.Name.of_string (Stanza.find "Package" p))
          (Package.of_stanza kind p) accu)
      (from_channel ic) accu
  in
  result

let parse_control_file kind filename keep f accu =
  with_in_file filename (fun ic ->
      parse_control_in_channel kind filename ic keep f accu)

let parse_config_from_in_channel ?(filename = "stdin") ic =
  let lexbuf = from_channel ic in
  let pos = lexbuf.lex_curr_p in
  lexbuf.lex_curr_p <- { pos with pos_fname = filename };
  try Parser.config_file Lexer.token lexbuf
  with Parser.Error ->
    let pos = Lexing.lexeme_start_p lexbuf in
    raise
      (Parsing_error
         (pos.pos_fname, true, pos.pos_lnum, pos.pos_cnum - pos.pos_bol))

let parse_config_file filename =
  with_in_file filename (parse_config_from_in_channel ~filename)

let file_content file =
  let lines = ref "" in
  let inchan = open_in file in
  try
    while true do
      lines := Printf.sprintf "%s%s\n" !lines (input_line inchan)
    done;
    ""
  with End_of_file ->
    close_in inchan;
    !lines

let dump_to_file name string =
  let outchan = open_out name in
  let () = output_string outchan string in
  close_out outchan

let dump_xhtml_to_file filename xhtml =
  let outchan = open_out filename in
  let xhtml = Format.asprintf "%a" (Tyxml.Html.pp ()) xhtml in
  let () = output_string outchan xhtml in
  close_out outchan

let parse_release string =
  let stanzas =
    Lexer.stanza_fold (fun p accu -> p :: accu) (Lexing.from_string string) []
  in
  match stanzas with
  | [ s ] -> s
  | _ -> failwith "not exactly 1 stanza in Release"