From a3d13828cdc39da95cad6ca192799ef5a49c4625 Mon Sep 17 00:00:00 2001
From: Christian Heimes <cheimes@redhat.com>
Date: Mon, 20 Jul 2015 10:42:31 +0200
Subject: [PATCH 17/18] Simplify exception handling in pkihelper

Several methods except OSError before they except shutil.Error. In
Python 3 the second except clause will be ignored because in Python 3
shutil.Error is a subclass of OSError.

The body of the except clauses only differs in the logging message. A
single except clause with an isinstance() check has the same effect.
---
 .../python/pki/server/deployment/pkihelper.py      | 56 ++++++++++------------
 1 file changed, 24 insertions(+), 32 deletions(-)

diff --git a/base/server/python/pki/server/deployment/pkihelper.py b/base/server/python/pki/server/deployment/pkihelper.py
index 5bc4ffab814891aadf0508d0ae20bb8cc315bc90..bdda989cb0f46890f5222357a5b7cb88e2d696a3 100644
--- a/base/server/python/pki/server/deployment/pkihelper.py
+++ b/base/server/python/pki/server/deployment/pkihelper.py
@@ -1462,14 +1462,12 @@ class Directory:
                               dir_perms, file_perms, symlink_perms,
                               dir_acls, file_acls, symlink_acls,
                               recursive_flag, critical_failure)
-        except OSError as exc:
-            config.pki_log.error(log.PKI_OSERROR_1, exc,
-                                 extra=config.PKI_INDENTATION_LEVEL_2)
-            if critical_failure:
-                raise
-        except shutil.Error as exc:
-            config.pki_log.error(log.PKI_SHUTIL_ERROR_1, exc,
-                                 extra=config.PKI_INDENTATION_LEVEL_2)
+        except (shutil.Error, OSError) as exc:
+            if isinstance(exc, shutil.Error):
+                msg = log.PKI_SHUTIL_ERROR_1
+            else:
+                msg = log.PKI_OSERROR_1
+            config.pki_log.error(msg, exc, extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure:
                 raise
         return
@@ -1667,14 +1665,12 @@ class File:
                 record.permissions = perms
                 record.acls = acls
                 self.manifest_db.append(record)
-        except OSError as exc:
-            config.pki_log.error(log.PKI_OSERROR_1, exc,
-                                 extra=config.PKI_INDENTATION_LEVEL_2)
-            if critical_failure:
-                raise
-        except shutil.Error as exc:
-            config.pki_log.error(log.PKI_SHUTIL_ERROR_1, exc,
-                                 extra=config.PKI_INDENTATION_LEVEL_2)
+        except (shutil.Error, OSError) as exc:
+            if isinstance(exc, shutil.Error):
+                msg = log.PKI_SHUTIL_ERROR_1
+            else:
+                msg = log.PKI_OSERROR_1
+            config.pki_log.error(msg, exc, extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure:
                 raise
         return
@@ -1727,14 +1723,12 @@ class File:
             record.permissions = perms
             record.acls = acls
             self.manifest_db.append(record)
-        except OSError as exc:
-            config.pki_log.error(log.PKI_OSERROR_1, exc,
-                                 extra=config.PKI_INDENTATION_LEVEL_2)
-            if critical_failure:
-                raise
-        except shutil.Error as exc:
-            config.pki_log.error(log.PKI_SHUTIL_ERROR_1, exc,
-                                 extra=config.PKI_INDENTATION_LEVEL_2)
+        except (shutil.Error, OSError) as exc:
+            if isinstance(exc, shutil.Error):
+                msg = log.PKI_SHUTIL_ERROR_1
+            else:
+                msg = log.PKI_OSERROR_1
+            config.pki_log.error(msg, exc, extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure:
                 raise
         return
@@ -1799,14 +1793,12 @@ class File:
                 record.permissions = perms
                 record.acls = acls
                 self.manifest_db.append(record)
-        except OSError as exc:
-            config.pki_log.error(log.PKI_OSERROR_1, exc,
-                                 extra=config.PKI_INDENTATION_LEVEL_2)
-            if critical_failure:
-                raise
-        except shutil.Error as exc:
-            config.pki_log.error(log.PKI_SHUTIL_ERROR_1, exc,
-                                 extra=config.PKI_INDENTATION_LEVEL_2)
+        except (shutil.Error, OSError) as exc:
+            if isinstance(exc, shutil.Error):
+                msg = log.PKI_SHUTIL_ERROR_1
+            else:
+                msg = log.PKI_OSERROR_1
+            config.pki_log.error(msg, exc, extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure:
                 raise
         return
-- 
2.4.3

