|
5 | 5 | logger = logging.getLogger(__name__)
|
6 | 6 |
|
7 | 7 |
|
| 8 | +CACHE_DIR_NAME = ".docstub_cache" |
| 9 | + |
| 10 | + |
8 | 11 | CACHEDIR_TAG_CONTENT = """\
|
9 |
| -Signature: 8a477f597d28d172789f06886806bc55\ |
10 |
| -# This file is a cache directory tag automatically created by docstub.\n" |
11 |
| -# For information about cache directory tags see https://bford.info/cachedir/\n" |
| 12 | +Signature: 8a477f597d28d172789f06886806bc55 |
| 13 | +# Mark this directory as a cache [1], created by docstub [2] |
| 14 | +# [1] https://bford.info/cachedir/ |
| 15 | +# [2] https://github.com/scientific-python/docstub |
| 16 | +""" |
| 17 | + |
| 18 | + |
| 19 | +GITHUB_IGNORE_CONTENT = """\ |
| 20 | +# Make git ignore this cache directory, created by docstub [1] |
| 21 | +# [1] https://github.com/scientific-python/docstub |
| 22 | +* |
12 | 23 | """
|
13 | 24 |
|
14 | 25 |
|
@@ -43,22 +54,42 @@ def create_cache(path):
|
43 | 54 | """
|
44 | 55 | path.mkdir(parents=True, exist_ok=True)
|
45 | 56 | cachdir_tag_path = path / "CACHEDIR.TAG"
|
46 |
| - cachdir_tag_content = ( |
47 |
| - "Signature: 8a477f597d28d172789f06886806bc55\n" |
48 |
| - "# This file is a cache directory tag automatically created by docstub.\n" |
49 |
| - "# For information about cache directory tags see https://bford.info/cachedir/\n" |
50 |
| - ) |
| 57 | + |
51 | 58 | if not cachdir_tag_path.is_file():
|
52 | 59 | with open(cachdir_tag_path, "w") as fp:
|
53 |
| - fp.write(cachdir_tag_content) |
| 60 | + fp.write(CACHEDIR_TAG_CONTENT) |
54 | 61 |
|
55 | 62 | gitignore_path = path / ".gitignore"
|
56 |
| - gitignore_content = ( |
57 |
| - "# This file is a cache directory automatically created by docstub.\n" "*\n" |
58 |
| - ) |
59 | 63 | if not gitignore_path.is_file():
|
60 | 64 | with open(gitignore_path, "w") as fp:
|
61 |
| - fp.write(gitignore_content) |
| 65 | + fp.write(GITHUB_IGNORE_CONTENT) |
| 66 | + |
| 67 | + |
| 68 | +def validate_cache(path): |
| 69 | + """Make sure the given path is a cache created by docstub. |
| 70 | +
|
| 71 | + Parameters |
| 72 | + ---------- |
| 73 | + path : Path |
| 74 | +
|
| 75 | + Raises |
| 76 | + ------ |
| 77 | + FileNotFoundError |
| 78 | + """ |
| 79 | + if not path.is_dir(): |
| 80 | + raise FileNotFoundError(f"expected '{path}' to be a valid directory") |
| 81 | + |
| 82 | + if not path.name == CACHE_DIR_NAME: |
| 83 | + raise FileNotFoundError( |
| 84 | + f"expected directory '{path}' be named '{CACHE_DIR_NAME}'" |
| 85 | + ) |
| 86 | + |
| 87 | + cachdir_tag_path = path / "CACHEDIR.TAG" |
| 88 | + if not cachdir_tag_path.is_file(): |
| 89 | + raise FileNotFoundError(f"expected '{path}' to contain a 'CACHEDIR.TAG' file") |
| 90 | + gitignore_path = path / ".gitignore" |
| 91 | + if not gitignore_path.is_file(): |
| 92 | + raise FileNotFoundError(f"expected '{path}' to contain a '.gitignore' file") |
62 | 93 |
|
63 | 94 |
|
64 | 95 | class FuncSerializer[T](Protocol):
|
|
0 commit comments