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

[WIP] Move most of the external tools calls to a single module #3217

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/client/opamAction.ml
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ let make_command st opam ?dir ?text_command (cmd, args) =
~resolve_path:OpamStateConfig.(not !r.dryrun)
~metadata:["context", context]
~verbose:(OpamConsole.verbose ())
cmd args
(OpamExternalTools.custom (cmd, args))

let remove_commands t nv =
match installed_opam_opt t nv with
Expand Down
2 changes: 1 addition & 1 deletion src/client/opamInitDefaults.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let default_compiler =
]

let eval_variables = [
OpamVariable.of_string "sys-ocaml-version", ["ocamlc"; "-vnum"],
OpamVariable.of_string "sys-ocaml-version", OpamExternalTools.OCaml.vnum,
"OCaml version present on your system independently of opam, if any";
]

Expand Down
2 changes: 1 addition & 1 deletion src/client/opamInitDefaults.mli
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ val repository_url: url

val default_compiler: formula

val eval_variables: (OpamVariable.t * string list * string) list
val eval_variables: (OpamVariable.t * OpamExternalTools.t * string) list

(** Default initial configuration file for use by [opam init] if nothing is
supplied. *)
Expand Down
2 changes: 1 addition & 1 deletion src/client/opamSolution.ml
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ let run_hook_job t name ?(local=[]) w =
OpamSystem.make_command
~verbose:(OpamConsole.verbose ())
~env:(OpamTypesBase.env_array shell_env)
~name ~text cmd args)
~name ~text (OpamExternalTools.custom (cmd, args)))
| [] -> None
in
let env v =
Expand Down
125 changes: 125 additions & 0 deletions src/core/opamExternalTools.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
(**************************************************************************)
(* *)
(* Copyright 2018 OCamlPro *)
Copy link
Member

Choose a reason for hiding this comment

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

this isn't copyright ocamlpro :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I was just copying the header from the other files. Should I put my name instead ?

Copy link
Member

Choose a reason for hiding this comment

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

Actually, in this instance I think it depends - is the module predominantly just refactoring?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, I think so.

(* *)
(* All rights reserved. This file is distributed under the terms of the *)
(* GNU Lesser General Public License version 2.1, with the special *)
(* exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)

type t = (string * string list)

let unpack x = x
let custom x = x

let has_space (cmd, _) = String.contains cmd ' '
let cmd_to_string (cmd, _) = cmd

let to_string (cmd, args) =
(* TODO: Add backslashs to quotes for each args *)
let quote x = if String.contains x ' ' then "'" ^ x ^ "'" else x in
Copy link
Member

Choose a reason for hiding this comment

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

This quoting logic seems like it might be replicated elsewhere in opam. Bos also has this

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I'll look around. I tried to be as close as the original code as possible (modulo quoting that is added because that seemed broken anyway)

Copy link
Member

Choose a reason for hiding this comment

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

Note that backslash-quoting in single quotes doesn't work for the shell. You have to write e.g. 'foo'\''bar ; and tabs should also be supported.

let args = List.map quote args in
String.concat " " (cmd::args)

module Unzip = struct
let extract ~file ~dir = ("unzip", [file; "-d"; dir])
end

module Tar = struct
let extract_gzip ~file ~dir = ("tar", ["xfz"; file; "-C"; dir])
let extract_bzip2 ~file ~dir = ("tar", ["xfj"; file; "-C"; dir])
let extract_xz ~file ~dir = ("tar", ["xfJ"; file; "-C"; dir])
let extract_lzma ~file ~dir = ("tar", ["xfY"; file; "-C"; dir])

let create_gzip ~output ~inputs = ("tar", ("czhf" :: output :: inputs))
end

module Patch = struct
let apply ~file = ("patch", ["-p1"; "-i"; file])
end

module Diff = struct
let dirs ~dir1 ~dir2 = ("diff", ["-ruaN"; dir1; dir2])
end

module Rm = struct
let recursive ~dir = ("rm", ["-rf"; dir])
end

module Cmd = struct
let rm_recursive ~dir = ("cmd", ["/d"; "/v:off"; "/c"; "rd"; "/s"; "/q"; dir])
let get_os_version = ("cmd", ["/C"; "ver"])
end

module Sw_vers = struct
let get_os_version = ("sw_vers", ["-productVersion"])
end

module Getprop = struct
let get_os_version = ("getprop", ["ro.build.version.release"])
end

module Sleep = struct
let one_second = ("sleep", ["1"])
end

module Cp = struct
let cp ~src ~dst = ("cp", [src; dst])
let recursive ~srcs ~dst = ("cp", ("-PRp" :: srcs @ [dst]))
end

module Mv = struct
let mv ~src ~dst = ("mv", [src; dst])
end

module Install = struct
let exec ~src ~dst = ("install", ["-m"; "0755"; src; dst])
let file ~src ~dst = ("install", ["-m"; "0644"; src; dst])
end

module Sysctl = struct
let get_hw_ncpu = ("sysctl", ["-n"; "hw.ncpu"])
end

module Getconf = struct
let get_nproc = ("getconf", ["_NPROCESSORS_ONLN"])
end

module Lsb_release = struct
let get_id = ("lsb_release", ["-i"; "-s"])
let get_release = ("lsb_release", ["-s"; "-r"])
end

module Tput = struct
let cols = ("tput", ["cols"])
end

module Stty = struct
let size = ("stty", ["size"])
end

