tmp_file.ml 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. (******************************************************************************)
  2. (* Copyright © Joly Clément, 2014-2015 *)
  3. (* *)
  4. (* leowzukw@oclaunch.eu.org *)
  5. (* *)
  6. (* Ce logiciel est un programme informatique servant à exécuter *)
  7. (* automatiquement des programmes à l'ouverture du terminal. *)
  8. (* *)
  9. (* Ce logiciel est régi par la licence CeCILL soumise au droit français et *)
  10. (* respectant les principes de diffusion des logiciels libres. Vous pouvez *)
  11. (* utiliser, modifier et/ou redistribuer ce programme sous les conditions *)
  12. (* de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA *)
  13. (* sur le site "http://www.cecill.info". *)
  14. (* *)
  15. (* En contrepartie de l'accessibilité au code source et des droits de copie, *)
  16. (* de modification et de redistribution accordés par cette licence, il n'est *)
  17. (* offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons, *)
  18. (* seule une responsabilité restreinte pèse sur l'auteur du programme, le *)
  19. (* titulaire des droits patrimoniaux et les concédants successifs. *)
  20. (* *)
  21. (* A cet égard l'attention de l'utilisateur est attirée sur les risques *)
  22. (* associés au chargement, à l'utilisation, à la modification et/ou au *)
  23. (* développement et à la reproduction du logiciel par l'utilisateur étant *)
  24. (* donné sa spécificité de logiciel libre, qui peut le rendre complexe à *)
  25. (* manipuler et qui le réserve donc à des développeurs et des professionnels *)
  26. (* avertis possédant des connaissances informatiques approfondies. Les *)
  27. (* utilisateurs sont donc invités à charger et tester l'adéquation du *)
  28. (* logiciel à leurs besoins dans des conditions permettant d'assurer la *)
  29. (* sécurité de leurs systèmes et ou de leurs données et, plus généralement, *)
  30. (* à l'utiliser et l'exploiter dans les mêmes conditions de sécurité. *)
  31. (* *)
  32. (* Le fait que vous puissiez accéder à cet en-tête signifie que vous avez *)
  33. (* pris connaissance de la licence CeCILL, et que vous en avez accepté les *)
  34. (* termes. *)
  35. (******************************************************************************)
  36. open Core.Std;;
  37. (* Type of the values *)
  38. type t = Tmp_biniou_t.tmp_file;;
  39. (* Function to write the tmp file *)
  40. let write (tmp_file:t) =
  41. (* Short name *)
  42. let name = Const.tmp_file in
  43. let biniou_tmp = Tmp_biniou_b.string_of_tmp_file tmp_file in
  44. Out_channel.write_all name ~data:biniou_tmp
  45. ;;
  46. (* XXX Using and keyword because each function can call each other *)
  47. (* Function to read the tmp file *)
  48. let rec read () =
  49. (* Short name *)
  50. let name = Const.tmp_file in
  51. (* Get the string corresponding to the file *)
  52. let file_content = In_channel.read_all name in
  53. try
  54. Tmp_biniou_b.tmp_file_of_string file_content
  55. (* In previous version, the JSON format was used, otherwise the file can
  56. * have a bad format. In this case, the Ag_ob_run.Error("Read error (1)")
  57. * exeption is throw. We catch it here *)
  58. with _ ->
  59. (* If file is not in the right format, delete it and create a new one.
  60. * Then, read it *)
  61. Messages.ok "Reinitialises tmp file\n";
  62. Sys.remove name;
  63. create_tmp_file ();
  64. read ()
  65. (* Function to create the tmp file *)
  66. and create_tmp_file () =
  67. (* An empty list, without rc, commands, launch... *)
  68. Tmp_biniou_v.create_tmp_file ~daemon:0 ~rc:[] ()
  69. (* Convert it to biniou *)
  70. |> write
  71. ;;
  72. (* Function to open tmp file *)
  73. let rec init () =
  74. (* If file do not exists, create it *)
  75. let file_exists = Sys.file_exists Const.tmp_file in
  76. match file_exists with
  77. | `No -> create_tmp_file ();
  78. init ()
  79. | `Unknown -> begin
  80. Sys.remove Const.tmp_file;
  81. init ()
  82. end
  83. | `Yes -> read ()
  84. ;;
  85. (* Get a log of values from the tmp file, like this
  86. * (cmd,number of launch) list *)
  87. let get_log ~rc_tmp =
  88. List.map ~f:(fun { Tmp_biniou_t.commands = (cmd,number) } ->
  89. (cmd,number)) rc_tmp
  90. ;;
  91. (* Verify that the value exist *)
  92. let verify_key_exist ~key entry =
  93. if entry = key then
  94. true
  95. else
  96. false
  97. ;;
  98. (* Return true if a program is in the rc file *)
  99. let rec is_prog_in_rc list_from_rc_file program =
  100. match list_from_rc_file with
  101. (* | None -> is_prog_in_rc program ~liste_from_rc_file:rc_content.progs *)
  102. | [] -> false
  103. | hd :: tl -> if hd = program then true else is_prog_in_rc tl program
  104. ;;
  105. (* Log when a program has been launched in a file in /tmp
  106. ~func is the function applied to the value
  107. ~cmd is the launched entry *)
  108. let log ~cmd ?(func= (+) 1 ) () =
  109. (* Make sure that file exists, otherwise strange things appears *)
  110. let file = init () in
  111. (* Get rc_file name *)
  112. let name = Lazy.force !Const.rc_file in
  113. (* Function to generate the new list with right number *)
  114. let new_li (li : Tmp_biniou_t.rc_entry list) =
  115. let open List.Assoc in
  116. (* Only number of launch associated with commands *)
  117. let l = get_log ~rc_tmp:li in
  118. find l cmd
  119. |> (function None -> add l cmd Const.default_launch | Some n -> add l cmd (func n))
  120. |> List.map ~f:(fun e -> { Tmp_biniou_t.commands = e})
  121. in
  122. (* Write the file with the new value *)
  123. let updated_li =
  124. List.Assoc.(find file.Tmp_biniou_t.rc name)
  125. |> Option.value ~default:[]
  126. |> new_li
  127. in
  128. write Tmp_biniou_t.{ file with rc = List.Assoc.add file.rc name
  129. updated_li }
  130. ;;
  131. (* Return current number *)
  132. let get_current () =
  133. failwith "Deprecated"
  134. ;;
  135. (* Get number of launch for each command in rc file, as follow:
  136. * (command:string, number of the command:int) list *)
  137. let get_accurate_log ?rc_name ~tmp () =
  138. let open List in
  139. (* Read rc *)
  140. (* XXX Forcing evaluation of lazy value Const.rc_file before it is
  141. * necessary *)
  142. let name : string = Option.value ~default:(Lazy.force !Const.rc_file) rc_name
  143. in
  144. let rc = File_com.init_rc ~rc:(Lazy.return name) () in
  145. let rc_in_tmp = get_log ~rc_tmp:(Assoc.find tmp.Tmp_biniou_t.rc name
  146. |> Option.value ~default:[])
  147. in
  148. map rc.Settings_t.progs ~f:(fun key ->
  149. Assoc.find rc_in_tmp key
  150. |> Option.value ~default:0
  151. |> (function number -> (key,number)))
  152. ;;
  153. (* Reset number of launch for a given command
  154. * cmd: number of the command to be reseted
  155. * num: number to reset *)
  156. let reset_cmd ~rc num cmd =
  157. (* Debugging *)
  158. [(num,"num") ; (cmd,"cmd")]
  159. |> List.map ~f:(fun (i , str) -> str ^ ": " ^ (Int.to_string i))
  160. |> List.iter ~f:(fun s -> Messages.debug s);
  161. let ac_log = get_accurate_log ~tmp:(init ()) () in
  162. (* The command (string) corresponding to the number *)
  163. let cmd_str =
  164. File_com.num_cmd2cmd ~rc cmd
  165. |> function
  166. Some s -> s
  167. | None -> failwith "Out of bound"
  168. in
  169. (* Current number of launch for that cmd *)
  170. let i = List.Assoc.find_exn ac_log cmd_str in
  171. sprintf "Last N for command '%s' was %i"
  172. cmd_str
  173. i
  174. |> Messages.info;
  175. sprintf "Restore with 'oclaunch reset %i %i'" i cmd
  176. |> Messages.tips;
  177. (* Do the work, set the number *)
  178. log ~func:(fun _ -> num) ~cmd:cmd_str ();
  179. sprintf "Reseted command '%s' to %i successfully" cmd_str num |> Messages.ok
  180. ;;
  181. (* Reset all commands to a number
  182. * num: number to reset *)
  183. let reset2num ~rc num =
  184. (* Debugging *)
  185. "Num: " ^ (Int.to_string num)
  186. |> Messages.debug;
  187. let ac_log = get_accurate_log ~tmp:(init ()) () in
  188. (* Erase number of launch for each command *)
  189. List.iter ac_log ~f:(fun ( cmd, _ ) ->
  190. log ~func:(fun _ -> num) ~cmd ())
  191. ;;
  192. (* Reset all command *)
  193. let reset_all () =
  194. Messages.debug "Preparing to reset all";
  195. let reset_without_ask () =
  196. (* Make sure that file exists, otherwise strange things appears *)
  197. let tmp = init () in
  198. (* Get rc_file name *)
  199. let name = Lazy.force !Const.rc_file in
  200. write Tmp_biniou_t.{ tmp with rc = List.Assoc.add tmp.rc name [] }
  201. in
  202. Messages.debug "Asking question";
  203. Messages.confirm "You will lose number of launch for every command. \
  204. Are you sure?"
  205. |> (fun answer -> sprintf "Answer %s" (Messages.answer2str answer) |> Messages.debug; answer) (* Spy *)
  206. |> function
  207. Messages.Yes -> reset_without_ask (); Messages.ok "Successfully reseted!"
  208. | Messages.No -> ()
  209. ;;