Exemple #1
0
 def build_validator(self, source):
     fp = FilePath(self._directory, source)
     if not fp.exists():
         return (None, 'File does not exists.')
     if fp.ext == '.cpp':
         binary = fp.chext('.bin')
         if binary.mtime() < fp.mtime() and not self._cpp_compiler(fp, binary):
             return (None, 'Failed to build validator.')
         return (Runnable(binary), 'OK')
     if fp.ext in ['.py', '.py3']:
         return (Runnable('python3', [str(source)]), 'OK')
     if fp.ext == '.py2':
         return (Runnable('python2', [str(source)]), 'OK')
     return (None, 'Not supported source file.')
Exemple #2
0
 def build_validator(self, source):
     fp = FilePath(self._directory, source)
     if not fp.exists():
         return (None, 'File does not exists.')
     if fp.ext == '.cpp':
         binary = fp.chext('.bin')
         if binary.mtime() < fp.mtime() and not self._cpp_compiler(
                 fp, binary):
             return (None, 'Failed to build validator.')
         return (Runnable(binary), 'OK')
     if fp.ext in ['.py', '.py3']:
         return (Runnable('python3', [str(source)]), 'OK')
     if fp.ext == '.py2':
         return (Runnable('python2', [str(source)]), 'OK')
     return (None, 'Not supported source file.')
Exemple #3
0
class Statement:
    """Represents a statement. A statement is formed by a latex source and a pdf
    file.
    """
    def __init__(self, directory, num=None, codename=None):
        """
        Args:
            directory (Directory): Directory to search for statement source file.
            num (int): Number of the statement in the contest starting from 0
        """
        assert FilePath(directory, 'statement.tex').exists()
        self._source = FilePath(directory, 'statement.tex')
        self._pdf = self._source.chext('.pdf')
        self._compiler = LatexCompiler()
        self._directory = directory
        self._num = num
        self._codename = codename

    @property
    def pdf(self):
        """Returns path to pdf file and compiles it if necessary.
        Returns:
            Optional[FilePath]: The file path if the binary is present or None
                if the pdf file cannot be generated.
        """
        if self._pdf.mtime() < self._source.mtime():
            (st, _msg) = self.build()
            if not st:
                return None
        return self._pdf

    def __str__(self):
        return str(self._source)

    @ui.work('PDF')
    def build(self, blank_page=False):
        """Compile statement latex source
        Args:
           blank_page (Optional[bool]) if true adds a blank page at the end of the
               problem.
        Returns:
           (bool, msg) a tuple containing status code and result message.

        """
        if self._num is not None:
            os.environ['OCIMATIC_PROBLEM_NUMBER'] = chr(ord('A') + self._num)
        if self._codename:
            os.environ['OCIMATIC_CODENAME'] = self._codename
        if blank_page:
            os.environ['OCIMATIC_BLANK_PAGE'] = 'True'
        st = self._compiler(self._source)
        return (st, 'OK' if st else 'FAILED')

    def io_samples(self):
        """Find sample input data in the satement
        Returns:
            List[FilePath]: list of paths
        """
        latex_file = self._source.open('r')
        samples = set()
        for line in latex_file:
            m = re.match(r'[^%]*\\sampleIO(\[[^\]]*\]){0,2}{([^}]+)}', line)
            if m:
                samples.add(m.group(2))
        latex_file.close()
        return [FilePath(self._directory, s) for s in samples]