Exemple #1
0
 def compile_folder(self,
                    directory,
                    write=True,
                    package=True,
                    *args,
                    **kwargs):
     """Compile a directory and returns paths to compiled files."""
     filepaths = []
     for dirpath, dirnames, filenames in os.walk(directory):
         if write is None or write is True:
             writedir = write
         else:
             writedir = os.path.join(write,
                                     os.path.relpath(dirpath, directory))
         for filename in filenames:
             if os.path.splitext(filename)[1] in code_exts:
                 destpath = self.compile_file(
                     os.path.join(dirpath, filename), writedir, package,
                     *args, **kwargs)
                 if destpath is not None:
                     filepaths.append(destpath)
         for name in dirnames[:]:
             if not is_special_dir(name) and name.startswith("."):
                 if logger.verbose:
                     logger.show_tabulated(
                         "Skipped directory", name,
                         "(explicitly pass as source to override).")
                 dirnames.remove(
                     name
                 )  # directories removed from dirnames won't appear in further os.walk iterations
     return filepaths
Exemple #2
0
    def watch(self, source, write=True, package=None, run=False, force=False):
        """Watches a source and recompiles on change."""
        from coconut.command.watch import Observer, RecompilationWatcher

        source = fixpath(source)

        print()
        logger.show_tabulated("Watching", showpath(source), "(press Ctrl-C to end)...")

        def recompile(path):
            if os.path.isfile(path) and os.path.splitext(path)[1] in code_exts:
                with self.handling_exceptions():
                    self.run_mypy(self.compile_path(path, write, package, run, force))

        observer = Observer()
        observer.schedule(RecompilationWatcher(recompile), source, recursive=True)

        with self.running_jobs():
            observer.start()
            try:
                while True:
                    time.sleep(watch_interval)
            except KeyboardInterrupt:
                logger.show("Got KeyboardInterrupt; stopping watcher.")
            finally:
                observer.stop()
                observer.join()
Exemple #3
0
    def watch(self, source, write=True, package=None, run=False, force=False):
        """Watches a source and recompiles on change."""
        from coconut.command.watch import Observer, RecompilationWatcher

        source = fixpath(source)

        print()
        logger.show_tabulated("Watching", showpath(source),
                              "(press Ctrl-C to end)...")

        def recompile(path):
            if os.path.isfile(path) and os.path.splitext(path)[1] in code_exts:
                with self.handling_exceptions():
                    self.run_mypy(
                        self.compile_path(path, write, package, run, force))

        observer = Observer()
        observer.schedule(RecompilationWatcher(recompile),
                          source,
                          recursive=True)

        with self.running_jobs():
            observer.start()
            try:
                while True:
                    time.sleep(watch_interval)
            except KeyboardInterrupt:
                logger.show("Got KeyboardInterrupt; stopping watcher.")
            finally:
                observer.stop()
                observer.join()
Exemple #4
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)
Exemple #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 self.show:
         print(compiled)
     if run:
         if destpath is None:
             self.execute(compiled, path=codepath, allow_show=False)
         else:
             self.execute_file(destpath)
Exemple #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)
Exemple #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)
Exemple #8
0
    def watch(self, source, write=True, package=None, run=False, force=False):
        """Watch a source and recompiles on change."""
        from coconut.command.watch import Observer, RecompilationWatcher

        source = fixpath(source)

        logger.show()
        logger.show_tabulated("Watching", showpath(source),
                              "(press Ctrl-C to end)...")

        def recompile(path):
            path = fixpath(path)
            if os.path.isfile(path) and os.path.splitext(path)[1] in code_exts:
                with self.handling_exceptions():
                    if write is True or write is None:
                        writedir = write
                    else:
                        # correct the compilation path based on the relative position of path to source
                        dirpath = os.path.dirname(path)
                        writedir = os.path.join(
                            write, os.path.relpath(dirpath, source))
                    filepaths = self.compile_path(path,
                                                  writedir,
                                                  package,
                                                  run,
                                                  force,
                                                  show_unchanged=False)
                    self.run_mypy(filepaths)

        watcher = RecompilationWatcher(recompile)
        observer = Observer()
        observer.schedule(watcher, source, recursive=True)

        with self.running_jobs():
            observer.start()
            try:
                while True:
                    time.sleep(watch_interval)
                    watcher.keep_watching()
            except KeyboardInterrupt:
                logger.show_sig("Got KeyboardInterrupt; stopping watcher.")
            finally:
                observer.stop()
                observer.join()
