# -*- coding: utf-8 -*-"""Enumeration of important paths on local file system."""importtypingasTimportsysimportsubprocessimportdataclassesfrompathlibimportPathfromfunctoolsimportcached_propertyfrom.helpersimportprint_commandfrom.loggerimportloggerifT.TYPE_CHECKING:# pragma: no coverfrom.defineimportPyWf
[docs]@dataclasses.dataclassclassPyWfPaths:""" Namespace class for accessing important paths. """
[docs]defrun_command(self:"PyWf",args:list[str],real_run:bool,cwd:T.Optional[Path]=None,check:bool=True,):""" Run a command in a subprocess, also print the command for debug, and optionally change the current working directory. :param args: The command and its arguments to run. :param real_run: If True, actually run the command; if False, just print it. :param cwd: The directory to change to before running the command. :param check: If True, raise an exception if the command fails. """ifcwdisNone:cwd=self.dir_project_rootlogger.info(f"cd to: {cwd}")print_command(args)ifreal_runisTrue:returnsubprocess.run(args,cwd=cwd,check=check)
@cached_propertydefdir_home(self:"PyWf")->Path:""" The user home directory. Example: ``${HOME}`` """returnPath.home()# --------------------------------------------------------------------------# Virtualenv# --------------------------------------------------------------------------_VENV_RELATED=None@propertydefdir_venv(self:"PyWf")->Path:""" The virtualenv directory. Example: ``${dir_project_root}/.venv`` """returnself.dir_project_root.joinpath(".venv")@propertydefdir_venv_bin(self:"PyWf")->Path:""" The bin folder in virtualenv. Example: ``${dir_project_root}/.venv/bin`` """returnself.dir_venv.joinpath("bin")
[docs]defget_path_venv_bin_cli(self,cmd:str)->Path:""" Get the path of a command in virtualenv bin folder. Example: ``${dir_project_root}/.venv/bin/${cmd}`` """returnself.dir_venv_bin.joinpath(cmd)
@propertydefpath_venv_bin_python(self:"PyWf")->Path:""" The python executable in virtualenv. Example: ``${dir_project_root}/.venv/bin/python`` """returnself.get_path_venv_bin_cli("python")@propertydefpath_venv_bin_pip(self:"PyWf")->Path:""" The pip command in virtualenv. Example: ``${dir_project_root}/.venv/bin/pip`` """returnself.get_path_venv_bin_cli("pip")@propertydefpath_venv_bin_pytest(self:"PyWf")->Path:""" The pytest command in virtualenv. Example: ``${dir_project_root}/.venv/bin/pytest`` """returnself.get_path_venv_bin_cli("pytest")@propertydefpath_venv_bin_sphinx_build(self:"PyWf")->Path:""" The sphinx-build executable in virtualenv. Example: ``${dir_project_root}/.venv/bin/sphinx-build`` """returnself.get_path_venv_bin_cli("sphinx-build")@propertydefpath_venv_bin_bin_jupyter(self:"PyWf")->Path:""" The jupyter executable in virtualenv. Example: ``${dir_project_root}/.venv/bin/jupyter`` """returnself.get_path_venv_bin_cli("jupyter")@propertydefpath_sys_executable(self:"PyWf")->Path:""" The current Python interpreter path. """returnPath(sys.executable)
[docs]defget_path_dynamic_bin_cli(self,cmd:str)->Path:""" Search multiple locations to get the absolute path of the CLI command. It searches the following locations in order: 1. the bin folder in virtualenv. 2. the global Python's bin folder. 3. Then use the raw command name (string) as the path. Example: ``${dir_project_root}/.venv/bin/${cmd}`` or ``${global_python_bin}/${cmd}`` """p=self.dir_venv_bin.joinpath(cmd)ifp.exists():returnpp=self.path_sys_executable.parent.joinpath(cmd)ifp.exists():returnpreturnPath(cmd)
@propertydefpath_bin_virtualenv(self:"PyWf")->Path:""" The virtualenv CLI command path. Example: ``${dir_project_root}/.venv/bin/virtualenv`` """returnself.get_path_dynamic_bin_cli("virtualenv")@propertydefpath_bin_poetry(self:"PyWf")->Path:""" The poetry CLI command path. Example: ``${dir_project_root}/.venv/bin/poetry`` """returnself.get_path_dynamic_bin_cli("poetry")@propertydefpath_bin_twine(self:"PyWf")->Path:""" The twine CLI command path. Example: ``${dir_project_root}/.venv/bin/twine`` """returnself.get_path_dynamic_bin_cli("twine")# --------------------------------------------------------------------------# Source code# --------------------------------------------------------------------------@propertydefdir_python_lib(self:"PyWf")->Path:""" The current Python library directory. Example: ``${dir_project_root}/${package_name}`` """returnself.dir_project_root.joinpath(self.package_name)@propertydefpath_version_py(self:"PyWf")->Path:""" Path to the ``_version.py`` file where the package version is defined. Example: ``${dir_project_root}/${package_name}/_version.py`` """returnself.dir_python_lib.joinpath("_version.py")# --------------------------------------------------------------------------# Pytest# --------------------------------------------------------------------------_PYTEST_RELATED=None@propertydefdir_tests(self:"PyWf")->Path:""" Unit test folder. Example: ``${dir_project_root}/tests`` """returnself.dir_project_root.joinpath("tests")@propertydefdir_tests_int(self:"PyWf")->Path:""" Integration test folder. Example: ``${dir_project_root}/tests_int`` """returnself.dir_project_root.joinpath("tests_int")@propertydefdir_tests_load(self:"PyWf")->Path:""" Load test folder. Example: ``${dir_project_root}/tests_load`` """returnself.dir_project_root.joinpath("tests_load")@propertydefdir_htmlcov(self:"PyWf")->Path:""" The code coverage test results HTML output folder. Example: ``${dir_project_root}/htmlcov`` """returnself.dir_project_root.joinpath("htmlcov")@propertydefpath_htmlcov_index_html(self:"PyWf")->Path:""" The code coverage test results HTML file. Example: ``${dir_project_root}/htmlcov/index.html`` """returnself.dir_htmlcov.joinpath("index.html")# --------------------------------------------------------------------------# Sphinx doc# --------------------------------------------------------------------------_SPHINX_DOC_RELATED=None@propertydefdir_sphinx_doc(self:"PyWf")->Path:""" Sphinx docs folder. Example: ``${dir_project_root}/docs`` """returnself.dir_project_root.joinpath("docs")@propertydefdir_sphinx_doc_source(self:"PyWf")->Path:""" Sphinx docs source code folder. Example: ``${dir_project_root}/docs/source`` """returnself.dir_sphinx_doc.joinpath("source")@propertydefdir_sphinx_doc_source_conf_py(self:"PyWf")->Path:""" Sphinx docs ``conf.py`` file path. Example: ``${dir_project_root}/docs/source/conf.py`` """returnself.dir_sphinx_doc_source.joinpath("conf.py")@propertydefdir_sphinx_doc_source_python_lib(self:"PyWf")->Path:""" The generated Python library API reference Sphinx docs folder. Example: ``${dir_project_root}/docs/source/${package_name}`` """returnself.dir_sphinx_doc_source.joinpath(self.package_name)@propertydefdir_sphinx_doc_build(self:"PyWf")->Path:""" The temp Sphinx doc build folder. Example: ``${dir_project_root}/docs/build """returnself.dir_sphinx_doc.joinpath("build")@propertydefdir_sphinx_doc_build_html(self:"PyWf")->Path:""" The built Sphinx doc build HTML folder. Example: ``${dir_project_root}/docs/build/html """returnself.dir_sphinx_doc_build.joinpath("html")@propertydefpath_sphinx_doc_build_index_html(self:"PyWf")->Path:""" The built Sphinx doc site entry HTML file path. Example: ``${dir_project_root}/docs/build/html/index.html or README.html """ifself.dir_sphinx_doc_source.joinpath("index.rst").exists():returnself.dir_sphinx_doc_build_html.joinpath("index.html")ifself.dir_sphinx_doc_source.joinpath("README.rst").exists():# pragma: no coverreturnself.dir_sphinx_doc_build_html.joinpath("README.html")raiseFileNotFoundError(str(self.dir_sphinx_doc_build_html.joinpath("index.html")))# pragma: no cover# --------------------------------------------------------------------------# Poetry# --------------------------------------------------------------------------_POETRY_RELATED=None@propertydefpath_requirements(self:"PyWf")->Path:""" The requirements.txt file path. Example: ``${dir_project_root}/requirements.txt`` """returnself.dir_project_root.joinpath("requirements.txt")@propertydefpath_requirements_dev(self:"PyWf")->Path:""" The requirements-dev.txt file path. Example: ``${dir_project_root}/requirements-dev.txt`` """returnself.dir_project_root.joinpath("requirements-dev.txt")@propertydefpath_requirements_test(self:"PyWf")->Path:""" The requirements-test.txt file path. Example: ``${dir_project_root}/requirements-test.txt`` """returnself.dir_project_root.joinpath("requirements-test.txt")@propertydefpath_requirements_doc(self:"PyWf")->Path:""" The requirements-doc.txt file path. Example: ``${dir_project_root}/requirements-doc.txt`` """returnself.dir_project_root.joinpath("requirements-doc.txt")@propertydefpath_requirements_automation(self:"PyWf")->Path:""" The requirements-automation.txt file path. Example: ``${dir_project_root}/requirements-automation.txt`` """returnself.dir_project_root.joinpath("requirements-automation.txt")@propertydefpath_poetry_lock(self:"PyWf")->Path:""" The poetry.lock file path. Example: ``${dir_project_root}/poetry.lock`` """returnself.dir_project_root.joinpath("poetry.lock")@propertydefpath_poetry_lock_hash_json(self:"PyWf")->Path:""" The poetry-lock-hash.json file path. It is the cache of the poetry.lock file hash. Example: ``${dir_project_root}/poetry-lock-hash.json`` """returnself.dir_project_root.joinpath("poetry-lock-hash.json")# ------------------------------------------------------------------------------# Build Related# ------------------------------------------------------------------------------_BUILD_RELATED=None@propertydefpath_pyproject_toml(self:"PyWf")->Path:""" The pyproject.toml file path. Example: ``${dir_project_root}/pyproject.toml`` """returnself.dir_project_root.joinpath("pyproject.toml")@propertydefdir_build(self:"PyWf")->Path:""" The build folder for Python or artifacts build. Example: ``${dir_project_root}/build`` """returnself.dir_project_root.joinpath("build")@propertydefdir_dist(self:"PyWf")->Path:""" The dist folder for Python package distribution (.whl file). Example: ``${dir_project_root}/dist`` """returnself.dir_project_root.joinpath("dist")# ------------------------------------------------------------------------------# AWS Related# ------------------------------------------------------------------------------_AWS_RELATED=None@propertydefpath_bin_aws(self:"PyWf")->Path:""" The AWS CLI executable path. Example: ``${dir_project_root}/.venv/bin/aws`` """returnself.get_path_dynamic_bin_cli("aws")