Package backend :: Package satellite_tools :: Module syncCache
[hide private]
[frames] | no frames]

Source Code for Module backend.satellite_tools.syncCache

  1  # 
  2  # Copyright (c) 2008--2016 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15  # Mechanism to persistently cache sync info (mostly post-parsed(XML) 
 16  #    package objects). 
 17  # 
 18   
 19  # system imports: 
 20  import os 
 21   
 22  # rhn imports: 
 23  from spacewalk.common import rhnCache 
 24  from spacewalk.common.rhnConfig import CFG, initCFG 
 25  from spacewalk.common.rhnLib import hash_object_id 
 26   
 27  # NOTE: this is a python 2.2-ism 
 28  __all__ = [] 
 29   
 30   
31 -class BaseCache:
32 _compressed = 1 33
34 - def __init__(self):
35 # Kind of kludgy - this may have weird side-effects if called from 36 # within the server code 37 rhnCache.CACHEDIR = CFG.SYNC_CACHE_DIR
38
39 - def cache_get(self, object_id, timestamp=None):
40 # Get the key 41 key = self._get_key(object_id) 42 return rhnCache.get(key, modified=timestamp, raw=0, 43 compressed=self._compressed)
44
45 - def cache_set(self, object_id, value, timestamp=None):
46 # Get the key 47 key = self._get_key(object_id) 48 return rhnCache.set(key, value, modified=timestamp, raw=0, 49 compressed=self._compressed)
50
51 - def cache_has_key(self, object_id, timestamp=None):
52 # Get the key 53 key = self._get_key(object_id) 54 return rhnCache.has_key(key, modified=timestamp)
55
56 - def _get_key(self, object_id):
57 raise NotImplementedError()
58 59
60 -class ChannelCache(BaseCache):
61
62 - def _get_key(self, object_id):
63 return os.path.join("satsync", "channels", str(object_id))
64 65
66 -class BasePackageCache(BaseCache):
67 _subdir = "__unknown__" 68
69 - def _get_key(self, object_id):
70 hash_val = hash_object_id(object_id, 2) 71 return os.path.join("satsync", self._subdir, hash_val, str(object_id))
72 73
74 -class ShortPackageCache(BasePackageCache):
75 _subdir = "short-packages" 76 _compressed = 0
77 78
79 -class PackageCache(BasePackageCache):
80 _subdir = "packages"
81 82
83 -class SourcePackageCache(BasePackageCache):
84 _subdir = "source-packages"
85 86
87 -class ErratumCache(BaseCache):
88 _subdir = "errata" 89
90 - def _get_key(self, object_id):
91 hash_val = hash_object_id(object_id, 1) 92 return os.path.join("satsync", self._subdir, hash_val, str(object_id))
93 94
95 -class KickstartableTreesCache(BaseCache):
96 _subdir = "kickstartable-trees" 97
98 - def _get_key(self, object_id):
99 return os.path.normpath(os.path.join("satsync", self._subdir, 100 object_id))
101 102 if __name__ == '__main__': 103 initCFG("server.satellite") 104 c = PackageCache() 105 pid = 'package-12345' 106 c.cache_set(pid, {'a': 1, 'b': 2}) 107 print(c.cache_get(pid)) 108