Skip to content

Commit c4c1bb5

Browse files
committed
Ensure tox-created directories contain CACHEDIR.TAG
Fixes #3334
1 parent 8cadfa2 commit c4c1bb5

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

docs/changelog/3342.feature.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Tox now creates ``CACHEDIR.TAG`` files in the workdir subdirectories it creates, so that tools like ``tar``
2+
can exclude them from e.g. backups where ephemeral directories are not desired.
3+
- by :user:`akx`

src/tox/tox_env/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from tox.execute.request import ExecuteRequest
1818
from tox.tox_env.errors import Fail, Recreate, Skip
1919
from tox.tox_env.info import Info
20-
from tox.util.path import ensure_empty_dir
20+
from tox.util.path import ensure_cachedir_dir, ensure_empty_dir
2121

2222
if TYPE_CHECKING:
2323
from tox.config.cli.parser import Parsed
@@ -314,7 +314,7 @@ def _handle_env_tmp_dir(self) -> None:
314314
env_tmp_dir.mkdir(parents=True, exist_ok=True)
315315

316316
def _handle_core_tmp_dir(self) -> None:
317-
self.core["temp_dir"].mkdir(parents=True, exist_ok=True)
317+
ensure_cachedir_dir(self.core["temp_dir"])
318318

319319
def _clean(self, transitive: bool = False) -> None: # noqa: ARG002, FBT001, FBT002
320320
if self._run_state["clean"]: # pragma: no branch

src/tox/tox_env/info.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from contextlib import contextmanager
1010
from typing import TYPE_CHECKING, Any, Iterator
1111

12+
from tox.util.path import ensure_cachedir_dir
13+
1214
if TYPE_CHECKING:
1315
from pathlib import Path
1416

@@ -63,7 +65,7 @@ def reset(self) -> None:
6365
self._content = {}
6466

6567
def _write(self) -> None:
66-
self._path.parent.mkdir(parents=True, exist_ok=True)
68+
ensure_cachedir_dir(self._path.parent)
6769
self._path.write_text(json.dumps(self._content, indent=2))
6870

6971

src/tox/tox_env/package.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
from filelock import FileLock
1212

13+
from tox.util.path import ensure_cachedir_dir
14+
1315
from .api import ToxEnv, ToxEnvCreateArgs
1416

1517
if TYPE_CHECKING:
@@ -69,7 +71,7 @@ def register_config(self) -> None:
6971
super().register_config()
7072
file_lock_path: Path = self.conf["env_dir"] / "file.lock"
7173
self._file_lock = FileLock(file_lock_path)
72-
file_lock_path.parent.mkdir(parents=True, exist_ok=True)
74+
ensure_cachedir_dir(file_lock_path.parent)
7375
self.core.add_config(
7476
keys=["package_root", "setupdir"],
7577
of_type=Path,

src/tox/util/path.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
from pathlib import Path
88

99

10+
CACHEDIR_TAG_CONTENT = b"""Signature: 8a477f597d28d172789f06886806bc55
11+
# This file is a cache directory tag created by the Tox automation project (https://tox.wiki/).
12+
# For information about cache directory tags, see:
13+
# http://www.brynosaurus.com/cachedir/
14+
"""
15+
16+
1017
def ensure_empty_dir(path: Path, except_filename: str | None = None) -> None:
1118
if path.exists():
1219
if path.is_dir():
@@ -24,6 +31,18 @@ def ensure_empty_dir(path: Path, except_filename: str | None = None) -> None:
2431
path.mkdir(parents=True)
2532

2633

34+
def ensure_cachedir_dir(path: Path) -> None:
35+
"""
36+
Ensure that the given path is a directory, exists and
37+
contains a `CACHEDIR.TAG` file.
38+
"""
39+
path.mkdir(parents=True, exist_ok=True)
40+
cachetag = path / "CACHEDIR.TAG"
41+
if not cachetag.is_file():
42+
cachetag.write_bytes(CACHEDIR_TAG_CONTENT)
43+
44+
2745
__all__ = [
46+
"ensure_cachedir_dir",
2847
"ensure_empty_dir",
2948
]

0 commit comments

Comments
 (0)