Esempio n. 1
0
    def compile(self,
                codepath,
                destpath=None,
                package=False,
                run=False,
                force=False,
                show_unchanged=True):
        """Compile a source Coconut file to a destination Python file."""
        with openfile(codepath, "r") as opened:
            code = readfile(opened)

        if destpath is not None:
            destdir = os.path.dirname(destpath)
            if not os.path.exists(destdir):
                os.makedirs(destdir)
            if package is True:
                self.create_package(destdir)

        foundhash = None if force else self.has_hash_of(
            destpath, code, package)
        if foundhash:
            if show_unchanged:
                logger.show_tabulated("Left unchanged", showpath(destpath),
                                      "(pass --force to override).")
            if self.show:
                print(foundhash)
            if run:
                self.execute_file(destpath)

        else:
            logger.show_tabulated("Compiling", showpath(codepath), "...")

            if package is True:
                compile_method = "parse_package"
            elif package is False:
                compile_method = "parse_file"
            else:
                raise CoconutInternalException("invalid value for package",
                                               package)

            def callback(compiled):
                if destpath is None:
                    logger.show_tabulated("Compiled", showpath(codepath),
                                          "without writing to file.")
                else:
                    with openfile(destpath, "w") as opened:
                        writefile(opened, compiled)
                    logger.show_tabulated("Compiled to", showpath(destpath),
                                          ".")
                if self.show:
                    print(compiled)
                if run:
                    if destpath is None:
                        self.execute(compiled, path=codepath, allow_show=False)
                    else:
                        self.execute_file(destpath)

            self.submit_comp_job(codepath, callback, compile_method, code)
Esempio n. 2
0
 def hashashof(self, destpath, code, package):
     """Determines if a file has the hash of the code."""
     if destpath is not None and os.path.isfile(destpath):
         with openfile(destpath, "r") as opened:
             compiled = readfile(opened)
             hashash = gethash(compiled)
             if hashash is not None and hashash == self.comp.genhash(package, code):
                 return compiled
     return None
Esempio n. 3
0
 def has_hash_of(self, destpath, code, package):
     """Determine if a file has the hash of the code."""
     if destpath is not None and os.path.isfile(destpath):
         with openfile(destpath, "r") as opened:
             compiled = readfile(opened)
         hashash = gethash(compiled)
         if hashash is not None and hashash == self.comp.genhash(package, code):
             return compiled
     return None
Esempio n. 4
0
 def has_hash_of(self, destpath, code, package_level):
     """Determine if a file has the hash of the code."""
     if destpath is not None and os.path.isfile(destpath):
         with univ_open(destpath, "r") as opened:
             compiled = readfile(opened)
         hashash = gethash(compiled)
         if hashash is not None and hashash == self.comp.genhash(code, package_level):
             return True
     return False
Esempio n. 5
0
    def compile(self, codepath, destpath=None, package=False, run=False, force=False):
        """Compiles a source Coconut file to a destination Python file."""
        logger.show_tabulated("Compiling", showpath(codepath), "...")

        with openfile(codepath, "r") as opened:
            code = readfile(opened)

        if destpath is not None:
            destdir = os.path.dirname(destpath)
            if not os.path.exists(destdir):
                os.makedirs(destdir)
            if package is True:
                self.create_package(destdir)

        foundhash = None if force else self.hashashof(destpath, code, package)
        if foundhash:
            logger.show_tabulated("Left unchanged", showpath(destpath), "(pass --force to override).")
            if self.show:
                print(foundhash)
            if run:
                self.execute_file(destpath)

        else:

            if package is True:
                compile_method = "parse_package"
            elif package is False:
                compile_method = "parse_file"
            else:
                raise CoconutInternalException("invalid value for package", package)

            def callback(compiled):
                if destpath is None:
                    logger.show_tabulated("Finished", showpath(codepath), "without writing to file.")
                else:
                    with openfile(destpath, "w") as opened:
                        writefile(opened, compiled)
                    logger.show_tabulated("Compiled to", showpath(destpath), ".")
                if self.show:
                    print(compiled)
                if run:
                    if destpath is None:
                        self.execute(compiled, path=codepath, allow_show=False)
                    else:
                        self.execute_file(destpath)

            self.submit_comp_job(codepath, callback, compile_method, code)