def root(): if request.method == "POST": l1 = request.form["l1-name"] l2 = request.form["l2-name"] lang = model.Language(l1=l1, l2=l2) user = model.User(language=lang) db.session.add(user) db.session.commit() if not model.User.query.all(): return render_template("language_setup.html") else: return render_template("main.html", articles=model.Article.query.all(), language=language())
def get_language(): """returns a test Language""" LANG_ARGS = { "name": "fakelang", "syntax_mode": "clike", "is_enabled": True, "run_script": "#!/bin/bash\nfakelang $1", } # create and add python lang lang = model.Language(**LANG_ARGS) db_session.add(lang) db_session.commit() return LANG_ARGS, lang
def load_languages(session): #populate languages table language_1 = ["en-US", "English"] language_2 = ["es-MX", "Spanish"] language_3 = ["fr-FR", "French"] language1 = model.Language( language_code=language_1[0], language_name=language_1[1]) language2 = model.Language( language_code=language_2[0], language_name=language_2[1]) language3=model.Language( language_code=language_3[0], language_name=language_3[1]) session.add(language1) session.add(language2) session.add(language3) session.commit()
def test_language(self): """test the language table""" LANG_ARGS = { "name": "fakelang", "syntax_mode": "clike", "is_enabled": True, "run_script": "#!/bin/bash\nfakelang $1", } # create and add python lang lang = model.Language(**LANG_ARGS) db_session.add(lang) db_session.commit() # fetch lang results = model.Language.query.filter_by(name=LANG_ARGS["name"]).all() self.assertEqual(len(results), 1)
def populate_db(): """Performs the initial database setup for the application """ current_app.logger.info("Initializing db tables") db_session.add_all([ model.UserRole("defendant"), model.UserRole("operator"), model.UserRole("judge"), model.UserRole("executioner"), model.UserRole("observer") ]) # TODO: extract these out into a folder db_session.add_all([ model.Language("python", "python", True, textwrap.dedent(''' #!/bin/bash cat $1 | python3 $2 exit $?''').strip()), model.Language("python2", "python", True, textwrap.dedent(''' #!/bin/bash cat $1 | python2 $2 exit $?''').strip()), model.Language("perl", "perl", True, textwrap.dedent(''' #!/bin/bash cat $1 | perl $2 exit $?''').strip()), model.Language("lua", "lua", True, textwrap.dedent(''' #!/bin/bash cat $1 | lua $2 exit $?''').strip()), model.Language("nodejs", "javascript", True, textwrap.dedent(''' #!/bin/bash cat $1 | node $2 exit $?''').strip()), model.Language("guile", "scheme", True, textwrap.dedent(''' #!/bin/bash cat $1 | guile --no-auto-compile $2 exit $?''').strip()), model.Language("fortran", "fortran", True, textwrap.dedent(''' #!/bin/bash cp /share/program /scratch/program.f cd /scratch gfortran -o program /scratch/program.f if [[ $? != 0 ]]; then exit $? fi cat $1 | ./program exit $?''').strip()), model.Language("c", "clike", True, textwrap.dedent(''' #!/bin/bash cp /share/program /scratch/program.c cd /scratch gcc -o program /scratch/program.c if [[ $? != 0 ]]; then exit $? fi cat $1 | ./program exit $?''').strip(), textwrap.dedent(''' #include <stdio.h> int main(int argc, const char* argv[]) { }''')), model.Language("c++", "clike", True, textwrap.dedent(''' #!/bin/bash cp /share/program /scratch/program.cpp cd /scratch g++ -o program /scratch/program.cpp if [[ $? != 0 ]]; then exit $? fi cat $1 | ./program exit $?''').strip(), textwrap.dedent(''' #include <iostream> int main() { std::cout << "Hello World!"; }''')), model.Language("java", "clike", True, textwrap.dedent(''' #!/bin/bash cp /share/program /scratch/Main.java cd /scratch /usr/lib/jvm/java-1.8-openjdk/bin/javac Main.java if [[ $? != 0 ]]; then exit $? fi cat $1 | /usr/lib/jvm/java-1.8-openjdk/bin/java Main exit $?''').strip(), textwrap.dedent(''' public class Main { public static void main(String[] args) { } }''')), model.Language("ruby", "ruby", True, textwrap.dedent(''' #!/bin/bash cat $1 | ruby $2 exit $?''').strip()), model.Language("rust", "rust", True, textwrap.dedent(''' #!/bin/bash cp /share/program /scratch/main.rs cd /scratch rustc /scratch/main.rs if [[ $? != 0 ]]; then exit $? fi cat $1 | ./main exit $?''').strip()) ]) db_session.add_all([ model.Configuration("strict_whitespace_diffing", "False", "bool", "admin"), model.Configuration("contestants_see_sample_output", "True", "bool", "defendant"), model.Configuration("max_user_submissions", "5", "integer", "defendant"), model.Configuration("user_submission_time_limit", "1", "integer", "defendant"), model.Configuration("max_output_length", str(10 * 1024), "integer", "defendant") ]) db_session.add_all([ model.ProblemType("input-output", '#!/bin/bash\ntest "$1" = "$2"') ]) db_session.commit() roles = {x.name: x for x in model.UserRole.query.all()} db_session.add_all([ model.User( "*****@*****.**", "Admin", "pass", user_roles=[roles['operator']]), model.User( "*****@*****.**", "Executioner", "epass", user_roles=[roles['executioner']]) ]) db_session.commit()
def add_lang(): """ Adds or edits a language Note: must be called from within a request context Returns: a redirect to the language view page """ name = request.form.get("name") syntax_mode = request.form.get("syntax_mode") is_enabled = request.form.get("is_enabled") run_script = request.form.get("run_script", "").replace('\r\n', '\n') default_template = request.form.get("default_template", "").replace('\r\n', '\n') if name is None: error = "Failed to add language due to undefined language name." current_app.logger.info(error) flash(error, "danger") return redirect(url_for("languages.languages_view")) if syntax_mode is None: error = "Failed to add language due to undefined syntax_mode." current_app.logger.info(error) flash(error, "danger") return redirect(url_for("languages.languages_view")) # convert is_enabled to a bool is_enabled_bool = util.checkbox_result_to_bool(is_enabled) if is_enabled_bool is None: error = "Failed to add language \'{}\' due to invalid is_enabled check.".format( name) current_app.logger.info(error) flash(error, "danger") return redirect(url_for("languages.languages_view")) lang_id = util.i(request.form.get('lang_id')) if lang_id: # edit lang = model.Language.query.filter_by(id=lang_id).one() lang.name = name lang.syntax_mode = syntax_mode lang.is_enabled = is_enabled_bool lang.run_script = run_script lang.default_template = default_template else: # add if is_dup_lang_name(name): error = "Failed to add language \'{}\' as language already exists.".format( name) current_app.logger.info(error) flash(error, "danger") return redirect(url_for("languages.languages_view")) lang = model.Language(name, syntax_mode, is_enabled_bool, run_script, default_template) db_session.add(lang) db_session.commit() return redirect(url_for("languages.languages_view"))
def populate_db(): """Performs the initial database setup for the application """ current_app.logger.info("Initializing db tables") db_session.add_all([ model.UserRole("defendant"), model.UserRole("operator"), model.UserRole("judge"), model.UserRole("executioner"), model.UserRole("observer"), ]) # TODO: extract these out into a folder db_session.add_all([ model.Language( "python", "python", True, textwrap.dedent(""" #!/bin/bash cat $input_file | python3 $program_file exit $?""").strip(), ), model.Language( "python2", "python", True, textwrap.dedent(""" #!/bin/bash cat $input_file | python2 $program_file exit $?""").strip(), ), model.Language( "perl", "perl", True, textwrap.dedent(""" #!/bin/bash cat $input_file | perl $program_file exit $?""").strip(), ), model.Language( "lua", "lua", True, textwrap.dedent(""" #!/bin/bash cat $input_file | lua $program_file exit $?""").strip(), ), model.Language( "nodejs", "javascript", True, textwrap.dedent(""" #!/bin/bash cat $input_file | node $program_file exit $?""").strip(), ), model.Language( "guile", "scheme", True, textwrap.dedent(""" #!/bin/bash cat $input_file | guile --no-auto-compile $program_file exit $?""").strip(), ), model.Language( "fortran", "fortran", True, textwrap.dedent(""" #!/bin/bash cp /share/program $scratch_dir/program.f cd $scratch_dir gfortran -o program $scratch_dir/program.f if [[ $? != 0 ]]; then exit $? fi cat $input_file | ./program exit $?""").strip(), ), model.Language( "c", "clike", True, textwrap.dedent(""" #!/bin/bash cp $program_file $scratch_dir/program.c cd $3 gcc -o program $scratch_dir/program.c if [[ $? != 0 ]]; then exit $? fi cat $input_file | ./program exit $?""").strip(), textwrap.dedent(""" #include <stdio.h> int main(int argc, const char* argv[]) { }"""), ), model.Language( "c++", "clike", True, textwrap.dedent(""" #!/bin/bash cp $program_file $scratch_dir/program.cpp cd $scratch_dir g++ -o program $scratch_dir/program.cpp if [[ $? != 0 ]]; then exit $? fi cat $input_file | ./program exit $?""").strip(), textwrap.dedent(""" #include <iostream> int main() { std::cout << "Hello World!"; }"""), ), model.Language( "java", "clike", True, textwrap.dedent(""" #!/bin/bash export PATH=$PATH:/usr/lib/jvm/java-1.8-openjdk/bin cp $program_file $scratch_dir/Main.java cd $scratch_dir javac Main.java if [[ $? != 0 ]]; then exit $? fi cat $input_file | java Main exit $?""").strip(), textwrap.dedent(""" public class Main { public static void main(String[] args) { } }"""), ), model.Language( "ruby", "ruby", True, textwrap.dedent(""" #!/bin/bash cat $input_file | ruby $program_file exit $?""").strip(), ), model.Language( "rust", "rust", True, textwrap.dedent(""" #!/bin/bash cp /share/program $scratch_dir/main.rs cd $scratch_dir rustc $scratch_dir/main.rs if [[ $? != 0 ]]; then exit $? fi cat $input_file | ./main exit $?""").strip(), textwrap.dedent(""" fn main() { } """).strip(), ), ]) db_session.add_all([ model.Configuration("strict_whitespace_diffing", "False", "bool", "admin"), model.Configuration("contestants_see_sample_output", "True", "bool", "defendant"), model.Configuration("max_user_submissions", "5", "integer", "defendant"), model.Configuration("user_submission_time_limit", "1", "integer", "defendant"), model.Configuration("max_output_length", str(10 * 1024), "integer", "defendant"), model.Configuration("run_refresh_interval_millseconds", 5000, "integer", "defendant"), model.Configuration("score_refresh_interval_millseconds", 30000, "integer", "defendant"), model.Configuration("misc_refresh_interval_millseconds", 12000, "integer", "defendant"), model.Configuration("extra_signup_fields", "[]", "json", "defendant"), ]) db_session.add_all( [model.ProblemType("input-output", '#!/bin/bash\ntest "$1" = "$2"')]) db_session.commit() roles = {x.name: x for x in model.UserRole.query.all()} db_session.add_all([ model.User("admin", "Admin", "pass", user_roles=[roles["operator"]]), model.User("exec", "Executioner", "epass", user_roles=[roles["executioner"]]), ]) db_session.commit() # Version scraper run with open("init_data/printver.py", "r") as f: src_code = "\n".join(f.readlines()) executioner_user = model.User.query.filter_by(username="******").scalar() python = model.Language.query.filter_by(name="python").scalar() empty_input = "" version_contest = model.Contest( name="version_contest", start_time=datetime.datetime.utcnow(), end_time=datetime.datetime.utcnow() + datetime.timedelta(hours=1), is_public=True, activate_time=datetime.datetime.utcnow(), freeze_time=None, deactivate_time=None, ) db_session.add(version_contest) db_session.commit() verscrape_run = model.Run( executioner_user, version_contest, python, None, datetime.datetime.utcnow(), src_code, empty_input, empty_input, True, None, ) db_session.add(verscrape_run) db_session.commit()