[PATCH 2/2] add test for CACHEDIR.TAG creation in build dir

0 views
Skip to first unread message

Felix Moessbauer

unread,
Feb 27, 2026, 6:24:42 AM (3 days ago) Feb 27
to kas-...@googlegroups.com, jan.k...@siemens.com, Felix Moessbauer
Signed-off-by: Felix Moessbauer <felix.mo...@siemens.com>
---
tests/test_commands.py | 14 ++++++++++++++
1 file changed, 14 insertions(+)

diff --git a/tests/test_commands.py b/tests/test_commands.py
index d0f7344ac..e891bf841 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -518,3 +518,17 @@ def test_cmd_not_found(monkeykas, tmpdir):
assert ret != 0
with pytest.raises(FileNotFoundError):
run_cmd(cmd, tmpdir, os.environ, fail=True)
+
+
+...@pytest.mark.dirsfromenv
+def test_build_dir_add_cachedir_tag(monkeykas, tmpdir):
+ tdir = str(tmpdir.mkdir('test_build_dir_add_cachedir_tag'))
+ shutil.rmtree(tdir, ignore_errors=True)
+ shutil.copytree('tests/test_commands', tdir)
+ monkeykas.chdir(tdir)
+
+ kas.kas(['checkout', 'test-local.yml'])
+ cachetag = monkeykas.get_kbd() / 'CACHEDIR.TAG'
+ assert cachetag.exists()
+ with cachetag.open() as f:
+ assert f.readline().startswith('Signature:')
--
2.51.0

Felix Moessbauer

unread,
Feb 27, 2026, 6:24:42 AM (3 days ago) Feb 27
to kas-...@googlegroups.com, jan.k...@siemens.com, Felix Moessbauer
The kas build directory contains temporary data and the build artifacts.
These qualify for a CACHEDIR.TAG, which helps to exclude these
directories from backups.

Signed-off-by: Felix Moessbauer <felix.mo...@siemens.com>
---
docs/command-line/environment-variables.inc | 1 +
kas/libcmds.py | 4 +++-
kas/libkas.py | 18 ++++++++++++++++++
3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/docs/command-line/environment-variables.inc b/docs/command-line/environment-variables.inc
index 9cc6884a5..62d366d60 100644
--- a/docs/command-line/environment-variables.inc
+++ b/docs/command-line/environment-variables.inc
@@ -58,6 +58,7 @@ overwritten using the ``env`` section of the config file.
| ``KAS_BUILD_DIR`` | The path of the build directory, |
| (C, K) | ``${KAS_WORK_DIR}/build`` is the default. |
| | The parent directory must exist if set. |
+| | kas adds a ``CACHEDIR.TAG``. |
+--------------------------+--------------------------------------------------+
| ``KAS_REPO_REF_DIR`` | The path to the repository reference directory. |
| (C, K) | Repositories in this directory are used as |
diff --git a/kas/libcmds.py b/kas/libcmds.py
index 05f0871ba..3a651bfb6 100644
--- a/kas/libcmds.py
+++ b/kas/libcmds.py
@@ -34,7 +34,8 @@ import base64
from pathlib import Path
from git.config import GitConfigParser
from .libkas import (ssh_cleanup_agent, ssh_setup_agent, ssh_no_host_key_check,
- get_build_environ, repos_fetch, repos_apply_patches)
+ get_build_environ, repos_fetch, repos_apply_patches,
+ add_cachedir_tag)
from .context import ManagedEnvironment as ME
from .context import get_context
from .includehandler import IncludeException
@@ -429,6 +430,7 @@ class SetupDir(Command):
def execute(self, ctx):
if not os.path.exists(ctx.build_dir):
os.mkdir(ctx.build_dir)
+ add_cachedir_tag(ctx.build_dir, "kas build directory")


class SetupSSHAgent(Command):
diff --git a/kas/libkas.py b/kas/libkas.py
index 801134f42..327803476 100644
--- a/kas/libkas.py
+++ b/kas/libkas.py
@@ -626,6 +626,24 @@ def ssh_no_host_key_check():
ssh_config)


+def add_cachedir_tag(dir, comment=None):
+ """
+ Create a CACHEDIR.TAG below dir. If a comment is provided,
+ as this to the tag as well.
+ """
+ cachetag = pathlib.Path(dir) / 'CACHEDIR.TAG'
+ if cachetag.exists():
+ return
+ with open(cachetag, 'w') as f:
+ logging.debug('create CACHEDIR.TAG in %s', str(dir))
+ f.write('Signature: 8a477f597d28d172789f06886806bc55\n')
+ f.write('# This file is a cache directory tag created by kas.\n'
+ '# For information about cache directory tags, see:\n'
+ '# https://bford.info/cachedir/spec.html\n')
+ if comment:
+ f.write(f'#\n# {comment}\n')
+
+
def setup_parser_common_args(parser):
from kas.libcmds import Macro

--
2.51.0

Jan Kiszka

unread,
Feb 27, 2026, 11:24:33 AM (3 days ago) Feb 27
to Felix Moessbauer, kas-...@googlegroups.com
Thanks, both applied.

Jan

--
Siemens AG, Foundational Technologies
Linux Expert Center

Florian Bezdeka

unread,
Feb 27, 2026, 11:45:47 AM (3 days ago) Feb 27
to Felix Moessbauer, kas-...@googlegroups.com, jan.k...@siemens.com
On Fri, 2026-02-27 at 12:24 +0100, 'Felix Moessbauer' via kas-devel
wrote:
> The kas build directory contains temporary data and the build artifacts.
> These qualify for a CACHEDIR.TAG, which helps to exclude these
> directories from backups.
>

Just minor drive-by comments, see below
s/as/add

> + """
> + cachetag = pathlib.Path(dir) / 'CACHEDIR.TAG'

os.path.join() ?

> + if cachetag.exists():
> + return
> + with open(cachetag, 'w') as f:
> + logging.debug('create CACHEDIR.TAG in %s', str(dir))

Why no format (f-) string as below?

> + f.write('Signature: 8a477f597d28d172789f06886806bc55\n')
> + f.write('# This file is a cache directory tag created by kas.\n'
> + '# For information about cache directory tags, see:\n'
> + '# https://bford.info/cachedir/spec.html\n')
> + if comment:
> + f.write(f'#\n# {comment}\n')
> +
> +
> def setup_parser_common_args(parser):
> from kas.libcmds import Macro
>
> --
> 2.51.0
>
> --
> You received this message because you are subscribed to the Google Groups "kas-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kas-devel+...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/kas-devel/20260227112428.2727325-1-felix.moessbauer%40siemens.com.

Jan Kiszka

unread,
Feb 27, 2026, 12:06:00 PM (3 days ago) Feb 27
to Florian Bezdeka, Felix Moessbauer, kas-...@googlegroups.com
Fixing up.

>> + """
>> + cachetag = pathlib.Path(dir) / 'CACHEDIR.TAG'
>
> os.path.join() ?
>

"/" is the join operator.

>> + if cachetag.exists():
>> + return
>> + with open(cachetag, 'w') as f:
>> + logging.debug('create CACHEDIR.TAG in %s', str(dir))
>
> Why no format (f-) string as below?
>

Valid cleanup, done in next now.
Reply all
Reply to author
Forward
0 new messages