-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_cudf_check.ml
148 lines (134 loc) · 5.08 KB
/
main_cudf_check.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
(*****************************************************************************)
(* libCUDF - CUDF (Common Upgrade Description Format) manipulation library *)
(* Copyright (C) 2009-2012 Stefano Zacchiroli <[email protected]> *)
(* *)
(* 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. *)
(*****************************************************************************)
open ExtLib
open Printf
open Cudf
open Cudf_checker
let cudf_arg = ref ""
let univ_arg = ref ""
let sol_arg = ref ""
let dump_arg = ref false
let cudf = ref None
let univ = ref None
let sol = ref None
let arg_spec = [
"-cudf", Arg.Set_string cudf_arg,
"parse the given CUDF (universe + request)" ;
"-univ", Arg.Set_string univ_arg, "parse the given package universe" ;
"-sol", Arg.Set_string sol_arg, "parse the given problem solution" ;
"-dump", Arg.Set dump_arg, "dump parse results to standard output" ;
]
let usage_msg =
"Usage: cudf-check [OPTION...]
In particular:
cudf-check -cudf FILE validate CUDF
cudf-check -cudf FILE -sol FILE validate CUDF and its solution
cudf-check -univ FILE validate package universe (no request)
Options:"
let die_usage () = Arg.usage arg_spec usage_msg ; exit 2
let print_inst_info inst =
match is_consistent inst with
| true, _ -> printf "original installation status consistent\n%!"; true
| false, Some r ->
printf "original installation status inconsistent (reason: %s)\n%!"
(explain_reason (r :> bad_solution_reason));
false
| _ -> assert false
let print_cudf (pre, univ, req) =
if !dump_arg then begin
let pre' = Option.default Cudf.default_preamble pre in
Cudf_printer.pp_cudf stdout (pre', univ, req)
end
let print_univ univ =
if !dump_arg then
Cudf_printer.pp_universe stdout univ
let print_sol_info inst sol =
match is_solution inst sol with
| true, _ -> printf "is_solution: true\n%!"; true
| false, rs ->
printf "is_solution: false (reason: %s)\n%!"
(String.concat "; " (List.map explain_reason rs));
false
let pp_loc (start_pos, end_pos) =
let line { Lexing.pos_lnum = l } = l in
if line start_pos = line end_pos
then sprintf "line: %d" (line start_pos)
else sprintf "lines: %d-%d" (line start_pos) (line end_pos)
let main () =
let load_univ p =
let pre,univ,req = Cudf_parser.load p in
univ
in
let fail_parse source msg loc =
eprintf "Error while parsing %s: %s\n" source msg ;
eprintf "Location: %s\n%!" (pp_loc loc) ;
exit 1
in
let exit_ rc = if rc then exit 0 else exit 1 in
if !cudf_arg <> "" then begin
try
let p = Cudf_parser.from_in_channel (open_in !cudf_arg) in
eprintf "loading CUDF ...\n%!";
(match Cudf_parser.load p with
| pre, univ, None ->
eprintf "Error: missing request description item.\n%!";
exit (-1)
| pre, univ, Some req -> cudf := Some (pre, univ, req))
with
| Cudf_parser.Parse_error (msg, loc) -> fail_parse "CUDF" msg loc
| Cudf.Constraint_violation _ as exn ->
eprintf "Error while loading CUDF from %s: %s\n%!"
!cudf_arg (Printexc.to_string exn);
exit (-1)
end;
if !univ_arg <> "" then begin
try
let p = Cudf_parser.from_in_channel (open_in !univ_arg) in
eprintf "loading package universe ...\n%!";
univ := Some (load_univ p)
with
| Cudf_parser.Parse_error (msg, loc) -> fail_parse "universe" msg loc
| Cudf.Constraint_violation _ as exn ->
eprintf "Error while loading universe from %s: %s\n%!"
!univ_arg (Printexc.to_string exn);
exit (-1)
end;
if !sol_arg <> "" then
(* postpone solution parsing, we need the input CUDF for that *)
sol := Some (Cudf_parser.from_in_channel (open_in !sol_arg));
match !cudf, !univ, !sol with
| Some (pre,univ,req), None, None ->
let rc = print_inst_info univ in
print_cudf (pre,univ,req);
exit_ rc
| Some (pre,univ,req), None, Some sol_parser ->
(try
eprintf "loading solution ...\n%!";
let _pre', sol = Cudf_parser.load_solution sol_parser univ in
let rc1 = print_inst_info univ in
let rc2 = print_sol_info (univ,req) sol in
print_cudf (pre,univ,req);
exit_ (rc1 && rc2)
with
| Cudf_parser.Parse_error (msg, loc) -> fail_parse "solution" msg loc
| Cudf.Constraint_violation _ as exn ->
eprintf "Error while loading solution from %s: %s\n%!"
!sol_arg (Printexc.to_string exn);
exit (-1))
| None, Some univ, None ->
let rc = print_inst_info univ in
print_univ univ;
exit_ rc
| _ -> die_usage ()
let _ =
Arg.parse arg_spec ignore usage_msg;
main()