ovperf-runner 0.1.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ovperf/COLLECTOR_MEASUREMENT_CHANGELOG.md +19 -0
- ovperf/MEASUREMENT_CHANGELOG.md +14 -0
- ovperf/__main__.py +10 -0
- ovperf/_git_metadata_helpers.py +87 -0
- ovperf/_retrieval.py +84 -0
- ovperf/_text.py +29 -0
- ovperf/assets.py +279 -0
- ovperf/capture.py +994 -0
- ovperf/collector.py +754 -0
- ovperf/git_metadata.py +383 -0
- ovperf/github_source.py +512 -0
- ovperf/handoff_checkpoint.py +432 -0
- ovperf/hardware.py +1251 -0
- ovperf/machine_setup.py +150 -0
- ovperf/measurement_version.py +232 -0
- ovperf/native_ovphysx_selection.py +197 -0
- ovperf/ovlib_versions.py +305 -0
- ovperf/ovrtx_nsys_metrics.py +627 -0
- ovperf/plugin.py +784 -0
- ovperf/recording.py +143 -0
- ovperf/report.py +191 -0
- ovperf/runner.py +1309 -0
- ovperf/s3.py +1298 -0
- ovperf/superluminal.py +98 -0
- ovperf_runner-0.1.6.dist-info/METADATA +86 -0
- ovperf_runner-0.1.6.dist-info/RECORD +31 -0
- ovperf_runner-0.1.6.dist-info/WHEEL +4 -0
- ovperf_runner-0.1.6.dist-info/entry_points.txt +3 -0
- ovperf_runner-0.1.6.dist-info/licenses/EXTERNAL-MATERIALS-NOTICE.txt +1 -0
- ovperf_runner-0.1.6.dist-info/licenses/LICENSE +203 -0
- ovperf_runner-0.1.6.dist-info/licenses/NOTICE +10 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Measurement changelog: collector
|
|
2
|
+
|
|
3
|
+
> **Location.** This changelog describes ovperf-collector releases but ships with
|
|
4
|
+
> the runner, which stamps its version on every test. It may move into the
|
|
5
|
+
> collector package later.
|
|
6
|
+
|
|
7
|
+
Changes to **ovperf-collector** (recorded here, in the runner package, so the
|
|
8
|
+
collector package itself is untouched) that alter what a collected metric means (sampling,
|
|
9
|
+
windowing, how CPU/memory/GPU values are computed). Add an entry only for those, not
|
|
10
|
+
for every release: the installed release is recorded separately on each test as
|
|
11
|
+
`dependency.ovperf-collector.version`. This is stamped on every test as
|
|
12
|
+
`measurement_collector_version`. The version is not derived from the installed
|
|
13
|
+
collector; bump it here when a supported collector release changes a metric's meaning.
|
|
14
|
+
Entries are level-2 headings: `## collector vN -- Title`.
|
|
15
|
+
|
|
16
|
+
## collector v1 -- Baseline
|
|
17
|
+
|
|
18
|
+
Collector measurement behaviour as it was before versioning was introduced
|
|
19
|
+
(releases up to and including 0.4.6).
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Measurement changelog: runner
|
|
2
|
+
|
|
3
|
+
Changes to the **runner** (collector, pytest plugin, timing, capture) that alter
|
|
4
|
+
what any benchmark's numbers mean, e.g. what `execution_time_s` spans, how
|
|
5
|
+
collector metrics are windowed, or what a capture adds. These apply to every
|
|
6
|
+
test the runner executes, so they are stamped as `measurement_runner_version`
|
|
7
|
+
alongside each test's library version. Changes inside one library's benchmarks
|
|
8
|
+
go in the `MEASUREMENT_CHANGELOG.md` beside those benchmarks instead (see
|
|
9
|
+
`ovperf.measurement_version`). Entries are level-2 headings:
|
|
10
|
+
`## runner vN -- Title`.
|
|
11
|
+
|
|
12
|
+
## runner v1 -- Baseline
|
|
13
|
+
|
|
14
|
+
Runner measurement behaviour as it was before versioning was introduced.
|
ovperf/__main__.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from collections.abc import Mapping, Sequence
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from .git_metadata import GitMetadata
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def primary_git_metadata(
|
|
13
|
+
run_git: GitMetadata,
|
|
14
|
+
records: Sequence[Mapping[str, Any]],
|
|
15
|
+
) -> GitMetadata:
|
|
16
|
+
test_metadata = [
|
|
17
|
+
dict_to_git_metadata(record.get("measured_git"))
|
|
18
|
+
for record in records
|
|
19
|
+
if record.get("measured_git")
|
|
20
|
+
]
|
|
21
|
+
explicit_test_metadata = [
|
|
22
|
+
metadata
|
|
23
|
+
for metadata in test_metadata
|
|
24
|
+
if metadata is not None and not same_git_metadata(metadata, run_git)
|
|
25
|
+
]
|
|
26
|
+
explicit_primary = single_git_metadata(explicit_test_metadata)
|
|
27
|
+
if explicit_primary is not None:
|
|
28
|
+
return explicit_primary
|
|
29
|
+
|
|
30
|
+
commits = {
|
|
31
|
+
metadata.commit_sha
|
|
32
|
+
for metadata in test_metadata
|
|
33
|
+
if metadata is not None and metadata.commit_sha
|
|
34
|
+
}
|
|
35
|
+
if len(commits) == 1:
|
|
36
|
+
commit_sha = next(iter(commits))
|
|
37
|
+
for metadata in test_metadata:
|
|
38
|
+
if metadata is not None and metadata.commit_sha == commit_sha:
|
|
39
|
+
return metadata
|
|
40
|
+
return run_git
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def same_git_metadata(left: GitMetadata, right: GitMetadata) -> bool:
|
|
44
|
+
return left.as_dict() == right.as_dict()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def single_git_metadata(values: list[GitMetadata]) -> GitMetadata | None:
|
|
48
|
+
unique: dict[
|
|
49
|
+
tuple[str | None, str | None, str, str | None, str | None, str | None],
|
|
50
|
+
GitMetadata,
|
|
51
|
+
] = {}
|
|
52
|
+
for metadata in values:
|
|
53
|
+
key = (
|
|
54
|
+
metadata.repository,
|
|
55
|
+
metadata.commit_sha,
|
|
56
|
+
metadata.source,
|
|
57
|
+
metadata.branch,
|
|
58
|
+
metadata.path,
|
|
59
|
+
metadata.error,
|
|
60
|
+
)
|
|
61
|
+
unique[key] = metadata
|
|
62
|
+
if len(unique) == 1:
|
|
63
|
+
return next(iter(unique.values()))
|
|
64
|
+
return None
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def dict_to_git_metadata(value: Any) -> GitMetadata | None:
|
|
68
|
+
if not isinstance(value, dict):
|
|
69
|
+
return None
|
|
70
|
+
return GitMetadata(
|
|
71
|
+
repository=value.get("repository"),
|
|
72
|
+
commit_sha=value.get("commit_sha"),
|
|
73
|
+
source=value.get("source") or "unknown",
|
|
74
|
+
branch=value.get("branch"),
|
|
75
|
+
path=value.get("path"),
|
|
76
|
+
error=value.get("error"),
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def unique_git_metadata(values: list[GitMetadata | None]) -> list[dict[str, Any]]:
|
|
81
|
+
unique: dict[tuple[str | None, str | None, str], dict[str, Any]] = {}
|
|
82
|
+
for metadata in values:
|
|
83
|
+
if metadata is None:
|
|
84
|
+
continue
|
|
85
|
+
key = (metadata.repository, metadata.commit_sha, metadata.source)
|
|
86
|
+
unique[key] = metadata.as_dict()
|
|
87
|
+
return list(unique.values())
|
ovperf/_retrieval.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
"""Initial URL policy for automatic Git and OV library retrieval.
|
|
5
|
+
|
|
6
|
+
This validates inputs, not redirects or links discovered by an installer.
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import re
|
|
11
|
+
from urllib.parse import parse_qsl, urlsplit, urlunsplit
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# A URL inside other text, such as a PEP 508 direct reference "name @ <url>":
|
|
15
|
+
# from its scheme to the next whitespace or double quote. An apostrophe can be
|
|
16
|
+
# part of a URL (in a password, a path or a query), so it does not end one.
|
|
17
|
+
_URL_IN_TEXT = re.compile(r"[A-Za-z][A-Za-z0-9+.-]*://[^\s\"]+")
|
|
18
|
+
# Characters that close a quote or a sentence after a URL rather than belong to it.
|
|
19
|
+
_URL_TRAILERS = "'.,;:!?)"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def validate_retrieval_url(value: object, *, allow_loopback: bool = True) -> str:
|
|
23
|
+
"""Require HTTPS, with credential-free literal loopback HTTP for fixtures.
|
|
24
|
+
|
|
25
|
+
Do not echo the input in errors: URLs may contain credentials or tokens.
|
|
26
|
+
Hostnames (including localhost), alternate IP spellings, and scoped IPv6
|
|
27
|
+
addresses are deliberately outside the loopback exception.
|
|
28
|
+
"""
|
|
29
|
+
policy = "Retrieval requires an HTTPS URL"
|
|
30
|
+
if allow_loopback:
|
|
31
|
+
policy += " or credential-free HTTP on 127.0.0.1 or [::1]"
|
|
32
|
+
if not isinstance(value, str) or any(
|
|
33
|
+
ord(char) <= 32 or ord(char) == 127 or char == "\\" for char in value
|
|
34
|
+
):
|
|
35
|
+
raise ValueError(policy)
|
|
36
|
+
try:
|
|
37
|
+
parts = urlsplit(value)
|
|
38
|
+
port = parts.port
|
|
39
|
+
valid = bool(parts.hostname) and (port is None or port > 0)
|
|
40
|
+
secure = parts.scheme == "https"
|
|
41
|
+
local = (
|
|
42
|
+
allow_loopback
|
|
43
|
+
and parts.scheme == "http"
|
|
44
|
+
and parts.hostname in {"127.0.0.1", "::1"}
|
|
45
|
+
and parts.username is None
|
|
46
|
+
and parts.password is None
|
|
47
|
+
)
|
|
48
|
+
except ValueError:
|
|
49
|
+
raise ValueError(policy) from None
|
|
50
|
+
if not valid or not (secure or local):
|
|
51
|
+
raise ValueError(policy)
|
|
52
|
+
return value
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def redact_urls(text: str) -> str:
|
|
56
|
+
"""``text`` with every URL in it passed through ``redact_url``.
|
|
57
|
+
|
|
58
|
+
A trailing apostrophe or sentence punctuation stays outside the URL and is
|
|
59
|
+
put back, so a quoted URL keeps its closing quote. Redacting twice gives
|
|
60
|
+
the same text.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
def redact(match: re.Match) -> str:
|
|
64
|
+
url = match.group(0)
|
|
65
|
+
core = url.rstrip(_URL_TRAILERS)
|
|
66
|
+
return redact_url(core) + url[len(core):]
|
|
67
|
+
|
|
68
|
+
return _URL_IN_TEXT.sub(redact, text)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def redact_url(value: str) -> str:
|
|
72
|
+
"""``value`` without URL user info or query values, which can carry credentials.
|
|
73
|
+
|
|
74
|
+
Text that is not an absolute URL is returned unchanged.
|
|
75
|
+
"""
|
|
76
|
+
try:
|
|
77
|
+
parts = urlsplit(value)
|
|
78
|
+
except ValueError:
|
|
79
|
+
return "<invalid URL>"
|
|
80
|
+
if not parts.scheme or not parts.netloc:
|
|
81
|
+
return value
|
|
82
|
+
host = parts.netloc.rpartition("@")[2]
|
|
83
|
+
query = "&".join(f"{name}=<redacted>" for name, _ in parse_qsl(parts.query, keep_blank_values=True))
|
|
84
|
+
return urlunsplit((parts.scheme, host, parts.path, query, parts.fragment))
|
ovperf/_text.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
_ON = frozenset({"1", "true", "yes", "on"})
|
|
8
|
+
_OFF = frozenset({"0", "false", "no", "off"})
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def env_flag(value: object, default: bool = False) -> bool:
|
|
12
|
+
"""An on/off setting: 1/true/yes/on or 0/false/no/off, in any case.
|
|
13
|
+
|
|
14
|
+
Unset, empty and unrecognized values give ``default``. Every boolean
|
|
15
|
+
setting the runner reads uses this, so they all accept the same words.
|
|
16
|
+
"""
|
|
17
|
+
text = "" if value is None else str(value).strip().lower()
|
|
18
|
+
if text in _ON:
|
|
19
|
+
return True
|
|
20
|
+
if text in _OFF:
|
|
21
|
+
return False
|
|
22
|
+
return default
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def clean_optional_text(value: object) -> str | None:
|
|
26
|
+
if value is None:
|
|
27
|
+
return None
|
|
28
|
+
text = str(value).strip()
|
|
29
|
+
return text or None
|
ovperf/assets.py
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import os
|
|
7
|
+
import re
|
|
8
|
+
import stat
|
|
9
|
+
import uuid
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Mapping, Protocol
|
|
13
|
+
from urllib.parse import quote, urlsplit
|
|
14
|
+
|
|
15
|
+
from .s3 import (
|
|
16
|
+
S3Client,
|
|
17
|
+
S3Error,
|
|
18
|
+
S3Location,
|
|
19
|
+
configured_s3_endpoint,
|
|
20
|
+
env_with_dotenv,
|
|
21
|
+
is_s3_uri,
|
|
22
|
+
s3_config_from_env,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
ASSET_CACHE_DIR_ENV = "OVPERF_ASSET_CACHE_DIR"
|
|
27
|
+
# A dotenv file to read S3 settings from for asset downloads. No file is read
|
|
28
|
+
# unless it is named here or passed as ``dotenv_path``; environment values win.
|
|
29
|
+
S3_DOTENV_FILE_ENV = "OVPERF_S3_DOTENV_FILE"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass(frozen=True)
|
|
33
|
+
class PerfAssetDownload:
|
|
34
|
+
source_uri: str
|
|
35
|
+
prompt_placeholder: str | None = None
|
|
36
|
+
filename: str | None = None
|
|
37
|
+
cache_dir: Path | None = None
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(frozen=True)
|
|
41
|
+
class DownloadedPerfAsset:
|
|
42
|
+
source_uri: str
|
|
43
|
+
path: Path
|
|
44
|
+
size_bytes: int
|
|
45
|
+
cache_hit: bool
|
|
46
|
+
|
|
47
|
+
def as_dict(self) -> dict[str, object]:
|
|
48
|
+
return {
|
|
49
|
+
"source_uri": self.source_uri,
|
|
50
|
+
"path": self.path.as_posix(),
|
|
51
|
+
"size_bytes": self.size_bytes,
|
|
52
|
+
"cache_hit": self.cache_hit,
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class _S3DownloadClient(Protocol):
|
|
57
|
+
def download_file(
|
|
58
|
+
self,
|
|
59
|
+
key: str,
|
|
60
|
+
path: Path,
|
|
61
|
+
location: S3Location | None = None,
|
|
62
|
+
) -> None:
|
|
63
|
+
...
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def download_perf_asset(
|
|
67
|
+
download: PerfAssetDownload,
|
|
68
|
+
*,
|
|
69
|
+
env: Mapping[str, str] | None = None,
|
|
70
|
+
client: _S3DownloadClient | None = None,
|
|
71
|
+
dotenv_path: Path | None = None,
|
|
72
|
+
) -> DownloadedPerfAsset:
|
|
73
|
+
"""Download an ``s3://`` asset into the cache, or reuse the cached copy.
|
|
74
|
+
|
|
75
|
+
S3 settings and credentials come from ``env`` (default: the process
|
|
76
|
+
environment), merged over a dotenv file only when one is named with
|
|
77
|
+
``dotenv_path`` or ``OVPERF_S3_DOTENV_FILE``. A ``.env`` in the working
|
|
78
|
+
folder is never read implicitly.
|
|
79
|
+
"""
|
|
80
|
+
source_uri = download.source_uri.strip()
|
|
81
|
+
if not source_uri:
|
|
82
|
+
raise ValueError("asset download source URI must not be empty")
|
|
83
|
+
if not is_s3_uri(source_uri):
|
|
84
|
+
raise ValueError(f"unsupported asset download URI: {source_uri}")
|
|
85
|
+
|
|
86
|
+
source_env = os.environ if env is None else env
|
|
87
|
+
named = dotenv_path or (str(source_env.get(S3_DOTENV_FILE_ENV) or "").strip() or None)
|
|
88
|
+
env = env_with_dotenv(source_env, dotenv_path=Path(named) if named else None, implicit_dotenv=False)
|
|
89
|
+
cache_dir, destination = _asset_cache_entry(download, env=env)
|
|
90
|
+
_require_inside_cache(destination, cache_dir, source_uri)
|
|
91
|
+
# A link is never a cached asset: it can point anywhere.
|
|
92
|
+
if not destination.is_symlink() and destination.is_file():
|
|
93
|
+
return DownloadedPerfAsset(
|
|
94
|
+
source_uri=source_uri,
|
|
95
|
+
path=destination,
|
|
96
|
+
size_bytes=destination.stat().st_size,
|
|
97
|
+
cache_hit=True,
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
config = s3_config_from_env(uri=source_uri, env=env, implicit_dotenv=False)
|
|
101
|
+
if config is None:
|
|
102
|
+
raise S3Error(f"could not configure S3 asset source: {source_uri}")
|
|
103
|
+
if not config.location.prefix:
|
|
104
|
+
raise S3Error(f"S3 asset URI must include an object key: {source_uri}")
|
|
105
|
+
|
|
106
|
+
destination.parent.mkdir(parents=True, exist_ok=True)
|
|
107
|
+
_require_inside_cache(destination, cache_dir, source_uri)
|
|
108
|
+
temporary_path = destination.with_name(
|
|
109
|
+
f".{destination.name}.{os.getpid()}.{uuid.uuid4().hex}.tmp"
|
|
110
|
+
)
|
|
111
|
+
try:
|
|
112
|
+
s3_client = client or S3Client(config)
|
|
113
|
+
s3_client.download_file(
|
|
114
|
+
config.location.prefix,
|
|
115
|
+
temporary_path,
|
|
116
|
+
location=config.location,
|
|
117
|
+
)
|
|
118
|
+
temporary_path.replace(destination)
|
|
119
|
+
finally:
|
|
120
|
+
temporary_path.unlink(missing_ok=True)
|
|
121
|
+
|
|
122
|
+
return DownloadedPerfAsset(
|
|
123
|
+
source_uri=source_uri,
|
|
124
|
+
path=destination,
|
|
125
|
+
size_bytes=destination.stat().st_size,
|
|
126
|
+
cache_hit=False,
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _asset_destination(
|
|
131
|
+
download: PerfAssetDownload,
|
|
132
|
+
*,
|
|
133
|
+
env: Mapping[str, str] | None,
|
|
134
|
+
) -> Path:
|
|
135
|
+
return _asset_cache_entry(download, env=env)[1]
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def _asset_cache_entry(
|
|
139
|
+
download: PerfAssetDownload,
|
|
140
|
+
*,
|
|
141
|
+
env: Mapping[str, str] | None,
|
|
142
|
+
) -> tuple[Path, Path]:
|
|
143
|
+
"""(cache folder, cache path) of the asset ``download`` names."""
|
|
144
|
+
cache_dir = (download.cache_dir or _asset_cache_dir(env)).expanduser()
|
|
145
|
+
location = S3Location.parse(download.source_uri.strip())
|
|
146
|
+
parts = [part for part in location.prefix.split("/") if part]
|
|
147
|
+
if not parts:
|
|
148
|
+
raise S3Error(
|
|
149
|
+
f"S3 asset URI must include an object filename: {download.source_uri}"
|
|
150
|
+
)
|
|
151
|
+
# Each key component becomes one encoded path component, so a key can
|
|
152
|
+
# never name a drive, a parent folder or a backslash path on any platform.
|
|
153
|
+
root = cache_dir / "s3"
|
|
154
|
+
_setting, endpoint = configured_s3_endpoint(env or {})
|
|
155
|
+
if endpoint:
|
|
156
|
+
# Another endpoint can hold different bytes under the same bucket/key.
|
|
157
|
+
# The folder is named after host, port and path only: never user info.
|
|
158
|
+
endpoint_parts = urlsplit(endpoint)
|
|
159
|
+
host = endpoint_parts.netloc.rpartition("@")[2]
|
|
160
|
+
root = cache_dir / "s3-endpoints" / _cache_component(host + endpoint_parts.path)
|
|
161
|
+
parent = root / _cache_component(location.bucket)
|
|
162
|
+
for part in parts[:-1]:
|
|
163
|
+
parent = parent / _cache_component(part)
|
|
164
|
+
name = _cache_component(parts[-1])
|
|
165
|
+
if download.filename:
|
|
166
|
+
requested = _safe_filename(download.filename)
|
|
167
|
+
if requested != name:
|
|
168
|
+
# A display-named copy gets a folder of its own beside the key's
|
|
169
|
+
# entry: two keys requested under one display name never share a
|
|
170
|
+
# cache entry, and one object can be cached under both names. An
|
|
171
|
+
# encoded key component never contains "+", so this folder never
|
|
172
|
+
# equals a key's own path.
|
|
173
|
+
parent, name = parent / f"{name}+named", requested
|
|
174
|
+
destination = parent / name
|
|
175
|
+
if not os.path.abspath(destination).startswith(os.path.abspath(cache_dir) + os.sep):
|
|
176
|
+
raise S3Error(f"S3 asset key does not map into the asset cache: {download.source_uri}")
|
|
177
|
+
return cache_dir, destination
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _require_inside_cache(destination: Path, cache_dir: Path, source_uri: str) -> None:
|
|
181
|
+
"""Refuse an entry whose real folder lies outside the real cache folder.
|
|
182
|
+
|
|
183
|
+
A link anywhere between the cache folder and the entry would make a cache
|
|
184
|
+
hit return, or a download write, a file outside the cache. The cache
|
|
185
|
+
folder itself may be a link (for example to another disk).
|
|
186
|
+
"""
|
|
187
|
+
real_cache = os.path.realpath(cache_dir)
|
|
188
|
+
real_folder = os.path.realpath(destination.parent)
|
|
189
|
+
try:
|
|
190
|
+
inside = os.path.commonpath([real_cache, real_folder]) == real_cache
|
|
191
|
+
except ValueError: # another drive
|
|
192
|
+
inside = False
|
|
193
|
+
if not inside:
|
|
194
|
+
raise S3Error(f"the asset cache entry for {source_uri} leads out of the asset cache through a link")
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def _cache_component(value: str) -> str:
|
|
198
|
+
component = quote(value, safe="-_.~")
|
|
199
|
+
if component in {"", ".", ".."}:
|
|
200
|
+
raise S3Error("S3 asset keys must not contain empty, '.' or '..' components")
|
|
201
|
+
return component
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _asset_cache_dir(env: Mapping[str, str] | None) -> Path:
|
|
205
|
+
source_env = os.environ if env is None else env
|
|
206
|
+
configured = str(source_env.get(ASSET_CACHE_DIR_ENV, "")).strip()
|
|
207
|
+
if configured:
|
|
208
|
+
return Path(configured).expanduser()
|
|
209
|
+
return default_asset_cache_dir(source_env)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def ensure_asset_cache_dir(env: Mapping[str, str] | None = None) -> Path:
|
|
213
|
+
"""The asset cache ``env`` selects, created by the current user; resolved.
|
|
214
|
+
|
|
215
|
+
``OVPERF_ASSET_CACHE_DIR`` when set, otherwise the private default. A
|
|
216
|
+
privileged capture resolves it before its worker starts as root and passes
|
|
217
|
+
it on as ``OVPERF_ASSET_CACHE_DIR``, so the root worker uses your cache
|
|
218
|
+
instead of rejecting it (it is not root's) or creating a root-owned one.
|
|
219
|
+
The real folder is returned, so a cache folder that is a link names the
|
|
220
|
+
folder the worker writes into, which the ownership hand-back then walks.
|
|
221
|
+
"""
|
|
222
|
+
cache = _asset_cache_dir(env)
|
|
223
|
+
cache.mkdir(parents=True, exist_ok=True)
|
|
224
|
+
return Path(os.path.realpath(cache))
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def default_asset_cache_dir(env: Mapping[str, str] | None = None) -> Path:
|
|
228
|
+
"""The per-user asset cache, created owner-only.
|
|
229
|
+
|
|
230
|
+
``%LOCALAPPDATA%\\ovperf\\assets`` on Windows, otherwise
|
|
231
|
+
``$XDG_CACHE_HOME/ovperf/assets`` (``~/.cache/ovperf/assets``). Never a
|
|
232
|
+
shared temporary folder: another local user could plant files there that
|
|
233
|
+
a later run would take for downloaded assets.
|
|
234
|
+
|
|
235
|
+
The ``assets`` folder must be yours and writable only by you. Its parent
|
|
236
|
+
``ovperf`` folder is shared with other ovperf tools, which may create it
|
|
237
|
+
with a group-writable umask (user private groups), so it only has to be
|
|
238
|
+
yours and not writable by everyone; links are refused for both.
|
|
239
|
+
"""
|
|
240
|
+
source_env = os.environ if env is None else env
|
|
241
|
+
if os.name == "nt":
|
|
242
|
+
base = str(source_env.get("LOCALAPPDATA", "")).strip() or str(Path.home() / "AppData" / "Local")
|
|
243
|
+
else:
|
|
244
|
+
base = str(source_env.get("XDG_CACHE_HOME", "")).strip() or str(Path.home() / ".cache")
|
|
245
|
+
root = Path(base).expanduser() / "ovperf"
|
|
246
|
+
cache = root / "assets"
|
|
247
|
+
for folder, others_may_not_write in ((root, 0o002), (cache, 0o022)):
|
|
248
|
+
try:
|
|
249
|
+
folder.mkdir(mode=0o700, parents=True)
|
|
250
|
+
except FileExistsError:
|
|
251
|
+
pass
|
|
252
|
+
_require_own_folder(folder, others_may_not_write)
|
|
253
|
+
return cache
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def _require_own_folder(folder: Path, others_may_not_write: int) -> None:
|
|
257
|
+
"""Refuse a link, a folder someone else owns, or one others can write.
|
|
258
|
+
|
|
259
|
+
``others_may_not_write`` holds the write bits that must be clear: 0o022
|
|
260
|
+
for a private folder, 0o002 for a shared parent.
|
|
261
|
+
"""
|
|
262
|
+
if os.name == "nt":
|
|
263
|
+
return
|
|
264
|
+
info = folder.lstat()
|
|
265
|
+
if not stat.S_ISDIR(info.st_mode) or info.st_uid != os.getuid() or info.st_mode & others_may_not_write:
|
|
266
|
+
rule = "others cannot write" if others_may_not_write & 0o020 else "not every user can write"
|
|
267
|
+
raise S3Error(
|
|
268
|
+
f"asset cache folder {folder} must be a folder you own that {rule}; "
|
|
269
|
+
f"fix its owner or mode, or set {ASSET_CACHE_DIR_ENV}"
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def _safe_path_part(value: str) -> str:
|
|
274
|
+
return re.sub(r"[^A-Za-z0-9_.-]+", "_", value).strip("_") or "asset"
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def _safe_filename(value: str) -> str:
|
|
278
|
+
filename = Path(value).name
|
|
279
|
+
return _safe_path_part(filename)
|