def compile_normal(self): try: s = util.read_file(self.name + ".R") util.write_file("wrapper.R", """ wrapper_R <- function() { %s } """ % s) util.write_file( "compiler.R", """ library("codetools") source("wrapper.R") checkUsage(wrapper_R) """) self.execute_compiler('Rscript compiler.R 1> /dev/null') except CompilationTooLong: print(Style.BRIGHT + Fore.RED + 'Compilation time exceeded!' + Style.RESET_ALL) return False util.del_file("compiler.R") util.del_file("wrapper.R") return True
def execute(self, tst, correct, iterations=1): if 'source_modifier' in self.handler and ( self.handler['source_modifier'] == 'no_main' or self.handler['source_modifier'] == 'structs'): util.copy_file(self.name + '.py', 'modified.py') ori = util.read_file(self.name + '.py') main = util.read_file('main.py') util.write_file('modified.py', '%s\n%s\n' % (ori, main)) exec = 'modified.py' else: exec = self.executable() if correct: ext = 'cor' print("python3 %s < %s.inp > %s.%s" % (exec, tst, tst, ext), end='') else: ext = 'py.out' """func.system("python3 %s < %s.inp > %s.%s" % (exec, tst, tst, ext))""" func = 'import os; os.system("python3 %s < %s.inp > %s.%s")' % ( exec, tst, tst, ext) time = timeit.timeit(func, number=iterations) / iterations util.del_file('modified.py') util.del_dir('__pycache__') return time
def gen_wrapper(self): util.write_file( 'py3c.py', """ #!/usr/bin/python3 import py_compile, sys py_compile.compile(sys.argv[1]) """)
def compile(self): try: self.gen_wrapper() code = util.read_file(self.name + '.py') util.write_file(self.name + '.py', code) self.execute_compiler('python3 py3c.py ' + self.name + '.py 1> /dev/null') except CompilationTooLong: print(Style.BRIGHT + Fore.RED + 'Compilation time exceeded!' + Style.RESET_ALL) return False self.del_wrapper() return True
def compile_no_main(self): # Modify the program util.copy_file(self.name + '.cc', 'modified.cc') ori = util.read_file('modified.cc') main = util.read_file('main.cc') util.write_file( 'modified.cc', """ #define main main__3 %s #undef main #define main main__2 %s #undef main int main() { return main__2(); } """ % (ori, main)) # Compile modified program util.del_file(self.executable()) try: self.execute_compiler('g++ ' + self.flags2() + ' modified.cc -o ' + self.executable()) except CompilationTooLong: print(Style.BRIGHT + Fore.RED + 'Compilation time exceeded!' + Style.RESET_ALL) util.del_file(self.executable()) util.del_file('modified.cc') return False # We are almost there util.del_file('modified.cc') if util.file_exists(self.executable()): return True else: print(Style.BRIGHT + Fore.RED + 'Unreported error.' + Style.RESET_ALL) util.del_file(self.executable()) return False
def gen_wrapper(self): util.write_file( "wrapper.java", """ class WrapperMain { public static void main (String[] args) { try { Main.main(args); System.exit(0); } catch (Throwable e) { // We hide the exception. // System.out.println(e); System.exit(1); } } } """)
def compile_normal(self): try: self.gen_wrapper() code = util.read_file(self.name + '.py') util.write_file(self.name + '.py', code) util.write_file( 'py3c.py', """#!/usr/bin/python3 import py_compile, sys py_compile.compile(sys.argv[1])""") self.execute_compiler('python3 py3c.py ' + self.name + '.py 1> /dev/null') except CompilationTooLong: print(Style.BRIGHT + Fore.RED + 'Compilation time exceeded!' + Style.RESET_ALL) return False self.del_wrapper() return True
def compile_no_main(self): if not self.compile_normal(): return False # Modify the program util.copy_file(self.name + '.py', 'modified.py') ori = util.read_file(self.name + '.py') main = util.read_file('main.py') util.write_file('modified.py', '%s\n%s\n' % (ori, main)) # Compile modified program try: self.gen_wrapper() self.execute_compiler('python3 py3c.py modified.py 1> /dev/null') except CompilationTooLong: print(Style.BRIGHT + Fore.RED + 'Compilation time exceeded!' + Style.RESET_ALL) return False self.del_wrapper() return True
def compile_no_main(self): # Modify the program util.copy_file(self.name + '.R', 'modified.R') ori = util.read_file('modified.R') main = util.read_file('main.R') util.write_file('modified.R', '%s\n%s\n' % (ori, main)) try: s = util.read_file("modified.R") util.write_file("wrapper.R", """ wrapper_R <- function() { %s } """ % s) util.write_file( "compiler.R", """ library("codetools") source("wrapper.R") checkUsage(wrapper_R) """ % s) self.execute_compiler('Rscript compiler.R 1> /dev/null') except CompilationTooLong: print(Style.BRIGHT + Fore.RED + 'Compilation time exceeded!' + Style.RESET_ALL) return False util.del_file("compiler.R") util.del_file("wrapper.R") util.del_file("modified.R") return True
def compile_no_main(self): util.copy_file(self.name + '.hs', 'modified.hs') ori = util.read_file('modified.hs') main = util.read_file('main.hs') util.write_file('modified.hs', """ %s %s """ % (ori, main)) util.del_file(self.executable()) try: self.execute_compiler('ghc ' + self.flags1() + ' modified.hs -o ' + self.executable() + ' 1> /dev/null') except CompilationTooLong: print(Style.BRIGHT + Fore.RED + 'Compilation time exceeded!' + Style.RESET_ALL) util.del_file(self.executable()) return False util.del_file('modified.hs') util.del_file('modified.hi') util.del_file('modified.o') return util.file_exists(self.executable())
def make_prints_3(lang, ori): ori = os.path.realpath(ori) dat = util.current_time() usr = util.get_username() hst = util.get_hostname() src = "%s@%s:%s" % (usr, hst, ori) sample2 = "" sample1 = "" tsts = sorted(glob.glob("*sample*.inp")) handler = util.read_yml("handler.yml") graphic = "" i = 0 for j in tsts: i += 1 jj = os.path.splitext(j)[0] if len(tsts) == 1: num = "" else: num = str(i) if handler["handler"] == "graphic": size = subprocess.getoutput( "identify -format '(%%w$\\\\times$%%h)' %s.cor" % jj) graphic = "[%s]" % size r = os.system("convert %s.cor %s.cor.eps" % (jj, jj)) if r != 0: print(Fore.RED + "ImageMagick error!", end='') if r == 256: print( " You must change it's security policy to be able to convert the images to EPS.", end='') print(Style.RESET_ALL) sys.exit(0) sample2 += r"\SampleTwoColInputOutput%s{%s}{%s}" % (graphic, jj, num) sample1 += r"\SampleOneColInputOutput%s{%s}{%s}" % (graphic, jj, num) scores = "" if util.file_exists("scores.yml"): scores = "scores.yml: \\verbatimtabinput{scores.yml}" t = r""" \documentclass[11pt]{article} \usepackage{jutge} \usepackage{lang.%s} \lstMakeShortInline@ \begin{document} \newcommand{\SampleTwoCol}{%s} \newcommand{\SampleOneCol}{%s} \DoProblem{%s} \subsection*{Metadata} \begin{verbatim} language: %s source: %s generation-time: %s\end{verbatim} problem.%s.yml: \verbatimtabinput{problem.%s.yml} handler.yml: \verbatimtabinput{handler.yml} %s \end{document} """ % (lang, sample2, sample1, lang, lang, src, dat, lang, lang, scores) util.write_file("main.tex", t) print(Style.BRIGHT + "Generating .pdf file... ", end=Style.RESET_ALL) sys.stdout.flush() r = os.system("latex -interaction scrollmode main > main.err") if r != 0: os.system('cat main.err') raise Exception(Fore.RED + "\nLaTeX error!" + Style.RESET_ALL) r = os.system("dvips main -o 1> /dev/null 2>/dev/null") if r != 0: raise Exception(Fore.RED + "\ndvips error!" + Style.RESET_ALL) r = os.system("ps2pdf main.ps main.pdf 1> /dev/null 2>/dev/null") if r != 0: raise Exception(Fore.RED + "\nps2pdf error!" + Style.RESET_ALL) os.remove("main.ps") os.system("mv main.pdf %s/problem.%s.pdf" % (ori, lang)) print(Fore.GREEN + 'Done!' + Style.RESET_ALL)