Use ReadFile.splitlines() rather than readlines
authorGuido Trotter <ultrotter@google.com>
Fri, 26 Jun 2009 11:17:10 +0000 (12:17 +0100)
committerGuido Trotter <ultrotter@google.com>
Fri, 26 Jun 2009 13:45:39 +0000 (14:45 +0100)
A few places in the code open a file "manually" rather than using our
wrapper function, because they need an array with the lines. Combining
the result of utils.ReadFile with splitlines() we get rid of the
exceptions.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

lib/backend.py
lib/hypervisor/hv_base.py
tools/lvmstrap

index 5bf45a9..b19b401 100644 (file)
@@ -1490,11 +1490,7 @@ def _OSOndiskAPIVersion(name, os_dir):
                    " a regular file" % os_dir)
 
   try:
-    f = open(api_file)
-    try:
-      api_versions = f.readlines()
-    finally:
-      f.close()
+    api_versions = utils.ReadFile(api_file).splitlines()
   except EnvironmentError, err:
     return False, ("Error while reading the API version file at %s: %s" %
                    (api_file, _ErrnoOrStr(err)))
index 2370887..cdb7702 100644 (file)
@@ -315,11 +315,7 @@ class BaseHypervisor(object):
 
     """
     try:
-      fh = file("/proc/meminfo")
-      try:
-        data = fh.readlines()
-      finally:
-        fh.close()
+      data = utils.ReadFile("/proc/meminfo").splitlines()
     except EnvironmentError, err:
       raise errors.HypervisorError("Failed to list node info: %s" % (err,))
 
index 8af2f61..0a17a5f 100755 (executable)
@@ -44,7 +44,7 @@ import sys
 import optparse
 import time
 
-from ganeti.utils import RunCmd
+from ganeti.utils import RunCmd, ReadFile
 from ganeti import constants
 
 USAGE = ("\tlvmstrap diskinfo\n"
@@ -424,9 +424,7 @@ def GetMountInfo():
    a mountpoint: device number dictionary
   """
 
-  f = open("/proc/mounts", "r")
-  mountlines = f.readlines()
-  f.close()
+  mountlines = ReadFile("/proc/mounts").splitlines()
   mounts = {}
   for line in mountlines:
     device, mountpoint, fstype, rest = line.split(None, 3)