let create_python_tool ~host ~(run_program : Machine.Make_fun.t) ~install_path
?check_bin ?version ?(python_version=`Python3)
(installation:install_tool_type * install_source_type) =
let open KEDSL in
let versionize ?version ~sep name = match version with
| None -> name
| Some v -> name ^ sep ^ v
in
let install_command, name =
match installation with
| (Pip, Package_PyPI pname) ->
["pip"; "install"; versionize ?version ~sep:"==" pname], pname
| (Pip, Package_Source (pname, source)) ->
["pip"; "install"; source], pname
| (Conda, Package_Conda pname) ->
["conda"; "install"; "-y"; versionize ?version ~sep:"=" pname], pname
| (Conda, Package_PyPI pname) ->
["conda"; "skeleton"; "pypi"; pname], pname
| _ -> failwith "Installation type not supported."
in
let main_subdir = name ^ "_conda_dir" in
let conda_env =
Conda.setup_environment ~python_version ~main_subdir install_path
(name ^ Option.value_map ~default:"" version ~f:(sprintf ".%s"))
in
let single_file_check id =
single_file ~host (bin_in_conda_environment ~conda_env id)
in
let exec_check =
match check_bin with
| None -> single_file_check name
| Some s -> single_file_check s
in
let ensure =
workflow_node exec_check
~name:("Installing Python tool: " ^ name)
~edges:[ depends_on Conda.(configured ~run_program ~host ~conda_env) ]
~make:(run_program
~requirements:[
`Internet_access; `Self_identification ["python"; "installation"]
]
Program.(
Conda.init_env ~conda_env ()
&& exec install_command)
)
in
let init = Conda.init_env ~conda_env () in
Machine.Tool.create Machine.Tool.Definition.(create name) ~ensure ~init