diff --git a/ocaml/Md5.ml b/ocaml/Md5.ml index 0cfe106..8abd5ca 100644 --- a/ocaml/Md5.ml +++ b/ocaml/Md5.ml @@ -92,7 +92,7 @@ let hash_file filename = |> Cryptokit.transform_string (Cryptokit.Hexa.encode ()) ) end | _ -> "" - + (** Cache containing the current value of the MD5 hash. *) let _hash = @@ -110,12 +110,17 @@ let hash () = else "" in - let new_md5 = files_to_track - |> List.map ~f:(fun x -> Printf.sprintf "%s/%s" ezfio_filename x) - |> List.map ~f:hash_file - |> String.concat - |> Cryptokit.hash_string (Cryptokit.Hash.md5 ()) - |> Cryptokit.transform_string (Cryptokit.Hexa.encode ()) + let md5_string = + files_to_track + |> List.map ~f:(fun x -> Printf.sprintf "%s/%s" ezfio_filename x) + |> List.map ~f:hash_file + |> String.concat + in + + let new_md5 = + md5_string + |> Cryptokit.hash_string (Cryptokit.Hash.md5 ()) + |> Cryptokit.transform_string (Cryptokit.Hexa.encode ()) in if (new_md5 <> old_md5) then begin diff --git a/ocaml/Qmcchem_md5.ml b/ocaml/Qmcchem_md5.ml index ab3ad0e..54669a6 100644 --- a/ocaml/Qmcchem_md5.ml +++ b/ocaml/Qmcchem_md5.ml @@ -1,6 +1,6 @@ open Core.Std -let run ?c ?d ~l ezfio_filename = +let run ?c ?d ~l ~update ezfio_filename = Qputils.set_ezfio_filename ezfio_filename; @@ -25,6 +25,43 @@ let run ?c ?d ~l ezfio_filename = Sys.file_exists_exn filename in + if (update) then + begin + Printf.printf "Updating\n%!" ; + let update_one old_key = + Qmcchem_edit.run ~c:false ~input:(filename_of_key old_key) ezfio_filename; + Md5.reset_hash (); + let new_key = + Md5.hash () + in + + if (old_key <> new_key) then + begin + let new_name = + String.concat ~sep:"/" [ ezfio_filename; "blocks"; new_key ] + and old_name = + String.concat ~sep:"/" [ ezfio_filename; "blocks"; old_key ] + in + Printf.printf "Renaming %s -> %s\n" old_name new_name; + try Sys.rename old_name new_name with + | Sys_error _ -> (); + + let old_name = + String.concat ~sep:"/" [ ezfio_filename; "input"; old_key ] + in + Printf.printf "Removing %s\n%!" old_name; + try Sys.remove old_name with + | Sys_error _ -> (); + end + in + let l = + Sys.ls_dir input_directory + in + List.iter l ~f:(fun x -> update_one x) ; + Printf.printf "Done\n%!" ; + end + ; + let () = match c with | None -> () @@ -79,8 +116,8 @@ let run ?c ?d ~l ezfio_filename = in - match (c,d,l) with - | (None,None,false) -> + match (c,d,l,update) with + | (None,None,false,false) -> Printf.printf "Current key :\n%s\n" (Md5.hash ()) | _ -> handle_options () @@ -94,6 +131,8 @@ let spec = ~doc:(" Show input differences with ") +> flag "l" no_arg ~doc:(" List all the saved MD5 keys.") + +> flag "update" no_arg + ~doc:(" Update to the latest MD5 format.") +> anon ("ezfio_file" %: string) @@ -106,7 +145,7 @@ let command = Manipulate input MD5 keys ") spec - (fun c d l ezfio_file () -> run ?c ?d ~l ezfio_file ) + (fun c d l update ezfio_file () -> run ?c ?d ~l ~update ezfio_file )