Пример #1
0
def pkg_load_exercise():
    # Query exercise DB object
    exercise_id = json.loads(request.data)['exercise_id']
    exercise = CourseExercise.query.filter_by(id=exercise_id).first()
    if exercise == None:
        raise PackageError("Exercise with id '" + exercise_id +
                           "' does not exist.")

    pkgName = exercise.title
    zipName = pkgName + '.zip'
    if docker_interface.file_exists(session['user_container_name'], pkgName):
        raise PackageError("A package with the name '" + packageName +
                           "' already exists.")

    try:
        with LFTransfer(session['user_container_name']) as lft:
            # Create zip file
            zipPath = os.path.join(lft.get_filetransfer_folder(), zipName)
            zipfb = open(zipPath, 'wb')
            zipfb.write(exercise.archive.decode('base64'))
            zipfb.close()
            # unzip in intermediate container
            zipf = zipfile.ZipFile(zipPath, 'r')
            zipf.extractall(lft.get_filetransfer_folder())
            zipf.close()
            # copy to user container
            lft.to_container(pkgName, pkgName)
        return jsonify(success=1)
    except:  # catch *all* exceptions
        app.logger.error("Unable to load exercise. " + str(sys.exc_info()[0]))
        raise PackageError("An internal error occurred.")
Пример #2
0
def pkg_new():
    packageName = json.loads(request.data)['packageName']
    
    # Create package root directory
    if docker_interface.file_exists(session['user_container_name'], packageName):
        app.logger.warning("Package already exists.")
        return jsonify(result=None)
    
    # Make sure templates are available
    templatePath = os.path.abspath('webrob/templates/package')
    if not os.path.exists(templatePath):
        app.logger.warning("Package templates missing at " + templatePath + ".")
        return jsonify(result=None)

    try:
        with LFTransfer(session['user_container_name']) as lft:
            pkgPath = os.path.join(lft.get_filetransfer_folder(), packageName)
            # Copy package template to user_data container while replacing some keywords
            for root, dirs, files in os.walk(templatePath):
                for f in files:
                    abs_p = os.path.join(root, f)
                    rel_p = os.path.relpath(abs_p, templatePath)
                    user_p = os.path.join(pkgPath, rel_p)
                    copy_template_file(abs_p, user_p, {
                        "pkgName": packageName,
                        "userName": session['user_container_name']
                    })
            lft.to_container(packageName, packageName)
    except:  # catch *all* exceptions
        app.logger.error(str(sys.exc_info()[0]))
        pkg_del(packageName)
    
    return jsonify(result=None)
Пример #3
0
def pkg_new():
    packageName = json.loads(request.data)['packageName']
    
    if docker_interface.file_exists(session['user_container_name'], packageName):
        raise PackageError("A package with the name '"+packageName+"' already exists.")
    
    # Make sure templates are available
    templatePath = '/opt/webapp/webrob/templates/package'
    if not os.path.exists(templatePath):
        raise PackageError("Package template could not be found.")

    try:
        with LFTransfer(session['user_container_name']) as lft:
            pkgPath = os.path.join(lft.get_filetransfer_folder(), packageName)
            
            # Copy package template to user_data container while replacing some keywords
            for root, dirs, files in os.walk(templatePath):
                for f in files:
                    abs_p = os.path.join(root, f)
                    rel_p = os.path.relpath(abs_p, templatePath)
                    user_p = os.path.join(pkgPath, rel_p)
                    copy_template_file(abs_p, user_p, {
                        "pkgName": packageName,
                        "userName": session['user_container_name']
                    })
            lft.to_container(packageName, packageName)
    except:  # catch *all* exceptions
        app.logger.error("Unable to create package.")
        app.logger.error(str(sys.exc_info()[0]))
        app.logger.error(str(traceback.format_exc()))
        pkg_del(packageName)
        raise PackageError("An internal error occurred.")
    
    return jsonify(success=1)
