@@ -18,30 +18,33 @@ and remove dir name =
18
18
let file = Filename. concat dir name in
19
19
if Sys. is_directory file then remove_dir file else Lwt_unix. unlink file
20
20
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 ))
32
26
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
37
31
38
32
(* The answer of the grader will be returned marshalled through a pipe:
39
33
type it explicitely and avoid any exceptions inside. *)
40
34
type grader_answer =
41
35
(Learnocaml_report .t , Grading .error ) Stdlib .result * string * string * string
42
36
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
+
43
46
let get_grade ?callback ?timeout ?dirname exo solution =
44
- with_temp_dir @@ fun cmis_dir ->
47
+ Lazy. force cmis_dir >> = fun cmis_dir ->
45
48
Lwt_io. flush_all () >> = fun () ->
46
49
flush_all () ;
47
50
let in_fd, out_fd = Unix. pipe ~cloexec: true () in
@@ -51,12 +54,6 @@ let get_grade ?callback ?timeout ?dirname exo solution =
51
54
Unix. close in_fd;
52
55
let oc = Unix. out_channel_of_descr out_fd in
53
56
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 ; *)
60
57
Load_path. init [ cmis_dir ] ;
61
58
Toploop_unix. initialize () ;
62
59
let divert name chan cb =
0 commit comments