def create_backup_of_shortcuts(config, user, dry_run=False): def _create_directory_if_needed(directory): if os.path.exists(directory): return logger.debug("Creating directory: %s" % directory) os.makedirs(directory) backup_dir = backup_directory(config) if backup_dir is None: logger.info( "No backups directory specified, so not backing up shortcuts.vdf before overwriting. See config.txt for more info" ) return _create_directory_if_needed(backup_dir) if not os.path.isdir(backup_dir): logger.warning( "Backup directory path is something other than a directory. Skipping backups" ) return backup_path = shortcuts_backup_path(backup_dir, user) # Make sure the user-specific backups dir exists _create_directory_if_needed(os.path.dirname(backup_path)) shortcuts.write_shortcuts(backup_path, shortcuts.get_shortcuts(user))
def test_sync_roms_for_user_both_adds_and_removes_roms(self): rom1 = model.ROM(name='Game1', path='/Path/to/game1', console=fixtures.consoles.flagged) shortcut1 = roms.rom_to_shortcut(rom1) rom2 = model.ROM(name='Game2', path='/Path/to/game2', console=fixtures.consoles.flagged) shortcut2 = roms.rom_to_shortcut(rom2) rom3 = model.ROM(name='Game3', path='/Path/to/game3', console=fixtures.consoles.flagged) shortcut3 = roms.rom_to_shortcut(rom3) rom4 = model.ROM(name='Game4', path='/Path/to/game4', console=fixtures.consoles.flagged) shortcut4 = roms.rom_to_shortcut(rom4) old_shortcuts = [shortcut1, shortcut2, shortcut4] self._set_users_shortcuts(old_shortcuts) self.synchronizer.sync_roms_for_user(self.user_fixture.get_context(), [rom1, rom2, rom3], None) new_shortcuts = shortcuts.get_shortcuts( self.user_fixture.get_context()) verify(self.mock_logger, times=2).info(any()) self.assertEquals(len(new_shortcuts), 3) self.assertIn(shortcut1, new_shortcuts) self.assertIn(shortcut2, new_shortcuts) self.assertIn(shortcut3, new_shortcuts)
def create_backup_of_shortcuts(config, user, dry_run=False): def _create_directory_if_needed(directory): if os.path.exists(directory): return logger.debug("Creating directory: %s" % directory) os.makedirs(directory) backup_dir = backup_directory(config) if backup_dir is None: logger.info("No backups directory specified, so not backing up shortcuts.vdf before overwriting. See config.txt for more info") return _create_directory_if_needed(backup_dir) if not os.path.isdir(backup_dir): logger.warning("Backup directory path is something other than a directory. Skipping backups") return backup_path = shortcuts_backup_path(backup_dir, user) # Make sure the user-specific backups dir exists _create_directory_if_needed(os.path.dirname(backup_path)) shortcuts.write_shortcuts(backup_path, shortcuts.get_shortcuts(user))
def test_get_and_set_shortcuts_creates_file_at_correct_path(self): tempdir = tempfile.mkdtemp() steam = model.Steam(tempdir) context = model.LocalUserContext(steam=steam, user_id='anonymous') # Create the `anonymous` directory, cause we can't open shortcuts.vdf for # writing if the containing directory doesn't exist os.makedirs(paths.user_config_directory(context)) self.assertFalse(os.path.exists(paths.shortcuts_path(context))) self.assertEqual([], shortcuts.get_shortcuts(context)) updated_shortcuts = [_dummy_shortcut()] shortcuts.set_shortcuts(context, updated_shortcuts) self.assertEqual(updated_shortcuts, shortcuts.get_shortcuts(context)) shutil.rmtree(tempdir)
def test_sync_roms_for_user_saves_shortcuts_after_running(self): rom1 = model.ROM(name = 'Game1', path = '/Path/to/game1', console = fixtures.consoles.flagged) shortcut1 = roms.rom_to_shortcut(rom1) self._set_users_shortcuts([]) self.synchronizer.sync_roms_for_user(self.user_fixture.get_context(), [rom1], None) updated_shortcuts = shortcuts.get_shortcuts(self.user_fixture.get_context()) self.assertEquals(updated_shortcuts, [shortcut1])
def _create_backup(self, user, dry_run=False): if dry_run: self.logger.debug("Not creating backup because its a dry run") return backup_path = self.config.shortcuts_backup_path(user) if backup_path is None: self.logger.info("No backups directory specified, so not backing up shortcuts.vdf before overwriting. See config.txt for more info") return shortcuts.write_shortcuts(backup_path, shortcuts.get_shortcuts(user))
def sync_roms_for_user(self, user, users_roms, consoles, dry_run=False): """ This function takes care of syncing ROMs. After this function exits, Steam will contain only non-Ice shortcuts and the ROMs represented by `roms`. """ # 'Unmanaged' is just the term I am using for shortcuts that the user has # added that Ice shouldn't delete. For example, something like a shortcut # to Plex would be 'Unmanaged' previous_managed_ids = self.managed_rom_archive.previous_managed_ids( user) logger.debug("Previous managed ids: %s" % previous_managed_ids) current_shortcuts = shortcuts.get_shortcuts(user) unmanaged_shortcuts = self.unmanaged_shortcuts(previous_managed_ids, current_shortcuts, consoles) logger.debug("Unmanaged shortcuts: %s" % unmanaged_shortcuts) current_ice_shortcuts = [ shortcut for shortcut in current_shortcuts if shortcut not in unmanaged_shortcuts ] logger.debug("Current Ice shortcuts: %s" % current_ice_shortcuts) # Generate a list of shortcuts out of our list of ROMs rom_shortcuts = list(map(roms.rom_to_shortcut, users_roms)) # Calculate which ROMs were added and which were removed so we can inform # the user removed = self.removed_shortcuts(current_ice_shortcuts, rom_shortcuts) list( map( lambda shortcut: logger.info("Removing ROM: `%s`" % shortcut. name), removed)) added = self.added_shortcuts(current_ice_shortcuts, rom_shortcuts) list( map( lambda shortcut: logger.info("Adding ROM: `%s`" % shortcut.name ), added)) # Set the updated shortcuts updated_shortcuts = unmanaged_shortcuts + rom_shortcuts logger.debug("Sync Result: %s" % updated_shortcuts) if dry_run: logger.debug("Not saving or updating history due to dry run") return logger.debug("Saving shortcuts") shortcuts.set_shortcuts(user, updated_shortcuts) # Update the archive new_managed_ids = list(map(shortcuts.shortcut_app_id, rom_shortcuts)) logger.debug("Updating archive to ids: %s" % new_managed_ids) self.managed_rom_archive.set_managed_ids(user, new_managed_ids)
def _create_backup(self, user, dry_run=False): if dry_run: logger.debug("Not creating backup because its a dry run") return backup_path = self.config.shortcuts_backup_path(user) if backup_path is None: logger.info( "No backups directory specified, so not backing up shortcuts.vdf before overwriting. See config.txt for more info" ) return shortcuts.write_shortcuts(backup_path, shortcuts.get_shortcuts(user))
def test_sync_roms_for_user_saves_shortcuts_after_running(self): rom1 = model.ROM(name='Game1', path='/Path/to/game1', console=fixtures.consoles.flagged) shortcut1 = roms.rom_to_shortcut(rom1) self._set_users_shortcuts([]) self.synchronizer.sync_roms_for_user(self.user_fixture.get_context(), [rom1], None) updated_shortcuts = shortcuts.get_shortcuts( self.user_fixture.get_context()) self.assertEquals(updated_shortcuts, [shortcut1])
def sync_roms_for_user(self, user, users_roms, consoles, dry_run=False): """ This function takes care of syncing ROMs. After this function exits, Steam will contain only non-Ice shortcuts and the ROMs represented by `roms`. """ # 'Unmanaged' is just the term I am using for shortcuts that the user has # added that Ice shouldn't delete. For example, something like a shortcut # to Plex would be 'Unmanaged' previous_managed_ids = self.managed_rom_archive.previous_managed_ids(user) logger.debug("Previous managed ids: %s" % previous_managed_ids) current_shortcuts = shortcuts.get_shortcuts(user) unmanaged_shortcuts = self.unmanaged_shortcuts(previous_managed_ids, current_shortcuts, consoles) logger.debug("Unmanaged shortcuts: %s" % unmanaged_shortcuts) current_ice_shortcuts = filter( lambda shortcut: shortcut not in unmanaged_shortcuts, current_shortcuts ) logger.debug("Current Ice shortcuts: %s" % current_ice_shortcuts) # Generate a list of shortcuts out of our list of ROMs rom_shortcuts = map(roms.rom_to_shortcut, users_roms) # Calculate which ROMs were added and which were removed so we can inform # the user removed = self.removed_shortcuts(current_ice_shortcuts, rom_shortcuts) map(lambda shortcut: logger.info("Removing ROM: `%s`" % shortcut.name), removed) added = self.added_shortcuts(current_ice_shortcuts, rom_shortcuts) map(lambda shortcut: logger.info("Adding ROM: `%s`" % shortcut.name), added) # Set the updated shortcuts updated_shortcuts = unmanaged_shortcuts + rom_shortcuts logger.debug("Sync Result: %s" % updated_shortcuts) if dry_run: logger.debug("Not saving or updating history due to dry run") return logger.debug("Saving shortcuts") shortcuts.set_shortcuts(user, updated_shortcuts) # Update the archive new_managed_ids = map(shortcuts.shortcut_app_id, rom_shortcuts) logger.debug("Updating archive to ids: %s" % new_managed_ids) self.managed_rom_archive.set_managed_ids(user, new_managed_ids)
def test_sync_roms_for_user_both_adds_and_removes_roms(self): rom1 = model.ROM(name = 'Game1', path = '/Path/to/game1', console = fixtures.consoles.flagged) shortcut1 = roms.rom_to_shortcut(rom1) rom2 = model.ROM(name = 'Game2', path = '/Path/to/game2', console = fixtures.consoles.flagged) shortcut2 = roms.rom_to_shortcut(rom2) rom3 = model.ROM(name = 'Game3', path = '/Path/to/game3', console = fixtures.consoles.flagged) shortcut3 = roms.rom_to_shortcut(rom3) rom4 = model.ROM(name = 'Game4', path = '/Path/to/game4', console = fixtures.consoles.flagged) shortcut4 = roms.rom_to_shortcut(rom4) old_shortcuts = [shortcut1, shortcut2, shortcut4] self._set_users_shortcuts(old_shortcuts) self.synchronizer.sync_roms_for_user(self.user_fixture.get_context(), [rom1, rom2, rom3], None) new_shortcuts = shortcuts.get_shortcuts(self.user_fixture.get_context()) verify(self.mock_logger, times=2).info(any()) self.assertEquals(len(new_shortcuts), 3) self.assertIn(shortcut1, new_shortcuts) self.assertIn(shortcut2, new_shortcuts) self.assertIn(shortcut3, new_shortcuts)
def test_sync_roms_for_user_keeps_unmanaged_shortcuts(self): random_shortcut = steam_model.Shortcut("Plex", "/Some/Random/Path/plex", "/Some/Random/Path", "", []) self._set_users_shortcuts([random_shortcut]) when(self.mock_archive).previous_managed_ids(self.user_fixture.get_context()).thenReturn([]) rom1 = model.ROM(name = 'Game1', path = '/Path/to/game1', console = fixtures.consoles.flagged) shortcut1 = roms.rom_to_shortcut(rom1) rom2 = model.ROM(name = 'Game2', path = '/Path/to/game2', console = fixtures.consoles.flagged) shortcut2 = roms.rom_to_shortcut(rom2) rom3 = model.ROM(name = 'Game3', path = '/Path/to/game3', console = fixtures.consoles.flagged) shortcut3 = roms.rom_to_shortcut(rom3) rom4 = model.ROM(name = 'Game4', path = '/Path/to/game4', console = fixtures.consoles.flagged) shortcut4 = roms.rom_to_shortcut(rom4) self.synchronizer.sync_roms_for_user(self.user_fixture.get_context(), [rom1, rom2, rom3, rom4], None) new_shortcuts = shortcuts.get_shortcuts(self.user_fixture.get_context()) self.assertEquals(len(new_shortcuts), 5) self.assertIn(random_shortcut, new_shortcuts) self.assertIn(shortcut1, new_shortcuts) self.assertIn(shortcut2, new_shortcuts) self.assertIn(shortcut3, new_shortcuts) self.assertIn(shortcut4, new_shortcuts)
def test_sync_roms_for_user_keeps_unmanaged_shortcuts(self): random_shortcut = steam_model.Shortcut("Plex", "/Some/Random/Path/plex", "/Some/Random/Path", "", []) self._set_users_shortcuts([random_shortcut]) when(self.mock_archive).previous_managed_ids( self.user_fixture.get_context()).thenReturn([]) rom1 = model.ROM(name='Game1', path='/Path/to/game1', console=fixtures.consoles.flagged) shortcut1 = roms.rom_to_shortcut(rom1) rom2 = model.ROM(name='Game2', path='/Path/to/game2', console=fixtures.consoles.flagged) shortcut2 = roms.rom_to_shortcut(rom2) rom3 = model.ROM(name='Game3', path='/Path/to/game3', console=fixtures.consoles.flagged) shortcut3 = roms.rom_to_shortcut(rom3) rom4 = model.ROM(name='Game4', path='/Path/to/game4', console=fixtures.consoles.flagged) shortcut4 = roms.rom_to_shortcut(rom4) self.synchronizer.sync_roms_for_user(self.user_fixture.get_context(), [rom1, rom2, rom3, rom4], None) new_shortcuts = shortcuts.get_shortcuts( self.user_fixture.get_context()) self.assertEquals(len(new_shortcuts), 5) self.assertIn(random_shortcut, new_shortcuts) self.assertIn(shortcut1, new_shortcuts) self.assertIn(shortcut2, new_shortcuts) self.assertIn(shortcut3, new_shortcuts) self.assertIn(shortcut4, new_shortcuts)
def user_shortcuts(self, uid): context = model.LocalUserContext(self.steam_fixture.get_steam(), uid) return shortcuts.get_shortcuts(context)