Commits

Anonymous committed 2a088a2 Draft

+remote_tar_{gz,bz2}

Comments (0)

Files changed (2)

         then mkdir dir 0o777
         else ()
 
+    let has_command argv =
+      127 <> run ~show:false argv
+
+    (* args -- see Filename.temp_file.
+       file will be removed on function return. *)
+    let with_temp_file ?(leave_on_fail=false) ?temp_dir prefix suffix f =
+      let fn = Filename.temp_file ?temp_dir prefix suffix in
+      let finally () = Sys.remove fn in
+      try
+        let r = f fn in
+        (finally (); r)
+      with
+      | e -> ((if leave_on_fail then () else finally ()); raise e)
+
+    let run_ok ~show argv =
+      match run ~show argv with
+      | 0 -> ()
+      | c -> failwith "command %s exited with code %i"
+          (dump_argv argv) c
 
   end
 

src/pre_template.ml

     val local_tar_gz : string -> source
     val local_tar_bz2 : string -> source
     val local_dir : string -> source
+    val remote_tar_gz : string -> source
+    val remote_tar_bz2 : string -> source
   end
  =
   struct
                         (Array.length arr)
 
     let local_tar_archive ~keys ~funcname infn =
-      Src
         (fun ~dest ->
            try
              if Sys.readdir dest <> [| |]
                funcname infn dest
         )
 
-    let local_tar_gz infn = local_tar_archive
+    let do_local_tar_gz ~funcname infn = local_tar_archive
       ~keys:"-xzf"
-      ~funcname:"Source.local_tar_gz"
+      ~funcname
       infn
 
-    let local_tar_bz2 infn = local_tar_archive
+    let local_tar_gz infn =
+      Src (do_local_tar_gz ~funcname:"Source.local_tar_gz" infn)
+
+    let do_local_tar_bz2 ~funcname infn = local_tar_archive
       ~keys:"-xjf"
-      ~funcname:"Source.local_tar_bz2"
+      ~funcname
       infn
 
+    let local_tar_bz2 infn = Src
+      (do_local_tar_bz2 ~funcname:"Source.local_tar_bz2" infn)
+
     let local_dir dir =
       Src
         (fun ~dest ->
                     dir dest
         )
 
+    let downloader = lazy
+      (if Reb_sys.has_command [| "wget" ; "--version" |]
+       then fun ~uri ~outfn ->
+         Reb_sys.run_ok ~show:true [| "wget" ; uri ; "-O" ; outfn |]
+       else if Reb_sys.has_command [| "curl" ; "--version" |]
+       then fun ~uri ~outfn ->
+         Reb_sys.run_ok ~show:true [| "curl" ; uri ; "-o" ; outfn |]
+       else
+         failwith "can't find wget or curl to download file"
+      )
+
+    let download ~uri ~funcname ~outfn =
+      try (Lazy.force downloader) ~uri ~outfn
+      with
+      | e -> failchain e
+          "%s: error downloading %S to %S"
+          funcname uri outfn
+
+    let remote_archive ~unpack ~uri ~funcname =
+      Src
+        (fun ~dest ->
+           Reb_sys.with_temp_file
+             ~leave_on_fail:true
+             "reb_downloaded_archive" ""
+             (fun outfn ->
+                ( download ~uri ~funcname ~outfn
+                ; unpack ~funcname outfn ~dest
+                )
+             )
+        )
+
+    let remote_tar_gz uri =
+      remote_archive
+         ~unpack:do_local_tar_gz
+         ~funcname:"Source.remote_tar_gz"
+         ~uri
+
+    let remote_tar_bz2 uri =
+      remote_archive
+        ~unpack:do_local_tar_bz2
+        ~funcname:"Source.remote_tar_bz2"
+        ~uri
+
   end
 
 (**************************************************)