[enh] Update app_info to show app installation status

This commit is contained in:
Jérôme Lebleu 2015-10-02 00:01:00 +02:00
parent 843f68e817
commit 7048e98a0a
2 changed files with 79 additions and 34 deletions

View file

@ -363,6 +363,10 @@ app:
arguments: arguments:
app: app:
help: Specific app ID help: Specific app ID
-s:
full: --show-status
help: Show app installation status
action: store_true
-r: -r:
full: --raw full: --raw
help: Return the full app_dict help: Return the full app_dict

View file

@ -173,6 +173,8 @@ def app_list(offset=None, limit=None, filter=None, raw=False):
if raw: if raw:
app_info['installed'] = installed app_info['installed'] = installed
if installed:
app_info['status'] = _get_app_status(app_id)
list_dict[app_id] = app_info list_dict[app_id] = app_info
else: else:
list_dict.append({ list_dict.append({
@ -193,37 +195,44 @@ def app_list(offset=None, limit=None, filter=None, raw=False):
return list_dict return list_dict
def app_info(app, raw=False): def app_info(app, show_status=False, raw=False):
""" """
Get app info Get app info
Keyword argument: Keyword argument:
app -- Specific app ID app -- Specific app ID
show_status -- Show app installation status
raw -- Return the full app_dict raw -- Return the full app_dict
""" """
try: if not _is_installed(app):
app_info = app_list(filter=app, raw=True)[app] raise MoulinetteError(errno.EINVAL,
except: m18n.n('app_not_installed', app))
app_info = {} if raw:
ret = app_list(filter=app, raw=True)[app]
if _is_installed(app):
with open(apps_setting_path + app +'/settings.yml') as f: with open(apps_setting_path + app +'/settings.yml') as f:
app_info['settings'] = yaml.load(f) ret['settings'] = yaml.load(f)
return ret
if raw: app_setting_path = apps_setting_path + app
return app_info
else: # Retrieve manifest and status
return { with open(app_setting_path + '/manifest.json') as f:
'name': app_info['manifest']['name'], manifest = json.loads(str(f.read()))
'description': _value_for_locale(app_info['manifest']['description']), status = _get_app_status(app, format_date=True)
# FIXME: Temporarly allow undefined license
'license': app_info['manifest'].get('license', info = {
m18n.n('license_undefined')), 'name': manifest['name'],
# FIXME: Temporarly allow undefined version 'description': _value_for_locale(manifest['description']),
'version' : app_info['manifest'].get('version', '-'), # FIXME: Temporarly allow undefined license
#TODO: Add more info 'license': manifest.get('license', m18n.n('license_undefined')),
} # FIXME: Temporarly allow undefined version
'version': manifest.get('version', '-'),
#TODO: Add more info
}
if show_status:
info['status'] = status
return info
def app_map(app=None, raw=False, user=None): def app_map(app=None, raw=False, user=None):
@ -335,17 +344,8 @@ def app_upgrade(auth, app=[], url=None, file=None):
app_setting_path = apps_setting_path +'/'+ app_id app_setting_path = apps_setting_path +'/'+ app_id
# Retrieve current app status # Retrieve current app status
status = {} status = _get_app_status(app_id)
try: status['remote'] = manifest.get('remote', None)
with open(app_setting_path + '/status.json') as f:
status = json.loads(str(f.read()))
except IOError:
logger.exception("status file not found for '%s'", app_id)
status = {
'installed_at': app_setting(app_id, 'install_time'),
}
finally:
status['remote'] = manifest.get('remote', None)
if original_app_id != app_id: if original_app_id != app_id:
# Replace original_app_id with the forked one in scripts # Replace original_app_id with the forked one in scripts
@ -791,9 +791,11 @@ def app_setting(app, key, value=None, delete=False):
app_settings = {} app_settings = {}
if value is None and not delete: if value is None and not delete:
# Get the value try:
if app_settings is not None and key in app_settings:
return app_settings[key] return app_settings[key]
except:
logger.exception("cannot get app setting '%s' for '%s'", key, app)
return None
else: else:
yaml_settings=['redirected_urls','redirected_regex'] yaml_settings=['redirected_urls','redirected_regex']
# Set the value # Set the value
@ -1000,6 +1002,45 @@ def app_ssowatconf(auth):
msignals.display(m18n.n('ssowat_conf_generated'), 'success') msignals.display(m18n.n('ssowat_conf_generated'), 'success')
def _get_app_status(app_id, format_date=False):
"""
Get app status or create it if needed
Keyword arguments:
app_id -- The app id
format_date -- Format date fields
"""
app_setting_path = apps_setting_path + app_id
if not os.path.isdir(app_setting_path):
raise MoulinetteError(errno.EINVAL, m18n.n('app_unknown'))
status = {}
try:
with open(app_setting_path + '/status.json') as f:
status = json.loads(str(f.read()))
except IOError:
logger.exception("status file not found for '%s'", app_id)
# Create app status
status = {
'installed_at': app_setting(app_id, 'install_time'),
'upgraded_at': app_setting(app_id, 'update_time'),
'remote': { 'type': None },
}
with open(app_setting_path + '/status.json', 'w+') as f:
json.dump(status, f)
if format_date:
for f in ['installed_at', 'upgraded_at']:
v = status.get(f, None)
if not v:
status[f] = '-'
else:
status[f] = time.strftime(m18n.n('format_datetime_short'),
time.gmtime(v))
return status
def _extract_app_from_file(path, remove=False): def _extract_app_from_file(path, remove=False):
""" """
Unzip or untar application tarball in app_tmp_folder, or copy it from a directory Unzip or untar application tarball in app_tmp_folder, or copy it from a directory