mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Merge pull request #125 from YunoHost/automatically_regenerate_actionmap_if_yml_file_is_modified
[enh] automatically regenerate cache if actionmap.yml is modified
This commit is contained in:
commit
9dd1ef37f3
1 changed files with 25 additions and 4 deletions
|
@ -368,17 +368,29 @@ class ActionsMap(object):
|
||||||
for n in namespaces:
|
for n in namespaces:
|
||||||
logger.debug("loading actions map namespace '%s'", n)
|
logger.debug("loading actions map namespace '%s'", n)
|
||||||
|
|
||||||
if use_cache:
|
actionsmap_yml = '%s/actionsmap/%s.yml' % (pkg.datadir, n)
|
||||||
|
actionsmap_yml_stat = os.stat(actionsmap_yml)
|
||||||
|
actionsmap_pkl = '%s/actionsmap/%s-%d-%d.pkl' % (
|
||||||
|
pkg.cachedir,
|
||||||
|
n,
|
||||||
|
actionsmap_yml_stat.st_size,
|
||||||
|
actionsmap_yml_stat.st_mtime
|
||||||
|
)
|
||||||
|
|
||||||
|
if use_cache and os.path.exists(actionsmap_pkl):
|
||||||
try:
|
try:
|
||||||
# Attempt to load cache
|
# Attempt to load cache
|
||||||
with open('%s/actionsmap/%s.pkl' % (pkg.cachedir, n)) as f:
|
with open(actionsmap_pkl) as f:
|
||||||
actionsmaps[n] = pickle.load(f)
|
actionsmaps[n] = pickle.load(f)
|
||||||
# TODO: Switch to python3 and catch proper exception
|
# TODO: Switch to python3 and catch proper exception
|
||||||
except IOError:
|
except IOError:
|
||||||
self.use_cache = False
|
self.use_cache = False
|
||||||
actionsmaps = self.generate_cache(namespaces)
|
actionsmaps = self.generate_cache(namespaces)
|
||||||
|
elif use_cache: # cached file doesn't exists
|
||||||
|
self.use_cache = False
|
||||||
|
actionsmaps = self.generate_cache(namespaces)
|
||||||
elif n not in actionsmaps:
|
elif n not in actionsmaps:
|
||||||
with open('%s/actionsmap/%s.yml' % (pkg.datadir, n)) as f:
|
with open(actionsmap_yml) as f:
|
||||||
actionsmaps[n] = ordered_yaml_load(f)
|
actionsmaps[n] = ordered_yaml_load(f)
|
||||||
|
|
||||||
# Load translations
|
# Load translations
|
||||||
|
@ -511,8 +523,17 @@ class ActionsMap(object):
|
||||||
with open(am_file, 'r') as f:
|
with open(am_file, 'r') as f:
|
||||||
actionsmaps[n] = yaml.load(f)
|
actionsmaps[n] = yaml.load(f)
|
||||||
|
|
||||||
|
# clean old cached files
|
||||||
|
for i in os.listdir('%s/actionsmap/' % pkg.cachedir):
|
||||||
|
if i.endswith(".pkl"):
|
||||||
|
os.remove('%s/actionsmap/%s' % (pkg.cachedir, i))
|
||||||
|
|
||||||
# Cache actions map into pickle file
|
# Cache actions map into pickle file
|
||||||
with pkg.open_cachefile('%s.pkl' % n, 'w', subdir='actionsmap') as f:
|
am_file_stat = os.stat(am_file)
|
||||||
|
|
||||||
|
pkl = '%s-%d-%d.pkl' % (n, am_file_stat.st_size, am_file_stat.st_mtime)
|
||||||
|
|
||||||
|
with pkg.open_cachefile(pkl, 'w', subdir='actionsmap') as f:
|
||||||
pickle.dump(actionsmaps[n], f)
|
pickle.dump(actionsmaps[n], f)
|
||||||
|
|
||||||
return actionsmaps
|
return actionsmaps
|
||||||
|
|
Loading…
Add table
Reference in a new issue