def update_project(project_data, project_id):
    """
    Update the Project

    :param project_data: The project Data
    :param project_id: The project ID to update
    :return: The updated project descriptor
    """
    session = db_session()
    project = session.query(Project).filter(Project.id == project_id).first()
    if project is None:
        raise NotFound("Project with id {} could not be found".format(project_id))

    # Update name
    if 'name' in project_data and project_data['name'] != project.name:
        if os.path.exists(get_project_path(project.workspace.path, project.rel_path)):
            new_name = shlex.quote(project_data['name'])
            old_path = get_project_path(project.workspace.path, project.rel_path)
            new_path = rreplace(old_path, project.name, new_name, 1)

            if os.path.exists(new_path):
                raise NameConflict("Invalid name parameter, workspace '{}' already exists".format(new_name))

            # Do not allow move directories outside of the workspaces_dir
            if not new_path.startswith(WORKSPACES_DIR):
                raise Exception("Invalid path parameter, you are not allowed to break out of {}".format(WORKSPACES_DIR))
            else:
                # Move the directory
                shutil.move(old_path, new_path)
                project.name = new_name
                project.rel_path = new_name
    set_data(project, project_data)
    sync_project_descriptor(project)
    db_session.commit()
    return project.as_dict()
def update_project(project_data, project_id):
    """
    Update the Project
    :param project_data:
    :param project_id:
    :return:
    """
    session = db_session()
    project = session.query(Project).filter(Project.id == project_id).first()
    if project is None:
        raise NotFound("Project with id {} could not be found".format(project_id))

    # Update name
    if 'name' in project_data and project_data['name'] != project.name:
        if os.path.exists(get_project_path(project.workspace.path, project.rel_path)):
            new_name = shlex.quote(project_data['name'])
            old_path = get_project_path(project.workspace.path, project.rel_path)
            new_path = rreplace(old_path, project.name, new_name, 1)

            if os.path.exists(new_path):
                raise NameConflict("Invalid name parameter, workspace '{}' already exists".format(new_name))

            # Do not allow move directories outside of the workspaces_dir
            if not new_path.startswith(WORKSPACES_DIR):
                raise Exception("Invalid path parameter, you are not allowed to break out of {}".format(WORKSPACES_DIR))
            else:
                # Move the directory
                shutil.move(old_path, new_path)
                project.name = new_name
                project.rel_path = new_name
    set_data(project, project_data)
    sync_project_descriptor(project)
    db_session.commit()
    return project.as_dict()
def update_workspace(workspace_data, wsid):
    """
    Updates the workspace with the given workspace data

    :param workspace_data: The new workspace configuration
    :param wsid: the workspace ID
    :return: The updated workspace
    """
    session = db_session()
    workspace = session.query(Workspace).filter(Workspace.id == int(wsid)).first()  # type: Workspace
    if workspace is None:
        raise NotFound("Workspace with id {} could not be found".format(wsid))

    # Update name
    if 'name' in workspace_data:
        if path.exists(workspace.path):
            new_name = workspace_data['name']
            old_path = workspace.path
            # only update if name has changed
            if new_name != workspace.name:
                new_path = rreplace(workspace.path, workspace.name, new_name, 1)

                if path.exists(new_path):
                    raise NameConflict("Invalid name parameter, workspace '{}' already exists".format(new_name))

                # Do not allow move directories outside of the workspaces_dir
                if not new_path.startswith(WORKSPACES_DIR):
                    raise Exception(
                        "Invalid path parameter, you are not allowed to break out of {}".format(WORKSPACES_DIR))
                else:
                    # Move the directory
                    shutil.move(old_path, new_path)
                    workspace.name = new_name
                    workspace.path = new_path
    for platform in workspace.platforms:
        deleted = True
        if 'platforms' in workspace_data:
            for updated_platform in workspace_data['platforms']:
                if 'id' in updated_platform and platform.id == updated_platform['id']:
                    deleted = False
                    break
        if deleted:
            session.delete(platform)
    if 'platforms' in workspace_data:
        for updated_platform in workspace_data['platforms']:
            platform = None
            if 'id' in updated_platform:
                platform = session.query(Platform). \
                    filter(Platform.id == updated_platform['id']). \
                    filter(Platform.workspace == workspace). \
                    first()
            if platform:
                # update existing
                test_url(updated_platform['name'], updated_platform['url'] + "/api/v2/packages")
                platform.name = updated_platform['name']
                platform.url = updated_platform['url']
                if 'token' in updated_platform:
                    platform.token_path = create_token_file(updated_platform['token'])
            else:
                # create new
                test_url(updated_platform['name'], updated_platform['url'] + "/api/v2/packages")  # TODO test this!
                new_platform = Platform(updated_platform['name'], updated_platform['url'], True, workspace)
                session.add(new_platform)
    for catalogue in workspace.catalogues:
        deleted = True
        if 'catalogues' in workspace_data:
            for updated_catalogue in workspace_data['catalogues']:
                if 'id' in updated_catalogue and catalogue.id == updated_catalogue['id']:
                    deleted = False
                    break
        if deleted:
            # check if catalogue is still referenced
            for project in workspace.projects:
                if catalogue.name in project.publish_to:
                    raise InvalidArgument(
                        "Cannot delete catalogue '{}' because it is still used in project '{}'!".format(catalogue.name,
                                                                                                        project.name))
            session.delete(catalogue)
    if 'catalogues' in workspace_data:
        for updated_catalogue in workspace_data['catalogues']:
            catalogue = None
            if 'id' in updated_catalogue:
                catalogue = session.query(Catalogue). \
                    filter(Catalogue.id == updated_catalogue['id']). \
                    filter(Catalogue.workspace == workspace). \
                    first()
            if catalogue:
                # update existing
                test_url(updated_catalogue['name'], updated_catalogue['url'])
                catalogue.name = updated_catalogue['name']
                catalogue.url = updated_catalogue['url']
            else:
                # create new
                test_url(updated_catalogue['name'], updated_catalogue['url'])
                new_catalogue = Catalogue(updated_catalogue['name'], updated_catalogue['url'], True, workspace)
                session.add(new_catalogue)
    update_workspace_descriptor(workspace)
    db_session.commit()
    return workspace.as_dict()
