File 0002-main-script-be-prepared-for-some-of-the-sysconfig-fi.patch of Package firewalld-rpcbind-helper.12369

From 41d2658643ff7056dbf2d0b65277d4e3983bdd78 Mon Sep 17 00:00:00 2001
From: Matthias Gerstner <matthias.gerstner@suse.de>
Date: Fri, 11 May 2018 13:03:32 +0200
Subject: [PATCH 3/4] main script: be prepared for some of the sysconfig files
 not existing

---
 firewall-rpc-helper.py | 49 ++++++++++++++++++++++++++++++++++---------------
 1 file changed, 34 insertions(+), 15 deletions(-)

diff --git a/firewall-rpc-helper.py b/firewall-rpc-helper.py
index 990b2f3..3f6e596 100755
--- a/firewall-rpc-helper.py
+++ b/firewall-rpc-helper.py
@@ -29,6 +29,7 @@ import argparse
 import subprocess
 import random
 import textwrap
+import errno
 
 def error(*args, **kwargs):
 	kwargs["file"] = sys.stderr
@@ -89,6 +90,9 @@ class FirewallRPC(object):
 				)
 			self.m_rpcbind_services = list(self.m_rpcbind_services)
 
+		def isInstalled(self):
+			return os.path.exists(self.m_sysconfig_file)
+
 	def __init__(self):
 
 		self.m_used_ports = set()
@@ -490,7 +494,10 @@ applicable (currently only firewall-cmd calls)."""),
 		print('-' * len(pattern.m_label))
 		print()
 		print("Static port configuration file:",
-				pattern.m_sysconfig_file)
+				pattern.m_sysconfig_file, end = '')
+		if not pattern.isInstalled():
+			print(" (package not installed)", end = '')
+		print()
 		print()
 
 		table_rows = []
@@ -557,14 +564,21 @@ applicable (currently only firewall-cmd calls)."""),
 
 		print("Reading current configuration from {}.".format(cfg))
 		print()
-		with open(cfg, 'r') as cfg_fd:
-			for line in cfg_fd.readlines():
-				line = self.processCfgLine(
-					line,
-					item_handler =
-						self.processCfgItemForChange
-				)
-				lines.append(line)
+		try:
+			with open(cfg, 'r') as cfg_fd:
+				for line in cfg_fd.readlines():
+					line = self.processCfgLine(
+						line,
+						item_handler =
+							self.processCfgItemForChange
+					)
+					lines.append(line)
+		except OSError as e:
+			if e.errno == errno.ENOENT:
+				print("Error: The package necessary for this pattern does not seem to be installed")
+				sys.exit(1)
+			else:
+				raise
 
 		print("Writing updated configuration to {}.".format(cfg))
 		with open(cfg, 'w') as cfg_fd:
@@ -789,12 +803,17 @@ applicable (currently only firewall-cmd calls)."""),
 		self.m_static_ports = {}
 		cfg = pattern.m_sysconfig_file
 
-		with open(cfg, 'r') as cfg_fd:
-			for line in cfg_fd.readlines():
-				self.processCfgLine(
-					line,
-					self.processCfgItemForParsing
-				)
+		try:
+			with open(cfg, 'r') as cfg_fd:
+				for line in cfg_fd.readlines():
+					self.processCfgLine(
+						line,
+						self.processCfgItemForParsing
+					)
+		except OSError as e:
+			if e.errno == errno.ENOENT:
+				# probably not installed
+				pass
 
 	def processCfgItemForParsing(self, key, val):
 
-- 
2.16.1

openSUSE Build Service is sponsored by