Exemple #1
0
    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
Exemple #2
0
    def compile_with(self, extra, tst):
        try:
            util.copy_file(self.name + ".hs", "work.hs")
            if util.file_exists("judge.hs"):
                os.system("cat judge.hs >> work.hs")
            f = open("work.hs", "a")
            print("main = do", file=f)
            for line in open(tst + ".inp").readlines():
                line = line.rstrip()
                if line.startswith("let "):
                    print("    %s" % line, file=f)
                else:
                    print("    print (%s)" % line, file=f)
            f.close()
            self.execute_compiler('ghc ' + self.flags1() +
                                  ' work.hs -o work.exe 1> /dev/null')
        except CompilationTooLong:
            print(Style.BRIGHT + Fore.RED + 'Compilation time exceeded!' +
                  Style.RESET_ALL)
            return False

        if util.file_exists('work.exe'):
            util.del_file("work.hi")
            util.del_file('work.exe')
            return True
        else:
            return False
Exemple #3
0
    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
Exemple #4
0
    def compile(self):
        util.del_file(self.executable())
        util.del_dir(self.name + '.dir')
        os.mkdir(self.name + ".dir")

        try:
            if util.file_exists("solution.cc"):
                util.system('cp solution.cc ' + self.name + '.dir/' +
                            'program.cc')
            elif util.file_exists("solution.hh"):
                util.system('cp solution.hh ' + self.name + '.dir/program.hh')
            else:
                print("There is no solution.cc nor solution.hh")

            util.system('cp public/* ' + self.name + '.dir')
            util.system('cp private/* ' + self.name + '.dir')

            os.chdir(self.name + '.dir')
            self.execute_compiler('g++ ' + self.flags1() + ' *.cc -o ../' +
                                  self.executable())
        except CompilationTooLong:
            print(Style.BRIGHT + Fore.RED + 'Compilation time exceeded!' +
                  Style.RESET_ALL)
            os.chdir('..')
            util.del_file(self.executable())
            return False
        os.chdir('..')
        if util.file_exists(self.executable()):
            util.system("(cd public && tar cf ../public.tar *)")
            util.system("(cd private && tar cf ../private.tar *)")
            return True
        else:
            return False
Exemple #5
0
def make_corrects(com, iterations=1):
    """Makes all correct files in the cwd."""
    print(Style.BRIGHT + "Generating correct files..." + Style.RESET_ALL)

    handler = util.read_yml("handler.yml")

    if not util.file_exists(com.executable()):
        raise Exception(Fore.RED + com.executable() + " does not exist" +
                        Style.RESET_ALL)
    for f in glob.glob("*.cor"):
        util.del_file(f)
    inps = sorted(glob.glob("*.inp"))
    for inp in inps:
        tst = os.path.splitext(inp)[0]
        time = com.execute(tst, True, iterations)

        if handler["handler"] == "graphic":
            os.rename("output.png", tst + ".cor")
        outsize = os.path.getsize(tst + ".cor")

        try:
            if 'Run' not in handler['compilers']:
                print()
        except Exception:
            print()

        print(Style.DIM + 'time: %f\t\tsize: %s' %
              (time, util.convert_bytes(outsize)) + Style.RESET_ALL)
Exemple #6
0
 def compile_normal(self):
     # Compile original program
     util.del_file(self.executable())
     try:
         self.execute_compiler('g++ ' + self.flags1() + ' ' + self.name +
                               '.cc -o ' + self.executable())
     except CompilationTooLong:
         print(Style.BRIGHT + Fore.RED + 'Compilation time exceeded!' +
               Style.RESET_ALL)
         util.del_file(self.executable())
         return False
     return util.file_exists(self.executable())
Exemple #7
0
    def compile_normal(self):
        for f in glob.glob('*.class'):
            util.del_file(f)
        try:
            util.copy_file(self.name + '.java', 'Main.java')
            self.gen_wrapper()
            self.execute_compiler('javac ' + self.flags1() + ' wrapper.java')
        except CompilationTooLong:
            print(Style.BRIGHT + Fore.RED + 'Compilation time exceeded!' +
                  Style.RESET_ALL)
            return False

        self.del_wrapper()
        return util.file_exists(self.executable())
