Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix has_command, CentOS detection, source installation and add OS update support #7

Merged
merged 7 commits into from
Mar 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
0.4 (2015-03-22):
* Add `-u` or `--update` flag to trigger an OS package set update
before performing external dependency installation. This is useful
on Debian or Ubuntu where package installation may fail if the
sets are not up-to-date with respect to latest security updates.
* Fix installation of source packages.
* Detect the CentOS distribution correctly.
* Fix external command detection to not go through a subshell.

0.3 (2015-01-27):
* Add OCaml 3.12.1 compatibility.
* Add `-i` flag to perform OPAM package installation after the
external OS dependencies are installed.
* Fixes for MacPorts and CentOS support.

0.2 (2015-01-21):
* Initial public release.
50 changes: 37 additions & 13 deletions depext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ let string_split char str =
aux 0

let has_command c =
let cmd = Printf.sprintf "/bin/sh -c command -v %s" c in
let cmd = Printf.sprintf "command -v %s" c in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this will work well. Based on experience with cmdliner that's a correct and multiplatform way of checking whether a command exists or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

command is POSIX, but I can believe that it's not portable despite that. Would it be possible for cmdliner to expose the find_cmd logic so that we dont have to cut and paste it here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible for cmdliner to expose the find_cmd logic so that we dont have to cut and paste it here?

I don't think cmdliner is the place to expose such things. I'd say either just c&p or use Bos which exposes this logic in its API (but that's another dependency).

try Sys.command cmd = 0 with Sys_error _ -> false

let run_command c =
let c = String.concat " " (List.map (Printf.sprintf "%S") c) in
let c = String.concat " " (List.map (Printf.sprintf "%s") c) in
if !debug then Printf.eprintf "+ %s\n%!" c;
Unix.system c

Expand Down Expand Up @@ -90,12 +90,12 @@ let distribution = function
in
List.hd (string_split ' ' (List.hd (lines_of_file release_file)))
in
match name with
| "Debian" -> Some `Debian
| "Ubuntu" -> Some `Ubuntu
| "Centos" -> Some `Centos
| "Fedora" -> Some `Fedora
| "Mageia" -> Some `Mageia
match String.lowercase name with
| "debian" -> Some `Debian
| "ubuntu" -> Some `Ubuntu
| "centos" -> Some `Centos
| "fedora" -> Some `Fedora
| "mageia" -> Some `Mageia
| s -> Some (`Other s)
with Not_found | Failure _ -> None)
| _ -> None
Expand Down Expand Up @@ -168,7 +168,9 @@ let install_packages_command distribution packages =
"pkg"::"install"::packages
| Some (`OpenBSD | `NetBSD) ->
"pkg_add"::packages
| _ ->
| Some (`Other d) ->
failwith ("Sorry, don't know how to install packages on your " ^ d ^ " system")
| None ->
failwith "Sorry, don't know how to install packages on your system"

let sudo os distribution cmd = match os, distribution with
Expand All @@ -182,6 +184,23 @@ let sudo os distribution cmd = match os, distribution with
) else cmd
| _ -> cmd

let update_command = function
| Some (`Debian | `Ubuntu) ->
["apt-get";"update"]
| Some (`Homebrew) ->
["brew"; "update"]
| Some (`Centos | `Fedora | `Mageia) ->
["yum"; "update"]
| _ -> ["echo"; "Skipping system update on this platform."]

let update os distribution =
let cmd = update_command distribution in
let cmd = sudo os distribution cmd in
match run_command cmd with
| Unix.WEXITED 0 ->
Printf.printf "# OS package update successful\n%!"
| _ -> failwith "OS package update failed"

let install os distribution = function
| [] -> ()
| os_packages ->
Expand Down Expand Up @@ -214,7 +233,7 @@ let run_source_scripts = function

(* Command-line handling *)

let main print_flags list short no_sources debug_arg install_arg opam_packages =
let main print_flags list short no_sources debug_arg install_arg update_arg opam_packages =
if debug_arg then debug := true;
let arch = arch () in
let os = os () in
Expand Down Expand Up @@ -244,7 +263,8 @@ let main print_flags list short no_sources debug_arg install_arg opam_packages =
Printf.printf "# The following scripts need to be run:\n# - %s\n%!"
(String.concat "\n# - " source_urls);
if os_packages = [] && source_urls = [] then
Printf.printf "# No extra OS packages requirements found.\n%!";
Printf.printf "# No extra OS packages requirements found.\n%!"
else (if update_arg then update os distribution);
install os distribution os_packages;
run_source_scripts source_urls;
if install_arg && opam_packages <> [] then
Expand Down Expand Up @@ -279,6 +299,10 @@ let debug_arg =
Arg.(value & flag &
info ~doc:"Print commands that are run by the program" ["d";"debug"])

let update_arg =
Arg.(value & flag &
info ~doc:"Update the OS package sets before installation" ["u";"update"])

let install_arg =
Arg.(value & flag &
info ~doc:"Install the packages through \"opam install\" after \
Expand All @@ -302,9 +326,9 @@ let command =
] in
let doc = "Query and install external dependencies of OPAM packages" in
Term.(pure main $ print_flags_arg $ list_arg $ short_arg $
no_sources_arg $ debug_arg $ install_arg $
no_sources_arg $ debug_arg $ install_arg $ update_arg $
packages_arg),
Term.info "opam-depext" ~version:"0.3" ~doc ~man
Term.info "opam-depext" ~version:"0.4" ~doc ~man

let () =
match Term.eval command with
Expand Down
5 changes: 3 additions & 2 deletions opam
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
opam-version: "1.2"
name: "depext"
version: "0.1"
version: "0.4"
maintainer: "Louis Gesbert <louis.gesbert@ocamlpro.com>"
authors: "Louis Gesbert <louis.gesbert@ocamlpro.com>"
authors: ["Louis Gesbert <louis.gesbert@ocamlpro.com>"
"Anil Madhavapeddy <anil@recoil.org>"]
homepage: "https://github.com/AltGr/opam-depext"
bug-reports: "https://github.com/AltGr/opam-depext/issues"
license: "LGPL-3.0 with OCaml linking exception"
Expand Down