[Pkg-libvirt-commits] [SCM] Libguestfs Debian packaging branch, experimental, updated. debian/1%1.21.40-1

Richard W.M. Jones rjones at redhat.com
Sat Jun 1 11:05:07 UTC 2013


The following commit has been merged in the experimental branch:
commit 0daa83cc9116a537cf154088a5a47c5c43ee6444
Author: Richard W.M. Jones <rjones at redhat.com>
Date:   Tue May 21 13:36:29 2013 +0100

    sysprep: Refactor code, introducing a function to replace a line in a file.

diff --git a/sysprep/sysprep_operation_hostname.ml b/sysprep/sysprep_operation_hostname.ml
index cbfecfd..83e197c 100644
--- a/sysprep/sysprep_operation_hostname.ml
+++ b/sysprep/sysprep_operation_hostname.ml
@@ -31,6 +31,24 @@ let hostname_perform g root =
   let distro = g#inspect_get_distro root in
   let major_version = g#inspect_get_major_version root in
 
+  (* Replace <key>=... entry in file.  The code assumes it's a small,
+   * plain text file.
+   *)
+  let replace_line_in_file filename key value =
+    let content =
+      if g#is_file filename then (
+        let lines = Array.to_list (g#read_lines filename) in
+        let lines = List.filter (
+          fun line -> not (string_prefix line (key ^ "="))
+        ) lines in
+        let lines = lines @ [sprintf "%s=%s" key value] in
+        String.concat "\n" lines ^ "\n"
+      ) else (
+        sprintf "%s=%s\n" key value
+      ) in
+    g#write filename content
+  in
+
   let update_etc_hostname () =
     g#write "/etc/hostname" !hostname;
     [ `Created_files ]
@@ -45,23 +63,7 @@ let hostname_perform g root =
   | "linux", ("debian"|"ubuntu"), _ -> update_etc_hostname ()
 
   | "linux", ("fedora"|"rhel"|"centos"|"scientificlinux"|"redhat-based"), _ ->
-    let filename = "/etc/sysconfig/network" in
-    if g#is_file filename then (
-      (* Replace HOSTNAME=... entry.  The code assumes it's a small,
-       * plain text file.
-       *)
-      let lines = Array.to_list (g#read_lines filename) in
-      let lines = List.filter (
-        fun line -> not (string_prefix line "HOSTNAME=")
-      ) lines in
-      let file =
-        String.concat "\n" lines ^
-          sprintf "\nHOSTNAME=%s\n" !hostname in
-      g#write filename file;
-    ) else (
-      let file = sprintf "HOSTNAME=%s\n" !hostname in
-      g#write filename file;
-    );
+    replace_line_in_file "/etc/sysconfig/network" "HOSTNAME" !hostname;
     [ `Created_files ]
 
   | "linux", ("opensuse"|"sles"|"suse-based"), _ ->

-- 
Libguestfs Debian packaging



More information about the Pkg-libvirt-commits mailing list