| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
import hashlib |
| 6 |
from typing import TYPE_CHECKING, Any, Dict |
| 7 |
|
| 8 |
if TYPE_CHECKING: |
| 9 |
from taskgraph.transforms.base import TransformConfig |
| 10 |
|
| 11 |
|
| 12 |
def get_checkout_dir(task: Dict[str, Any]) -> str: |
| 13 |
worker = task["worker"] |
| 14 |
if worker["os"] == "windows": |
| 15 |
return "build" |
| 16 |
elif worker["implementation"] == "docker-worker": |
| 17 |
return f"{task['run']['workdir']}/checkouts" |
| 18 |
else: |
| 19 |
return "checkouts" |
| 20 |
|
| 21 |
|
| 22 |
def get_checkout_cache_name(config: "TransformConfig", task: Dict[str, Any]) -> str: |
| 23 |
repo_configs = config.repo_configs |
| 24 |
cache_name = "checkouts" |
| 25 |
|
| 26 |
# Robust checkout does not clean up subrepositories, so ensure that tasks |
| 27 |
# that checkout different sets of paths have separate caches. |
| 28 |
# See https://bugzilla.mozilla.org/show_bug.cgi?id=1631610 |
| 29 |
if len(repo_configs) > 1: |
| 30 |
checkout_paths = { |
| 31 |
"\t".join([repo_config.path, repo_config.prefix]) |
| 32 |
for repo_config in sorted( |
| 33 |
repo_configs.values(), key=lambda repo_config: repo_config.path |
| 34 |
) |
| 35 |
} |
| 36 |
checkout_paths_str = "\n".join(checkout_paths).encode("utf-8") |
| 37 |
digest = hashlib.sha256(checkout_paths_str).hexdigest() |
| 38 |
cache_name += f"-repos-{digest}" |
| 39 |
|
| 40 |
# Sparse checkouts need their own cache because they can interfere |
| 41 |
# with clients that aren't sparse aware. |
| 42 |
if task["run"]["sparse-profile"]: |
| 43 |
cache_name += "-sparse" |
| 44 |
|
| 45 |
return cache_name |
| 46 |
|
| 47 |
|
| 48 |
CACHES = { |
| 49 |
"cargo": {"env": "CARGO_HOME"}, |
| 50 |
"checkout": { |
| 51 |
"cache_dir": get_checkout_dir, |
| 52 |
"cache_name": get_checkout_cache_name, |
| 53 |
}, |
| 54 |
"npm": {"env": "npm_config_cache"}, |
| 55 |
"pip": {"env": "PIP_CACHE_DIR"}, |
| 56 |
"uv": {"env": "UV_CACHE_DIR"}, |
| 57 |
} |