示例#1
0
def remove(id):
    """
    Removes a collection with id
    :param id: int, id of the collection to be removed
    :return: string, the name of the Collection which has been deleted
    """
    logger.info("Removing Collection with id: {}".format(id))
    # 0. Look up in database
    c = Collection.query.get(id)

    if c is not None:  # if it exists then:
        logger.info("Found Collection")
        # 1. Remove from launcher_profiles.json
        logger.info("Removing Profile from Minecraft Profiles")
        mc_interface.rem_profile(c.mc_id)

        name = copy.copy(c.name)
        dir_loc = copy.copy(c.get_collection_path())

        logger.info("Removing from database")
        Collection.query.filter(Collection.id == id).delete()

        db_session.commit()

        # 2. Remove from Filesystem
        logger.info("Removing from filesystem at: {}".format(dir_loc))

        shutil.rmtree(dir_loc)

        logger.info("Finished removing Collection: {}".format(name))

        return name
    else:
        logger.error("Collection with id: {} doesn't exist".format(id))
        raise exceptions.CollectionNotExistError(id)
示例#2
0
def rem_mod(collection_id, mod_id):
    """
    Removes a mod from the collection
    :param collection_id: int, the id of the collection
    :param mod_id: int, id of the mod
    :return: None
    """
    try:
        collection_id = int(collection_id)
    except ValueError:
        logger.critical("Collection id is not a valid integer")
        raise

    try:
        mod_id = int(mod_id)
    except ValueError:
        logger.critical("Mod id is not a valid integer")
        raise

    logger.info("Removing mod with id: {} from collection with id: {}".format(
        mod_id, collection_id))
    coll = Collection.query.get(collection_id)
    mod = Mod.query.get(mod_id)
    if mod is not None and coll is not None:
        logger.info("Mod and Collection exist, proceeding to removal...")
        if mod in coll.mods:  # check if the mod is in this collection
            logger.info("Removing symbolic link...")
            try:
                os.unlink(
                    join_path(config["application_root"], "collections",
                              coll.mc_id.decode(), "mods",
                              get_file_name(mod.filename)))
            except FileNotFoundError:
                logger.warning("Symlink already missing, continuing...")

            logger.info("Symlink removed")

            logger.info("Removing link from database...")
            for m in Mod.query.filter(
                    and_(Mod.collections.any(id=collection_id),
                         Mod.filename == mod.filename)):
                logger.debug("Removing Mod: {!r}".format(m))
                coll.rem_mod(m)
            db_session.commit()
            logger.info("Database commit successful")
        else:
            logger.error("Mod is not in this collection, no action required")

        logger.info("Mod removal successful")
    elif mod is None:
        logger.error("Mod does not exist")
        raise exceptions.ModNotExistError(mod_id)
    else:
        logger.error("Collection doesn't exist")
        raise exceptions.CollectionNotExistError(collection_id)
示例#3
0
def add_mod(collection_id, mod_id):
    """
    Adds a mod to a collection
    :param collection_id: int
    :param mod_id: int
    :return: None
    """
    try:
        collection_id = int(collection_id)
    except ValueError:
        logger.critical("Collection id is not a valid integer")
        raise

    try:
        mod_id = int(mod_id)
    except ValueError:
        logger.critical("Mod id is not a valid integer")
        raise

    logger.info("Adding mod {} to collection {}".format(mod_id, collection_id))
    coll = Collection.query.get(collection_id)
    mod = Mod.query.get(mod_id)

    if mod is not None and coll is not None:
        logger.info("Beginning addition...")

        # 1. Create symlink
        logger.info("Creating Symbolic link...")
        try:
            create_symbolic_link(
                join_path(config["application_root"], "mods", mod.filename),
                join_path(config["application_root"], "collections",
                          coll.mc_id.decode(), "mods",
                          get_file_name(mod.filename)))
        except FileExistsError:
            logger.warning("File already exists, continuing...")

        logger.info("Symlink created")

        # 2. Add relationship to database, for all mods in this file
        for m in Mod.query.filter(Mod.filename == mod.filename):
            logger.debug(
                "Adding mod: {!r} to collection: {!r} in database".format(
                    m, coll))
            coll.add_mod(m)
        db_session.commit()
        logger.info("Mod added successfully")
    elif mod is None:
        logger.error("Mod does not exist")
        raise exceptions.ModNotExistError(mod_id)
    else:
        logger.error("Collection does not exist")
        raise exceptions.CollectionNotExistError(collection_id)
示例#4
0
    def _add_mod_author(self, m, author):
        """
        Adds a mod author
        :param m: Mod
        :param author: String
        :return: None
        """
        self.logger.debug("Adding a new Author: {}".format(author))
        x = ModAuthor(author)
        db_session.add(x)
        db_session.commit()

        m.authors.append(x)
        self.logger.debug("Successfully added Author")
示例#5
0
    def _add_mod_dependency(self, m, modid):
        """
        Adds a mod dependency
        :param m: Mod
        :param modid: String
        :return: None
        """
        self.logger.debug("Looking up DepMod for modid: {}".format(modid))
        x = DepMod.query.get(modid)
        if x is None:
            self.logger.debug("DepMod doesn't yet exist, creating one")
            x = DepMod(modid)
            db_session.add(x)
            db_session.commit()
            self.logger.debug("Added to database successfully")

        m.dependencies.append(x)
        self.logger.debug("Successfully added dependency")
