From 85e39092632265d9071041a01ee8ca66a95e98c6 Mon Sep 17 00:00:00 2001
From: Abhishek Koneru <akoneru@redhat.com>
Date: Mon, 1 Jul 2013 17:44:46 -0400
Subject: [PATCH 89/89] Fixes for issues reported by pylint.

Fixes for issues in other files.
Ticket #316
---
 base/common/python/pki/upgrade.py          |   35 ++++++++--------
 base/server/python/pki/server/upgrade.py   |   20 ++++-----
 base/server/src/engine/pkihelper.py        |   62 +++++++++++++++-------------
 base/server/src/engine/pkimanifest.py      |   21 +++++-----
 base/server/src/pkispawn                   |   24 +++++------
 base/server/src/scriptlets/finalization.py |    5 ++-
 6 files changed, 84 insertions(+), 83 deletions(-)

diff --git a/base/common/python/pki/upgrade.py b/base/common/python/pki/upgrade.py
index e9e40b15cdfd8c3b8e9222b6f92e9ad58cdb06a4..2d74d1c2f36fde21f3f4c8f3a9f0a36d3b6f3b0b 100644
--- a/base/common/python/pki/upgrade.py
+++ b/base/common/python/pki/upgrade.py
@@ -45,7 +45,7 @@ class Version(object):
         if isinstance(obj, str):
 
             # parse <version>-<release>
-            pos = parts = obj.find('-')
+            pos = obj.find('-')
 
             if pos > 0:
                 self.version = obj[0:pos]
@@ -77,31 +77,28 @@ class Version(object):
     # release is ignored in comparisons
 
     def __eq__(self, other):
-        return self.major == other.major and \
-            self.minor == other.minor and \
-            self.patch == other.patch
-
+        return (self.major == other.major and
+            self.minor == other.minor and
+            self.patch == other.patch)
 
     def __lt__(self, other):
         if self.major < other.major:
             return True
 
-        if self.major == other.major and \
-            self.minor < other.minor:
+        if (self.major == other.major and
+            self.minor < other.minor):
             return True
 
-        if self.major == other.major and \
-            self.minor == other.minor and \
-            self.patch < other.patch:
+        if (self.major == other.major and
+            self.minor == other.minor and
+            self.patch < other.patch):
             return True
 
         return False
 
-
     def __repr__(self):
         return self.version
 
