예제 #1
0
def confirm(problemName, programFile):
    print("Are you sure you want to submit?")
    print("Problem: " + problemName)
    print("File: " + programFile["relativePath"])
    print("Language: " + guessLanguage(programFile))

    return yes()
예제 #2
0
파일: fileutils.py 프로젝트: Duckapple/Kat
def createBoilerplate(problemName, overrideLanguage=None):
    from helpers.programSelector import guessLanguage, formatProgramFile
    cfg = getConfig()
    if overrideLanguage:
        lang = overrideLanguage.lower()
    else:
        lang = cfg.get("kat", {}).get("language").lower()
    if lang in cfg["Initialize commands"]:
        cmd = cfg["Initialize commands"].get(lang).split()
        subprocess.run([p for p in cmd], cwd=problemName)
        if lang.lower() == 'rust':
            f = open(os.path.join(problemName, 'rust-toolchain'), 'w')
            f.write('1.26.0')
            f.close()
        return
    directory = os.path.dirname(os.path.realpath(
        __file__)) + "/../boilerplate"  #todo please make this better
    boilerplates = {
        guessLanguage(formatProgramFile(f)): f
        for f in os.listdir(directory)
        if os.path.isfile(os.path.join(directory, f))
    }

    fileName = problemName
    if lang in cfg["Naming"]:
        naming = cfg.get("Naming").get(lang)
        namingFn = namingSchemeConverters[naming]
        fileName = namingFn(fileName)

    if lang.lower() in boilerplates:
        boilerplate = boilerplates[lang]
        fileType = "." + boilerplates[lang].split(".")[-1]
        shutil.copy2(
            directory + "/" + boilerplate,
            problemName + "/" + fileName + fileType,
        )
    else:
        fileType = [
            file for (file, k) in cfg["File associations"].items()
            if k.lower() == lang.lower()
        ]
        if fileType:
            open(problemName + "/" + fileName + fileType[0], "a").close()
        else:
            #Language does not exist, delete the folder

            print(
                f"Error, unable to resolve {lang} to a language that could be run. Please check if your spelling matches the one used by kat tool."
            )
            print(f"These are the supported languages:")
            print(", ".join(cfg["File associations"].values()))
예제 #3
0
def postSubmission(session, problemName, programFile):
    login(session)

    url = getConfigUrl("submissionurl", "submit")
    language = guessLanguage(programFile)

    if language == -1:
        print("Could not guess language for " + programFile)
        raise Exception("Could not guess language for " + programFile)

    language = formatLanguage(language)
    data = {
        "submit": "true",
        "submit_ctr": 2,
        "language": language,
        "problem": problemName,
        "script": "true",
    }

    if requiresClass(programFile):
        data["mainclass"] = detectClassName(programFile)

    sub_files = []
    undoBOM(programFile["relativePath"])
    with open(programFile["relativePath"]) as sub_file:
        sub_files.append((
            "sub_file[]",
            (programFile["name"], sub_file.read(), "application/octet-stream"),
        ))

    response = session.post(url, data=data, files=sub_files, headers=HEADERS)

    body = response.content.decode("utf-8").replace("<br />", "\n")
    match = re.search(r"Submission ID: ([0-9]+)", body)

    if match is None:
        print(
            "Submission was received, but could not read ID from response.",
            "Visit the submission manually in the browser.",
        )
        print("Response was: " + body)
        return -1

    return match.group(1).strip()
예제 #4
0
def createBoilerplate(problemName):
    from helpers.programSelector import formatCommand, guessLanguage, formatProgramFile
    cfg = getConfig()
    lang = cfg.get("kat", "language")
    if lang in cfg["Initialize commands"]:
        cmd = cfg["Initialize commands"].getcommand(lang)
        subprocess.run([p for p in cmd], cwd=problemName)
        return
    directory = os.path.dirname(os.path.realpath(__file__)) + "/../boilerplate"
    boilerplates = {
        guessLanguage(formatProgramFile(f)): f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))
    }
    if lang in boilerplates:
        boilerplate = boilerplates[lang]
        fileType = "." + boilerplates[lang].split(".")[-1]
        shutil.copy2(
            directory + "/" + boilerplate,
            problemName + "/" + problemName + fileType,
        )
    else:
        fileType = [file for (file, k) in cfg["File associations"].items() if k.lower() == lang.lower()]
        open(problemName + "/" + problemName + fileType[0], "a").close()