Exemple #8
0
    def execute(self, tst, correct, iterations=1):
        if correct:
            ext = 'cor'
            print("runhaskell work.hs > %s.%s" % (tst, ext))
        else:
            ext = 'hs.out'

        self.compile_work(tst)
        """func.system("runhaskell work.hs > %s.%s" % (tst, ext))"""
        func = 'import os; os.system("runhaskell work.hs > %s.%s")' % (tst,
                                                                       ext)
        time = timeit.timeit(func, number=iterations) / iterations

        util.del_file('work.hs')

        return time
Exemple #9
0
    def execute(self, tst, correct, iterations=1):
        if correct:
            ext = 'cor'
            print("python3 %s.py > %s.%s" % (self.name, tst, ext))
        else:
            ext = 'py.out'

        if self.compile_with(tst + ".inp"):
            os.system("cat solution.py %s.inp > work.py" % tst)
            func = 'import os; os.system("python3 work.py > %s.%s")' % (tst,
                                                                        ext)
            time = timeit.timeit(func, number=iterations) / iterations
            util.del_dir('__pycache__')
            util.del_file('work.py')
            return time
        else:
            return -1
Exemple #10
0
    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
Exemple #11
0
    def compile_no_main(self):
        # esta fet a sac!!! cal fer-ho be

        for f in glob.glob('*.class'):
            util.del_file(f)
        try:
            # create Solution.class
            self.execute_compiler('javac ' + self.flags1() + ' ' + self.name +
                                  '.java')
            # create Main.class
            self.execute_compiler('javac ' + self.flags1() + ' main.java')
            # create JudgeMain.class
            self.gen_wrapper()
            self.execute_compiler('javac ' + self.flags1() + ' wrapper.java')
        except CompilationTooLong:
            print(Style.BRIGHT + Fore.RED + 'Compilation time exceeded!' +
                  Style.RESET_ALL)
            return False
        self.del_wrapper()
        return util.file_exists('Main.class')
Exemple #12
0
    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
Exemple #13
0
    def compile(self):
        util.del_file(self.executable())
        util.del_dir(self.name + ".dir")

        if not util.file_exists("solution"):
            raise Exception("There is no solution directory")
        if not util.file_exists("public"):
            raise Exception("There is no public directory")
        if not util.file_exists("private"):
            raise Exception("There is no private directory")

        try:
            util.mkdir(self.name + '.dir')
            util.system('cp solution/*  public/* private/* ' + self.name +
                        '.dir')
            os.chdir(self.name + '.dir')

            self.execute_compiler('make program.exe 1> make.log')
        except CompilationTooLong:
            print(Style.BRIGHT + Fore.RED + 'Compilation time exceeded!' +
                  Style.RESET_ALL)
            os.chdir('..')
            return False

        os.chdir('..')

        if util.file_exists(self.name + '.dir/program.exe'):
            util.copy_file(self.name + '.dir/program.exe',
                           './' + self.executable())

        util.del_dir(self.name + ".dir")

        if util.file_exists(self.executable()):
            util.system("(cd public && tar cf ../public.tar *)")
            util.system("(cd private && tar cf ../private.tar *)")
            util.system("(cd solution && tar cf ../solution.tar *)")
            return True
        else:
            return False
Exemple #14
0
    def compile_normal(self):
        util.del_file(self.executable())
        try:
            self.execute_compiler('ghc ' + self.flags1() + ' ' + self.name +
                                  '.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(self.name + '.hi')
        util.del_file(self.name + '.o')
        util.file_exists(self.executable())
        return True
Exemple #15
0
    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())
