Replace the "clean-pwd" session with "clean-cwd"
This commit is contained in:
parent
ded4691177
commit
2147dbf9d0
1 changed files with 47 additions and 23 deletions
70
noxfile.py
70
noxfile.py
|
|
@ -28,8 +28,9 @@ as unified tasks to assure the quality of the source code:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import collections
|
import collections
|
||||||
import glob
|
import itertools
|
||||||
import os
|
import os
|
||||||
|
import pathlib
|
||||||
import tempfile
|
import tempfile
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
from typing import Any, Generator
|
from typing import Any, Generator
|
||||||
|
|
@ -446,17 +447,23 @@ def create_develop_environment(session: Session) -> None:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _expand(*patterns: str) -> Generator[str, None, None]:
|
def _expand(*patterns: str) -> Generator[pathlib.Path, None, None]:
|
||||||
"""Expand glob patterns into paths.
|
"""Expand glob patterns into (resolved) paths.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
*patterns: the patterns to be expanded
|
*patterns: patterns to be expanded
|
||||||
|
|
||||||
Yields:
|
Yields:
|
||||||
expanded: a single expanded path
|
expanded: an expanded path
|
||||||
""" # noqa:RST213
|
"""
|
||||||
for pattern in patterns:
|
for pattern in patterns:
|
||||||
yield from glob.glob(pattern.strip())
|
if not (pattern := pattern.strip()):
|
||||||
|
continue
|
||||||
|
|
||||||
|
expanded_paths = pathlib.Path.cwd().glob(pattern)
|
||||||
|
|
||||||
|
for path in expanded_paths:
|
||||||
|
yield path.resolve()
|
||||||
|
|
||||||
|
|
||||||
@nox.session(name='init-project', python=MAIN_PYTHON, venv_backend='none')
|
@nox.session(name='init-project', python=MAIN_PYTHON, venv_backend='none')
|
||||||
|
|
@ -466,28 +473,45 @@ def init_project(session):
|
||||||
session.run('uv', 'run', 'pre-commit', 'install', f'--hook-type={type_}')
|
session.run('uv', 'run', 'pre-commit', 'install', f'--hook-type={type_}')
|
||||||
|
|
||||||
|
|
||||||
@nox.session(name='clean-pwd', python=MAIN_PYTHON, venv_backend='none')
|
@nox.session(name='clean-cwd', python=MAIN_PYTHON, venv_backend='none')
|
||||||
def clean_pwd(session):
|
def clean_cwd(session: Session) -> None:
|
||||||
"""Remove (almost) all glob patterns listed in .gitignore.
|
"""Remove (almost) all glob patterns listed in git's ignore file.
|
||||||
|
|
||||||
The difference compared to `git clean -X` is that this task
|
Compared to `git clean -X`, never remove local environment variables
|
||||||
does not remove pyenv's .python-version file and poetry's
|
in the ".env" and ".envrc" files, pyenv's ".python-version" file,
|
||||||
virtual environment.
|
and the local develop environment in ".venv".
|
||||||
|
|
||||||
|
An existing ".cache/" folder is always deleted first.
|
||||||
"""
|
"""
|
||||||
exclude = frozenset(('.env', '.python-version', '.venv/', 'venv/'))
|
do_not_accept_arguments(session)
|
||||||
|
|
||||||
with open('.gitignore') as file_handle:
|
do_not_remove = {
|
||||||
paths = file_handle.readlines()
|
'.env',
|
||||||
|
'.envrc',
|
||||||
|
'.python-version',
|
||||||
|
'.venv',
|
||||||
|
}
|
||||||
|
|
||||||
for path in _expand(*paths):
|
with pathlib.Path('.gitignore').open() as fp:
|
||||||
if path.startswith('#'):
|
ignored_patterns = {
|
||||||
|
pattern for pattern in fp if pattern and not pattern.startswith(('#', '!'))
|
||||||
|
}
|
||||||
|
|
||||||
|
with pathlib.Path('.gitignore').open() as fp:
|
||||||
|
excluded_patterns = {pattern[1:] for pattern in fp if pattern.startswith('!')}
|
||||||
|
|
||||||
|
# Paths are resolved into absolute ones to avoid accidental matches
|
||||||
|
excluded_paths = {
|
||||||
|
pathlib.Path(path).resolve()
|
||||||
|
for path in do_not_remove | set(_expand(*excluded_patterns))
|
||||||
|
}
|
||||||
|
|
||||||
|
for path in itertools.chain([pathlib.Path('.cache')], _expand(*ignored_patterns)):
|
||||||
|
# The `path` must not be a sub-path of an `excluded_path`
|
||||||
|
if {path, *path.parents} & excluded_paths:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for excluded in exclude:
|
session.run('rm', '-rf', path)
|
||||||
if path.startswith(excluded):
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
session.run('rm', '-rf', path)
|
|
||||||
|
|
||||||
|
|
||||||
def do_not_accept_arguments(session: Session, *, raise_error: bool = True) -> None:
|
def do_not_accept_arguments(session: Session, *, raise_error: bool = True) -> None:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue