Пример #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)
Пример #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
Пример #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
Пример #4
0
 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)
Пример #5
0
 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 run:
         runpath = destpath if destpath is not None else codepath
         self.execute(compiled, path=runpath, isolate=True)
     elif self.show:
         print(compiled)
Пример #6
0
 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)
Пример #7
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)
Пример #8
0
 def create_package(self, dirpath):
     """Sets up a package directory."""
     dirpath = fixpath(dirpath)
     filepath = os.path.join(dirpath, "__coconut__.py")
     with openfile(filepath, "w") as opened:
         writefile(opened, self.comp.getheader("__coconut__"))
Пример #9
0
 def create_package(self, dirpath):
     """Set up a package directory."""
     dirpath = fixpath(dirpath)
     filepath = os.path.join(dirpath, "__coconut__.py")
     with openfile(filepath, "w") as opened:
         writefile(opened, self.comp.getheader("__coconut__"))
Пример #10
0
 def create_package(self, dirpath):
     """Sets up a package directory."""
     filepath = os.path.join(fixpath(dirpath), "__coconut__.py")
     with openfile(filepath, "w") as opened:
         writefile(opened, self.comp.headers("package"))