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
def callback(compiled): if destpath is None: logger.show_tabulated("Compiled", showpath(codepath), "without writing to file.") else: with univ_open(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, argv_source_path=codepath)
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 univ_open(codepath, "r") as opened: code = readfile(opened) package_level = -1 if destpath is not None: destpath = fixpath(destpath) destdir = os.path.dirname(destpath) if not os.path.exists(destdir): os.makedirs(destdir) if package is True: package_level = self.get_package_level(codepath) if package_level == 0: self.create_package(destdir) foundhash = None if force else self.has_hash_of(destpath, code, package_level) 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, argv_source_path=codepath) else: logger.show_tabulated("Compiling", showpath(codepath), "...") def callback(compiled): if destpath is None: logger.show_tabulated("Compiled", showpath(codepath), "without writing to file.") else: with univ_open(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, argv_source_path=codepath) if package is True: self.submit_comp_job(codepath, callback, "parse_package", code, package_level=package_level) elif package is False: self.submit_comp_job(codepath, callback, "parse_file", code) else: raise CoconutInternalException("invalid value for package", package)
def get_template(template): """Read the given template file.""" with univ_open(os.path.join(template_dir, template) + template_ext, "r") as template_file: return template_file.read()
from coconut.requirements import ( using_modern_setuptools, requirements, extras, ) # ----------------------------------------------------------------------------------------------------------------------- # SETUP: # ----------------------------------------------------------------------------------------------------------------------- if not using_modern_setuptools and "bdist_wheel" in sys.argv: raise RuntimeError( "bdist_wheel not supported for setuptools versions < 18 (run '{python} -m pip install --upgrade setuptools' to fix)" .format(python=sys.executable)) with univ_open("README.rst", "r") as readme_file: readme = readme_file.read() setuptools.setup( name=package_name, version=VERSION, description=description, long_description=readme, url=website_url, author=author, author_email=author_email, install_requires=requirements, extras_require=extras, packages=setuptools.find_packages(exclude=list(exclude_install_dirs), ), include_package_data=True, zip_safe=False,
from coconut.constants import ( version_str_tag, without_toc, with_toc, ) from coconut.util import univ_open import pydata_sphinx_theme # NOQA import myst_parser # NOQA # ----------------------------------------------------------------------------------------------------------------------- # README: # ----------------------------------------------------------------------------------------------------------------------- with univ_open("README.rst", "r") as readme_file: readme = readme_file.read() with univ_open("index.rst", "w") as index_file: index_file.write(readme.replace(without_toc, with_toc)) # ----------------------------------------------------------------------------------------------------------------------- # DEFINITIONS: # ----------------------------------------------------------------------------------------------------------------------- from coconut.constants import ( # NOQA project, copyright, author, highlight_language, ) version = VERSION release = version_str_tag
def create_package(self, dirpath): """Set up a package directory.""" filepath = os.path.join(dirpath, "__coconut__.py") with univ_open(filepath, "w") as opened: writefile(opened, self.comp.getheader("__coconut__"))