pgreco / rpms / ipa

Forked from forks/areguera/rpms/ipa 4 years ago
Clone
Blob Blame History Raw
From 5138ff563b1ac870ecc77485c200d839e17622a8 Mon Sep 17 00:00:00 2001
From: David Kupka <dkupka@redhat.com>
Date: Thu, 4 Aug 2016 16:07:08 +0200
Subject: [PATCH] schema cache: Do not read fingerprint and format from cache

Fingerprint can be obtained from schema filename of from ServerInfo
instance. Use FORMAT in path to avoid openening schema just to read its
format.

https://fedorahosted.org/freeipa/ticket/6048

Reviewed-By: Jan Cholasta <jcholast@redhat.com>
---
 ipaclient/remote_plugins/schema.py | 29 +++++------------------------
 1 file changed, 5 insertions(+), 24 deletions(-)

diff --git a/ipaclient/remote_plugins/schema.py b/ipaclient/remote_plugins/schema.py
index b49ccbb7f7905e2561598d66beca6d8b1d5ed48e..c62b74408b448faf794ed9e2b315b03fc1a5a315 100644
--- a/ipaclient/remote_plugins/schema.py
+++ b/ipaclient/remote_plugins/schema.py
@@ -19,6 +19,7 @@ from ipalib import errors, parameters, plugable
 from ipalib.frontend import Object
 from ipalib.output import Output
 from ipalib.parameters import DefaultFrom, Flag, Password, Str
+from ipapython.ipautil import fsdecode
 from ipapython.dn import DN
 from ipapython.dnsutil import DNSName
 from ipapython.ipa_log_manager import log_mgr
@@ -437,7 +438,7 @@ class Schema(object):
 
     """
     namespaces = {'classes', 'commands', 'topics'}
-    _DIR = os.path.join(USER_CACHE_PATH, 'ipa', 'schema')
+    _DIR = os.path.join(USER_CACHE_PATH, 'ipa', 'schema', FORMAT)
 
     def __init__(self, api, server_info, client):
         self._dict = {}
@@ -483,34 +484,14 @@ class Schema(object):
         path = os.path.join(self._DIR, filename)
         return _LockedZipFile(path, mode)
 
-    def _get_schema_fingerprint(self, schema):
-        try:
-            fmt = json.loads(schema.read('format'))
-        except KeyError:
-            fmt = '0'
-
-        if fmt != FORMAT:
-            raise RuntimeError('invalid format')
-
-        return json.loads(schema.read('fingerprint'))
-
     def _fetch(self, client):
         if not client.isconnected():
             client.connect(verbose=False)
 
-        fps = []
         try:
-            files = os.listdir(self._DIR)
+            fps = [fsdecode(f) for f in os.listdir(self._DIR)]
         except EnvironmentError:
-            pass
-        else:
-            for filename in files:
-                try:
-                    with self._open_schema(filename, 'r') as schema:
-                        fps.append(
-                            unicode(self._get_schema_fingerprint(schema)))
-                except Exception:
-                    continue
+            fps = []
 
         kwargs = {u'version': u'2.170'}
         if fps:
@@ -537,7 +518,7 @@ class Schema(object):
 
     def _read_schema(self):
         with self._open_schema(self._fingerprint, 'r') as schema:
-            self._dict['fingerprint'] = self._get_schema_fingerprint(schema)
+            self._dict['fingerprint'] = self._fingerprint
 
             for name in schema.namelist():
                 ns, _slash, key = name.partition('/')
-- 
2.7.4