Skip to content
Newer
Older
100644 168 lines (131 sloc) 4.65 KB
aa67aa9 @samoht Big refactoring work.
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 (* *)
9 (* OPAM is distributed in the hope that it will be useful, *)
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
16 module Version = struct
17
18 type t = string
19
20 let to_string x = x
21
22 let of_string x = x
23
24 let compare = Debian.Version.compare
25
26 module O = struct
27 type t = string
28 let to_string = to_string
29 let compare = compare
30 end
31
32 module Set = OpamMisc.Set.Make(O)
33
34 module Map = OpamMisc.Map.Make(O)
35
36 end
37
38 module Name = struct
39
40 type t = string
41
42 let to_string x = x
43
44 let of_string x = x
45
46 let compare n1 n2 =
47 match compare (String.lowercase n1) (String.lowercase n2) with
48 | 0 -> compare n1 n2
49 | i -> i
50
51 module O = struct
52 type t = string
53 let to_string = to_string
54 let compare = compare
55 end
56
57 module Set = OpamMisc.Set.Make(O)
58
59 module Map = OpamMisc.Map.Make(O)
60
61 end
62
63 type t = {
64 name : Name.t;
65 version: Version.t;
66 }
67
68 let create name version = { name; version }
69
70 let name t = t.name
71
72 let version t = t.version
73
74 let sep = '.'
75
76 let of_string_opt s =
77 if OpamMisc.contains s ' ' || OpamMisc.contains s '\n' then
78 None
79 else match OpamMisc.cut_at s sep with
80 | None -> None
81 | Some (n, v) -> Some { name = Name.of_string n; version = Version.of_string v }
82
83 let of_string s = match of_string_opt s with
84 | Some x -> x
85 | None -> OpamGlobals.error_and_exit "%s is not a valid versioned package name" s
86
87 (* XXX: this function is quite hackish, as it mainly depends on the shape the paths
88 built in path.ml *)
54eff3f @tuong fix the heuristic to retrieve NAME and VERSION
tuong authored
89 let of_filename f =
aa67aa9 @samoht Big refactoring work.
samoht authored
90 let f = OpamMisc.strip (OpamFilename.to_string f) in
54eff3f @tuong fix the heuristic to retrieve NAME and VERSION
tuong authored
91 let base = Filename.basename f in
92 let parent = Filename.basename (Filename.dirname f) in
93 match base with
64192f6 @samoht Ignore changes in descr files.
samoht authored
94 | "opam" | "url" -> of_string_opt parent
54eff3f @tuong fix the heuristic to retrieve NAME and VERSION
tuong authored
95 | _ ->
96 if Filename.check_suffix base ".opam" then
97 of_string_opt (Filename.chop_suffix base ".opam")
98 else if Filename.check_suffix base "+opam.tar.gz" then
99 of_string_opt (Filename.chop_suffix base "+opam.tar.gz")
100 else
101 match parent with
102 | "files" ->
103 let parent2 = Filename.basename (Filename.dirname (Filename.dirname f)) in
104 of_string_opt parent2
105 | _ -> None
aa67aa9 @samoht Big refactoring work.
samoht authored
106
107 let of_dirname d =
108 of_string_opt (OpamFilename.Base.to_string (OpamFilename.basename_dir d))
109
110 let of_dpkg d =
111 { name = Name.of_string d.Debian.Packages.name;
112 version = Version.of_string d.Debian.Packages.version }
113
114 let of_cudf table pkg =
115 let real_version =
116 Debian.Debcudf.get_real_version
117 table
118 (pkg.Cudf.package, pkg.Cudf.version) in
119 { name = Name.of_string (Common.CudfAdd.decode pkg.Cudf.package);
120 version = Version.of_string real_version; }
121
122 let to_string t =
123 Printf.sprintf "%s%c%s" (Name.to_string t.name) sep (Version.to_string t.version)
124
125 module O = struct
126 type tmp = t
127 type t = tmp
128 let compare nv1 nv2 =
129 match Name.compare nv1.name nv2.name with
130 | 0 -> Version.compare nv1.version nv2.version
131 | i -> i
132 let to_string = to_string
133 end
134
135 module Set = OpamMisc.Set.Make (O)
136
137 module Map = OpamMisc.Map.Make (O)
138
139 let to_map nv =
140 Set.fold (fun nv map ->
141 let name = name nv in
142 let version = version nv in
143 let versions =
144 if Name.Map.mem name map then
145 Name.Map.find name map
146 else
147 Version.Set.empty in
148 Name.Map.add name (Version.Set.add version versions) (Name.Map.remove name map)
149 ) nv Name.Map.empty
150
151 let versions_of_packages nvset =
152 Set.fold
153 (fun nv vset -> Version.Set.add (version nv) vset)
154 nvset
155 Version.Set.empty
156
157 let opam_files dir =
158 if OpamFilename.exists_dir dir then (
159 let files = OpamFilename.list_files dir in
160 let files = List.filter (fun f -> OpamFilename.check_suffix f ".opam") files in
161 List.fold_left (fun set file ->
162 match of_filename file with
163 | None -> set
164 | Some nv -> Set.add nv set
165 ) Set.empty files
166 ) else
167 Set.empty
Something went wrong with that request. Please try again.