-
 class PKIUpgradeTracker(object):
 
     def __init__(self, name, filename,
@@ -121,14 +118,16 @@ class PKIUpgradeTracker(object):
 
     def remove(self):
 
-        if verbose: print 'Removing ' + self.name + ' tracker.'
+        if verbose: 
+            print 'Removing ' + self.name + ' tracker.'
 
         self.remove_version()
         self.remove_index()
 
     def set(self, version):
 
-        if verbose: print 'Setting ' + self.name + ' tracker to version ' + str(version) + '.'
+        if verbose: 
+            print 'Setting ' + self.name + ' tracker to version ' + str(version) + '.'
 
         self.set_version(version)
         self.remove_index()
@@ -325,7 +324,7 @@ class PKIUpgradeScriptlet(object):
         if os.path.exists(oldfiles):
 
             # restore all backed up files
-            for root, dirnames, filenames in os.walk(oldfiles):
+            for root, _, filenames in os.walk(oldfiles):  #unused item _ for dirnames
                 path = root[len(oldfiles):]
                 for filename in filenames:
                     source = root + '/' + filename
@@ -466,11 +465,11 @@ class PKIUpgrader(object):
                 continue
 
             # load scriptlet class
-            vars = {}
-            execfile(os.path.join(version_dir, filename), vars)
+            variables = {}
+            execfile(os.path.join(version_dir, filename), variables)
 
             # create scriptlet object
-            scriptlet = vars[classname]()
+            scriptlet = variables[classname]()
 
             scriptlet.upgrader = self
             scriptlet.version = version
diff --git a/base/server/python/pki/server/upgrade.py b/base/server/python/pki/server/upgrade.py
index 5de961df398f28d05e3f05d82c715afb6f6c2d02..b89cef607ef6bd0ca4bf64fbec18a78c8edc59e9 100644
--- a/base/server/python/pki/server/upgrade.py
+++ b/base/server/python/pki/server/upgrade.py
@@ -166,14 +166,14 @@ class PKIServerUpgrader(pki.upgrade.PKIUpgrader):
         if self.instanceName and self.instanceType:
             return [pki.server.PKIInstance(self.instanceName, self.instanceType)]
 
-        list = []
+        instance_list = []
 
         if not self.instanceType or self.instanceType >= 10:
             if os.path.exists(os.path.join(pki.server.REGISTRY_DIR, 'tomcat')):
                 for instanceName in os.listdir(pki.server.INSTANCE_BASE_DIR):
                     if not self.instanceName or \
                         self.instanceName == instanceName:
-                        list.append(pki.server.PKIInstance(instanceName))
+                        instance_list.append(pki.server.PKIInstance(instanceName))
 
         if not self.instanceType or self.instanceType == 9:
             for s in pki.server.SUBSYSTEM_TYPES:
@@ -182,11 +182,11 @@ class PKIServerUpgrader(pki.upgrade.PKIUpgrader):
                         os.listdir(os.path.join(pki.server.REGISTRY_DIR, s)):
                         if not self.instanceName or \
                             self.instanceName == instanceName:
-                            list.append(pki.server.PKIInstance(instanceName, 9))
+                            instance_list.append(pki.server.PKIInstance(instanceName, 9))
 
-        list.sort()
+        instance_list.sort()
 
-        return list
+        return instance_list
 
 
     def subsystems(self, instance):
@@ -194,14 +194,14 @@ class PKIServerUpgrader(pki.upgrade.PKIUpgrader):
         if self.subsystemName:
             return [pki.server.PKISubsystem(instance, self.subsystemName)]
 
-        list = []
+        subsystem_list = []
 
         if instance.type >= 10:
             registry_dir = os.path.join(pki.server.REGISTRY_DIR, 'tomcat',
                 instance.name)
             for subsystemName in os.listdir(registry_dir):
                 if subsystemName in pki.server.SUBSYSTEM_TYPES:
-                    list.append(pki.server.PKISubsystem(instance, subsystemName))
+                    subsystem_list.append(pki.server.PKISubsystem(instance, subsystemName))
         else:
             for subsystemName in pki.server.SUBSYSTEM_TYPES:
                 registry_dir = os.path.join(
@@ -209,11 +209,11 @@ class PKIServerUpgrader(pki.upgrade.PKIUpgrader):
                     subsystemName,
                     instance.name)
                 if os.path.exists(registry_dir):
-                    list.append(pki.server.PKISubsystem(instance, subsystemName))
+                    subsystem_list.append(pki.server.PKISubsystem(instance, subsystemName))
 
-        list.sort()
+        subsystem_list.sort()
 
-        return list
+        return subsystem_list
 
     def get_tracker(self, instance, subsystem=None):
 
diff --git a/base/server/src/engine/pkihelper.py b/base/server/src/engine/pkihelper.py
index 18b7f08fb8872d4b31d3195832aad35f9ec74497..563c925d50596833e904de393fcecd981971550c 100644
--- a/base/server/src/engine/pkihelper.py
+++ b/base/server/src/engine/pkihelper.py
@@ -1079,6 +1079,7 @@ class Directory:
     def __init__(self, deployer):
         self.master_dict = deployer.master_dict
         self.identity = deployer.identity
+        self.manifest_db = deployer.manifest_db
 
     def create(self, name, uid=None, gid=None,
                perms=config.PKI_DEPLOYMENT_DEFAULT_DIR_PERMISSIONS,
@@ -1103,7 +1104,7 @@ class Directory:
                                      extra=config.PKI_INDENTATION_LEVEL_3)
                 os.chown(name, uid, gid)
                 # Store record in installation manifest
-                record = manifest.record()
+                record = manifest.Record()
                 record.name = name
                 record.type = manifest.RECORD_TYPE_DIRECTORY
                 record.user = self.master_dict['pki_user']
@@ -1112,7 +1113,7 @@ class Directory:
                 record.gid = gid
                 record.permissions = perms
                 record.acls = acls
-                manifest.database.append(record)
+                self.manifest_db.append(record)
             elif not os.path.isdir(name):
                 config.pki_log.error(
                     log.PKI_DIRECTORY_ALREADY_EXISTS_NOT_A_DIRECTORY_1, name,
@@ -1161,7 +1162,7 @@ class Directory:
                 os.chown(name, uid, gid)
                 # Store record in installation manifest
                 if not silent:
-                    record = manifest.record()
+                    record = manifest.Record()
                     record.name = name
                     record.type = manifest.RECORD_TYPE_DIRECTORY
                     record.user = self.master_dict['pki_user']
@@ -1170,7 +1171,7 @@ class Directory:
                     record.gid = gid
                     record.permissions = perms
                     record.acls = acls
-                    manifest.database.append(record)
+                    self.manifest_db.append(record)
             else:
                 config.pki_log.error(
                     log.PKI_DIRECTORY_MISSING_OR_NOT_A_DIRECTORY_1, name,
@@ -1276,7 +1277,7 @@ class Directory:
                                     extra=config.PKI_INDENTATION_LEVEL_3)
                                 os.chown(temp_file, uid, gid)
                                 # Store record in installation manifest
-                                record = manifest.record()
+                                record = manifest.Record()
                                 record.name = name
                                 record.type = manifest.RECORD_TYPE_FILE
                                 record.user = self.master_dict['pki_user']
@@ -1285,7 +1286,7 @@ class Directory:
                                 record.gid = gid
                                 record.permissions = file_perms
                                 record.acls = file_acls
-                                manifest.database.append(record)
+                                self.manifest_db.append(record)
                             else:
                                 symlink = entity
                                 config.pki_log.debug(
@@ -1302,7 +1303,7 @@ class Directory:
                                     extra=config.PKI_INDENTATION_LEVEL_3)
                                 os.lchown(symlink, uid, gid)
                                 # Store record in installation manifest
-                                record = manifest.record()
+                                record = manifest.Record()
                                 record.name = name
                                 record.type = manifest.RECORD_TYPE_SYMLINK
                                 record.user = self.master_dict['pki_user']
@@ -1311,7 +1312,7 @@ class Directory:
                                 record.gid = gid
                                 record.permissions = symlink_perms
                                 record.acls = symlink_acls
-                                manifest.database.append(record)
+                                self.manifest_db.append(record)
                         for name in dirs:
                             temp_dir = os.path.join(root, name)
                             config.pki_log.debug(
@@ -1328,7 +1329,7 @@ class Directory:
                                 extra=config.PKI_INDENTATION_LEVEL_3)
                             os.chown(temp_dir, uid, gid)
                             # Store record in installation manifest
-                            record = manifest.record()
+                            record = manifest.Record()
                             record.name = name
                             record.type = manifest.RECORD_TYPE_DIRECTORY
                             record.user = self.master_dict['pki_user']
@@ -1337,7 +1338,7 @@ class Directory:
                             record.gid = gid
                             record.permissions = dir_perms
                             record.acls = dir_acls
-                            manifest.database.append(record)
+                            self.manifest_db.append(record)
                 else:
                     config.pki_log.debug(
                         log.PKIHELPER_IS_A_DIRECTORY_1, name,
@@ -1354,7 +1355,7 @@ class Directory:
                                          extra=config.PKI_INDENTATION_LEVEL_3)
                     os.chown(name, uid, gid)
                     # Store record in installation manifest
-                    record = manifest.record()
+                    record = manifest.Record()
                     record.name = name
                     record.type = manifest.RECORD_TYPE_DIRECTORY
                     record.user = self.master_dict['pki_user']
@@ -1363,7 +1364,7 @@ class Directory:
                     record.gid = gid
                     record.permissions = dir_perms
                     record.acls = dir_acls
-                    manifest.database.append(record)
+                    self.manifest_db.append(record)
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
@@ -1437,6 +1438,7 @@ class File:
         self.master_dict = deployer.master_dict
         self.slots = deployer.slots
         self.identity = deployer.identity
+        self.manifest_db = deployer.manifest_db
 
     def create(self, name, uid=None, gid=None,
                perms=config.PKI_DEPLOYMENT_DEFAULT_FILE_PERMISSIONS,
@@ -1461,7 +1463,7 @@ class File:
                                      extra=config.PKI_INDENTATION_LEVEL_3)
                 os.chown(name, uid, gid)
                 # Store record in installation manifest
-                record = manifest.record()
+                record = manifest.Record()
                 record.name = name
                 record.type = manifest.RECORD_TYPE_FILE
                 record.user = self.master_dict['pki_user']
@@ -1470,7 +1472,7 @@ class File:
                 record.gid = gid
                 record.permissions = perms
                 record.acls = acls
-                manifest.database.append(record)
+                self.manifest_db.append(record)
             elif not os.path.isfile(name):
                 config.pki_log.error(
                     log.PKI_FILE_ALREADY_EXISTS_NOT_A_FILE_1, name,
@@ -1519,7 +1521,7 @@ class File:
                 os.chown(name, uid, gid)
                 # Store record in installation manifest
                 if not silent:
-                    record = manifest.record()
+                    record = manifest.Record()
                     record.name = name
                     record.type = manifest.RECORD_TYPE_FILE
                     record.user = self.master_dict['pki_user']
@@ -1528,7 +1530,7 @@ class File:
                     record.gid = gid
                     record.permissions = perms
                     record.acls = acls
-                    manifest.database.append(record)
+                    self.manifest_db.append(record)
             else:
                 config.pki_log.error(
                     log.PKI_FILE_MISSING_OR_NOT_A_FILE_1, name,
@@ -1608,7 +1610,7 @@ class File:
                                      extra=config.PKI_INDENTATION_LEVEL_3)
                 os.chown(new_name, uid, gid)
                 # Store record in installation manifest
-                record = manifest.record()
+                record = manifest.Record()
                 record.name = new_name
                 record.type = manifest.RECORD_TYPE_FILE
                 record.user = self.master_dict['pki_user']
@@ -1617,7 +1619,7 @@ class File:
                 record.gid = gid
                 record.permissions = perms
                 record.acls = acls
-                manifest.database.append(record)
+                self.manifest_db.append(record)
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
@@ -1668,7 +1670,7 @@ class File:
                                  extra=config.PKI_INDENTATION_LEVEL_3)
             os.chown(name, uid, gid)
             # Store record in installation manifest
-            record = manifest.record()
+            record = manifest.Record()
             record.name = name
             record.type = manifest.RECORD_TYPE_FILE
             record.user = self.master_dict['pki_user']
@@ -1677,7 +1679,7 @@ class File:
             record.gid = gid
             record.permissions = perms
             record.acls = acls
-            manifest.database.append(record)
+            self.manifest_db.append(record)
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
@@ -1737,7 +1739,7 @@ class File:
                                      extra=config.PKI_INDENTATION_LEVEL_3)
                 os.chown(new_name, uid, gid)
                 # Store record in installation manifest
-                record = manifest.record()
+                record = manifest.Record()
                 record.name = new_name
                 record.type = manifest.RECORD_TYPE_FILE
                 record.user = self.master_dict['pki_user']
@@ -1746,7 +1748,7 @@ class File:
                 record.gid = gid
                 record.permissions = perms
                 record.acls = acls
-                manifest.database.append(record)
+                self.manifest_db.append(record)
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
@@ -1787,7 +1789,7 @@ class File:
                                      extra=config.PKI_INDENTATION_LEVEL_3)
                 os.chown(name, uid, gid)
                 # Store record in installation manifest
