Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Automatically downgrade packages when they are not available upstream…

… anymore.

This should fix #235
  • Loading branch information...
commit 92b35d713819122bdddf31dedc05b29d70b6280d 1 parent 20c8149
Thomas Gazagnaire authored

Showing 1 changed file with 130 additions and 123 deletions. Show diff stats Hide diff stats

  1. 253  src/opamClient.ml
253  src/opamClient.ml
@@ -56,51 +56,6 @@ let print_state t =
56 56
   log "INSTALLED : %s" (OpamPackage.Set.to_string t.installed);
57 57
   log "REINSTALL : %s" (OpamPackage.Set.to_string t.reinstall)
58 58
 
59  
-let available_packages root opams compiler_version config pinned packages =
60  
-  (* Remove the packages which does not fullfil the compiler
61  
-     constraints *)
62  
-  let ocaml_version =
63  
-    let opam_version = OpamFile.Config.alias config in
64  
-    if opam_version = OpamAlias.default then (
65  
-      let current = OpamCompiler.Version.current () in
66  
-      let system = OpamFile.Config.system_version config in
67  
-      match current, system  with
68  
-      | None  , None   -> OpamGlobals.error_and_exit "No OCaml compiler installed."
69  
-      | None  , Some s ->
70  
-        if not (OpamMisc.confirm "No OCaml compiler found. Continue ?") then
71  
-          OpamGlobals.exit 0
72  
-        else
73  
-          s
74  
-      | Some c, Some s ->
75  
-        if s <> c
76  
-          && not (OpamMisc.confirm "The version of OCaml in your path (%S) \
77  
-                             is not the same as the one OPAM has been \
78  
-                             initialized with (%S). Continue ?"
79  
-                    (OpamCompiler.Version.to_string c)
80  
-                    (OpamCompiler.Version.to_string s)) then
81  
-          OpamGlobals.exit 1
82  
-        else
83  
-          s
84  
-      | Some c, None   -> c
85  
-    ) else
86  
-      match compiler_version with
87  
-      | Some v -> v
88  
-      | None   -> OpamGlobals.error_and_exit "No OCaml compiler defined."in
89  
-  let filter nv =
90  
-    let opam = OpamPackage.Map.find nv opams in
91  
-    let consistent_ocaml_version =
92  
-      let atom (r,v) = OpamCompiler.Version.compare ocaml_version r v in
93  
-      match OpamFile.OPAM.ocaml_version opam with
94  
-      | None   -> true
95  
-      | Some c -> OpamFormula.eval atom c in
96  
-    let consistent_pinned_version =
97  
-      not (OpamPackage.Name.Map.mem (OpamPackage.name nv) pinned) ||
98  
-        match OpamPackage.Name.Map.find (OpamPackage.name nv) pinned with
99  
-        | Version v -> v = OpamPackage.version nv
100  
-        | _         -> true (* any version is fine, as this will be overloaded on install *) in
101  
-      consistent_ocaml_version && consistent_pinned_version in
102  
-    OpamPackage.Set.filter filter packages
103  
-
104 59
 let packages_in_dir dir =
105 60
   log "packages in dir %s" (OpamFilename.Dir.to_string dir);
