Source file stdUtils.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
open ExtLib
open Dose_common
include Util.Logging (struct
let label = "doseparse.stdutils"
end)
let get_architectures native_edsp foreign_edsp native_opt foreign_opt =
let cmd = "apt-config dump" in
let arch = ref None in
let archs = ref [] in
let aux () =
let out = Std.input_list (Unix.open_process_in cmd) in
List.iter
(fun s ->
let (key, value) = ExtString.String.split s " " in
if key = "APT::Architecture" then
arch := Some (ExtString.String.slice ~first:1 ~last:(-2) value)
else if key = "APT::Architectures::" || key = "APT::Architectures" then
let s = ExtString.String.slice ~first:1 ~last:(-2) value in
if s <> "" then
archs := ExtString.String.slice ~first:1 ~last:(-2) value :: !archs)
out ;
debug
"Automatically set native as %s and foreign archs as %s"
(Option.get !arch)
(String.concat "," !archs)
in
let (na, fa) =
match ((native_edsp, foreign_edsp), (native_opt, foreign_opt)) with
| ((None, []), (None, None)) ->
aux () ;
(!arch, List.filter (( <> ) (Option.get !arch)) !archs)
| ((_, l), (Some a, None)) -> (Some a, l)
| ((Some a, _), (None, Some l)) ->
(Some a, l)
| ((_, _), (Some a, Some l)) ->
(Some a, l)
| ((Some a, l), (None, None)) -> (Some a, l)
| ((None, []), (None, _)) ->
fatal "Native arch is missing while Foregin archs are specified"
| (_, _) -> fatal "Unable to compute native and foreign arch information"
in
(match (na, fa) with
| (Some a, l)
when (native_edsp, foreign_edsp) = (None, [])
|| (native_edsp, foreign_edsp) = (Some a, l) ->
notice
"Setting Native Architecture to %s and Foreign Architectures to %s"
a
(String.concat "," l)
| (Some a, l) ->
info
"Overriding EDSP. Setting Native Architecture to %s and Foreign \
Architectures to %s"
a
(String.concat "," l)
| _ -> fatal "Error Setting Native Architecture") ;
(na, fa)
let pp_versions_table fmt (from_cudf, pkglist) =
List.iter
(fun pkg ->
let (p, v) = from_cudf (pkg.Cudf.package, pkg.Cudf.version) in
Format.fprintf fmt "%s=%d=%s@." p pkg.Cudf.version v)
pkglist
let if_application ?(alternatives = []) filename main =
let normalize f =
let bf = Filename.basename f in
try
if String.ends_with bf ".d.byte" then
String.slice ~last:(String.find bf ".d.byte") bf
else if String.ends_with bf ".p.native" then
String.slice ~last:(String.find bf ".p.native") bf
else Filename.chop_extension bf
with Invalid_argument _ -> bf
in
let names = List.map normalize (filename :: alternatives) in
let invoked_as = normalize Sys.argv.(0) in
if List.exists (( = ) invoked_as) names then (
try if main () = 0 then exit 0 else exit 1 with
| Unix.Unix_error (err, _, arg) ->
Printf.eprintf "%s %s" (Unix.error_message err) arg ;
Stdlib.exit 64
| exn ->
Printexc.print_backtrace stderr ;
Printf.eprintf "The applications raised this exception : " ;
Printf.eprintf "%s\n" (Printexc.to_string exn) ;
Stdlib.exit 64)
else (
Printf.eprintf
"you are using %s as a module and not as an executable\n"
Sys.argv.(0) ;
Printf.eprintf
"%s can be run as an exactable if named : %s\n"
Sys.argv.(0)
(ExtString.String.join " , " names))