-                record = manifest.record()
+                record = manifest.Record()
                 record.name = name
                 record.type = manifest.RECORD_TYPE_FILE
                 record.user = self.master_dict['pki_user']
@@ -1796,7 +1798,7 @@ class File:
                 record.gid = gid
                 record.permissions = perms
                 record.acls = acls
-                manifest.database.append(record)
+                self.manifest_db.append(record)
             elif not os.path.isfile(name):
                 config.pki_log.error(
                     log.PKI_FILE_ALREADY_EXISTS_NOT_A_FILE_1, name,
@@ -1819,6 +1821,7 @@ class Symlink:
     def __init__(self, deployer):
         self.master_dict = deployer.master_dict
         self.identity = deployer.identity
+        self.manifest_db = deployer.manifest_db
 
     def create(self, name, link, uid=None, gid=None,
                acls=None, allow_dangling_symlink=False, critical_failure=True):
@@ -1847,7 +1850,7 @@ class Symlink:
                                      extra=config.PKI_INDENTATION_LEVEL_3)
                 os.lchown(link, uid, gid)
                 # Store record in installation manifest
-                record = manifest.record()
+                record = manifest.Record()
                 record.name = link
                 record.type = manifest.RECORD_TYPE_SYMLINK
                 record.user = self.master_dict['pki_user']