Пример #4
0
def pkg_load_exercise():
    # Query exercise DB object
    exercise_id = json.loads(request.data)['exercise_id']
    exercise = CourseExercise.query.filter_by(id=exercise_id).first()
    if exercise==None:
        raise PackageError("Exercise with id '"+exercise_id+"' does not exist.")
    
    pkgName = exercise.title
    zipName = pkgName + '.zip'
    if docker_interface.file_exists(session['user_container_name'], pkgName):
        raise PackageError("A package with the name '"+packageName+"' already exists.")
    
    try:
        with LFTransfer(session['user_container_name']) as lft:
            # Create zip file
            zipPath = os.path.join(lft.get_filetransfer_folder(), zipName)
            zipfb = open(zipPath, 'wb')
            zipfb.write(exercise.archive.decode('base64'))
            zipfb.close()
            # unzip in intermediate container
            zipf = zipfile.ZipFile(zipPath, 'r')
            zipf.extractall(lft.get_filetransfer_folder())
            zipf.close()
            # copy to user container
            lft.to_container(pkgName, pkgName)
        return jsonify(success=1)
    except:  # catch *all* exceptions
        app.logger.error("Unable to load exercise. " + str(sys.exc_info()[0]))
        raise PackageError("An internal error occurred.")
Пример #5
0
def pkg_new():
    packageName = json.loads(request.data)['packageName']

    # Create package root directory
    if docker_interface.file_exists(session['user_container_name'],
                                    packageName):
        app.logger.warning("Package already exists.")
        return jsonify(result=None)

    # Make sure templates are available
    templatePath = os.path.abspath('webrob/templates/package')
    if not os.path.exists(templatePath):
        app.logger.warning("Package templates missing at " + templatePath +
                           ".")
        return jsonify(result=None)

    try:
        with LFTransfer(session['user_container_name']) as lft:
            pkgPath = os.path.join(lft.get_filetransfer_folder(), packageName)
            # Copy package template to user_data container while replacing some keywords
            for root, dirs, files in os.walk(templatePath):
                for f in files:
                    abs_p = os.path.join(root, f)
                    rel_p = os.path.relpath(abs_p, templatePath)
                    user_p = os.path.join(pkgPath, rel_p)
                    copy_template_file(
                        abs_p, user_p, {
                            "pkgName": packageName,
                            "userName": session['user_container_name']
                        })
            lft.to_container(packageName, packageName)
    except:  # catch *all* exceptions
        app.logger.error("Unable to create package.")
        app.logger.error(str(sys.exc_info()[0]))
        app.logger.error(str(traceback.format_exc()))
        pkg_del(packageName)

    return jsonify(result=None)
Пример #6
0
def pkg_new():
    packageName = json.loads(request.data)['packageName']

    if docker_interface.file_exists(session['user_container_name'],
                                    packageName):
        raise PackageError("A package with the name '" + packageName +
                           "' already exists.")

    # Make sure templates are available
    templatePath = '/opt/webapp/webrob/templates/package'
    if not os.path.exists(templatePath):
        raise PackageError("Package template could not be found.")

    try:
        with LFTransfer(session['user_container_name']) as lft:
            pkgPath = os.path.join(lft.get_filetransfer_folder(), packageName)

            # Copy package template to user_data container while replacing some keywords
            for root, dirs, files in os.walk(templatePath):
                for f in files:
                    abs_p = os.path.join(root, f)
                    rel_p = os.path.relpath(abs_p, templatePath)
                    user_p = os.path.join(pkgPath, rel_p)
                    copy_template_file(
                        abs_p, user_p, {
                            "pkgName": packageName,
                            "userName": session['user_container_name']
                        })
            lft.to_container(packageName, packageName)
    except:  # catch *all* exceptions
        app.logger.error("Unable to create package.")
        app.logger.error(str(sys.exc_info()[0]))
        app.logger.error(str(traceback.format_exc()))
        pkg_del(packageName)
        raise PackageError("An internal error occurred.")

    return jsonify(success=1)
Пример #7
0
def _check_pkg_name_and_raise_error_if_already_exists(pkg_name):
    if docker_interface.file_exists(session['user_container_name'], pkg_name):
        raise PackageError("A package with the name '" + pkg_name +
                           "' already exists.")
    return pkg_name