Minor fixes for content cacher

This commit is contained in:
Ivan Kravets
2017-02-01 02:49:25 +02:00
parent ba58b4ba8a
commit 3d6dab39ca
2 changed files with 10 additions and 9 deletions

View File

@ -137,8 +137,6 @@ class ContentCache(object):
return return
self.cache_dir = cache_dir or join(util.get_home_dir(), ".cache") self.cache_dir = cache_dir or join(util.get_home_dir(), ".cache")
if not self.cache_dir:
os.makedirs(self.cache_dir)
self._db_path = join(self.cache_dir, "db.data") self._db_path = join(self.cache_dir, "db.data")
def __enter__(self): def __enter__(self):
@ -152,7 +150,7 @@ class ContentCache(object):
continue continue
line = line.strip() line = line.strip()
expire, path = line.split("=") expire, path = line.split("=")
if time() < int(expire): if time() < int(expire) and isfile(path):
newlines.append(line) newlines.append(line)
continue continue
found = True found = True
@ -172,6 +170,8 @@ class ContentCache(object):
pass pass
def _lock_dbindex(self): def _lock_dbindex(self):
if not self.cache_dir:
os.makedirs(self.cache_dir)
self._lockfile = LockFile(self.cache_dir) self._lockfile = LockFile(self.cache_dir)
if self._lockfile.is_locked() and \ if self._lockfile.is_locked() and \
(time() - getmtime(self._lockfile.lock_file)) > 10: (time() - getmtime(self._lockfile.lock_file)) > 10:
@ -200,8 +200,6 @@ class ContentCache(object):
return h.hexdigest() return h.hexdigest()
def get(self, key): def get(self, key):
if not self.cache_dir:
return None
cache_path = self.get_cache_path(key) cache_path = self.get_cache_path(key)
if not isfile(cache_path): if not isfile(cache_path):
return None return None
@ -212,7 +210,7 @@ class ContentCache(object):
return data return data
def set(self, key, data, valid): def set(self, key, data, valid):
if not self.cache_dir or 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)
@ -238,8 +236,11 @@ class ContentCache(object):
return True return True
def clean(self): def clean(self):
if self.cache_dir and isdir(self.cache_dir): if not self.cache_dir or not isdir(self.cache_dir):
util.rmtree_(self.cache_dir) return
if not self._lock_dbindex():
return
util.rmtree_(self.cache_dir)
def sanitize_setting(name, value): def sanitize_setting(name, value):

View File

@ -64,7 +64,7 @@ def on_platformio_start(ctx, force, caller):
app.set_session_var("caller_id", caller) app.set_session_var("caller_id", caller)
telemetry.on_command() telemetry.on_command()
if ctx.args and (ctx.args[0] == "upgrade" or "update" in ctx.args): if ctx.args and ctx.args[0] == "upgrade":
clean_cache() clean_cache()
if not in_silence(ctx): if not in_silence(ctx):
after_upgrade(ctx) after_upgrade(ctx)