From 99073dc606fd1360295244a55e06c1c1b129bdca Mon Sep 17 00:00:00 2001 From: Mathieu Comandon Date: Tue, 30 May 2017 13:32:48 -0700 Subject: [PATCH] Better sync for ScummVM games (Fixes #546) --- lutris/pga.py | 20 ++++++++++++++------ lutris/services/scummvm.py | 24 +++++++++++++++++++++++- lutris/services/xdg.py | 20 +++++++++----------- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/lutris/pga.py b/lutris/pga.py index 2fe0b9e22..858020747 100644 --- a/lutris/pga.py +++ b/lutris/pga.py @@ -166,7 +166,7 @@ def get_games_where(**conditions): list: Rows matching the query """ - query = "select * from games where " + query = "select * from games" condition_fields = [] condition_values = [] for field, value in conditions.items(): @@ -181,14 +181,22 @@ def get_games_where(**conditions): if extra_condition == 'in': if not hasattr(value, '__iter__'): raise ValueError("Value should be an iterable (%s given)" % value) - condition_fields.append('{{}} in ({})'.format( - ', '.join('?' * len(value)) - )) - condition_values = list(chain(condition_values, value)) + if value: + condition = "{}" + " in ({})".format( + ', '.join('?' * len(value)) + ) + condition_fields.append(condition) + condition_values = list(chain(condition_values, value)) else: condition_fields.append("{} = ?".format(field)) condition_values.append(value) - query += " AND ".join(condition_fields) + condition = " AND ".join(condition_fields) + if condition: + query = " WHERE ".join((query, condition)) + else: + # FIXME: Inspect and document why we should return an empty list when + # no condition is present. + return [] return sql.db_query(PGA_DB, query, tuple(condition_values)) diff --git a/lutris/services/scummvm.py b/lutris/services/scummvm.py index 6be609bf6..ac9dca1cf 100644 --- a/lutris/services/scummvm.py +++ b/lutris/services/scummvm.py @@ -19,6 +19,7 @@ def mark_as_installed(scummvm_id, name, path): game_id = pga.add_or_update( name=name, runner='scummvm', + installer_slug=INSTALLER_SLUG, slug=slug, installed=1, configpath=config_id, @@ -36,6 +37,14 @@ def mark_as_installed(scummvm_id, name, path): return game_id +def mark_as_uninstalled(game_info): + logger.info("Uninstalling %s", game_info['name']) + return pga.add_or_update( + id=game_info['id'], + installed=0 + ) + + def get_scummvm_games(): if not os.path.exists(SCUMMVM_CONFIG_FILE): logger.info("No ScummVM config found") @@ -53,5 +62,18 @@ def get_scummvm_games(): def sync_with_lutris(): + scummvm_games = { + game['slug']: game + for game in pga.get_games_where(runner='scummvm', + installer_slug=INSTALLER_SLUG, + installed=1) + } + seen = set() + for scummvm_id, name, path in get_scummvm_games(): - mark_as_installed(scummvm_id, name, path) + slug = slugify(name) + seen.add(slug) + if slug not in scummvm_games.keys(): + mark_as_installed(scummvm_id, name, path) + for slug in set(scummvm_games.keys()).difference(seen): + mark_as_uninstalled(scummvm_games[slug]) diff --git a/lutris/services/xdg.py b/lutris/services/xdg.py index 28ce09745..1639b69d1 100644 --- a/lutris/services/xdg.py +++ b/lutris/services/xdg.py @@ -69,21 +69,19 @@ def mark_as_installed(appid, runner_name, game_info): def mark_as_uninstalled(game_info): - assert 'id' in game_info - assert 'name' in game_info - logger.info('Setting %s as uninstalled' % game_info['name']) - game_id = pga.add_or_update( + logger.info('Uninstalling %s' % game_info['name']) + return pga.add_or_update( id=game_info['id'], - runner='', installed=0 ) - return game_id def sync_with_lutris(): - desktop_games_map = { + desktop_games = { game['slug']: game - for game in pga.get_games_where(runner='linux', installer_slug=INSTALLER_SLUG) + for game in pga.get_games_where(runner='linux', + installer_slug=INSTALLER_SLUG, + installed=1) } seen = set() @@ -96,7 +94,7 @@ def sync_with_lutris(): logger.info("Found desktop game \"{}\" (app: {}, slug: {})".format(name, appid, slug)) seen.add(slug) - if slug not in desktop_games_map.keys(): + if slug not in desktop_games.keys(): game_info = { 'name': name, 'slug': slug, @@ -107,8 +105,8 @@ def sync_with_lutris(): } mark_as_installed(appid, 'linux', game_info) - for slug in set(desktop_games_map.keys()).difference(seen): - mark_as_uninstalled(desktop_games_map[slug]) + for slug in set(desktop_games.keys()).difference(seen): + mark_as_uninstalled(desktop_games[slug]) def iter_xdg_apps():