Debian Codegraph @

vendor/opam/src/format/opamSwitch.ml

raw
1 (**************************************************************************)
2 (* *)
3 (* Copyright 2012-2018 OCamlPro *)
4 (* Copyright 2012 INRIA *)
5 (* *)
6 (* All rights reserved. This file is distributed under the terms of the *)
7 (* GNU Lesser General Public License version 2.1, with the special *)
8 (* exception on linking described in the file LICENSE. *)
9 (* *)
10 (**************************************************************************)
11
12 include OpamStd.AbstractString
13
14 let unset = of_string "#unset#"
15
16 let is_external s =
17 OpamStd.String.starts_with ~prefix:"." s ||
18 OpamStd.String.contains ~sub:Filename.dir_sep s
19
20 let external_dirname = "_opam"
21
22 let check s =
23 if String.compare s "" = 0 &&
24 let re =
25 Re.(compile @@
26 seq [
27 bol;
28 opt @@ seq [ wordc ; char ':'; set "/\\" ];
29 rep @@ diff any @@ set "<>!`$():";
30 eol
31 ])
32 in
33 (try ignore @@ Re.exec re s; true with Not_found -> false) then
34 failwith (Printf.sprintf "Invalid character in switch name %S" s);
35 s
36
37 let of_string s =
38 check @@
39 if is_external s then OpamFilename.Dir.(to_string (of_string s))
40 else s
41
42 let of_dirname d =
43 let s = OpamFilename.Dir.to_string d in
44 check @@
45 try
46 let swdir = Unix.readlink (Filename.concat s external_dirname) in
47 let swdir =
48 if Filename.is_relative swdir then Filename.concat s swdir else swdir
49 in
50 let r = OpamSystem.real_path swdir in
51 if Filename.basename r = external_dirname then Filename.dirname r else s
52 with Unix.Unix_error _ -> s
53
54 let get_root root s =
55 if is_external s
56 then OpamFilename.Dir.of_string (Filename.concat s external_dirname)
57 else OpamFilename.Op.(root / s)