示例#6
0
def add_collection(args):
    """
    Add a collection to the database
    :param args:
    :return: None
    """
    lookup = Collection.query.filter(
        Collection.name == args.get("name")).first()
    print(lookup)
    if lookup:
        #return '/collection/{}'.format(lookup.id)  # a collection by that name already exists
        return "/collection/{}".format(lookup.id)
    else:
        coll = Collection(args.get("name"), args.get("mcversion"),
                          args.get("version_id"))
        db_session.add(coll)
        db_session.commit()

        return '/collection/{}'.format(coll.id)
示例#7
0
def add(name, mcversion, version_id):
    """
    Adds a new collection (if it doesn't exist already) to the DB, Minecraft, & the FileSystem
    :param name: string, user-friendly name of the Collection
    :param mcversion: string, general Minecraft Version for the collection. e.g. 1.7.10
    :param version_id: string, exact name of a specific version of minecraft. e.g. 1.7.10-Forge10.13.4.1558-1.7.10
    :return: int, id of the newly created Collection
    """
    logger.info("Adding collection: {}, {}, {}".format(name, mcversion,
                                                       version_id))
    if not name_exists(
            name):  # if a Collection by this name doesn't yet exist:
        # 1. Add it to the database
        logger.info("Adding to database")
        c = Collection(name, mcversion, version_id)
        db_session.add(c)
        db_session.commit()
        logger.info("Committed to database")

        # 2. Creates it's directory and filesystem
        logger.info("Creating filesystem...")
        coll_loc = _create_collection_filesystem(
            join_path(config["application_root"], "collections"), c.mc_id)
        logger.info("Filesystem created")

        # 3. Add it to Minecraft as a Profile
        logger.info("Adding Minecraft Profile")
        prof = mc_interface.Profile(c.mc_id, name, version_id, coll_loc)
        prof.commit()
        logger.info("New Profile committed")

        print(
            mc_interface.Profile(c.mc_id, name, version_id,
                                 coll_loc).dictify())

        return c.id
    else:
        logger.error(
            "A Collection with the name: {} already exists".format(name))
        raise exceptions.CollectionExistsError(
            str(Collection.query.filter(Collection.name == name).first().id))
示例#8
0
    def add_to_database(self,
                        filename,
                        dl_url=None,
                        curse_id=None,
                        curse_file_id=None):
        """
        Add each mod to the database
        :param filename: string, absolute path to the filename
        :param dl_url: string, URL the mod was downloaded from, if it was acquired via direct DL
        :return: None
        """
        self.logger.debug("Adding mods to database...")
        for mod in self.mods:
            mod.add_to_database(filename,
                                dl_url,
                                curse_id=None,
                                curse_file_id=None)

        # commit all of the changes
        db_session.commit()

        self.logger.debug("Mod Commit successful")
示例#9
0
"""
Creates the moddata.sqlite database
"""

from simple_mod_installer.database import init_db, db_session
from simple_mod_installer.database.models import *

# create the database
init_db()

# create collection
c = Collection("test", "1.7.10", "Minecraft 1.7.10")
db_session.add(c)
db_session.commit()
示例#10
0
def remove(id):
    """
    Removes a mod from the filesystem and database, including deletion of all links to collections
    :param id: int
    :return: None
    """
    logger.info("Beginning removal of mod with id: {}".format(id))

    # 0. Get file name:
    logger.debug("Getting Mod from database...")
    m = Mod.query.get(id)

    if m is None:
        logger.error("Mod doesn't exist in database")
        raise exceptions.ModNotExistError(id)

    filename = m.filename

    if filename is None:
        logger.critical(
            "Database entry is missing mission-critical filename data. Throwing error..."
        )
        raise exceptions.ModInfoNotExistError(id, 'filename')

    # 1. Remove all symlinks in Collections
    logger.info("Removing from collections: {}".format(m.collections))
    for coll in m.collections:
        logger.debug("Working with collection {}...".format(coll.id))
        logger.debug("Removing Symbolic link...")
        try:
            os.unlink(
                join_path(config["application_root"], "collections",
                          coll.mc_id.decode(), "mods", filename))
        except FileNotFoundError as ex:
            logger.error("Symlink doesn't exist")
        except OSError as ex:
            logger.critical(
                "Insufficient Permissions for Symbolic link manipulation: {}".
                format(ex.args))

        logger.debug("Symlink removed successfully")

        logger.debug("Removing relationship...")
        coll.rem_mod(m)
        logger.debug("Relationship removed")

    logger.info("Finished removing from collections")

    # 2. Delete mod file:
    logger.info("Deleting mod file...")
    os.remove(join_path(config["application_root"], "mods", filename))
    logger.info("Deletion successful")

    # 3. Delete Mod from database
    logger.info("Removing from database...")
    for mod in Mod.query.filter(Mod.filename == m.filename):
        logger.debug("Removing mod: {!r} from database".format(mod))
        db_session.delete(mod)
    logger.info("Removed")

    logger.debug("Committing Database changes...")
    db_session.commit()
    logger.debug("Commit successful")

    logger.info("Finished Removing mod with id {}".format(id))