Exemple #16
0
    def compile(self):
        util.del_file("work")
        util.del_file("work.hi")
        util.del_file("work.o")
        util.copy_file(self.name + '.hs', "work.hs")
        f = open("work.hs", "a")
        print("""main = do print "OK" """, file=f)
        f.close()

        try:
            self.execute_compiler('ghc -O3 work.hs 1> /dev/null')
        except CompilationTooLong:
            print(Style.BRIGHT + Fore.RED + 'Compilation time exceeded!' +
                  Style.RESET_ALL)
            return False

        util.del_file("work")
        util.del_file("work.hi")
        util.del_file("work.hs")
        util.del_file("work.o")
        return True
Exemple #17
0
 def del_wrapper(self):
     util.del_file('py3c.py')
Exemple #18
0
def verify_program(program, correct_extension='', iterations=1):
    """Verify that program compiles and gets AC for each test."""

    if not util.file_exists("handler.yml"):
        raise Exception("handler.yml does not exist")
    handler = util.read_yml("handler.yml")
    if handler["handler"] != "std" and handler["handler"] != "graphic":
        raise Exception("unknown handler")

    # compile
    available_list = []
    supported_list = compilers.compiler_extensions(handler.get('compilers'))

    file_list = [
        x for x in sorted(glob.glob(program + ".*")) if not x.endswith('.exe')
    ]
    if file_list == []:
        file_list = [program]

    solution_list = []
    excluded_extensions = ['exe', 'dir', correct_extension]
    for file in file_list:
        exclude = False
        for extension in excluded_extensions:
            if file.split('.')[-1] == extension: exclude = True
        if not exclude: solution_list.append(file)
    if solution_list == []: return

    print(Style.BRIGHT + "Compiling supported programs..." + Style.RESET_ALL)
    for solution in solution_list:
        name = os.path.splitext(solution)[0]
        ext = os.path.splitext(solution)[-1][1:]
        if ext in supported_list:
            com = compilers.compiler(supported_list[ext], handler, name)
            ret = com.compile()
            available_list.append([solution, com])

    print()
    unsupported_list = [
        x for x in solution_list
        if (x not in [y[0] for y in available_list] and x[-1] != '~'
            and not x.endswith('bak'))
    ]
    if unsupported_list != []:
        print(
            Fore.YELLOW +
            "NOTICE: The following solutions are still not supported and will NOT be verified: ",
            end='')
        for elem in unsupported_list:
            if elem != unsupported_list[-1]:
                print(elem, end=', ')
            else:
                print(elem + '\n' + Style.RESET_ALL)

    for f in glob.glob("*.out"):
        util.del_file(f)
    # execute on tests
    has_failed = False
    tests = sorted(glob.glob("*.inp"))
    for solution, compiler in sorted(available_list,
                                     key=lambda tup: tup[0].lower()):
        print("Verifying " + solution + "...")
        ext = os.path.splitext(solution)[-1]
        for test in tests:
            tst = os.path.splitext(test)[0]
            time = compiler.execute(tst, False, iterations)
            if handler["handler"] == "graphic":
                os.rename("output.png", tst + ext + ".out")
            outsize = os.path.getsize(tst + ext + ".out")

            r = subprocess.call(["cmp", tst + ext + ".out", tst + ".cor"])
            if r:
                has_failed = True
                msg = "WA"
            else:
                msg = "OK"
                util.del_file(tst + ext + ".out")
            print((Fore.GREEN if msg == 'OK' else Fore.RED) +
                  "%s.inp:\t\t%s\ntime: %f\t\tsize: %s" %
                  (tst, msg, time, util.convert_bytes(outsize)) +
                  Style.RESET_ALL)
            if outsize > 2000000 and not os.path.isfile(tst + ext + ".ops"):
                print(
                    Fore.YELLOW +
                    'Warning: The output file is bigger than 2MB, you may need a .ops file for this test case.'
                )
        print()

    if has_failed:
        print(
            Fore.RED +
            "Some solutions are not correct! Please check them and try again."
            + Style.RESET_ALL)
        sys.exit(0)
Exemple #19
0
 def del_wrapper(self):
     util.del_file('wrapper.java')