summaryrefslogtreecommitdiff
path: root/mdbp/common.py
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2021-05-27 11:54:04 +0200
committerHelmut Grohne <helmut@subdivi.de>2021-05-27 11:54:04 +0200
commit69fdfcdda423b55c7251e4d46e82563cda98a63d (patch)
tree4299a69f4ae7461065f3f1bb48ac9f2ba557f058 /mdbp/common.py
parentafff0dc73188ac323ec0265cc6b39c67f9fd7474 (diff)
downloadmdbp-69fdfcdda423b55c7251e4d46e82563cda98a63d.tar.gz
add new field .output.artifacts to schema
Using the field you can specify an ORed set of positive glob-style patterns for artifacts to retain in the .output.directory. It defaults to including all artifacts.
Diffstat (limited to 'mdbp/common.py')
-rw-r--r--mdbp/common.py10
1 files changed, 10 insertions, 0 deletions
diff --git a/mdbp/common.py b/mdbp/common.py
index ba4b20d..df434cb 100644
--- a/mdbp/common.py
+++ b/mdbp/common.py
@@ -4,6 +4,7 @@
from __future__ import annotations
import argparse
import contextlib
+import fnmatch
import hashlib
import importlib.resources
import json
@@ -181,3 +182,12 @@ def tar_add(tarobj: tarfile.TarFile, path: pathlib.Path) -> None:
info.mtime = int(statres.st_mtime)
with path.open("rb") as fobj:
tarobj.addfile(info, fobj)
+
+
+def clean_dir(directory: pathlib.Path, patterns: typing.List[str]) -> None:
+ """Delete all entries of `directory` that match none of the given
+ `patterns`."""
+ for entry in directory.iterdir():
+ if not any(fnmatch.fnmatchcase(entry.name, pattern)
+ for pattern in patterns):
+ entry.unlink()