106 61
   if OpamFilename.exists_dir dir then (
@@ -136,46 +91,6 @@ let compiler_of_alias t alias =
136 91
   try Some (List.assoc alias t.aliases)
137 92
   with Not_found -> None
138 93
 
139  
-let load_state () =
140  
-  let root = OpamPath.default () in
141  
-  log "root path is %s" (OpamFilename.Dir.to_string root);
142  
-
143  
-  let config = OpamFile.Config.read (OpamPath.config root) in
144  
-  let alias = OpamFile.Config.alias config in
145  
-  let aliases = OpamFile.Aliases.safe_read (OpamPath.aliases root) in
146  
-  let compiler =
147  
-    try Some (List.assoc alias aliases)
148  
-    with Not_found -> None in
149  
-  let compiler_version = match compiler with
150  
-    | None   -> None
151  
-    | Some c ->
152  
-      let comp = OpamFile.Comp.read (OpamPath.compiler root c) in
153  
-      Some (OpamFile.Comp.version comp) in
154  
-  let opams =
155  
-    OpamPackage.Set.fold (fun nv map ->
156  
-      let opam = OpamFile.OPAM.read (OpamPath.opam root nv) in
157  
-      OpamPackage.Map.add nv opam map
158  
-    ) (packages_in_dir (OpamPath.opam_dir root)) OpamPackage.Map.empty in
159  
-  let repositories =
160  
-    List.fold_left (fun map repo ->
161  
-      let repo_p = OpamPath.Repository.create root repo in
162  
-      let config = OpamFile.Repo_config.read (OpamPath.Repository.config repo_p) in
163  
-      OpamRepositoryName.Map.add repo config map
164  
-    ) OpamRepositoryName.Map.empty (OpamFile.Config.repositories config) in
165  
-  let repo_index = OpamFile.Repo_index.safe_read (OpamPath.repo_index root) in
166  
-  let pinned = OpamFile.Pinned.safe_read (OpamPath.Alias.pinned root alias) in
167  
-  let installed = OpamFile.Installed.safe_read (OpamPath.Alias.installed root alias) in
168  
-  let reinstall = OpamFile.Reinstall.safe_read (OpamPath.Alias.reinstall root alias) in
169  
-  let packages = packages_in_dir (OpamPath.opam_dir root) in
170  
-  let available_packages = lazy (available_packages root opams compiler_version config pinned packages) in
171  
-  let t = {
172  
-    root; alias; compiler; compiler_version; repositories; opams;
173  
-    packages; available_packages; installed; reinstall;
174  
-    repo_index; config; aliases; pinned;
175  
-  } in
176  
-  print_state t;
177  
-  t
178  
-
179 94
 let config t =
180 95
   OpamFile.Config.read (OpamPath.config t.root)
181 96
 
@@ -266,6 +181,133 @@ let dot_config t nv =
266 181
 let reinstall t =
267 182
   OpamFile.Reinstall.safe_read (OpamPath.Alias.reinstall t.root t.alias)
268 183
 
  184
+let mem_repository t name =
  185
+  OpamRepositoryName.Map.exists (fun n _ -> n = name) t.repositories
  186
+
  187
+let find_repo_by_name t name =
  188
+  OpamRepositoryName.Map.find name t.repositories
  189
+
  190
+let find_repo_aux repositories root repo_index nv =
  191
+  log "find_repo %s" (OpamPackage.to_string nv);
  192
+  let name = OpamPackage.name nv in
  193
+  let rec aux = function
  194
+    | []          -> None
  195
+    | r :: repo_s ->
  196
+        let repo = OpamRepositoryName.Map.find r repositories in
  197
+        let repo_p = OpamPath.Repository.create root r in
  198
+        let opam_f = OpamPath.Repository.opam repo_p nv in
  199
+        if OpamFilename.exists opam_f then (
  200
+          Some (repo_p, repo)
  201
+        ) else
  202
+          aux repo_s in
  203
+  if OpamPackage.Name.Map.mem name repo_index then
  204
+    aux (OpamPackage.Name.Map.find name repo_index)
  205
+  else
  206
+    None
  207
+
  208
+let find_repo t nv =
  209
+  find_repo_aux t.repositories t.root t.repo_index nv
  210
+
  211
+let mem_repo t nv =
  212
+  find_repo t nv <> None
  213
+
  214
+let with_repo t nv fn =
  215
+  match find_repo t nv with
  216
+  | None ->
  217
+    OpamGlobals.error_and_exit
  218
+      "Unable to find a repository containing %s"
  219
+      (OpamPackage.to_string nv)
  220
+  | Some (repo_p, repo) -> fn repo_p repo
  221
+
  222
+let available_packages root opams repositories repo_index compiler_version config pinned packages =
  223
+  (* Remove the packages which does not fullfil the compiler
  224
+     constraints *)
  225
+  let ocaml_version =
  226
+    let opam_version = OpamFile.Config.alias config in
  227
+    if opam_version = OpamAlias.default then (
  228
+      let current = OpamCompiler.Version.current () in
  229
+      let system = OpamFile.Config.system_version config in
  230
+      match current, system  with
  231
+      | None  , None   -> OpamGlobals.error_and_exit "No OCaml compiler installed."
  232
+      | None  , Some s ->
  233
+        if not (OpamMisc.confirm "No OCaml compiler found. Continue ?") then
  234
+          OpamGlobals.exit 0
  235
+        else
  236
+          s
  237
+      | Some c, Some s ->
  238
+        if s <> c
  239
+          && not (OpamMisc.confirm "The version of OCaml in your path (%S) \
  240
+                             is not the same as the one OPAM has been \
  241
+                             initialized with (%S). Continue ?"
  242
+                    (OpamCompiler.Version.to_string c)
  243
+                    (OpamCompiler.Version.to_string s)) then
  244
+          OpamGlobals.exit 1
  245
+        else
  246
+          s
  247
+      | Some c, None   -> c
  248
+    ) else
  249
+      match compiler_version with
  250
+      | Some v -> v
  251
+      | None   -> OpamGlobals.error_and_exit "No OCaml compiler defined."in
  252
+  let filter nv =
  253
+    let opam = OpamPackage.Map.find nv opams in
  254
+    let available () = find_repo_aux repositories root repo_index nv <> None in
  255
+    let consistent_ocaml_version () =
  256
+      let atom (r,v) = OpamCompiler.Version.compare ocaml_version r v in
  257
+      match OpamFile.OPAM.ocaml_version opam with
  258
+      | None   -> true
  259
+      | Some c -> OpamFormula.eval atom c in
  260
+    let consistent_pinned_version () =
  261
+      not (OpamPackage.Name.Map.mem (OpamPackage.name nv) pinned) ||
  262
+        match OpamPackage.Name.Map.find (OpamPackage.name nv) pinned with
  263
+        | Version v -> v = OpamPackage.version nv
  264
+        | _         -> true (* any version is fine, as this will be overloaded on install *) in
  265
+    available ()
  266
+    && consistent_ocaml_version ()
  267
+    && consistent_pinned_version () in
  268
+  OpamPackage.Set.filter filter packages
  269
+
  270
+let load_state () =
  271
+  let root = OpamPath.default () in
  272
+  log "root path is %s" (OpamFilename.Dir.to_string root);
  273
+
  274
+  let config = OpamFile.Config.read (OpamPath.config root) in
  275
+  let alias = OpamFile.Config.alias config in
  276
+  let aliases = OpamFile.Aliases.safe_read (OpamPath.aliases root) in
  277
+  let compiler =
  278
+    try Some (List.assoc alias aliases)
  279
+    with Not_found -> None in
  280
+  let compiler_version = match compiler with
  281
+    | None   -> None
  282
+    | Some c ->
  283
+      let comp = OpamFile.Comp.read (OpamPath.compiler root c) in
  284
+      Some (OpamFile.Comp.version comp) in
  285
+  let opams =
  286
+    OpamPackage.Set.fold (fun nv map ->
  287
+      let opam = OpamFile.OPAM.read (OpamPath.opam root nv) in
  288
+      OpamPackage.Map.add nv opam map
  289
+    ) (packages_in_dir (OpamPath.opam_dir root)) OpamPackage.Map.empty in
  290
+  let repositories =
  291
+    List.fold_left (fun map repo ->
  292
+      let repo_p = OpamPath.Repository.create root repo in
  293
+      let config = OpamFile.Repo_config.read (OpamPath.Repository.config repo_p) in
  294
+      OpamRepositoryName.Map.add repo config map
  295
+    ) OpamRepositoryName.Map.empty (OpamFile.Config.repositories config) in
  296
+  let repo_index = OpamFile.Repo_index.safe_read (OpamPath.repo_index root) in
  297
+  let pinned = OpamFile.Pinned.safe_read (OpamPath.Alias.pinned root alias) in
  298
+  let installed = OpamFile.Installed.safe_read (OpamPath.Alias.installed root alias) in
  299
+  let reinstall = OpamFile.Reinstall.safe_read (OpamPath.Alias.reinstall root alias) in
  300
+  let packages = packages_in_dir (OpamPath.opam_dir root) in
  301
+  let available_packages =
  302
+    lazy (available_packages root opams repositories repo_index compiler_version config pinned packages) in
  303
+  let t = {
  304
+    root; alias; compiler; compiler_version; repositories; opams;
  305
+    packages; available_packages; installed; reinstall;
  306
+    repo_index; config; aliases; pinned;
  307
+  } in
  308
+  print_state t;
  309
+  t
  310
+
269 311
 type main_function =
270 312
   | Read_only of (unit -> unit)
271 313
   | Write_lock of (unit -> unit)
@@ -293,12 +335,6 @@ let check f =
293 335
       "Cannot find %s. Have you run 'opam init first ?"
294 336
       (OpamFilename.Dir.to_string root)
295 337
 
296  
-let mem_repository t name =
297  
-  OpamRepositoryName.Map.exists (fun n _ -> n = name) t.repositories
298  
-
299  
-let find_repository t name =
300  
-  OpamRepositoryName.Map.find name t.repositories
301  
-
302 338
 let print_updated t updated pinned_updated =
303 339
   let new_packages =
304 340
     OpamPackage.Set.filter (fun nv -> not (OpamPackage.Set.mem nv t.installed)) updated in
@@ -487,7 +523,7 @@ let update_repo_index t =
487 523
   OpamPackage.Name.Map.iter (fun n repo_s ->
488 524
     let all_versions = ref OpamPackage.Version.Set.empty in
489 525
     List.iter (fun r ->
490  
-      let repo = find_repository t r in
  526
+      let repo = find_repo_by_name t r in
491 527
       let repo_p = OpamPath.Repository.create t.root repo.repo_name in
492 528
       let available_versions = OpamRepository.versions repo_p n in
493 529
       OpamPackage.Version.Set.iter (fun v ->
@@ -582,35 +618,6 @@ let update_repositories t ~show_compilers repositories =
582 618
   if not (OpamFilename.exists default_compiler) then
583 619
     create_default_compiler_description t
584 620
 
585  
-let find_repo t nv =
586  
-  log "find_repo %s" (OpamPackage.to_string nv);
587  
-  let name = OpamPackage.name nv in
588  
-  let rec aux = function
589  
-    | []          -> None
590  
-    | r :: repo_s ->
591  
-        let repo = find_repository t r in
592  
-        let repo_p = OpamPath.Repository.create t.root r in
593  
-        let opam_f = OpamPath.Repository.opam repo_p nv in
594  
-        if OpamFilename.exists opam_f then (
595  
-          Some (repo_p, repo)
596  
-        ) else
597  
-          aux repo_s in
598  
-  if OpamPackage.Name.Map.mem name t.repo_index then
599  
-    aux (OpamPackage.Name.Map.find name t.repo_index)
600  
-  else
601  
-    None
602  
-
603  
-let mem_repo t nv =
604  
-  find_repo t nv <> None
605  
-
606  
-let with_repo t nv fn =
607  
-  match find_repo t nv with
608  
-  | None ->
609  
-    OpamGlobals.error_and_exit
610  
-      "Unable to find a repository containing %s"
611  
-      (OpamPackage.to_string nv)
612  
-  | Some (repo_p, repo) -> fn repo_p repo
613  
-
614 621
 let update_pinned_package t nv pin =
615 622
   let kind = kind_of_pin_option pin in
616 623
   let path = OpamFilename.raw_dir (path_of_pin_option pin) in
@@ -2342,12 +2349,12 @@ let upload upload repo =
2342 2349
   | None ->
2343 2350
       if OpamPackage.Name.Map.mem name t.repo_index then
2344 2351
         (* We upload the package to the first available repository. *)
2345  
-        find_repository t (List.hd (OpamPackage.Name.Map.find name t.repo_index))
  2352
+        find_repo_by_name t (List.hd (OpamPackage.Name.Map.find name t.repo_index))
2346 2353
       else
2347 2354
         OpamGlobals.error_and_exit "No repository found to upload %s" (OpamPackage.to_string nv)
2348 2355
   | Some repo ->
2349 2356
       if mem_repository t repo then
2350  
-        find_repository t repo
  2357
+        find_repo_by_name t repo
2351 2358
       else
2352 2359
         OpamGlobals.error_and_exit "Unbound repository %S (available = %s)"
2353 2360
           (OpamRepositoryName.to_string repo)

0 notes on commit 92b35d7

Please sign in to comment.
Something went wrong with that request. Please try again.