python3 - 2to3 + miscell obvious tweaks
[sfa.git] / sfa / importer / __init__.py
index 65e57ec..8981793 100644 (file)
@@ -1,21 +1,27 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
+
+# pylint: disable=c0111, w1201, w0622
 
-import sys
 from datetime import datetime
 
 from sfa.util.xrn import get_authority, hrn_to_urn
 from sfa.generic import Generic
 from sfa.util.config import Config
-from sfa.util.sfalogging import _SfaLogger
+from sfa.util.sfalogging import init_logger, logger as default_logger
 from sfa.trust.hierarchy import Hierarchy
-#from sfa.trust.trustedroots import TrustedRoots
+# from sfa.trust.trustedroots import TrustedRoots
 from sfa.trust.gid import create_uuid
 # using global alchemy.session() here is fine
 # as importer is on standalone one-shot process
 from sfa.storage.alchemy import global_dbsession
 from sfa.storage.model import RegRecord, RegAuthority, RegUser
-from sfa.trust.certificate import convert_public_key, Keypair
 
+# note on logging
+# it is doubtful that anyone ever used the ability to
+# pass a logger to this class, and that can probably be
+# thrown away.
+# However a quick attempt showed that it seems to
+# also require changes in the Generic layer
 
 class Importer:
 
@@ -25,24 +31,28 @@ class Importer:
             self.auth_hierarchy = auth_hierarchy
         else:
             self.auth_hierarchy = Hierarchy()
-        if logger is not None:
-            self.logger = logger
+        if logger is None:
+            # redirect in sfa-import.log
+            self.logger = default_logger
+            init_logger('import')
         else:
-            self.logger = _SfaLogger(
-                logfile='/var/log/sfa_import.log', loggername='importlog')
-            self.logger.setLevelFromOptVerbose(self.config.SFA_API_LOGLEVEL)
+            self.logger = logger
+        self.logger.setLevelFromOptVerbose(self.config.SFA_API_LOGLEVEL)
 # ugly side effect so that other modules get it right
         import sfa.util.sfalogging
         sfa.util.sfalogging.logger = logger
 #        self.TrustedRoots = TrustedRoots(self.config.get_trustedroots_dir())
 
     # check before creating a RegRecord entry as we run this over and over
-    def record_exists(self, type, hrn):
-        return global_dbsession.query(RegRecord).filter_by(hrn=hrn, type=type).count() != 0
+    @staticmethod
+    def record_exists(type, hrn):
+        return (global_dbsession.query(RegRecord)
+                .filter_by(hrn=hrn, type=type).count() != 0)
 
     def create_top_level_auth_records(self, hrn):
         """
-        Create top level db records (includes root and sub authorities (local/remote)
+        Create top level db records
+        includes root and sub authorities (local/remote)
         """
         # make sure parent exists
         parent_hrn = get_authority(hrn)
@@ -64,27 +74,6 @@ class Importer:
             self.logger.info(
                 "SfaImporter: imported authority (parent) %s " % auth_record)
 
-    def create_sm_client_record(self):
-        """
-        Create a user record for the Slicemanager service.
-        """
-        hrn = self.interface_hrn + '.slicemanager'
-        urn = hrn_to_urn(hrn, 'user')
-        if not self.auth_hierarchy.auth_exists(urn):
-            self.logger.info("SfaImporter: creating Slice Manager user")
-            self.auth_hierarchy.create_auth(urn)
-
-        if self.record_exists('user', hrn):
-            return
-        auth_info = self.auth_hierarchy.get_auth_info(hrn)
-        user_record = RegUser(hrn=hrn, gid=auth_info.get_gid_object(),
-                              authority=get_authority(hrn))
-        user_record.just_created()
-        global_dbsession.add(user_record)
-        global_dbsession.commit()
-        self.logger.info(
-            "SfaImporter: importing user (slicemanager) %s " % user_record)
-
     def create_interface_records(self):
         """
         Create a record for each SFA interface
@@ -126,8 +115,9 @@ class Importer:
         if importer_class:
             begin_time = datetime.utcnow()
             self.logger.info(30 * '=')
-            self.logger.info("Starting import on %s, using class %s from flavour %s" %
-                             (begin_time, importer_class.__name__, generic.flavour))
+            self.logger.info(
+                "Starting import on %s, using class %s from flavour %s" %
+                (begin_time, importer_class.__name__, generic.flavour))
             testbed_importer = importer_class(auth_hierarchy, self.logger)
             if testbed_importer:
                 testbed_importer.add_options(options)