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(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 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 archive(problemName, folder=".archive/"): if os.path.exists(folder + problemName): return if not os.path.exists(problemName): promptToFetch(problemName) return shutil.move(problemName, folder + problemName) print("📦 Moved problem", problemName, "to " + folder[:-1])
def unarchive(problemName): folder = findProblemLocation(problemName) if folder is None: print("️️⚠️ You do not have this problem in your files") promptToFetch(problemName) if not folder: return shutil.move(folder + problemName, problemName) print("📦 Moved problem", problemName, "to main folder")
def unarchive(problemName, folder=None): if folder is None: folder = findProblemLocation(problemName) if folder is None: print("️️⚠️ You do not have this problem in your files") promptToFetch(problemName) if not folder: return try: shutil.move(folder + problemName, problemName) print(f"📦 Moved problem, {problemName}, to main folder from {folder}") except FileNotFoundError: print(f'Error: Problem "{problemName}" was not found in {folder}')
def testCommand(args, options): problemName = args[0] testIndice = args[1:] # gets test indexes for singulartests singularTest = len(testIndice) > 0 # bool for singulartests directory = os.path.join(os.getcwd(), problemName) # if it's a range of numbers (1-4) convert to list [1,2,3,4] # otherwise, convert to ints if singularTest: if not testIndice[0].isdigit(): rangeIs = testIndice[0].split("-") testIndice = list(range(int(rangeIs[0]), int(rangeIs[1]) + 1)) else: testIndice = [int(x) for x in testIndice] if not os.path.exists(problemName): promptToFetch(args, options) return # if programFile is not given, we will attempt to guess it programFile = 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 i, (inF, ansF) in enumerate(zip(inFiles, ansFiles)): if singularTest: if i + 1 in testIndice: result = runSingleTest(command, directory, inF, ansF) else: result = True else: result = runSingleTest(command, directory, inF, ansF) if not result: passed = False if passed and "archive" in options: archiveCommand(args, options)
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 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