• Home
  • Raw
  • Download

Lines Matching +full:path +full:- +full:key

1 # -*- coding: utf-8 -*-
3 # Use of this source code is governed by a BSD-style license that can be
6 """Contains on-disk caching functionality."""
25 # pylint: disable=protected-access
54 """Encapsulates operations on a cache key reference.
63 with cache.Lookup(key) as ref:
68 path = PrepareItem()
69 ref.SetDefault(path, lock=True)
71 # yield the path to the cached entry to consuming code.
72 yield ref.path
75 def __init__(self, cache, key): argument
77 self.key = key
80 self._lock = cache._LockForKey(key)
81 self._entry_lock = cache._LockForKey(key, suffix='.entry_lock')
84 def path(self): member in CacheReference
85 """Returns on-disk path to the cached item."""
86 return self._cache.GetKeyPath(self.key)
124 def _Assign(self, path): argument
125 self._cache._Insert(self.key, path)
129 self._cache._InsertText(self.key, text)
133 self._cache._Remove(self.key)
134 osutils.SafeUnlink(self._lock.path)
135 osutils.SafeUnlink(self._entry_lock.path)
138 return self._cache._KeyExists(self.key)
141 def Assign(self, path): argument
142 """Insert a file or a directory into the cache at the referenced key."""
143 self._Assign(path)
147 """Create a file containing |text| and assign it to the key.
177 default_path: The path to assign if the entry doesn't exist.
189 Key entries can be files or directories. Access to the cache is provided
199 self.staging_dir = os.path.join(cache_dir, self._STAGING_DIR)
204 def _KeyExists(self, key): argument
205 return os.path.lexists(self.GetKeyPath(key))
207 def GetKeyPath(self, key): argument
208 """Get the on-disk path of a key."""
209 return os.path.join(self._cache_dir, '+'.join(key))
211 def _LockForKey(self, key, suffix=None): argument
212 """Returns an unacquired lock associated with a key."""
214 key_path = self.GetKeyPath(key)
215 osutils.SafeMakedirsNonRoot(os.path.dirname(key_path),
217 lock_path = os.path.join(self._cache_dir, os.path.dirname(key_path),
218 os.path.basename(key_path) + suffix)
224 def _Insert(self, key, path): argument
225 """Insert a file or a directory into the cache at a given key."""
226 self._Remove(key)
227 key_path = self.GetKeyPath(key)
228 osutils.SafeMakedirsNonRoot(os.path.dirname(key_path),
230 shutil.move(path, key_path)
232 def _InsertText(self, key, text): argument
235 file_path = os.path.join(tempdir, 'tempfile')
237 self._Insert(key, file_path)
239 def _Remove(self, key): argument
240 """Remove a key from the cache."""
241 if self._KeyExists(key):
243 shutil.move(self.GetKeyPath(key), tempdir)
245 def GetKey(self, path): argument
246 """Returns the key for an item's path in the cache."""
247 if self._cache_dir in path:
248 path = os.path.relpath(path, self._cache_dir)
249 return tuple(path.split('+'))
256 key_path = os.path.join(root, f)
257 if os.path.exists(key_path + self._lock_suffix):
258 # Test for the presence of the key's lock file to determine if this
259 # is the root key path, or some file nested within a key's dir.
263 def Lookup(self, key): argument
264 """Get a reference to a given key."""
265 return CacheReference(self, key)
280 for key in self.ListKeys():
281 path = self.GetKeyPath(key)
282 mtime = max(os.path.getmtime(path), os.path.getctime(path))
284 datetime.datetime.now() - datetime.datetime.fromtimestamp(mtime))
286 self.Lookup(key).Remove()
287 keys_removed.append(key)
298 # only fetches files via non-gs URIs.
306 retry_util.RunCurl(['--fail', url, '-o', local_path],
309 def _Insert(self, key, url): # pylint: disable=arguments-differ argument
313 DiskCache._Insert(self, key, o.path)
319 DiskCache._Insert(self, key, local_path.name)
322 def Untar(path, cwd, sudo=False): argument
325 comp = cros_build_lib.CompressionExtToType(path)
328 cmd += ['-I', cros_build_lib.FindCompressor(comp)]
329 functor(cmd + ['-xpf', path], cwd=cwd, debug_level=logging.DEBUG, quiet=True)
335 def _Insert(self, key, tarball_path): # pylint: disable=arguments-differ argument
340 with osutils.TempDir(prefix='tarball-cache',
345 tarball_path = o.path
348 tarball_path = os.path.join(tempdir, os.path.basename(o.path))
351 extract_path = os.path.join(tempdir, 'extract')
354 DiskCache._Insert(self, key, extract_path)
356 def _KeyExists(self, key): argument
359 The normal _KeyExists just checks to see if the key path exists in the cache
362 future untarring in non-test scripts.
367 key_path = self.GetKeyPath(key)
375 return os.path.exists(key_path)