Skip to content

Commit

Permalink
[#346] Add a skip-steam-check CLI parameter
Browse files Browse the repository at this point in the history
This should make Ice significantly easier to use on SteamOS, where everything has a `steam` prefix.

Also refactor how the CLI interacts with IceEngine, as I want to stop passing around `options`. To stop doing the parameter duplication between `main` and `run` I extracted the "catch exceptions" functionality into a decorator.
  • Loading branch information
scottrice committed Jan 24, 2016
1 parent 4152a88 commit 2c7e04c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
13 changes: 13 additions & 0 deletions ice/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# encoding: utf-8

from logs import logger

def catch_exceptions(msg):
def decorator(func):
def wrapped(*args, **kwargs):
try:
func(*args, **kwargs)
except Exception as e:
logger.exception(msg)
return wrapped
return decorator
15 changes: 13 additions & 2 deletions ice/runners/command_line_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def __init__(self, steam=None, filesystem=None):
def get_command_line_args(self, argv):
parser = argparse.ArgumentParser()
parser.add_argument('pdebug', type=bool, nargs='?', help="Pastes debug logs to pastebin to include with bug reports.")
parser.add_argument('-s', '--skip-steam-check', action='store_true', help="Skips checking whether Steam is running")
# Config options
parser.add_argument('-c', '--config', type=str, default=None)
parser.add_argument('-C', '--consoles', type=str, default=None)
parser.add_argument('-e', '--emulators', type=str, default=None)
Expand All @@ -37,5 +39,14 @@ def run(self, argv):
debug.paste_debug_logs()
return

engine = IceEngine(self.steam, filesystem = self.filesystem, options = options)
engine.main(dry_run=options.dry_run)
engine = IceEngine(
self.steam,
filesystem = self.filesystem,
file_overrides = {
'config': options.config,
'consoles': options.consoles,
'emulators': options.emulators,
})
engine.run(
skip_steam_check=options.skip_steam_check,
dry_run=options.dry_run)
41 changes: 26 additions & 15 deletions ice/runners/ice_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from ice import backups
from ice import configuration
from ice import consoles
from ice import decorators
from ice import emulators
from ice import paths
from ice import settings
Expand All @@ -27,9 +28,18 @@
from ice.steam_grid_updater import SteamGridUpdater
from ice.steam_shortcut_synchronizer import SteamShortcutSynchronizer

STEAM_CHECK_SKIPPED_WARNING = """\
Not checking whether Steam is running. Any changes made may be overwritten \
when Steam exits.\
"""

class IceEngine(object):

def __init__(self, steam, filesystem, options):
def __init__(
self,
steam,
filesystem,
file_overrides):
"""Valid options for creating an IceEngine are as follows:
* config - The path to the config file to use. Searches the default paths
Expand All @@ -48,9 +58,9 @@ def __init__(self, steam, filesystem, options):
self.users = filter(is_user_context, steam_module.local_user_contexts(self.steam))

logger.debug("Initializing Ice")
self.config = settings.load_configuration(filesystem, options.config)
self.emulators = settings.load_emulators(filesystem, options.emulators)
self.consoles = settings.load_consoles(self.emulators, filesystem, options.consoles)
self.config = settings.load_configuration(filesystem, file_overrides['config'])
self.emulators = settings.load_emulators(filesystem, file_overrides['emulators'])
self.consoles = settings.load_consoles(self.emulators, filesystem, file_overrides['consoles'])

parser = ROMParser()
self.rom_finder = ROMFinder(self.config, filesystem, parser)
Expand All @@ -60,13 +70,16 @@ def __init__(self, steam, filesystem, options):

self.grid_updater = SteamGridUpdater(settings.image_provider(self.config))

def validate_environment(self):
def validate_environment(self, skip_steam_check):
"""
Validate that the current environment meets all of Ice's requirements.
"""
with EnvironmentChecker(self.filesystem) as env_checker:
# If Steam is running then any changes we make will be overwritten
env_checker.require_program_not_running("Steam")
if not skip_steam_check:
# If Steam is running then any changes we make will be overwritten
env_checker.require_program_not_running("Steam")
else:
logger.warning(STEAM_CHECK_SKIPPED_WARNING)
# I'm not sure if there are situations where this won't exist, but I
# assume that it does everywhere and better safe than sorry
env_checker.require_directory_exists(self.steam.userdata_directory)
Expand All @@ -90,14 +103,18 @@ def create_backup(self, user, dry_run=False):
else:
backups.create_backup_of_shortcuts(self.config, user)

def run(self, dry_run=False):
@decorators.catch_exceptions("An exception occurred while running Ice")
def run(
self,
skip_steam_check=False,
dry_run=False):
if self.steam is None:
logger.error("Cannot run Ice because Steam doesn't appear to be installed")
return

logger.info("=========== Starting Ice ===========")
try:
self.validate_environment()
self.validate_environment(skip_steam_check)
except EnvCheckerError as e:
logger.info("Ice cannot run because of issues with your system.\n")
logger.info("* %s" % e.message)
Expand All @@ -115,12 +132,6 @@ def run(self, dry_run=False):
self.shortcut_synchronizer.sync_roms_for_user(user, roms, self.consoles, dry_run=dry_run)
self.grid_updater.update_artwork_for_rom_collection(user, roms, dry_run=dry_run)

def main(self, dry_run=False):
try:
self.run(dry_run=dry_run)
except Exception as error:
logger.exception("An exception occurred while running Ice")

# Logging methods. The purpose of these methods isn't so much to log things as
# they are to inform the user of the state of their setup (as Ice sees it).
# They were originally on ice_logging but since they require knowledge of
Expand Down

0 comments on commit 2c7e04c

Please sign in to comment.