[augeas-devel] [PATCH] Array handling shellvars_array lens added.

Frederik Wagner fnerdwq at googlemail.com
Tue Mar 23 20:38:56 UTC 2010


Lens shellvars_array has been added to treat  variables in specific
files as array of words, e.g. in /etc/sysconfig/ 'kernel' and
'bootloader'.

* lenses/shellvars.aug:
    removed include for /etc/sysconfig/kernel
* lenses/shellvars_array.aug:
    new lens
* lenses/tests/test_shellvars_array.aug:
    test for new lens
---
 lenses/shellvars.aug                  |    1 -
 lenses/shellvars_array.aug            |   41 +++++++++++++++++++++++
 lenses/tests/test_shellvars_array.aug |   57 +++++++++++++++++++++++++++++++++
 3 files changed, 98 insertions(+), 1 deletions(-)
 create mode 100644 lenses/shellvars_array.aug
 create mode 100644 lenses/tests/test_shellvars_array.aug

diff --git a/lenses/shellvars.aug b/lenses/shellvars.aug
index 814b7f1..ef9e6db 100644
--- a/lenses/shellvars.aug
+++ b/lenses/shellvars.aug
@@ -69,7 +69,6 @@ module Shellvars =
       sc_incl "irda" .
       sc_incl "irqbalance" .
       sc_incl "kdump" .
-      sc_incl "kernel" .
       sc_incl "keyboard" .
       sc_incl "kudzu" .
       sc_incl "libvirtd" .
diff --git a/lenses/shellvars_array.aug b/lenses/shellvars_array.aug
new file mode 100644
index 0000000..1198ff3
--- /dev/null
+++ b/lenses/shellvars_array.aug
@@ -0,0 +1,41 @@
+(* Generic lens for shell-script config files like the ones found *)
+(* in /etc/sysconfig, where a string needs to be split into       *)
+(* single words.                                                  *)
+module Shellvars_array =
+  autoload xfm
+
+  let eol = Util.eol
+
+  let key_re = /[A-Za-z0-9_]+/ 
+  let eq = Util.del_str "="
+  let comment = Util.comment
+  let empty   = Util.empty
+
+  let char  = /[^ "\t\n]/
+
+  (* Array values of the form "val1 val2 val3". *)
+  let array =
+    let array_value = store char+ in
+    del /\"[ \t]*/ "\"" . counter "values" .
+      [ seq "values" . array_value ] .
+      [ del /[ \t\n]+/ " "  . seq "values" .array_value ] *
+      . del /[ \t]*\"/ "\""
+
+  (* delete the case with emtpy "" *)
+  let kv = [ key key_re . eq . (del "\"\"" "\"\""|array) . eol ]
+
+  let lns = ( comment | empty | kv )*
+
+  let sc_incl (n:string) = (incl ("/etc/sysconfig/" . n))
+  let filter_sysconfig =
+      sc_incl "bootloader" .
+      sc_incl "kernel" 
+
+  let filter = filter_sysconfig
+             . Util.stdexcl
+
+  let xfm = transform lns filter
+
+(* Local Variables: *)
+(* mode: caml       *)
+(* End:             *)
diff --git a/lenses/tests/test_shellvars_array.aug b/lenses/tests/test_shellvars_array.aug
new file mode 100644
index 0000000..3309667
--- /dev/null
+++ b/lenses/tests/test_shellvars_array.aug
@@ -0,0 +1,57 @@
+(* Test for shell array handling lens *)
+module Test_shellvars_array =
+
+  let array_vals = "# Some comment
+MODULES_LOADED_ON_BOOT=\"ipv6 sunrpc\"
+
+DEFAULT_APPEND=\"showopts noresume console=tty0 console=ttyS0,115200n8 ro\"
+
+LOADER_TYPE=\"grub\"
+"
+
+  test Shellvars_array.lns get array_vals =
+    { "#comment" = "Some comment" }
+    { "MODULES_LOADED_ON_BOOT"
+      { "1" = "ipv6" }
+      { "2" = "sunrpc" }
+    }
+    {  }
+    { "DEFAULT_APPEND"
+      { "1" = "showopts" }
+      { "2" = "noresume" }
+      { "3" = "console=tty0" }
+      { "4" = "console=ttyS0,115200n8" }
+      { "5" = "ro" }
+    }
+    {  }
+    { "LOADER_TYPE"
+      { "1" = "grub" }
+    }
+
+
+  test Shellvars_array.lns put "VAR=\"test1\t  \ntest2\"\n" after
+    set "VAR/01" "test3"
+    = "VAR=\"test1\t  \ntest2 test3\"\n"
+
+  test Shellvars_array.lns get "VAR=\"test1 test2\"\n" =
+    { "VAR" 
+      { "1" = "test1" }
+      { "2" = "test2" } 
+    }
+
+  test Shellvars_array.lns put array_vals after
+      set "FAILSAVE_APPEND/01" "console=ttyS0" ;
+      rm "LOADER_TYPE" ;
+      rm "MODULES_LOADED_ON_BOOT/1" ;
+      set "DEFAULT_APPEND/01" "teststring"
+  = "# Some comment
+MODULES_LOADED_ON_BOOT=\"sunrpc\"
+
+DEFAULT_APPEND=\"showopts noresume console=tty0 console=ttyS0,115200n8 ro teststring\"
+
+FAILSAVE_APPEND=\"console=ttyS0\"
+"
+
+(* Local Variables: *)
+(* mode: caml       *)
+(* End:             *)
-- 
1.7.0




More information about the augeas-devel mailing list