Parcourir la source

Start to handle biniou format in tmp file

 - Advantages :
   - Faster parsing
   - Make harder manual and unwanted modifications
   - Save place
 - What was done
   - Add a atdgen file for tmp file
   - Make generated file ignored by git
   - Add oasis prebuild commands
   - Update several ocaml file
 - TODO:
   - Remove trace of older format (JSON)
   - Handle misformatted file
   - Test that everything works well
Leo il y a 10 ans
Parent
commit
01acbd4b63
6 fichiers modifiés avec 89 ajouts et 15 suppressions
  1. 3 0
      .gitignore
  2. 5 0
      _oasis
  3. 3 3
      setup.ml
  4. 2 6
      src/const.ml
  5. 45 0
      src/tmp_biniou.atd
  6. 31 6
      src/tmp_file.ml

+ 3 - 0
.gitignore

@@ -3,6 +3,9 @@ oclaunch.byte
 settings_j.ml*
 settings_t.ml*
 settings_v.ml*
+tmp_biniou_b.ml*
+tmp_biniou_t.ml*
+tmp_biniou_v.ml*
 # Oasis
 _build
 _tags

+ 5 - 0
_oasis

@@ -19,6 +19,11 @@ Description: OcLaunch is a command-line tool to launch successively (each time t
 PreBuildCommand: atdgen -t ./src/settings.atd
 PreBuildCommand: atdgen -j ./src/settings.atd
 PreBuildCommand: atdgen -v ./src/settings.atd
+PreBuildCommand: atdgen -b ./src/tmp_biniou.atd
+PreBuildCommand: atdgen -t ./src/tmp_biniou.atd
+PreBuildCommand: atdgen -v ./src/tmp_biniou.atd
+# Test, to be shure that atgen commands above run
+PreBuildCommand: echo "Atdgen executed"
 
 Executable oclaunch
   Path:       src

+ 3 - 3
setup.ml

@@ -1,7 +1,7 @@
 (* setup.ml generated for the first time by OASIS v0.4.5 *)
 
 (* OASIS_START *)
-(* DO NOT EDIT (digest: 6fa4a45c7eb8a4ae5696d45c5cab2ecd) *)
+(* DO NOT EDIT (digest: 26dbfb28e1a29a2f3ce82b2723b36731) *)
 (*
    Regenerated by OASIS v0.4.5
    Visit http://oasis.forge.ocamlcore.org for more information and
@@ -6707,7 +6707,7 @@ let setup_t =
                pre_command =
                  [
                     (OASISExpr.EBool true,
-                      Some (("atdgen", ["-v"; "./src/settings.atd"])))
+                      Some (("echo", ["\"Atdgen"; "executed\""])))
                  ];
                post_command = [(OASISExpr.EBool true, None)]
             };
@@ -6778,7 +6778,7 @@ let setup_t =
        };
      oasis_fn = Some "_oasis";
      oasis_version = "0.4.5";
-     oasis_digest = Some "\214\217i\178\219\129\178;\014\221\200^\185!9\206";
+     oasis_digest = Some "}o\207j\175\255y\025\150\239!\026Sx\151\242";
      oasis_exec = None;
      oasis_setup_args = [];
      setup_update = false

+ 2 - 6
src/const.ml

@@ -46,9 +46,5 @@ let home = match (Sys.getenv "HOME") with
 
 (* Some settings variales *)
 let rc_file = home ^ "/" ^ ".oclaunch_rc.json";;
