Beispiel #1
0
 def __init__(self, name, create=True, timeout=None, version=None):
   super(Cache, self).__init__(
       _Table, name, create=create, timeout=timeout, version=version)
   self._persistent = False
   # Check if the db file exists and is an sqlite3 db.
   # Surprise, we have to do the heavy lifting.
   # That stops here.
   try:
     with open(name, 'r') as f:
       actual_magic = f.read(len(self._EXPECTED_MAGIC))
       if actual_magic != self._EXPECTED_MAGIC:
         raise exceptions.CacheInvalid(
             '[{}] is not a persistent cache.'.format(self.name))
     self._persistent = True
   except IOError as e:
     if e.errno in (errno.EISDIR, errno.EACCES):
       raise exceptions.CacheInvalid(
           '[{}] is not a persistent cache.'.format(self.name))
     elif e.errno != errno.ENOENT:
       raise
     elif not create:
       raise exceptions.CacheNotFound(
           'Persistent cache [{}] not found.'.format(self.name))
   self._db = sqlite3.connect(name)
   self.cursor = self._db.cursor()
   self._restricted = set(['__lock__'])
   self._tables = {}
   self._metadata = None
   self._start = persistent_cache_base.Now()
   try:
     self.InitializeMetadata()
   except exceptions.Error:
     # Make sure we clean up any dangling resources.
     self.Close(commit=False)
     raise
Beispiel #2
0
 def __init__(self, name, create=True, timeout=None, version=None):
     super(Cache, self).__init__(_Table,
                                 name,
                                 create=create,
                                 timeout=timeout,
                                 version=version)
     lock_name = '__lock__'
     self._restricted = set([lock_name])
     self._tables = {}
     self._metadata = None
     self._start = persistent_cache_base.Now()
     self._lock_path = os.path.join(self.name, lock_name)
     self._lock = None
     self._persistent = False
     if not os.path.exists(self.name):
         if not create:
             raise exceptions.CacheNotFound(
                 'Persistent cache [{}] not found.'.format(self.name))
     elif not os.path.exists(self._lock_path):
         raise exceptions.CacheInvalid(
             '[{}] is not a persistent cache.'.format(self.name))
     else:
         # self.name exists and is a directory, and self._lock_path exists.
         self._persistent = True
         self._lock = files.FileLock(self._lock_path, timeout_secs=2)
         self._lock.Lock()
     try:
         self.InitializeMetadata()
     except exceptions.Error:
         # Make sure we clean up any dangling resources.
         self.Close(commit=False)
         raise