Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 239 lines (192 sloc) 6.096 kb
ac99a15 @samoht More WIP ...
samoht authored
1 (***********************************************************************)
2 (* *)
3 (* Copyright 2012 OCamlPro *)
4 (* Copyright 2012 INRIA *)
5 (* *)
6 (* All rights reserved. This file is distributed under the terms of *)
7 (* the GNU Public License version 3.0. *)
8 (* *)
4ca3c23 @samoht Remove TypeRex from headers
samoht authored
9 (* OPAM is distributed in the hope that it will be useful, *)
ac99a15 @samoht More WIP ...
samoht authored
10 (* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
11 (* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)
12 (* GNU General Public License for more details. *)
13 (* *)
14 (***********************************************************************)
15
aa67aa9 @samoht Big refactoring work.
samoht authored
16 module type SET = sig
17 include Set.S
18 val map: (elt -> elt) -> t -> t
19 val choose_one : t -> elt
20 val of_list: elt list -> t
21 val to_string: t -> string
22 val find: (elt -> bool) -> t -> elt
23 end
24 module type MAP = sig
25 include Map.S
26 val to_string: ('a -> string) -> 'a t -> string
27 val values: 'a t -> 'a list
28 val merge_max: (key -> 'a -> 'a -> 'a option) -> 'a t -> 'a t -> 'a t
29 val of_list: (key * 'a) list -> 'a t
30 end
31 module type ABSTRACT = sig
32 type t
33 val of_string: string -> t
34 val to_string: t -> string
35 module Set: SET with type elt = t
36 module Map: MAP with type key = t
37 end
38
39 module type OrderedType = sig
40 include Set.OrderedType
41 val to_string: t -> string
42 end
43
44 module Set = struct
45
46 module Make (O : OrderedType) = struct
47
48 module S = Set.Make(O)
49
50 include S
51
52 let choose_one s =
53 match elements s with
54 | [x] -> x
55 | [] -> raise Not_found
56 | _ -> invalid_arg "choose_one"
57
58 let of_list l =
59 List.fold_left (fun set e -> add e set) empty l
60
61 let to_string s =
62 let l = fold (fun nv l -> O.to_string nv :: l) s [] in
63 Printf.sprintf "{ %s }" (String.concat ", " l)
64
65 let map f t =
66 of_list (List.map f (elements t))
67
68 let find fn s =
69 choose (filter fn s)
70
71 end
72
73 end
74
75 module Map = struct
76
77 module Make (O : OrderedType) = struct
78
79 module M = Map.Make(O)
80
81 include M
82
83 let values map = List.map snd (bindings map)
84
85 let merge_max f =
86 merge
87 (fun k -> function
88 | None -> fun x -> x
89 | Some o1 -> function
90 | None -> Some o1
91 | Some o2 -> f k o1 o2)
92
93 let to_string string_of_value m =
94 let s (k,v) = Printf.sprintf "%s:%s" (O.to_string k) (string_of_value v) in
95 let l = fold (fun k v l -> s (k,v)::l) m [] in
96 Printf.sprintf "{ %s }" (String.concat ", " l)
97
98 let of_list l =
99 List.fold_left (fun map (k,v) -> add k v map) empty l
100
101 end
102
103 end
104
105 module Base = struct
106 type t = string
107 let of_string x = x
108 let to_string x = x
109 module O = struct
110 type t = string
111 let to_string = to_string
112 let compare = compare
113 end
114 module Set = Set.Make(O)
115 module Map = Map.Make(O)
116 end
117
ac99a15 @samoht More WIP ...
samoht authored
118 let filter_map f l =
119 let rec loop accu = function
120 | [] -> List.rev accu
121 | h :: t ->
374d808 fix the order of packages that are proposed by "opam remove"
Frédéric Tuong authored
122 match f h with
123 | None -> loop accu t
124 | Some x -> loop (x::accu) t in
ac99a15 @samoht More WIP ...
samoht authored
125 loop [] l
126
a30a0b9 @samoht Improve the reporting of errors
samoht authored
127 module OInt = struct
128 type t = int
129 let compare = compare
aa67aa9 @samoht Big refactoring work.
samoht authored
130 let to_string = string_of_int
a30a0b9 @samoht Improve the reporting of errors
samoht authored
131 end
132
c58504d @samoht remove spaces (cont)
samoht authored
133 module IntMap = Map.Make(OInt)
a30a0b9 @samoht Improve the reporting of errors
samoht authored
134 module IntSet = Set.Make(OInt)
135
136 module OString = struct
137 type t = string
138 let compare = compare
aa67aa9 @samoht Big refactoring work.
samoht authored
139 let to_string x = x
a30a0b9 @samoht Improve the reporting of errors
samoht authored
140 end
141
155f7f9 @samoht Tell the user to unset some variable which can be problematic during the...
samoht authored
142 module StringSet = Set.Make(OString)
a30a0b9 @samoht Improve the reporting of errors
samoht authored
143 module StringMap = Map.Make(OString)
ba39c27 @samoht More WIP.
samoht authored
144
aa67aa9 @samoht Big refactoring work.
samoht authored
145 module OP = struct
146
147 let (|>) f g x = g (f x)
148
149 end
763e341 @samoht [git] Fix the git repository pluggins.
samoht authored
150
f896017 @samoht Improve the parsing of optional dependencies.
samoht authored
151 let string_of_list f l =
152 Printf.sprintf "{%s}"
153 (String.concat ", " (List.map f l))
154
aa67aa9 @samoht Big refactoring work.
samoht authored
155 let strip str =
763e341 @samoht [git] Fix the git repository pluggins.
samoht authored
156 let p = ref 0 in
157 let l = String.length str in
158 let fn = function
159 | ' ' | '\t' | '\r' | '\n' -> true
c58504d @samoht remove spaces (cont)
samoht authored
160 | _ -> false in
763e341 @samoht [git] Fix the git repository pluggins.
samoht authored
161 while !p < l && fn (String.unsafe_get str !p) do
162 incr p;
163 done;
164 let p = !p in
165 let l = ref (l - 1) in
166 while !l >= p && fn (String.unsafe_get str !l) do
167 decr l;
168 done;
169 String.sub str p (!l - p + 1)
54edd49 @samoht Import from typerex a module to handle correctly synchronous command pro...
samoht authored
170
b097165 @samoht [curl] Add a new repository backend, based on curl/wget, as rsync-ing ov...
samoht authored
171 let starts_with ~prefix s =
172 String.length s >= String.length prefix
173 && String.sub s 0 (String.length prefix) = prefix
174
d90f6a5 @samoht Update output to be more user-friendly.
samoht authored
175 let ends_with ~suffix s =
176 String.length s >= String.length suffix
177 && String.sub s (String.length s - String.length suffix) (String.length suffix) = suffix
178
b097165 @samoht [curl] Add a new repository backend, based on curl/wget, as rsync-ing ov...
samoht authored
179 let remove_prefix ~prefix s =
180 if starts_with prefix s then
181 String.sub s (String.length prefix) (String.length s - String.length prefix)
182 else
aa67aa9 @samoht Big refactoring work.
samoht authored
183 OpamGlobals.error_and_exit "%s is not a prefix of %s" prefix s
516f6ce @samoht Generate $name.env files containing the environments variables in the di...
samoht authored
184
185 let cut_at_aux fn s sep =
186 try
187 let i = fn s sep in
188 let name = String.sub s 0 i in
189 let version = String.sub s (i+1) (String.length s - i - 1) in
190 Some (name, version)
191 with _ ->
192 None
193
194 let cut_at = cut_at_aux String.index
195
196 let rcut_at = cut_at_aux String.rindex
b097165 @samoht [curl] Add a new repository backend, based on curl/wget, as rsync-ing ov...
samoht authored
197
cf68a80 @samoht opam-repo-convert + opam init works
samoht authored
198 let contains s c =
199 try let _ = String.index s c in true
200 with Not_found -> false
201
8a65023 @samoht Fix #32
samoht authored
202 let split s c =
6ced874 @samoht Add a way to compose easily opam variables.
samoht authored
203 Pcre.split (Re_perl.compile (Re.char c)) s
b097165 @samoht [curl] Add a new repository backend, based on curl/wget, as rsync-ing ov...
samoht authored
204
1a8edd1 @samoht Support for local env variables when building a package
samoht authored
205 (* Remove from a ':' separated list of string the one with the given prefix *)
206 let reset_env_value ~prefix v =
8a65023 @samoht Fix #32
samoht authored
207 let v = split v ':' in
1a8edd1 @samoht Support for local env variables when building a package
samoht authored
208 List.filter (fun v -> not (starts_with ~prefix v)) v
3c80380 @samoht Fix issues with path pining
samoht authored
209
210 (* if rsync -arv return 4 lines, this means that no files have changed *)
211 let rsync_trim = function
212 | [] -> []
213 | _ :: t ->
214 match List.rev t with
215 | _ :: _ :: _ :: l -> List.filter ((<>) "./") l
216 | _ -> []
be28113 @samoht [list/search] improve the behavior of list search
samoht authored
217
218 let exact_match re s =
219 try
220 let subs = Re.exec re s in
221 let subs = Array.to_list (Re.get_all_ofs subs) in
222 let n = String.length s in
223 let subs = List.filter (fun (s,e) -> s=0 && e=n) subs in
224 List.length subs > 0
225 with Not_found ->
226 false
aa67aa9 @samoht Big refactoring work.
samoht authored
227
228 let confirm fmt =
229 Printf.ksprintf (fun msg ->
230 OpamGlobals.msg "%s [Y/n] " msg;
231 if not !OpamGlobals.yes then
232 match read_line () with
233 | "y" | "Y"
234 | "" -> true
235 | _ -> false
236 else
237 true
238 ) fmt
Something went wrong with that request. Please try again.