forked from platformio/platformio-core
Implement a delete functionality for State & ContentCache
This commit is contained in:
@ -142,31 +142,8 @@ class ContentCache(object):
|
|||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
if not self._db_path or not isfile(self._db_path):
|
if not self._db_path or not isfile(self._db_path):
|
||||||
return self
|
return self
|
||||||
found = False
|
|
||||||
newlines = []
|
|
||||||
with open(self._db_path) as fp:
|
|
||||||
for line in fp.readlines():
|
|
||||||
if "=" not in line:
|
|
||||||
continue
|
|
||||||
line = line.strip()
|
|
||||||
expire, path = line.split("=")
|
|
||||||
if time() < int(expire) and isfile(path):
|
|
||||||
newlines.append(line)
|
|
||||||
continue
|
|
||||||
found = True
|
|
||||||
if isfile(path):
|
|
||||||
try:
|
|
||||||
remove(path)
|
|
||||||
if not listdir(dirname(path)):
|
|
||||||
util.rmtree_(dirname(path))
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if found and self._lock_dbindex():
|
|
||||||
with open(self._db_path, "w") as fp:
|
|
||||||
fp.write("\n".join(newlines) + "\n")
|
|
||||||
self._unlock_dbindex()
|
|
||||||
|
|
||||||
|
self.delete()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __exit__(self, type_, value, traceback):
|
def __exit__(self, type_, value, traceback):
|
||||||
@ -190,6 +167,7 @@ class ContentCache(object):
|
|||||||
def _unlock_dbindex(self):
|
def _unlock_dbindex(self):
|
||||||
if self._lockfile:
|
if self._lockfile:
|
||||||
self._lockfile.release()
|
self._lockfile.release()
|
||||||
|
return True
|
||||||
|
|
||||||
def get_cache_path(self, key):
|
def get_cache_path(self, key):
|
||||||
assert len(key) > 3
|
assert len(key) > 3
|
||||||
@ -213,20 +191,19 @@ class ContentCache(object):
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
def set(self, key, data, valid):
|
def set(self, key, data, valid):
|
||||||
|
cache_path = self.get_cache_path(key)
|
||||||
|
if isfile(cache_path):
|
||||||
|
self.delete(key)
|
||||||
if not data:
|
if not data:
|
||||||
return
|
return
|
||||||
if not isdir(self.cache_dir):
|
if not isdir(self.cache_dir):
|
||||||
os.makedirs(self.cache_dir)
|
os.makedirs(self.cache_dir)
|
||||||
tdmap = {"s": 1, "m": 60, "h": 3600, "d": 86400}
|
tdmap = {"s": 1, "m": 60, "h": 3600, "d": 86400}
|
||||||
assert valid.endswith(tuple(tdmap.keys()))
|
assert valid.endswith(tuple(tdmap.keys()))
|
||||||
cache_path = self.get_cache_path(key)
|
|
||||||
expire_time = int(time() + tdmap[valid[-1]] * int(valid[:-1]))
|
expire_time = int(time() + tdmap[valid[-1]] * int(valid[:-1]))
|
||||||
|
|
||||||
if not self._lock_dbindex():
|
if not self._lock_dbindex():
|
||||||
return False
|
return False
|
||||||
with open(self._db_path, "a") as fp:
|
|
||||||
fp.write("%s=%s\n" % (str(expire_time), cache_path))
|
|
||||||
self._unlock_dbindex()
|
|
||||||
|
|
||||||
if not isdir(dirname(cache_path)):
|
if not isdir(dirname(cache_path)):
|
||||||
os.makedirs(dirname(cache_path))
|
os.makedirs(dirname(cache_path))
|
||||||
@ -235,6 +212,43 @@ class ContentCache(object):
|
|||||||
json.dump(data, fp)
|
json.dump(data, fp)
|
||||||
else:
|
else:
|
||||||
fp.write(str(data))
|
fp.write(str(data))
|
||||||
|
with open(self._db_path, "a") as fp:
|
||||||
|
fp.write("%s=%s\n" % (str(expire_time), cache_path))
|
||||||
|
|
||||||
|
return self._unlock_dbindex()
|
||||||
|
|
||||||
|
def delete(self, keys=None):
|
||||||
|
""" Keys=None, delete expired items """
|
||||||
|
if not keys:
|
||||||
|
keys = []
|
||||||
|
if not isinstance(keys, list):
|
||||||
|
keys = [keys]
|
||||||
|
paths_for_delete = [self.get_cache_path(k) for k in keys]
|
||||||
|
found = False
|
||||||
|
newlines = []
|
||||||
|
with open(self._db_path) as fp:
|
||||||
|
for line in fp.readlines():
|
||||||
|
if "=" not in line:
|
||||||
|
continue
|
||||||
|
line = line.strip()
|
||||||
|
expire, path = line.split("=")
|
||||||
|
if time() < int(expire) and isfile(path) and \
|
||||||
|
path not in paths_for_delete:
|
||||||
|
newlines.append(line)
|
||||||
|
continue
|
||||||
|
found = True
|
||||||
|
if isfile(path):
|
||||||
|
try:
|
||||||
|
remove(path)
|
||||||
|
if not listdir(dirname(path)):
|
||||||
|
util.rmtree_(dirname(path))
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if found and self._lock_dbindex():
|
||||||
|
with open(self._db_path, "w") as fp:
|
||||||
|
fp.write("\n".join(newlines) + "\n")
|
||||||
|
self._unlock_dbindex()
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -277,6 +291,12 @@ def set_state_item(name, value):
|
|||||||
data[name] = value
|
data[name] = value
|
||||||
|
|
||||||
|
|
||||||
|
def delete_state_item(name):
|
||||||
|
with State(lock=True) as data:
|
||||||
|
if name in data:
|
||||||
|
del data[name]
|
||||||
|
|
||||||
|
|
||||||
def get_setting(name):
|
def get_setting(name):
|
||||||
_env_name = "PLATFORMIO_SETTING_%s" % name.upper()
|
_env_name = "PLATFORMIO_SETTING_%s" % name.upper()
|
||||||
if _env_name in environ:
|
if _env_name in environ:
|
||||||
|
Reference in New Issue
Block a user