def handle_build(args: List[str], lazy: bool = True, quiet: bool = False) -> None: if args: if not quiet: print("Please not put any arguments for handle_build") return settings = Settings.get_saved() if not settings.has_problem(): print("You don't seem to have a problem loaded") return if lazy: modif_time = str(os.stat(settings.working_file).st_mtime_ns) if str(modif_time) == settings.last_build_time: print("No change since last update. Not building.") return if build_target(settings.working_file, settings.language.compile_fmt_str): if not quiet: print("Compilation Success") else: if not quiet: print("Compilation Failed")
def _handle_template(args: List[str]): if len(args) == 2 and args[0] == "set": language_code = args[1] language = Language.get_by_code(language_code) if language is None: print("Language not found") template_filename = input( "Please enter the path to your template: ").strip() if not os.path.isfile(template_filename): response = "" while not response: response = input( "This does not seem to be a normal file. Do you wish to continue? [yn]: " ).strip().lower() if response == "n": return elif response != "y": response = "" settings = Settings.get_saved() settings.set_language(language, template_filename) settings.save() else: print("Usage: dmoj config template set {LANGUAGE}") print("The list of choices are:") for lang in Language.get_all(): print("\t{}".format(lang)) print("") print("They can be found in ~/.contest-cli/dmoj/languages.json")
def handle_submit(args: List[str]): if len(args) != 0: print("Please do not include any arguments after 'submit'") return settings = Settings.get_saved() if not settings.has_problem(): print("You don't seem to have a problem set up") return submit_url = get_problem_submit_url(settings.problem_id) session = get_session() r1 = session.get(submit_url) if r1 is None or r1.status_code != 200 or r1.url != submit_url: print( "Error getting submit page. Check your internet connection or login." ) return soup = BeautifulSoup(r1.text, 'html.parser') options = soup.find("select", {"id": "id_language"}).find_all("option") csrfmiddlewaretoken = soup.find("input", {"name": "csrfmiddlewaretoken"})["value"] language_id = None for option in options: if option["data-name"] == settings.language.name: language_id = option["data-id"] break if language_id is None: print("Looks like your language is not supported on this language") return r2 = session.post(submit_url, data={ "csrfmiddlewaretoken": csrfmiddlewaretoken, "source": open(settings.working_file, "r").read(), "language": language_id, "judge": "" }, headers={"referer": r1.url}) if r2 is None or r2.status_code != 200: print("Error submitting") return submission_id = r2.url[r2.url.rfind("/") + 1:] if r2.url != get_submission_url(submission_id): print( "Something went wrong with our submission. We were redirected to {}" .format(r2.url)) return _handle_submission_results(submission_id, session)
def handle_test(args: List[str]): settings = Settings.get_saved() if not settings.has_problem(): print("You don't seem to have a problem set") return problem_attrs_filename = os.path.join( settings.sample_data_folder, settings.sample_problem_attribute_filename) if not os.path.isfile(problem_attrs_filename): print( "There doesn't seem to be a problem attributes file. Did you change a setting?" ) return problem_attrs = json.load(open(problem_attrs_filename, "r")) samples = problem_attrs.get("samples", []) for sample in samples: sample_in_filename = os.path.join(settings.sample_data_folder, sample + ".in") sample_out_filename = os.path.join(settings.sample_data_folder, sample + ".out") if not os.path.isfile(sample_in_filename): print("Sample {} doesn't have an input file... Skipping") continue if not os.path.isfile(sample_out_filename): print("Sample {} doesn't have an input file... Skipping") continue user_out_filename = os.path.join(settings.sample_data_folder, sample + ".tmp") user_out_file = open(user_out_filename, "rb") if settings.language.is_compiled: proc = subprocess.Popen(settings.language.run_fmt_str.replace( "${OUTPUT}", settings.language.outfile), stdin=subprocess.PIPE, stdout=subprocess.PIPE) # hopefully you don't have gigabytes of data proc.stdin.write(open(sample_in_filename, "rb").read()) proc.stdin.flush() total_output_len = 0 while True: return_code = proc.poll() if return_code is not None: # TODO Handle codes print("Returned with code {}".format(return_code)) break else: new_output = proc.stdout.readline() total_output_len += new_output if total_output_len + total_output_len > 1024 * 1024: print(status_code.COLORED_OLE) break user_out_file.write(new_output) else: print("We don't support non-compiled stuff yet") user_out_file.close()
def handle_make(args: List[str]) -> None: settings = Settings.get_saved() cur_dir_name = os.path.basename(os.getcwd()) if settings.allowed_paths: # Check if path is not allowed allowed = False basename = os.path.basename(os.getcwd()) for path in settings.allowed_paths: if os.path.expanduser(path).startswith( os.getcwd()) or path == basename: allowed = True break if not allowed: print( "You don't seem to be in one of these directories: {}".format( settings.allowed_paths)) return if len(args) != 1: print("Expected exactly 1 argument after `make`") return problem_id = args[0] if not settings.has_language(): print( "You don't seem to have a template. Please set one up using `dmoj config template`" ) return sess = get_session() if sess is None: print("No session: please log in using `config login`") return r = sess.get(get_problem_url(problem_id)) if r.status_code != 200: print("Error: status code {}".format(r.status_code)) return soup = BeautifulSoup(r.text, "html.parser") r = sess.get(get_problem_api_url(problem_id)) if r.status_code != 200: print("Error: status code {}".format(r.status_code)) return api_data = json.loads(r.text) content = soup.find("div", "content-description").find("div") dct = {} idx = "" ext = "" for child in content.children: if child.name == "h4": text = child.text.strip() if text.startswith("Sample Input"): idx = text[13:].strip() if not idx: idx = "unnamed" ext = "in" elif text.startswith("Sample Output"): idx = text[14:].strip() if not idx: idx = "unnamed" ext = "out" elif child.name == "pre" and idx and ext: if "{}.{}".format(idx, ext) not in dct: code = child.find("code") dct["{}.{}".format(idx, ext)] = code.text.strip() problem_name = api_data["name"] languages = api_data["languages"] if settings.language.code not in languages: print( "Your language ({}) is not supported. Please change to one from {} before submitting." .format(settings.language.code, languages)) os.makedirs(settings.sample_data_folder, exist_ok=True) for (k, v) in dct.items(): with open(os.path.join(settings.sample_data_folder, k), "w") as f: f.write(v) json.dump( { "languages": languages, "samples": list(k[:-3] for k in dct.keys() if k.endswith(".in") and k[:-3] + ".out" in dct) }, open(os.path.join(settings.sample_data_folder, "problem_attrs.json"), "w"), sort_keys=True, indent=4) regex_matcher = RegExMatcher.get_default() output_base_filename = regex_matcher(problem_id) output_full_filename = os.path.join( os.getcwd(), output_base_filename + "." + settings.language.suffix) os.makedirs(os.path.dirname(output_full_filename), exist_ok=True) settings.set_problem(problem_id, output_full_filename) settings.save() with open(settings.template_filename, "r") as fin, open(output_full_filename, "w") as fout: for line in fin: # TODO: replace things like ${username} and whatnot fout.write(line)