10
1
mirror of https://gitlab.com/scemama/qmcchem.git synced 2024-12-27 23:03:32 +01:00
qmcchem/ocaml/Qmcchem_md5.ml

166 lines
4.3 KiB
OCaml
Raw Normal View History

2015-12-19 02:35:13 +01:00
2016-01-31 00:29:36 +01:00
let run ?c ?d ~l ~update ezfio_filename =
2015-12-19 02:35:13 +01:00
Qputils.set_ezfio_filename ezfio_filename;
2015-12-19 02:35:13 +01:00
let input_directory =
2018-03-14 17:02:52 +01:00
Lazy.force QmcMd5.input_directory
2015-12-19 02:35:13 +01:00
in
let handle_options () =
let current_md5 =
2018-03-14 17:02:52 +01:00
QmcMd5.hash ()
2015-12-19 02:35:13 +01:00
in
let filename_of_key key =
Filename.concat input_directory key
in
let key_is_valid key =
let filename =
filename_of_key key
in
2019-07-19 17:06:01 +02:00
Sys.file_exists filename
2015-12-19 02:35:13 +01:00
in
2016-01-31 00:29:36 +01:00
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;
2018-03-14 17:02:52 +01:00
QmcMd5.reset_hash ();
2016-01-31 00:29:36 +01:00
let new_key =
2018-03-14 17:02:52 +01:00
QmcMd5.hash ()
2016-01-31 00:29:36 +01:00
in
if (old_key <> new_key) then
begin
2019-07-19 17:06:01 +02:00
let prefix =
Filename.concat ezfio_filename "blocks"
in
2016-01-31 00:29:36 +01:00
let new_name =
2019-07-19 17:06:01 +02:00
Filename.concat prefix new_key
2016-01-31 00:29:36 +01:00
and old_name =
2019-07-19 17:06:01 +02:00
Filename.concat prefix old_key
2016-01-31 00:29:36 +01:00
in
Printf.printf "Renaming %s -> %s\n" old_name new_name;
try Sys.rename old_name new_name with
| Sys_error _ -> ();
let old_name =
2019-07-19 17:06:01 +02:00
String.concat "/" [ ezfio_filename; "input"; old_key ]
2016-01-31 00:29:36 +01:00
in
Printf.printf "Removing %s\n%!" old_name;
2019-07-22 11:31:16 +02:00
try Sys.remove old_name with
2016-01-31 00:29:36 +01:00
| Sys_error _ -> ();
end
in
2019-07-19 17:06:01 +02:00
Sys.readdir input_directory
|> Array.iter (fun x -> update_one x) ;
2016-01-31 00:29:36 +01:00
Printf.printf "Done\n%!" ;
end
;
2015-12-19 02:35:13 +01:00
let () =
match c with
| None -> ()
| Some new_md5 ->
if (key_is_valid new_md5) then
Qmcchem_edit.run ~c:false ~input:(filename_of_key new_md5) ezfio_filename
else
failwith ("Error: " ^ new_md5 ^ " does not exist")
in
let () =
match l with
| false -> ()
| true ->
2019-07-19 17:06:01 +02:00
Sys.readdir input_directory
|> Array.iter (fun md5 ->
2015-12-19 02:35:13 +01:00
let filename =
Filename.concat input_directory md5
in
let this =
if (md5 = current_md5) then
"<-"
else
""
in
2019-07-22 11:31:16 +02:00
let date =
let open Unix in
localtime (stat filename).st_mtime
|> Time.string_of_date
2015-12-19 02:35:13 +01:00
in
Printf.printf "%s : %s %s\n" md5 date this)
in
let () =
match d with
| None -> ()
| Some other_key ->
if (key_is_valid other_key) then
let command =
2019-07-19 17:06:01 +02:00
String.concat " "
2015-12-19 02:35:13 +01:00
[ "diff" ; "-u" ; "-w" ;
(filename_of_key current_md5) ;
(filename_of_key other_key) ]
in
2019-07-19 17:06:01 +02:00
ignore @@ Unix.system command
2015-12-19 02:35:13 +01:00
else
failwith ("Error: " ^ other_key ^ " does not exist")
in
()
in
2016-01-31 00:29:36 +01:00
match (c,d,l,update) with
| (None,None,false,false) ->
2018-03-14 17:02:52 +01:00
Printf.printf "Current key :\n%s\n" (QmcMd5.hash ())
2015-12-19 02:35:13 +01:00
| _ -> handle_options ()
2019-07-22 12:19:12 +02:00
let command () =
2019-07-19 17:06:01 +02:00
let open Command_line in
begin
set_header_doc (Sys.argv.(0) ^ " - QMC=Chem command");
set_description_doc "Manipulate input MD5 keys";
[ { short='c' ; long="clear" ; opt=Optional ;
doc="Change to input to <key>" ;
arg=With_arg "<string>" ; };
{ short='d' ; long="diff" ; opt=Optional ;
doc="Show input differences with <key>" ;
arg=With_arg "<string>" ; };
{ short='l' ; long="list" ; opt=Optional ;
doc="List all the saved MD5 keys." ;
arg=Without_arg ; };
{ short='u' ; long="update" ; opt=Optional ;
doc="Update to the latest MD5 format." ;
arg=Without_arg ; };
anonymous "EZFIO_DIR" Mandatory "EZFIO directory";
]
|> set_specs
end;
let update = Command_line.get_bool "update" in
let c = Command_line.get "clear" in
let d = Command_line.get "diff" in
let l = Command_line.get_bool "list" in
2019-07-22 11:31:16 +02:00
let ezfio_file =
match Command_line.anon_args () with
| ezfio_file :: [] -> ezfio_file
| _ -> (Command_line.help () ; failwith "Inconsistent command line")
in
2019-07-19 17:06:01 +02:00
run ?c ?d ~l ~update ezfio_file
2015-12-19 02:35:13 +01:00