Skip to content

Commit 0b865c9

Browse files
authored
Exclude cache directories from backups using CACHEDIR.TAG (python#9018)
This helps to prevent bloating backups with caches. See https://bford.info/cachedir/ for more information about the specification – it's supported by attic.
1 parent cbf7705 commit 0b865c9

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

mypy/build.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ def _build(sources: List[BuildSource],
269269
reports.finish()
270270
if not cache_dir_existed and os.path.isdir(options.cache_dir):
271271
add_catch_all_gitignore(options.cache_dir)
272+
exclude_from_backups(options.cache_dir)
272273

273274

274275
def default_data_dir() -> str:
@@ -1117,6 +1118,22 @@ def add_catch_all_gitignore(target_dir: str) -> None:
11171118
pass
11181119

11191120

1121+
def exclude_from_backups(target_dir: str) -> None:
1122+
"""Exclude the directory from various archives and backups supporting CACHEDIR.TAG.
1123+
1124+
If the CACHEDIR.TAG file exists the function is a no-op.
1125+
"""
1126+
cachedir_tag = os.path.join(target_dir, "CACHEDIR.TAG")
1127+
try:
1128+
with open(cachedir_tag, "x") as f:
1129+
f.write("""Signature: 8a477f597d28d172789f06886806bc55
1130+
# This file is a cache directory tag automtically created by mypy.
1131+
# For information about cache directory tags see https://bford.info/cachedir/
1132+
""")
1133+
except FileExistsError:
1134+
pass
1135+
1136+
11201137
def create_metastore(options: Options) -> MetadataStore:
11211138
"""Create the appropriate metadata store."""
11221139
if options.sqlite_cache:

mypy/test/testcheck.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ def verify_cache(self, module_data: List[Tuple[str, str, str]], a: List[str],
277277
raise AssertionError("cache data discrepancy %s != %s" %
278278
(missing_paths, busted_paths))
279279
assert os.path.isfile(os.path.join(manager.options.cache_dir, ".gitignore"))
280+
cachedir_tag = os.path.join(manager.options.cache_dir, "CACHEDIR.TAG")
281+
assert os.path.isfile(cachedir_tag)
282+
with open(cachedir_tag) as f:
283+
assert f.read().startswith("Signature: 8a477f597d28d172789f06886806bc55")
280284

281285
def find_error_message_paths(self, a: List[str]) -> Set[str]:
282286
hits = set()

test-data/unit/check-incremental.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3722,6 +3722,9 @@ import b
37223722
{"snapshot": {"__main__": "a7c958b001a45bd6a2a320f4e53c4c16", "a": "d41d8cd98f00b204e9800998ecf8427e", "b": "d41d8cd98f00b204e9800998ecf8427e", "builtins": "c532c89da517a4b779bcf7a964478d67"}, "deps_meta": {"@root": {"path": "@root.deps.json", "mtime": 0}, "__main__": {"path": "__main__.deps.json", "mtime": 0}, "a": {"path": "a.deps.json", "mtime": 0}, "b": {"path": "b.deps.json", "mtime": 0}, "builtins": {"path": "builtins.deps.json", "mtime": 0}}}
37233723
[file ../.mypy_cache/.gitignore]
37243724
# Another hack to not trigger a .gitignore creation failure "false positive"
3725+
[file ../.mypy_cache/CACHEDIR.TAG]
3726+
Signature: 8a477f597d28d172789f06886806bc55
3727+
# Another another hack to not trigger a CACHEDIR.TAG creation failure "false positive"
37253728
[file b.py.2]
37263729
# uh
37273730
-- Every file should get reloaded, since the cache was invalidated

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy