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

Allow post-install-commands hooks to modify/remove installed files #4388

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
3 changes: 2 additions & 1 deletion doc/pages/Manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,8 @@ them modified with [`opam option --global`](man/opam-option.html).
modified during the installation of the package.
Note that this hook is run after the scan for installed files is
done, so any additional installed files won't be recorded and must be taken
care of by a `pre-remove-commands` hook.
care of by a `pre-remove-commands` hook. However, modified or deleted installed
files during the `post-install-commands` will be handled correctly by `opam`.
- <a id="configfield-pre-session-commands">`pre-session-commands: [ [ <term> { <filter> } ... ] { <filter> } ... ]`</a>,
<a id="configfield-post-session-commands">`post-session-commands: [ [ <term> { <filter> } ... ] { <filter> } ... ]`</a>:
These commands will be run once respectively before and after the sequence of
Expand Down
1 change: 1 addition & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ New option/command/subcommand are prefixed with ◈.

## Install
* The stdout of `pre-` and `post-session` hooks is now propagated to the user [#4382 @AltGr - fix #4359]
* `post-install` hooks are allowed to modify or remove installed files, the but not add new ones. Those changes are integrated in changes file [#4388 @lefessan]

## Remove
*
Expand Down
20 changes: 15 additions & 5 deletions src/client/opamAction.ml
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,8 @@ let install_package t ?(test=false) ?(doc=false) ?build_dir nv =
| Some e -> Some e
| None -> try process_dot_install t nv dir; None with e -> Some e
in
let root = t.switch_global.root in
let switch_prefix = OpamPath.Switch.root root t.switch in
let post_install error changes =
let local =
let added =
Expand All @@ -885,17 +887,25 @@ let install_package t ?(test=false) ?(doc=false) ?build_dir nv =
(OpamVariable.of_string "installed-files")
(Some (L added))
in
let hooks =
get_wrapper t opam wrappers ~local OpamFile.Wrappers.post_install
in
let has_hooks = match hooks with [] -> false | _ -> true in
OpamProcess.Job.of_fun_list ~keep_going:true
(List.map (fun cmd () -> mk_cmd cmd)
(get_wrapper t opam wrappers ~local OpamFile.Wrappers.post_install))
(List.map (fun cmd () -> mk_cmd cmd) hooks)
@@+ fun error_post ->
match error, error_post with
| Some err, _ -> Done (Some err, changes)
| None, Some (_cmd, r) -> Done (Some (OpamSystem.Process_error r), changes)
| None, None -> Done (None, changes)
| None, None ->
let changes =
if has_hooks then
OpamDirTrack.update switch_prefix changes
else
changes
in
Done (None, changes)
in
let root = t.switch_global.root in
let switch_prefix = OpamPath.Switch.root root t.switch in
let rel_meta_dir =
OpamFilename.(Base.of_string (remove_prefix_dir switch_prefix
(OpamPath.Switch.meta root t.switch)))
Expand Down
30 changes: 30 additions & 0 deletions src/core/opamDirTrack.ml
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,33 @@ let revert ?title ?(verbose=OpamConsole.verbose()) ?(force=false)
(OpamStd.Option.to_string (fun pr ->
if pr then "[hash] " else "[tms] ") pre) ^ x) lf))
cannot))

let update prefix t =
let removed = ref [] in
lefessan marked this conversation as resolved.
Show resolved Hide resolved
let prefix = OpamFilename.Dir.to_string prefix in
let update_digest file digest =
match
let filename = Filename.concat prefix file in
let precise = is_precise_digest digest in
item_digest ( item_of_filename ~precise filename )
with
| exception Unix.Unix_error ( ENOENT, _, _) ->
removed := file :: !removed;
digest
| exception _exn -> digest
| digest -> digest
in
let t =
SM.mapi (fun file change ->
match change with
| Added digest -> Added (update_digest file digest)
| Removed -> Removed
| Contents_changed digest ->
Contents_changed (update_digest file digest)
| Perm_changed digest -> Perm_changed (update_digest file digest)
| Kind_changed digest -> Kind_changed (update_digest file digest)
) t
in
List.fold_left (fun t file ->
SM.remove file t
) t !removed
4 changes: 4 additions & 0 deletions src/core/opamDirTrack.mli
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ val revert:
val check:
OpamFilename.Dir.t -> t ->
(OpamFilename.t * [`Unchanged | `Removed | `Changed]) list

(** Reload all the digests from the directory [prefix]. Remove a file
from the map if it has been removed from the file-system. *)
val update : OpamFilename.Dir.t -> t -> t