Exemple #9
0
 def compile_folder(self, directory, write=True, package=True, run=False, force=False):
     """Compiles a directory and returns paths to compiled files."""
     filepaths = []
     for dirpath, dirnames, filenames in os.walk(directory):
         if write is None or write is True:
             writedir = write
         else:
             writedir = os.path.join(write, os.path.relpath(dirpath, directory))
         for filename in filenames:
             if os.path.splitext(filename)[1] in code_exts:
                 destpath = self.compile_file(os.path.join(dirpath, filename), writedir, package, run, force)
                 if destpath is not None:
                     filepaths.append(destpath)
         for name in dirnames[:]:
             if not is_special_dir(name) and name.startswith("."):
                 if logger.verbose:
                     logger.show_tabulated("Skipped directory", name, "(explicitly pass as source to override).")
                 dirnames.remove(name)  # directories removed from dirnames won't appear in further os.walk iteration
     return filepaths
Exemple #10
0
 def compile_folder(self, directory, write=True, package=True, *args, **kwargs):
     """Compile a directory and returns paths to compiled files."""
     if not isinstance(write, bool) and os.path.isfile(write):
         raise CoconutException("destination path cannot point to a file when compiling a directory")
     filepaths = []
     for dirpath, dirnames, filenames in os.walk(directory):
         if isinstance(write, bool):
             writedir = write
         else:
             writedir = os.path.join(write, os.path.relpath(dirpath, directory))
         for filename in filenames:
             if os.path.splitext(filename)[1] in code_exts:
                 with self.handling_exceptions():
                     destpath = self.compile_file(os.path.join(dirpath, filename), writedir, package, *args, **kwargs)
                     if destpath is not None:
                         filepaths.append(destpath)
         for name in dirnames[:]:
             if not is_special_dir(name) and name.startswith("."):
                 if logger.verbose:
                     logger.show_tabulated("Skipped directory", name, "(explicitly pass as source to override).")
                 dirnames.remove(name)  # directories removed from dirnames won't appear in further os.walk iterations
     return filepaths
Exemple #11
0
    def watch(self, source, write=True, package=None, run=False, force=False):
        """Watch a source and recompiles on change."""
        from coconut.command.watch import Observer, RecompilationWatcher

        source = fixpath(source)

        logger.show()
        logger.show_tabulated("Watching", showpath(source), "(press Ctrl-C to end)...")

        def recompile(path):
            path = fixpath(path)
            if os.path.isfile(path) and os.path.splitext(path)[1] in code_exts:
                with self.handling_exceptions():
                    if write is True or write is None:
                        writedir = write
                    else:
                        # correct the compilation path based on the relative position of path to source
                        dirpath = os.path.dirname(path)
                        writedir = os.path.join(write, os.path.relpath(dirpath, source))
                    filepaths = self.compile_path(path, writedir, package, run, force, show_unchanged=False)
                    self.run_mypy(filepaths)

        watcher = RecompilationWatcher(recompile)
        observer = Observer()
        observer.schedule(watcher, source, recursive=True)

        with self.running_jobs():
            observer.start()
            try:
                while True:
                    time.sleep(watch_interval)
                    watcher.keep_watching()
            except KeyboardInterrupt:
                logger.show_sig("Got KeyboardInterrupt; stopping watcher.")
            finally:
                observer.stop()
                observer.join()