-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
198 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# ruff: noqa: E501 | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
|
||
def rm( | ||
*patterns: str, | ||
cwd: str = ".", | ||
verbosity: Union[int, str] = 0, | ||
dry_run: bool = False, | ||
): | ||
""" | ||
This function is intended for use in a script task to delete files and directories | ||
matching the given patterns, as a platform agnostic alternative to the `rm -rf` | ||
Example usage: | ||
.. code-block:: toml | ||
[tool.poe.tasks.clean] | ||
script = "poethepoet.scripts:rm('.mypy_cache', '.pytest_cache', './**/__pycache__')" | ||
""" | ||
verbosity = int(verbosity) | ||
|
||
for pattern in patterns: | ||
matches = list(Path(cwd).glob(pattern)) | ||
if verbosity > 0 and not matches: | ||
print(f"No files or directories to delete matching {pattern!r}") | ||
elif verbosity >= 0 and len(matches) > 1: | ||
print(f"Deleting paths matching {pattern!r}") | ||
|
||
for match in matches: | ||
_delete_path(match, verbosity, dry_run) | ||
|
||
|
||
def _delete_path(path: Path, verbosity: int, dry_run: bool): | ||
import shutil | ||
|
||
if path.is_dir(): | ||
if verbosity > 0: | ||
print(f"Deleting directory '{path}'") | ||
if not dry_run: | ||
shutil.rmtree(path) | ||
else: | ||
if verbosity > 0: | ||
print(f"Deleting file '{path}'") | ||
if not dry_run: | ||
path.unlink() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.