-(* Set tmp file, in witch stock launches *)
-let tmp_file = "/tmp/oclaunch_trace.json";; (* File where launch are logged *)
-(* Template for the tmp file *)
-let (tmp_file_template:Yojson.Basic.json) = `Assoc
-                                              [ "cmd", `List [];
-                                              "num", `Int 0 ];;
+(* Set tmp file, in witch stock launches, in biniou format *)
+let tmp_file = "/tmp/oclaunch_trace.bi";; (* File where launch are logged *)

+ 45 - 0
src/tmp_biniou.atd

@@ -0,0 +1,45 @@
+(******************************************************************************)
+(* Copyright © Joly Clément, 2014                                             *)
+(*                                                                            *)
+(*  leowzukw@vmail.me                                                         *)
+(*                                                                            *)
+(*  Ce logiciel est un programme informatique servant à exécuter              *)
+(*  automatiquement des programmes à l'ouverture du terminal.                 *)
+(*                                                                            *)
+(*  Ce logiciel est régi par la licence CeCILL soumise au droit français et   *)
+(*  respectant les principes de diffusion des logiciels libres. Vous pouvez   *)
+(*  utiliser, modifier et/ou redistribuer ce programme sous les conditions    *)
+(*  de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA    *)
+(*  sur le site "http://www.cecill.info".                                     *)
+(*                                                                            *)
+(*  En contrepartie de l'accessibilité au code source et des droits de copie, *)
+(*  de modification et de redistribution accordés par cette licence, il n'est *)
+(*  offert aux utilisateurs qu'une garantie limitée.  Pour les mêmes raisons, *)
+(*  seule une responsabilité restreinte pèse sur l'auteur du programme,  le   *)
+(*  titulaire des droits patrimoniaux et les concédants successifs.           *)
+(*                                                                            *)
+(*  A cet égard  l'attention de l'utilisateur est attirée sur les risques     *)
+(*  associés au chargement,  à l'utilisation,  à la modification et/ou au     *)
+(*  développement et à la reproduction du logiciel par l'utilisateur étant    *)
+(*  donné sa spécificité de logiciel libre, qui peut le rendre complexe à     *)
+(*  manipuler et qui le réserve donc à des développeurs et des professionnels *)
+(*  avertis possédant  des  connaissances  informatiques approfondies.  Les   *)
+(*  utilisateurs sont donc invités à charger  et  tester  l'adéquation  du    *)
+(*  logiciel à leurs besoins dans des conditions permettant d'assurer la      *)
+(*  sécurité de leurs systèmes et ou de leurs données et, plus généralement,  *)
+(*  à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.        *)
+(*                                                                            *)
+(*  Le fait que vous puissiez accéder à cet en-tête signifie que vous avez    *)
+(*  pris connaissance de la licence CeCILL, et que vous en avez accepté les   *)
+(*  termes.                                                                   *)
+(******************************************************************************)
+
+(* Tmp file is where launch are logged in.
+ * This atd file allow to use biniou easier in tmp file. The purpuse is to save
+ * place, get faster parsing and make harder manual, unwanted modification. A special command line is
+ * available to change current number *)
+
+type tmp_file = {
+    command: string list; (* A list of command, for future use *)
+    number: int; (* The current number/state of the launches *)
+}

+ 31 - 6
src/tmp_file.ml

@@ -36,9 +36,33 @@
 
 open Core.Std;;
 
+(* XXX Using and keyword because each function can call each other *)
+(* Function to read the tmp file *)
+let rec read ~name =
+    (* Get the string corresponding to the file *)
+    let file_content = In_channel.read_all name in
+    try
+        Tmp_biniou_b.tmp_file_of_string file_content
+    (* In previous version, the JSON format was used, otherwise the file can
+     * have a bad format. In this case, the Ag_ob_run.Error("Read error (1)")
+     * exeption is throw. We catch it here *)
+    with Ag_ob_run.Error("Read error (1)") ->
+        (* If file is not in the right format, delete it and create a new one.
+         * Then, read it *)
+        Sys.remove name;
+        create_tmp_file ~name;
+        read ~name
+
+(* Function to write the tmp file *)
+and write ~name (tmp_file:Tmp_biniou_t.tmp_file) =
+    let biniou_tmp = Tmp_biniou_b.string_of_tmp_file tmp_file in
+    Out_channel.write_all name ~data:biniou_tmp
+
 (* Function to create the tmp file *)
-let create_tmp_file ~name =
-  Yojson.Basic.pretty_to_channel (Out_channel.create name) Const.tmp_file_template
+and create_tmp_file ~name =
+    Tmp_biniou_v.create_tmp_file ~command:[] ~number:0 ()
+    (* Convert it to biniou *)
+    |> write ~name
 ;;
 
 (* Function to open tmp file *)
@@ -74,10 +98,11 @@ let rec is_prog_in_rc list_from_rc_file program =
 (* Log when a program has been launched in a file in /tmp
    ~func is the function applied to the value *)
 let log ?(func= (+) 1 ) ~file_name =
-  let file = Yojson.Basic.from_file file_name in
-  match file with
-    | `Assoc [( a, `List b ); ("num", `Int c)] -> let new_value = `Assoc [( a, `List b ); ("num", `Int (c |> func))] in Yojson.Basic.to_file file_name new_value
-    | _ -> failwith "Incorrect format"
+  (* We will use tmp_file type *)
+  let open Tmp_biniou_t in
+  let file = read ~name:file_name in
+  (* Write the file with the new value *)
+  write ~name:file_name { file with number = (func file.number)}
 ;;
 
 (* Reset command number in two ways :