@@ -18,30 +18,33 @@ and remove dir name =
1818 let file = Filename. concat dir name in
1919 if Sys. is_directory file then remove_dir file else Lwt_unix. unlink file
2020
21- let with_temp_dir f =
22- let rec get_dir () =
23- let d =
24- Filename. concat
25- (Filename. get_temp_dir_name () )
26- (Printf. sprintf " grader_%06X" (Random. int 0xFFFFFF ))
27- in
28- Lwt. catch (fun () -> Lwt_unix. mkdir d 0o700 >> = fun () -> Lwt. return d)
29- @@ function
30- | Unix. Unix_error (Unix. EEXIST, _ , _ ) -> get_dir ()
31- | e -> raise e
21+ let rec mk_temp_dir () =
22+ let d =
23+ Filename. concat
24+ (Filename. get_temp_dir_name () )
25+ (Printf. sprintf " grader_%06X" (Random. int 0xFFFFFF ))
3226 in
33- get_dir () >> = fun dir ->
34- Lwt. catch
35- ( fun () -> f dir >> = fun res -> remove_dir dir >> = fun ( ) -> Lwt. return res )
36- ( fun e -> remove_dir dir >> = fun () -> Lwt. fail e)
27+ Lwt. catch ( fun () -> Lwt_unix. mkdir d 0o700 >> = fun () -> Lwt. return d)
28+ @@ function
29+ | Unix. Unix_error ( Unix. EEXIST, _ , _ ) -> mk_temp_dir ( )
30+ | e -> Lwt. fail e
3731
3832(* The answer of the grader will be returned marshalled through a pipe:
3933 type it explicitely and avoid any exceptions inside. *)
4034type grader_answer =
4135 (Learnocaml_report .t , Grading .error ) Stdlib .result * string * string * string
4236
37+ let cmis_dir = lazy begin
38+ mk_temp_dir () >> = fun cmis_dir ->
39+ let module ResDump = OCamlResFormats. Files (OCamlResSubFormats. Raw ) in
40+ ResDump. output { OCamlResFormats. base_output_dir = cmis_dir }
41+ Embedded_cmis. root;
42+ Lwt_main. at_exit (fun () -> remove_dir cmis_dir);
43+ Lwt. return cmis_dir
44+ end
45+
4346let get_grade ?callback ?timeout ?dirname exo solution =
44- with_temp_dir @@ fun cmis_dir ->
47+ Lazy. force cmis_dir >> = fun cmis_dir ->
4548 Lwt_io. flush_all () >> = fun () ->
4649 flush_all () ;
4750 let in_fd, out_fd = Unix. pipe ~cloexec: true () in
@@ -51,12 +54,6 @@ let get_grade ?callback ?timeout ?dirname exo solution =
5154 Unix. close in_fd;
5255 let oc = Unix. out_channel_of_descr out_fd in
5356 let (ret: grader_answer ) =
54- let module ResDump =
55- OCamlResFormats. Files (OCamlResSubFormats. Raw ) in
56- let dump_cmis =
57- ResDump. output { OCamlResFormats. base_output_dir = cmis_dir } in
58- dump_cmis Embedded_cmis. root ;
59- (* dump_cmis Embedded_grading_cmis.root ; *)
6057 Load_path. init [ cmis_dir ] ;
6158 Toploop_unix. initialize () ;
6259 let divert name chan cb =
0 commit comments