├── .github └── workflows │ └── ci.yml ├── .gitignore ├── LICENSE.txt ├── README.md ├── pyproject.toml ├── src └── hatch_pip_deepfreeze │ ├── __init__.py │ ├── hooks.py │ └── plugin.py └── tests ├── __init__.py └── test_dummy.py /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | tags: 8 | - "v[0-9]+.[0-9]+(.[0-9]+)?" 9 | pull_request: 10 | branches: 11 | - main 12 | 13 | concurrency: 14 | group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }} 15 | cancel-in-progress: true 16 | 17 | env: 18 | PYTHONUNBUFFERED: "1" 19 | FORCE_COLOR: "1" 20 | 21 | jobs: 22 | test: 23 | name: Python ${{ matrix.python-version }} on ${{ startsWith(matrix.os, 'macos-') && 'macOS' || startsWith(matrix.os, 'windows-') && 'Windows' || 'Linux' }} 24 | runs-on: ${{ matrix.os }} 25 | strategy: 26 | fail-fast: true 27 | matrix: 28 | os: [ubuntu-latest, windows-latest, macos-latest] 29 | python-version: ["3.7", "3.8", "3.9", "3.10", "3.11.0-alpha.7 - 3.11"] 30 | steps: 31 | - uses: actions/checkout@v3 32 | - name: Set up Python ${{ matrix.python-version }} 33 | uses: actions/setup-python@v4 34 | with: 35 | python-version: ${{ matrix.python-version }} 36 | - name: Install hatch 37 | run: pipx install hatch 38 | - name: Run tests 39 | run: hatch run cov 40 | 41 | publish: 42 | runs-on: ubuntu-latest 43 | needs: 44 | - test 45 | if: startsWith(github.ref, 'refs/tags') 46 | steps: 47 | - uses: actions/checkout@v3 48 | - uses: actions/setup-python@v4 49 | - name: Build a binary wheel and a source tarball 50 | run: pipx run build 51 | - name: Publish distribution 📦 to PyPI 52 | uses: pypa/gh-action-pypi-publish@release/v1 53 | with: 54 | user: __token__ 55 | password: ${{ secrets.pypi_token }} 56 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | 162 | .envrc 163 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022-present Stéphane Bidoul 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hatch-pip-deepfreeze 2 | 3 | [![PyPI - Version](https://img.shields.io/pypi/v/hatch-pip-deepfreeze.svg)](https://pypi.org/project/hatch-pip-deepfreeze) 4 | [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/hatch-pip-deepfreeze.svg)](https://pypi.org/project/hatch-pip-deepfreeze) 5 | 6 | ----- 7 | 8 | A [hatch](https://pypi.org/project/hatch/) virtual 9 | [environment](https://hatch.pypa.io/latest/config/environment/overview/) plugin to lock 10 | dependencies with [pip-deepfreeze](https://pypi.org/project/pip-deepfreeze/). 11 | 12 | ## Installation 13 | 14 | `hatch-pip-deepfreeze` must be installed in the same environment as `hatch` itself. 15 | 16 | If `hatch` has been installed with `pipx`: 17 | 18 | ```console 19 | pipx runpip hatch install hatch-pip-deepfreeze 20 | ``` 21 | 22 | If `hatch` has been installed with `pip` in the user environment: 23 | 24 | ```console 25 | pip install --user hatch-pip-deepfreeze 26 | ``` 27 | 28 | ## Usage 29 | 30 | In the `tool.hatch.envs.{name}` section, add `type = "pip-deepfreeze"`. 31 | Such environments behave similarly to the standard hatch `virtual` environments, 32 | except the installation and synchronization is performed using `pip-deepfreeze sync`. 33 | 34 | This will automatically generate locked dependencies in `requirements.txt`, and 35 | uninstall unneeded dependencies after removing them from `pyproject.toml`. 36 | It also pins optional dependencies groups in `requirements-{extra}.txt`. 37 | 38 | You can use the `features` to install `project.optional-dependencies` in environments. 39 | 40 | Note that this pluging does not support per environment `dependencies`, because 41 | `pip-deepfreeze` works exclusively with `project.optional-dependencies` for that. 42 | Fortunately this is well supported by `hatch`. 43 | 44 | As an example, you can adapt a `pyproject.toml` generated by `hatch new` like so: 45 | 46 | Declare optional dependencies for test: 47 | 48 | ```toml 49 | [project.optional-dependencies] 50 | test = [ 51 | "pytest", 52 | "pytest-cov", 53 | ] 54 | ``` 55 | 56 | Update the default environment section to remove `dependencies` and add `features`: 57 | 58 | ```toml 59 | [tool.hatch.envs.default] 60 | type = "pip-deepfreeze" 61 | features = ["test"] 62 | ``` 63 | 64 | Use the usual `hatch` environment activation features such as `hatch shell` and notice 65 | `pip-deepfreeze` in action. 66 | 67 | ## License 68 | 69 | `hatch-pip-deepfreeze` is distributed under the terms of the 70 | [MIT](https://spdx.org/licenses/MIT.html) license. 71 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["hatchling", "hatch-vcs"] 3 | build-backend = "hatchling.build" 4 | 5 | [project] 6 | name = "hatch-pip-deepfreeze" 7 | description = 'Lock hatch environments using pip-deepfreeze' 8 | readme = "README.md" 9 | requires-python = ">=3.7" 10 | license = "MIT" 11 | keywords = [] 12 | authors = [ 13 | { name = "Stéphane Bidoul", email = "stephane.bidoul@acsone.eu" }, 14 | ] 15 | classifiers = [ 16 | "Development Status :: 4 - Beta", 17 | "Programming Language :: Python", 18 | "Programming Language :: Python :: 3.7", 19 | "Programming Language :: Python :: 3.8", 20 | "Programming Language :: Python :: 3.9", 21 | "Programming Language :: Python :: 3.10", 22 | "Programming Language :: Python :: 3.11", 23 | "Programming Language :: Python :: Implementation :: CPython", 24 | "Programming Language :: Python :: Implementation :: PyPy", 25 | "Framework :: Hatch", 26 | ] 27 | dependencies = ["hatch", "pip-deepfreeze"] 28 | dynamic = ["version"] 29 | 30 | [project.entry-points.hatch] 31 | pip-deepfreeze = "hatch_pip_deepfreeze.hooks" 32 | 33 | [project.urls] 34 | Documentation = "https://github.com/sbidoul/hatch-pip-deepfreeze#readme" 35 | Issues = "https://github.com/sbidoul/hatch-pip-deepfreeze/issues" 36 | Source = "https://github.com/sbidoul/hatch-pip-deepfreeze" 37 | 38 | [tool.hatch.version] 39 | source = "vcs" 40 | 41 | [tool.hatch.envs.default] 42 | dependencies = [ 43 | "pytest", 44 | "pytest-cov", 45 | ] 46 | [tool.hatch.envs.default.scripts] 47 | cov = "pytest --cov-report=term-missing --cov-config=pyproject.toml --cov=hatch_pip_deepfreeze --cov=tests" 48 | no-cov = "cov --no-cov" 49 | 50 | [[tool.hatch.envs.test.matrix]] 51 | python = ["37", "38", "39", "310", "311"] 52 | 53 | [tool.coverage.run] 54 | branch = true 55 | parallel = true 56 | omit = [ 57 | ] 58 | 59 | [tool.coverage.report] 60 | exclude_lines = [ 61 | "no cov", 62 | "if __name__ == .__main__.:", 63 | "if TYPE_CHECKING:", 64 | ] 65 | -------------------------------------------------------------------------------- /src/hatch_pip_deepfreeze/__init__.py: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2022-present Stéphane Bidoul 2 | # 3 | # SPDX-License-Identifier: MIT 4 | -------------------------------------------------------------------------------- /src/hatch_pip_deepfreeze/hooks.py: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2022-present Stéphane Bidoul 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | from hatchling.plugin import hookimpl 6 | 7 | from .plugin import PipDeepfreezeEnvironment 8 | 9 | 10 | @hookimpl 11 | def hatch_register_environment(): 12 | return PipDeepfreezeEnvironment 13 | -------------------------------------------------------------------------------- /src/hatch_pip_deepfreeze/plugin.py: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2022-present Stéphane Bidoul 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | import sys 6 | from typing import List 7 | 8 | from hatch.env.virtual import VirtualEnvironment 9 | 10 | 11 | class PipDeepfreezeEnvironment(VirtualEnvironment): 12 | PLUGIN_NAME = "pip-deepfreeze" 13 | 14 | def _pip_df_sync_cmd(self) -> List[str]: 15 | if self.environment_dependencies: 16 | raise NotImplementedError( 17 | "Per environment dependencies are not supported " 18 | "in pip-deepfreeze environments. " 19 | "Please use project.optional-dependencies and " 20 | "tool.hatch.envs..features." 21 | ) 22 | cmd = [ 23 | sys.executable, 24 | "-m", 25 | "pip_deepfreeze", 26 | "sync", 27 | "--uninstall-unneeded", 28 | ] 29 | if self.features: 30 | cmd += ["--extras", ",".join(self.features)] 31 | return cmd 32 | 33 | def install_project(self): 34 | raise NotImplementedError( 35 | "Project must be installed in dev mode in pip-deepfreeze environments." 36 | ) 37 | 38 | def install_project_dev_mode(self): 39 | with self.safe_activation(): 40 | self.platform.check_command(self._pip_df_sync_cmd()) 41 | 42 | def dependencies_in_sync(self): 43 | # always sync, pip-df sync is relatively fast 44 | return False 45 | 46 | def sync_dependencies(self): 47 | with self.safe_activation(): 48 | self.platform.check_command(self._pip_df_sync_cmd()) 49 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2022-present Stéphane Bidoul 2 | # 3 | # SPDX-License-Identifier: MIT 4 | -------------------------------------------------------------------------------- /tests/test_dummy.py: -------------------------------------------------------------------------------- 1 | def test_dummy(): 2 | ... 3 | --------------------------------------------------------------------------------