module Uname = struct
let kern_name = ("uname", ["-s"])
let kern_version = ("uname", ["-r"])
let arch = ("uname", ["-m"])
let freebsd_version = ("uname", ["-U"])
end

module Openssl = struct
let sha256 ~file = ("openssl", ["sha256"; file])
let sha512 ~file = ("openssl", ["sha512"; file])
end

module Git = struct
let get_user_name = ("git", ["config"; "--get"; "user.name"])
let get_user_email = ("git", ["config"; "--get"; "user.email"])
end

module OCaml = struct
let vnum = ("ocaml", ["-vnum"])
end

module Command = struct
let lookup ~cmd = ("/bin/sh", ["-c"; "command -v "^cmd])
end
123 changes: 123 additions & 0 deletions src/core/opamExternalTools.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
(**************************************************************************)
(* *)
(* Copyright 2018 OCamlPro *)
(* *)
(* All rights reserved. This file is distributed under the terms of the *)
(* GNU Lesser General Public License version 2.1, with the special *)
(* exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)

type t

val unpack : t -> (string * string list)

(** NOTE: Please try to use this function as little as possible *)
val custom : (string * string list) -> t

val has_space : t -> bool
val cmd_to_string : t -> string

(** For printing only *)
val to_string : t -> string

module Unzip : sig
val extract : file:string -> dir:string -> t
end

module Tar : sig
val extract_gzip : file:string -> dir:string -> t
val extract_bzip2 : file:string -> dir:string -> t
val extract_xz : file:string -> dir:string -> t
val extract_lzma : file:string -> dir:string -> t
val create_gzip : output:string -> inputs:string list -> t
end

module Patch : sig
val apply : file:string -> t
end

module Diff : sig
val dirs : dir1:string -> dir2:string -> t
end

module Rm : sig
val recursive : dir:string -> t
end

module Cmd : sig
val rm_recursive : dir:string -> t
val get_os_version : t
end

module Sw_vers : sig
val get_os_version : t
end

module Getprop : sig
val get_os_version : t
end

module Sleep : sig
val one_second : t
end

module Cp : sig
val cp : src:string -> dst:string -> t
val recursive : srcs:string list -> dst:string -> t
end

module Mv : sig
val mv : src:string -> dst:string -> t
end

module Install : sig
val exec : src:string -> dst:string -> t
val file : src:string -> dst:string -> t
end

module Sysctl : sig
val get_hw_ncpu : t
end

module Getconf : sig
val get_nproc : t
end

module Lsb_release : sig
val get_id : t
val get_release : t
end

module Tput : sig
val cols : t
end

module Stty : sig
val size : t
end

module Uname : sig
val kern_name : t
val kern_version : t
val arch : t
val freebsd_version : t
end

module Openssl : sig
val sha256 : file:string -> t
val sha512 : file:string -> t
end

module Git : sig
val get_user_name : t
val get_user_email : t
end

module OCaml : sig
val vnum : t
end

module Command : sig
val lookup : cmd:string -> t
end
4 changes: 2 additions & 2 deletions src/core/opamFilename.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ let exec dirname ?env ?name ?metadata ?keep_going cmds =

let move_dir ~src ~dst =
OpamSystem.command ~verbose:(OpamSystem.verbose_for_base_commands ())
[ "mv"; Dir.to_string src; Dir.to_string dst ]
(OpamExternalTools.Mv.mv ~src:(Dir.to_string src) ~dst:(Dir.to_string dst))

let exists_dir dirname =
try (Unix.stat (Dir.to_string dirname)).Unix.st_kind = Unix.S_DIR
Expand Down Expand Up @@ -220,7 +220,7 @@ let install ?exec ~src ~dst () =
let move ~src ~dst =
if src <> dst then
OpamSystem.command ~verbose:(OpamSystem.verbose_for_base_commands ())
[ "mv"; to_string src; to_string dst ]
(OpamExternalTools.Mv.mv ~src:(to_string src) ~dst:(to_string dst))

let readlink src =
if exists src then
Expand Down
3 changes: 2 additions & 1 deletion src/core/opamFilename.mli
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ val env_of_list: (string * string) list -> string array

(** Execute a list of commands in a given directory *)
val exec: Dir.t -> ?env:(string * string) list -> ?name:string ->
?metadata:(string * string) list -> ?keep_going:bool -> string list list -> unit
?metadata:(string * string) list -> ?keep_going:bool ->
OpamExternalTools.t list -> unit

(** Move a directory *)
val move_dir: src:Dir.t -> dst:Dir.t -> unit
Expand Down
6 changes: 5 additions & 1 deletion src/core/opamHash.ml
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,17 @@ let to_json s = `String (to_string s)
let to_path (kind,s) =
[string_of_kind kind; String.sub s 0 2; s]

let command_of_kind = function
| `SHA256 -> OpamExternalTools.Openssl.sha256
| `SHA512 -> OpamExternalTools.Openssl.sha512

let compute ?(kind=default_kind) file = match kind with
| `MD5 -> md5 (Digest.to_hex (Digest.file file))
| (`SHA256 | `SHA512) as kind ->
try
if not OpamCoreConfig.(!r.use_openssl) then raise Exit else
match
OpamSystem.read_command_output ["openssl"; string_of_kind kind; file]
OpamSystem.read_command_output (command_of_kind kind ~file)
with
| [l] ->
let len = len kind in
Expand Down
Loading