Exemple #4
0
def update_workspace(workspace_data, wsid):
    """
    Updates the workspace with the given workspace data

    :param workspace_data: The new workspace configuration
    :param wsid: the workspace ID
    :return: The updated workspace
    """
    session = db_session()
    workspace = session.query(Workspace).filter(
        Workspace.id == int(wsid)).first()  # type: Workspace
    if workspace is None:
        raise NotFound("Workspace with id {} could not be found".format(wsid))

    # Update name
    if 'name' in workspace_data:
        if path.exists(workspace.path):
            new_name = workspace_data['name']
            old_path = workspace.path
            # only update if name has changed
            if new_name != workspace.name:
                new_path = rreplace(workspace.path, workspace.name, new_name,
                                    1)

                if path.exists(new_path):
                    raise NameConflict(
                        "Invalid name parameter, workspace '{}' already exists"
                        .format(new_name))

                # Do not allow move directories outside of the workspaces_dir
                if not new_path.startswith(WORKSPACES_DIR):
                    raise Exception(
                        "Invalid path parameter, you are not allowed to break out of {}"
                        .format(WORKSPACES_DIR))
                else:
                    # Move the directory
                    shutil.move(old_path, new_path)
                    workspace.name = new_name
                    workspace.path = new_path
    for platform in workspace.platforms:
        deleted = True
        if 'platforms' in workspace_data:
            for updated_platform in workspace_data['platforms']:
                if 'id' in updated_platform and platform.id == updated_platform[
                        'id']:
                    deleted = False
                    break
        if deleted:
            session.delete(platform)
    if 'platforms' in workspace_data:
        for updated_platform in workspace_data['platforms']:
            platform = None
            if 'id' in updated_platform:
                platform = session.query(Platform). \
                    filter(Platform.id == updated_platform['id']). \
                    filter(Platform.workspace == workspace). \
                    first()
            if platform:
                # update existing
                test_url(updated_platform['name'],
                         updated_platform['url'] + "/api/v2/packages")
                platform.name = updated_platform['name']
                platform.url = updated_platform['url']
                if 'token' in updated_platform:
                    platform.token_path = create_token_file(
                        updated_platform['token'])
            else:
                # create new
                test_url(updated_platform['name'], updated_platform['url'] +
                         "/api/v2/packages")  # TODO test this!
                new_platform = Platform(updated_platform['name'],
                                        updated_platform['url'], True,
                                        workspace)
                session.add(new_platform)
    for catalogue in workspace.catalogues:
        deleted = True
        if 'catalogues' in workspace_data:
            for updated_catalogue in workspace_data['catalogues']:
                if 'id' in updated_catalogue and catalogue.id == updated_catalogue[
                        'id']:
                    deleted = False
                    break
        if deleted:
            # check if catalogue is still referenced
            for project in workspace.projects:
                if catalogue.name in project.publish_to:
                    raise InvalidArgument(
                        "Cannot delete catalogue '{}' because it is still used in project '{}'!"
                        .format(catalogue.name, project.name))
            session.delete(catalogue)
    if 'catalogues' in workspace_data:
        for updated_catalogue in workspace_data['catalogues']:
            catalogue = None
            if 'id' in updated_catalogue:
                catalogue = session.query(Catalogue). \
                    filter(Catalogue.id == updated_catalogue['id']). \
                    filter(Catalogue.workspace == workspace). \
                    first()
            if catalogue:
                # update existing
                test_url(updated_catalogue['name'], updated_catalogue['url'])
                catalogue.name = updated_catalogue['name']
                catalogue.url = updated_catalogue['url']
            else:
                # create new
                test_url(updated_catalogue['name'], updated_catalogue['url'])
                new_catalogue = Catalogue(updated_catalogue['name'],
                                          updated_catalogue['url'], True,
                                          workspace)
                session.add(new_catalogue)
    update_workspace_descriptor(workspace)
    db_session.commit()
    return workspace.as_dict()