def testCommand(args, options): problemName = args[0] directory = os.path.join(os.getcwd(), problemName) if not os.path.exists(problemName): promptToFetch(args, options) return # if programFile is not given, we will attempt to guess it programFile = (formatProgramFile(args[1]) if args[1:] else selectProgramFile(problemName)) if programFile == -1: return print("🔎 Running tests on " + programFile["name"]) if shouldCompile(programFile): if compile(programFile, directory) == -1: return inFiles, ansFiles = getTestFiles(problemName) passed = True command = getRunCommand(programFile) if command == -1: return for inF, ansF in zip(inFiles, ansFiles): result = runSingleTest(command, directory, inF, ansF) if not result: passed = False if passed and "archive" in options: archiveCommand(args, options)
def watchCommand(data): problemName = data["problem"] directory = os.path.join(os.getcwd(), problemName) if not os.path.exists(problemName): promptToFetch(problemName) return # if programFile is not given, we will attempt to guess it programFile = (formatProgramFile(data["file"]) if "file" in data and data['file'] else selectProgramFile(problemName)) if not programFile: return observer = Observer() event_handler = KatWatchEventHandler( { **data, "file": programFile["relativePath"] }, observer) observer.schedule(event_handler, directory) observer.start() print("🕵️ Watching " + programFile["relativePath"] + " for changes. Press Ctrl+C to terminate.") try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join()
def watchCommand(args, options): problemName = args[0] directory = os.path.join(os.getcwd(), problemName) if not os.path.exists(problemName): promptToFetch(args, options) return # if programFile is not given, we will attempt to guess it programFile = (formatProgramFile(args[1]) if args[1:] else selectProgramFile(problemName)) if programFile == -1: return event_handler = KatWatchEventHandler(problemName, programFile) observer = Observer() observer.schedule(event_handler, directory) observer.start() print("🕵️ Watching " + programFile["relativePath"] + " for changes") try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join()
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()))
def submitCommand(data): problemName: str = data["problem"] checkCorrectDomain(problemName, "submit") if not os.path.exists(problemName): promptToFetch(problemName) return Response.Error # if programFile is not given, we will attempt to guess it programFile = (formatProgramFile(data.get("file")) if data.get("file") else selectProgramFile(problemName)) if programFile == None: raise Exception("Could not guess programFile") if "force" not in data or not data.get('force'): response = confirm(problemName, programFile) if not response: return Response.Aborted try: session = requests.Session() print("📨 Submitting " + problemName + "...") id = postSubmission(session, problemName, programFile) print( f"📬 Submission Successful (url {getConfigUrl('submissionsurl', 'submissions')}/{id})" ) if id == None: return False response = Response.Failure try: response = printUntilDone(id, problemName, session) except: pass if data.get('sound'): if response == Response.Success: winsound() elif response == Response.Failure: losesound() if response == Response.Success: if data.get('archive'): archive(problemName, ".solved/") return response except requests.exceptions.ConnectionError: print("Connection error: Please check your connection") return Response.Error
def submitCommand(args, options): problemName = args[0] directory = os.path.join(os.getcwd(), problemName) if not os.path.exists(problemName): promptToFetch(args, options) return Response.Error # if programFile is not given, we will attempt to guess it programFile = (formatProgramFile(args[1]) if args[1:] else selectProgramFile(problemName)) if programFile == -1: raise Exception("Could not guess programFile") if "force" not in options: response = confirm(problemName, programFile) if not response: return Response.Aborted config = getConfig() session = requests.Session() print("📨 Submitting " + problemName + "...") id = postSubmission(config, session, problemName, programFile) print("📬 Submission Successful (url https://open.kattis.com/submissions/" + id + ")") if id == -1: return False response = Response.Failure try: response = printUntilDone(id, problemName, config, session, options) except: pass if "sound" in options: if response == Response.Success: winsound() elif response == Response.Failure: losesound() if response == Response.Success: if "archive" in options: archiveCommand(problemName, options, ".solved/") return response
def submitCommand(data): problemName = data["problem"] if not os.path.exists(problemName): promptToFetch(problemName) return Response.Error # if programFile is not given, we will attempt to guess it programFile = (formatProgramFile(data.get("file")) if data.get("file") else selectProgramFile(problemName)) if programFile == -1: raise Exception("Could not guess programFile") if "force" not in data or not data.get('force'): response = confirm(problemName, programFile) if not response: return Response.Aborted session = requests.Session() print("📨 Submitting " + problemName + "...") id = postSubmission(session, problemName, programFile) print("📬 Submission Successful (url " + getConfigUrl("submissionsurl", "submissions") + "/" + id + ")") if id == -1: return False response = Response.Failure try: response = printUntilDone(id, problemName, session) except: pass if data.get('sound'): if response == Response.Success: winsound() elif response == Response.Failure: losesound() if response == Response.Success: if data.get('archive'): archive(problemName, ".solved/") return response
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()
def testCommand(data: dict): problemName = data['problem'] directory = os.path.join(os.getcwd(), problemName) if not os.path.exists(problemName): promptToFetch(problemName) return # if programFile is not given, we will attempt to guess it programFile = (formatProgramFile(data["file"]) if data.get('file') else selectProgramFile(problemName)) if not programFile: return print("🔎 Running tests on " + programFile["name"]) if shouldCompile(programFile): if compile(programFile, directory) == -1: return inFiles, ansFiles = getTestFiles(problemName) command = getRunCommand(programFile) if command == -1: return testsToRun = data.get('interval') passed = True times = [] for i, (inF, ansF) in enumerate(zip(inFiles, ansFiles)): if testsToRun and i not in testsToRun: continue result, time = runSingleTest(command, directory, inF, ansF) if not result: passed = False else: times.append(time) times.sort() if passed: if len(times) == 1: print(f"🕑 Test took {times[0]:0.2f} seconds") else: print( f"🕑 Tests took from {times[0]:0.2f} to {times[-1]:0.2f} seconds" ) shouldEnd = None if passed: if data.get('submit'): submitCommand({ "problem": problemName, "file": programFile['relativePath'] }) shouldEnd = True if data.get('archive'): archive(problemName) shouldEnd = True if shouldEnd: return shouldEnd
def testCommand(data: dict): problemName = data['problem'] directory = os.path.join(os.getcwd(), problemName) if not os.path.exists(problemName): promptToFetch(problemName) return # if programFile is not given, we will attempt to guess it programFile = (formatProgramFile(data["file"]) if data.get('file') else selectProgramFile(problemName)) if not programFile: return command = getAndPrepareRunCommand(programFile) if command == None: return print("🔎 Running tests on " + programFile["name"]) inFiles, ansFiles = getTestFiles(problemName) if not inFiles and not ansFiles: print("This problem doesn't seem to have any tests...") print("Will not test.") return testsToRun = data.get('interval') passed = True times = [] try: for i, (inF, ansF) in enumerate(zip(inFiles, ansFiles)): if testsToRun and i not in testsToRun: continue result, time = runSingleTest(command, directory, inF, ansF) if not result: passed = False else: times.append(time) except KeyboardInterrupt: print('Ending tests due to keyboard interrupt...') return times.sort() if passed: if len(times) == 1: print(f"🕑 Test took {times[0]:0.2f} seconds") else: print( f"🕑 Tests took from {times[0]:0.2f} to {times[-1]:0.2f} seconds" ) shouldEnd = None if passed: if data.get('submit'): submitCommand({ "problem": problemName, "file": programFile['relativePath'], "archive": data.get('archive') }) shouldEnd = True elif data.get('archive'): archive(problemName) shouldEnd = True if shouldEnd: return shouldEnd