]> rtime.felk.cvut.cz Git - jailhouse.git/commitdiff
tools: config-create: Simplify optional file handling and generator mode
authorJan Kiszka <jan.kiszka@siemens.com>
Sun, 20 Jul 2014 22:13:15 +0000 (00:13 +0200)
committerJan Kiszka <jan.kiszka@siemens.com>
Mon, 28 Jul 2014 04:35:56 +0000 (06:35 +0200)
Stop passing exceptions from input_open to its callers: An optional file
can be returned as empty (derived from /dev/null), same on failures
during collector creation. The typical mode for input_open is 'r', so we
can save passing this explicitly in most cases. Same for optional which
is generally False.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
tools/jailhouse-config-create

index 0f51758f5f8de9c35019016f77b9fcee049af181..20e43c785154d7d59f47f8e723af1c6316cb392e 100755 (executable)
@@ -136,7 +136,7 @@ class MemRegion:
 
 def parse_iomem():
     regions = []
-    f, e = input_open('/proc/iomem', False, 'r')
+    f = input_open('/proc/iomem')
     for line in f:
         r = MemRegion.parse_iomem_line(line)
         ## XXX what else to ignore??
@@ -188,22 +188,19 @@ def kmg_multiply_str(str):
     return 0
 
 
-def input_open(name, optional, *args):
+def input_open(name, mode='r', optional=False):
     inputs['files_opt' if optional else 'files'].add(name)
     try:
-        f = open(options.root + name, *args)
+        f = open(options.root + name, mode)
     except Exception as e:
-        if optional:
-            return None, e
+        if optional or options.generate_collector:
+            return open("/dev/null", mode)
         raise e
-    return f, None
+    return f
 
 
 def input_readline(name, optional=False):
-    f, e = input_open(name, optional, 'r')
-    if f is None and optional:
-        return ''
-
+    f = input_open(name, optional=optional)
     line = f.readline()
     f.close()
     return line
@@ -259,16 +256,11 @@ def parse_dmar_devscope(f):
 # parsing of DMAR ACPI Table
 # see Intel VT-d Spec chapter 8
 def parse_dmar():
-    f, e = input_open('/sys/firmware/acpi/tables/DMAR', True, 'rb')
-    if not f:
-        if options.generate_collector:
-            return 0, []
-        if e:
-            raise e
-        raise RuntimeError('could not find DMAR ACPI table')
-
+    f = input_open('/sys/firmware/acpi/tables/DMAR', 'rb')
     signature = f.read(4)
     if signature != b'DMAR':
+        if options.generate_collector:
+            return 0, []
         raise RuntimeError('incorrect input file format %s' % signature)
     (length,) = struct.unpack('<I', f.read(4))
     f.seek(48)
@@ -329,7 +321,7 @@ def parse_dmar():
 
 def parse_ioports():
     pm_timer_base = None
-    f, e = input_open('/proc/ioports', False, 'r')
+    f = input_open('/proc/ioports')
     for line in f:
         if line.endswith('ACPI PM_TMR\n'):
             pm_timer_base = int(line.split('-')[0], 16)