@@ -1857,7 +1860,7 @@ class Symlink:
                 record.permissions = \
                     config.PKI_DEPLOYMENT_DEFAULT_SYMLINK_PERMISSIONS
                 record.acls = acls
-                manifest.database.append(record)
+                self.manifest_db.append(record)
             elif not os.path.islink(link):
                 config.pki_log.error(
                     log.PKI_SYMLINK_ALREADY_EXISTS_NOT_A_SYMLINK_1, link,
@@ -1903,7 +1906,7 @@ class Symlink:
                 os.lchown(link, uid, gid)
                 # Store record in installation manifest
                 if not silent:
-                    record = manifest.record()
+                    record = manifest.Record()
                     record.name = link
                     record.type = manifest.RECORD_TYPE_SYMLINK
                     record.user = self.master_dict['pki_user']
@@ -1913,7 +1916,7 @@ class Symlink:
                     record.permissions = \
                         config.PKI_DEPLOYMENT_DEFAULT_SYMLINK_PERMISSIONS
                     record.acls = acls
-                    manifest.database.append(record)
+                    self.manifest_db.append(record)
             else:
                 config.pki_log.error(
                     log.PKI_SYMLINK_MISSING_OR_NOT_A_SYMLINK_1, link,
@@ -3497,7 +3500,8 @@ class PKIDeployer:
         # Global dictionary variables
         self.master_dict = pki_master_dict
         self.slots = pki_slots_dict
-
+        self.manifest_db = []
+        
         # Utility objects
         self.identity = Identity(self)
         self.namespace = Namespace(self)
diff --git a/base/server/src/engine/pkimanifest.py b/base/server/src/engine/pkimanifest.py
index aaabbf5fd3324555b38c1beb97657b33584f88ce..b48bfb8c426d760e95ea73cd4d04bb25d3525305 100644
--- a/base/server/src/engine/pkimanifest.py
+++ b/base/server/src/engine/pkimanifest.py
@@ -20,10 +20,7 @@
 #
 
 # System Imports
-from collections import namedtuple
 import csv
-import sys
-
 
 # PKI Deployment Imports
 import pkiconfig as config
@@ -37,7 +34,7 @@ RECORD_TYPE_SYMLINK = "symlink"
 
 
 # PKI Deployment Manifest Record Class
-class record(object):
+class Record(object):
     __slots__ = "name", \
                "type", \
                "user", \
@@ -46,6 +43,9 @@ class record(object):
                "gid", \
                "permissions", \
                "acls",
+               
+    def __init__(self):
+        pass
 
     def items(self):
         "dict style items"
@@ -64,9 +64,11 @@ class record(object):
 
 
 # PKI Deployment Manifest File Class
-class file:
-    global database
-    filename = None
+class File:
+    
+    def __init__(self, database):
+        self.filename = None
+        self.database = database
 
     def register(self, name):
         self.filename = name
@@ -75,7 +77,7 @@ class file:
         try:
             with open(self.filename, "wt") as fd:
                 c = csv.writer(fd)
-                for record in database:
+                for record in self.database:
                     c.writerow(tuple(record))
         except IOError as exc:
             config.pki_log.error(log.PKI_IOERROR_1, exc,
@@ -93,6 +95,3 @@ class file:
                                  extra=config.PKI_INDENTATION_LEVEL_1)
             raise
 
-# PKI Deployment Global Named Tuples
-database = []
-file = file()
diff --git a/base/server/src/pkispawn b/base/server/src/pkispawn
index 75a04b4a6e498ade6910b9487403ec1fbd95811a..a47f8f199c9ea759ff72cccfdb63f44580cf5bd1 100755
--- a/base/server/src/pkispawn
+++ b/base/server/src/pkispawn
@@ -28,16 +28,13 @@ if not hasattr(sys, "hexversion") or sys.hexversion < 0x020700f0:
     print "Please upgrade to at least Python 2.7.0."
     sys.exit(1)
 try:
-    import argparse
     import ldap
-    import logging
     import os
     import requests
     import socket
     import struct
     import subprocess
     import time
-    import urllib2
     import traceback
     from time import strftime as date
     from pki.deployment import pkiconfig as config
@@ -373,7 +370,7 @@ def main(argv):
         instance = scriptlet.PkiScriptlet()
         try:
             rv = instance.spawn(deployer)
-        except:
+        except Exception:
             log_error_details()
             print
             print "Installation failed."
@@ -412,15 +409,16 @@ def print_install_information(pki_master_dict):
     print log.PKI_SPAWN_INFORMATION_FOOTER
 
 def log_error_details():
-        type, value, stacktrace = sys.exc_info()
-        config.pki_log.debug("Error Type: " + type.__name__, extra=config.PKI_INDENTATION_LEVEL_2)
-        config.pki_log.debug("Error Message: " + str(value), extra=config.PKI_INDENTATION_LEVEL_2)
-        stacktrace_list = traceback.format_list(traceback.extract_tb(stacktrace))
-        stacktrace = ""
-        for l in stacktrace_list:
-            stacktrace += l
-        config.pki_log.debug(stacktrace, extra=config.PKI_INDENTATION_LEVEL_2)
-        sys.exc_clear()
+    
+    type, value, stacktrace = sys.exc_info()
+    config.pki_log.debug("Error Type: " + type.__name__, extra=config.PKI_INDENTATION_LEVEL_2)
+    config.pki_log.debug("Error Message: " + str(value), extra=config.PKI_INDENTATION_LEVEL_2)
+    stacktrace_list = traceback.format_list(traceback.extract_tb(stacktrace))
+    stacktrace = ""
+    for l in stacktrace_list:
+        stacktrace += l
+    config.pki_log.debug(stacktrace, extra=config.PKI_INDENTATION_LEVEL_2)
+    sys.exc_clear()
 
 # PKI Deployment Entry Point
 if __name__ == "__main__":
diff --git a/base/server/src/scriptlets/finalization.py b/base/server/src/scriptlets/finalization.py
index 45b4a3ceb888a4a01822dc3f4c50234ae985cb41..529f9d45d6f698a7469b6c7a344e1bd0a630c54c 100644
--- a/base/server/src/scriptlets/finalization.py
+++ b/base/server/src/scriptlets/finalization.py
@@ -53,8 +53,9 @@ class PkiScriptlet(pkiscriptlet.AbstractBasePkiScriptlet):
                             extra=config.PKI_INDENTATION_LEVEL_2)
         # for record in manifest.database:
         #     print tuple(record)
-        manifest.file.register(deployer.master_dict['pki_manifest'])
-        manifest.file.write()
+        manifest_file = manifest.File(deployer.manifest_db)
+        manifest_file.register(deployer.master_dict['pki_manifest'])
+        manifest_file.write()
         deployer.file.modify(deployer.master_dict['pki_manifest'], silent=True)
 
         # Also, for debugging/auditing purposes, save a timestamped copy of
-- 
1.7.10.2

