Esempio n. 1
0
    def collect_lemmas_doc(
        cls,
        doc: CoqDocument,
        ast_sexp_list: List[SexpNode],
        serapi_options: str,
    ) -> List[Lemma]:
        lemmas_doc: List[Lemma] = list()
        data_index = doc.get_data_index()

        # Maintain a stack of module
        modules: List[str] = list()

        # Prepare qualified name prefix
        qprefix_this_doc = "./" + doc.file_name[:-2]  # Remove .v
        for m in cls.RE_PATH_TO_QUALIFIED_PREFIX.finditer(serapi_options):
            path = m.group("path")
            if path != ".": path = "./" + path
            qprefix = m.group("qprefix")

            if qprefix_this_doc.startswith(path):
                qprefix_this_doc = qprefix + qprefix_this_doc[len(path):]
                break
            # end if
        # end for
        if qprefix_this_doc.startswith("./"):
            qprefix_this_doc = qprefix_this_doc[len("./"):]
        qprefix_this_doc = qprefix_this_doc.replace("/", ".")

        for sent_i, sent in enumerate(doc.sentences):
            ast_sexp = ast_sexp_list[sent_i]
            vernac = SexpAnalyzer.analyze_vernac(ast_sexp)

            if vernac.vernac_type in cls.VTYPES_MODULE_BEG:
                # (VernacExpr()(VernacDefineModule()  (  (   v   ( Id <module name>)) ...
                #  0         1 2 20               21  22 220  2201    22011
                module_name = vernac.vernac_sexp[2][2][0][1][
                    1].content_no_quote
                modules.append(module_name)
            elif vernac.vernac_type in cls.VTYPES_MODULE_END:
                # (VernacExpr()(VernacEndSegment  (  (   v   ( Id <module name>)) ...
                #  0         1 2 20               21 210  2101    21011
                try:
                    module_name = vernac.vernac_sexp[2][1][0][1][
                        1].content_no_quote
                except:
                    print(vernac.vernac_sexp.pretty_format())
                    raise
                # end try
                if len(modules) > 0 and module_name == modules[-1]:
                    modules.pop(
                    )  # EndModule and EndSection share the same vernac type
            elif vernac.vernac_type in cls.VTYPES_LEMMA:
                # (VernacExpr()(VernacStartTheoremProof Lemma ( ( ( ( ( v (       Id <lemma name>))
                #  0         1 2 20                     21   22   2200000 2200001    22000011
                lemma = Lemma()
                lemma.data_index = data_index

                lemma.name = vernac.vernac_sexp[2][2][0][0][0][0][1][
                    1].content_no_quote
                lemma.qname = qprefix_this_doc + "." + ".".join(modules +
                                                                [lemma.name])

                # Find lemma content, after the first token matching the lemma name
                tok_i = 0
                for tok in sent.tokens:
                    if tok.content == lemma.name: break
                    tok_i += 1
                # end for
                if tok_i == len(sent.tokens):
                    LoggingUtils.log_and_raise(
                        cls.logger,
                        f"Lemma name {lemma.name} didn't appear in the source code {sent.str_with_space()}",
                        Exception)

                lemma.vernac_command = sent.tokens[:tok_i]
                lemma.statement = sent.tokens[tok_i + 1:]
                lemma.ast_sexp = vernac.vernac_sexp

                lemmas_doc.append(lemma)
            # end if
        # end for

        # Use sername to get the backend representations
        lemma_qnames: str = "".join([l.qname + "\n" for l in lemmas_doc])
        lemma_qnames_file = BashUtils.get_temp_file()
        IOUtils.dump(lemma_qnames_file, lemma_qnames, IOUtils.Format.txt)

        lemma_qnames_backend_sexps_str: str = BashUtils.run(
            f"sername {serapi_options} --require-lib={qprefix_this_doc} {lemma_qnames_file}",
            expected_return_code=0).stdout
        IOUtils.rm(lemma_qnames_file)
        for qname_backend_sexp_str in lemma_qnames_backend_sexps_str.splitlines(
        ):
            qname, backend_sexp_str = qname_backend_sexp_str.split(":", 1)
            backend_sexp = SexpParser.parse(backend_sexp_str)

            for lemma in lemmas_doc:
                if lemma.qname == qname:
                    lemma.backend_sexp = backend_sexp
                    break
                # end if
            # end for
        # end for

        lemmas_doc = [l for l in lemmas_doc if l.backend_sexp is not None]
        return lemmas_doc
Esempio n. 2
0
    def submit_script(cls,
            cluster: str,
            name: str,
            log_path: Path,
            script: str,
            queue: str = None,
            timeout: str = None,
            require_conda: bool = True,
            conda_env: str = None,
            modules: List[str] = None,
    ) -> int:
        # Get default values
        if modules is None:
            modules = TACCRunnerConsts.modules[cluster]
        # end if
        if queue is None:
            queue = TACCRunnerConsts.queue[cluster]
        # end if
        if timeout is None:
            timeout = TACCRunnerConsts.timeout[cluster]
        # end if
        if conda_env is None:
            conda_env = TACCRunnerConsts.conda_env[cluster]
        # end if

        # Prepare submit script
        IOUtils.mk_dir(log_path)

        s = f"""#!/bin/bash
#SBATCH -J {name}               # Job name
#SBATCH -o {log_path}/%j.stdout # Name of stdout output file(%j expands to jobId)
#SBATCH -e {log_path}/%j.stderr # Name of stderr output file(%j expands to jobId)
#SBATCH -p {queue}              # Queue name
#SBATCH -N 1                    # Total number of nodes requested
#SBATCH -n 1                    # Total number of mpi tasks requested
#SBATCH -t {timeout}            # Max run time (hh:mm:ss)
#SBATCH [email protected]
#SBATCH --mail-type=ALL
# The next line is required if the user has more than one project
#SBATCH -A {TACCRunnerConsts.allocation}      # Allocation name to charge job against

module reset
module unload python2
"""
        for m in modules:
            s += f"module load {m}\n"
        # end for
        s += f"""
module list
echo "START: $(date)"

# Launch serial code...
# Do not use ibrun or any other MPI launcher
"""

        if require_conda:
            s += f"""
unset PYTHONPATH
source {TACCRunnerConsts.conda_init_path[cluster]}
conda activate {conda_env}
"""

        s += f"""
cd {Macros.python_dir}
{script}

echo "END: $(date)"
"""

        # Submit the script
        submit_script = BashUtils.get_temp_file()
        IOUtils.dump(submit_script, s, IOUtils.Format.txt)
        receipt = BashUtils.run(f"sbatch {submit_script}", expected_return_code=0).stdout

        # Get job id as the last number in output
        job_id = int(receipt.splitlines()[-1].split()[-1])

        # Save the script at log_path as well
        BashUtils.run(f"mv {submit_script} {log_path}/{job_id}.sh")

        return job_id