seanerstack 0.1.3__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.
- seanerstack/__init__.py +5 -0
- seanerstack/auth.py +143 -0
- seanerstack/bootstrap.py +161 -0
- seanerstack/buildcache.py +68 -0
- seanerstack/cli.py +397 -0
- seanerstack/config.py +93 -0
- seanerstack/docsgen.py +146 -0
- seanerstack/isolation.py +243 -0
- seanerstack/paths.py +52 -0
- seanerstack/recipe.py +108 -0
- seanerstack/secrets.py +60 -0
- seanerstack/sync.py +381 -0
- seanerstack/validate.py +202 -0
- seanerstack-0.1.3.data/data/share/seanerstack/config.toml +37 -0
- seanerstack-0.1.3.data/data/share/seanerstack/environments/demobus-dev/spack.yaml +10 -0
- seanerstack-0.1.3.data/data/share/seanerstack/environments/flux-core-dev/spack.yaml +10 -0
- seanerstack-0.1.3.data/data/share/seanerstack/environments/properdocs-dev/spack.yaml +10 -0
- seanerstack-0.1.3.data/data/share/seanerstack/spack-repo-index.yaml +4 -0
- seanerstack-0.1.3.data/data/share/seanerstack/spack_repo/seanerstack/packages/demobus/package.py +38 -0
- seanerstack-0.1.3.data/data/share/seanerstack/spack_repo/seanerstack/packages/properdocs/package.py +34 -0
- seanerstack-0.1.3.data/data/share/seanerstack/spack_repo/seanerstack/packages/seanergys_meta/package.py +13 -0
- seanerstack-0.1.3.data/data/share/seanerstack/spack_repo/seanerstack/repo.yaml +4 -0
- seanerstack-0.1.3.dist-info/METADATA +224 -0
- seanerstack-0.1.3.dist-info/RECORD +27 -0
- seanerstack-0.1.3.dist-info/WHEEL +4 -0
- seanerstack-0.1.3.dist-info/entry_points.txt +3 -0
- seanerstack-0.1.3.dist-info/licenses/LICENSE +202 -0
seanerstack/isolation.py
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
import os
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
import shlex
|
|
8
|
+
import shutil
|
|
9
|
+
import stat
|
|
10
|
+
from typing import Mapping
|
|
11
|
+
|
|
12
|
+
from .config import Config
|
|
13
|
+
from .paths import repository_path
|
|
14
|
+
from .auth import spack_resource_environment
|
|
15
|
+
from .secrets import SecretStore
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# Spack declares this in its own defaults; the private scope has to repeat it
|
|
19
|
+
# because it replaces those defaults rather than merging with them.
|
|
20
|
+
BUILTIN_REPO_GIT = "https://github.com/spack/spack-packages.git"
|
|
21
|
+
|
|
22
|
+
# Binary build-cache hashes depend on the recipes, so every session has to load
|
|
23
|
+
# the same builtin commit: spack-packages v2026.10.0, package API v2.2, which
|
|
24
|
+
# Spack 1.2 supports. Bump it together with the Spack version used in CI.
|
|
25
|
+
BUILTIN_REPO_COMMIT = "91cfb5b973c311f742cd31061fe224d40f1fd830"
|
|
26
|
+
|
|
27
|
+
# Spack tests for the presence of this variable, not its value, so an inherited
|
|
28
|
+
# one has to be removed rather than overridden. It switches the user scope off,
|
|
29
|
+
# and that scope is where the private configuration lives.
|
|
30
|
+
UNSET_ON_ACTIVATION = ("SPACK_DISABLE_LOCAL_CONFIG",)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass(frozen=True)
|
|
34
|
+
class SpackLayout:
|
|
35
|
+
state: Path
|
|
36
|
+
config: Path
|
|
37
|
+
store: Path
|
|
38
|
+
stage: Path
|
|
39
|
+
cache: Path
|
|
40
|
+
source_cache: Path
|
|
41
|
+
environments: Path
|
|
42
|
+
package_repos: Path
|
|
43
|
+
bin: Path
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def layout(state_dir: Path) -> SpackLayout:
|
|
47
|
+
state = state_dir.expanduser().resolve()
|
|
48
|
+
root = state / "spack"
|
|
49
|
+
return SpackLayout(
|
|
50
|
+
state=state,
|
|
51
|
+
config=root / "config",
|
|
52
|
+
store=root / "store",
|
|
53
|
+
stage=root / "stage",
|
|
54
|
+
cache=root / "cache",
|
|
55
|
+
source_cache=root / "source-cache",
|
|
56
|
+
environments=root / "environments",
|
|
57
|
+
package_repos=root / "package_repos",
|
|
58
|
+
bin=root / "bin",
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def resolve_state_dir(value: Path | None) -> Path:
|
|
63
|
+
if value is not None:
|
|
64
|
+
return value.expanduser().resolve()
|
|
65
|
+
raw = os.environ.get("SEANERSTACK_STATE_DIR")
|
|
66
|
+
if raw:
|
|
67
|
+
return Path(raw).expanduser().resolve()
|
|
68
|
+
raise ValueError("Spack isolation requires --state-dir DIR or SEANERSTACK_STATE_DIR")
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def resolve_real_spack(explicit: Path | None = None) -> Path:
|
|
72
|
+
if explicit is not None:
|
|
73
|
+
path = explicit.expanduser().resolve()
|
|
74
|
+
elif os.environ.get("SEANERSTACK_REAL_SPACK"):
|
|
75
|
+
path = Path(os.environ["SEANERSTACK_REAL_SPACK"]).expanduser().resolve()
|
|
76
|
+
else:
|
|
77
|
+
found = shutil.which("spack")
|
|
78
|
+
if not found:
|
|
79
|
+
raise RuntimeError("spack executable not found in PATH; provide --spack-executable FILE")
|
|
80
|
+
path = Path(found).resolve()
|
|
81
|
+
if not path.is_file():
|
|
82
|
+
raise ValueError(f"Spack executable does not exist: {path}")
|
|
83
|
+
return path
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _write(path: Path, text: str) -> None:
|
|
87
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
88
|
+
path.write_text(text, encoding="utf-8")
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _write_if_missing(path: Path, text: str) -> None:
|
|
92
|
+
if not path.exists():
|
|
93
|
+
_write(path, text)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def prepare_isolation(state_dir: Path, real_spack: Path) -> SpackLayout:
|
|
97
|
+
x = layout(state_dir)
|
|
98
|
+
for directory in (x.config, x.store, x.stage, x.cache, x.source_cache, x.environments, x.package_repos, x.bin):
|
|
99
|
+
directory.mkdir(parents=True, exist_ok=True)
|
|
100
|
+
|
|
101
|
+
# Double-colon keys intentionally replace lower-precedence configuration,
|
|
102
|
+
# preventing site/instance/user configuration from merging into Seanerstack.
|
|
103
|
+
_write_if_missing(
|
|
104
|
+
x.config / "config.yaml",
|
|
105
|
+
"config::\n"
|
|
106
|
+
f" install_tree:\n root: {x.store}\n"
|
|
107
|
+
f" build_stage:\n - {x.stage}\n"
|
|
108
|
+
f" source_cache: {x.source_cache}\n"
|
|
109
|
+
f" misc_cache: {x.cache / 'misc'}\n"
|
|
110
|
+
f" test_stage: {x.cache / 'test'}\n"
|
|
111
|
+
f" environments_root: {x.environments}\n",
|
|
112
|
+
)
|
|
113
|
+
_write_if_missing(x.config / "packages.yaml", "packages:: {}\n")
|
|
114
|
+
_write_if_missing(x.config / "mirrors.yaml", "mirrors:: {}\n")
|
|
115
|
+
_write_if_missing(x.config / "upstreams.yaml", "upstreams:: {}\n")
|
|
116
|
+
# Merged, not replaced. Spack's concretizer defaults carry structured keys
|
|
117
|
+
# it reads unconditionally, such as `externals:completion`; replacing the
|
|
118
|
+
# section wholesale leaves them unset and concretization fails with
|
|
119
|
+
# "Unknown value for concretizer:externals:completion: None". Unlike
|
|
120
|
+
# packages, mirrors, and repos, this section is not a boundary against
|
|
121
|
+
# outside configuration, so merging costs nothing.
|
|
122
|
+
_write_if_missing(x.config / "concretizer.yaml", "concretizer:\n reuse: false\n")
|
|
123
|
+
# The private scope replaces lower-precedence configuration, so it has to
|
|
124
|
+
# carry `builtin` itself. Spack's defaults declare it as a git remote, and
|
|
125
|
+
# without it no package loads at all: Seanerstack recipes, and every recipe
|
|
126
|
+
# they depend on, import from spack_repo.builtin. Seanerstack comes first so
|
|
127
|
+
# its own recipes take precedence.
|
|
128
|
+
#
|
|
129
|
+
# Rewritten every time, so an existing state follows a new pin or a moved
|
|
130
|
+
# installation. The clone directory names the commit: a new pin gets a fresh
|
|
131
|
+
# clone at that commit, where reusing the old one would need `spack repo update`.
|
|
132
|
+
_write(
|
|
133
|
+
x.config / "repos.yaml",
|
|
134
|
+
"repos::\n"
|
|
135
|
+
f" seanerstack: {repository_path()}\n"
|
|
136
|
+
" builtin:\n"
|
|
137
|
+
f" git: {BUILTIN_REPO_GIT}\n"
|
|
138
|
+
f" commit: {BUILTIN_REPO_COMMIT}\n"
|
|
139
|
+
f" destination: {x.package_repos / f'builtin-{BUILTIN_REPO_COMMIT[:12]}'}\n",
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
shim = x.bin / "spack"
|
|
143
|
+
_write(
|
|
144
|
+
shim,
|
|
145
|
+
"#!/bin/sh\n"
|
|
146
|
+
'if [ -z "${SEANERSTACK_REAL_SPACK:-}" ] || [ -z "${SEANERSTACK_SPACK_CONFIG_DIR:-}" ]; then\n'
|
|
147
|
+
' echo "seanerstack: isolated spack launcher requires an activated Seanerstack environment" >&2\n'
|
|
148
|
+
" exit 2\n"
|
|
149
|
+
"fi\n"
|
|
150
|
+
'exec "$SEANERSTACK_REAL_SPACK" -C "$SEANERSTACK_SPACK_CONFIG_DIR" "$@"\n',
|
|
151
|
+
)
|
|
152
|
+
shim.chmod(shim.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
|
|
153
|
+
return x
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def spack_env(x: SpackLayout) -> dict[str, str]:
|
|
157
|
+
# SPACK_DISABLE_LOCAL_CONFIG is deliberately not set. It switches the user
|
|
158
|
+
# scope off entirely, which also disables SPACK_USER_CONFIG_PATH, leaving
|
|
159
|
+
# no writable scope at all: `spack config add` and `spack compiler find`
|
|
160
|
+
# then fall through to the site scope inside the Spack checkout, where the
|
|
161
|
+
# `::` keys below override them and they have no effect. Pointing the user
|
|
162
|
+
# scope at the private directory achieves the isolation on its own, and
|
|
163
|
+
# SPACK_SYSTEM_CONFIG_PATH already takes care of the system scope.
|
|
164
|
+
return {
|
|
165
|
+
"SPACK_USER_CONFIG_PATH": str(x.config),
|
|
166
|
+
"SPACK_SYSTEM_CONFIG_PATH": str(x.state / "spack" / "system-config-disabled"),
|
|
167
|
+
"SPACK_USER_CACHE_PATH": str(x.cache),
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def spack_command(x: SpackLayout, real_spack: Path, *args: str) -> list[str]:
|
|
172
|
+
return [str(real_spack), "-C", str(x.config), *args]
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _export(name: str, value: str) -> str:
|
|
176
|
+
return f"export {name}={shlex.quote(value)}"
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def activation_script(
|
|
180
|
+
*,
|
|
181
|
+
state_dir: Path,
|
|
182
|
+
real_spack: Path,
|
|
183
|
+
config: Config,
|
|
184
|
+
secrets: SecretStore | None = None,
|
|
185
|
+
environ: Mapping[str, str] | None = None,
|
|
186
|
+
) -> str:
|
|
187
|
+
env = dict(os.environ if environ is None else environ)
|
|
188
|
+
if env.get("SEANERSTACK_ACTIVE") == "1":
|
|
189
|
+
raise ValueError("Seanerstack is already active; deactivate it before activating another state directory")
|
|
190
|
+
x = prepare_isolation(state_dir, real_spack)
|
|
191
|
+
|
|
192
|
+
changes = spack_env(x)
|
|
193
|
+
changes.update({
|
|
194
|
+
"SEANERSTACK_ACTIVE": "1",
|
|
195
|
+
"SEANERSTACK_STATE_DIR": str(x.state),
|
|
196
|
+
"SEANERSTACK_REAL_SPACK": str(real_spack),
|
|
197
|
+
"SEANERSTACK_SPACK_CONFIG_DIR": str(x.config),
|
|
198
|
+
"PATH": f"{x.bin}{os.pathsep}{env.get('PATH', '')}",
|
|
199
|
+
})
|
|
200
|
+
if secrets is not None and secrets.path is not None:
|
|
201
|
+
for resource in config.resources.values():
|
|
202
|
+
# A vault need not hold credentials for every shipped build cache.
|
|
203
|
+
if (
|
|
204
|
+
resource.kind == "spack-buildcache"
|
|
205
|
+
and resource.credentials
|
|
206
|
+
and secrets.has_credentials(resource.credentials)
|
|
207
|
+
):
|
|
208
|
+
changes.update(spack_resource_environment(resource, secrets))
|
|
209
|
+
|
|
210
|
+
lines = []
|
|
211
|
+
for name in (*changes, *UNSET_ON_ACTIVATION):
|
|
212
|
+
marker = f"SEANERSTACK_SAVED_{name}_SET"
|
|
213
|
+
saved = f"SEANERSTACK_SAVED_{name}"
|
|
214
|
+
if name in env:
|
|
215
|
+
lines.extend((_export(marker, "1"), _export(saved, env[name])))
|
|
216
|
+
else:
|
|
217
|
+
lines.extend((_export(marker, "0"), f"unset {saved}"))
|
|
218
|
+
lines.extend(_export(name, value) for name, value in changes.items())
|
|
219
|
+
lines.extend(f"unset {name}" for name in UNSET_ON_ACTIVATION)
|
|
220
|
+
return "\n".join(lines) + "\n"
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def deactivation_script(environ: Mapping[str, str] | None = None) -> str:
|
|
224
|
+
env = dict(os.environ if environ is None else environ)
|
|
225
|
+
if env.get("SEANERSTACK_ACTIVE") != "1":
|
|
226
|
+
raise ValueError("Seanerstack is not active")
|
|
227
|
+
|
|
228
|
+
managed = [
|
|
229
|
+
"SPACK_DISABLE_LOCAL_CONFIG", "SPACK_USER_CONFIG_PATH", "SPACK_SYSTEM_CONFIG_PATH",
|
|
230
|
+
"SPACK_USER_CACHE_PATH", "SEANERSTACK_ACTIVE", "SEANERSTACK_STATE_DIR",
|
|
231
|
+
"SEANERSTACK_REAL_SPACK", "SEANERSTACK_SPACK_CONFIG_DIR", "PATH",
|
|
232
|
+
]
|
|
233
|
+
managed.extend(name for name in env if name.startswith("SEANERSTACK_RESOURCE_") and not name.startswith("SEANERSTACK_SAVED_"))
|
|
234
|
+
lines = []
|
|
235
|
+
for name in dict.fromkeys(managed):
|
|
236
|
+
marker = f"SEANERSTACK_SAVED_{name}_SET"
|
|
237
|
+
saved = f"SEANERSTACK_SAVED_{name}"
|
|
238
|
+
if env.get(marker) == "1":
|
|
239
|
+
lines.append(_export(name, env.get(saved, "")))
|
|
240
|
+
else:
|
|
241
|
+
lines.append(f"unset {name}")
|
|
242
|
+
lines.extend((f"unset {marker}", f"unset {saved}"))
|
|
243
|
+
return "\n".join(lines) + "\n"
|
seanerstack/paths.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
import sysconfig
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _source_share_path() -> Path:
|
|
9
|
+
return Path(__file__).resolve().parents[2] / "data" / "share" / "seanerstack"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _installed_share_path() -> Path:
|
|
13
|
+
data_root = sysconfig.get_path("data")
|
|
14
|
+
if not data_root:
|
|
15
|
+
raise RuntimeError("Python did not report an installation data directory")
|
|
16
|
+
return Path(data_root).resolve() / "share" / "seanerstack"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def share_path() -> Path:
|
|
20
|
+
"""Return the Seanerstack shared-data directory.
|
|
21
|
+
|
|
22
|
+
A source checkout is preferred when the Python module itself is imported from
|
|
23
|
+
that checkout. Installed wheels otherwise resolve through Python's ``data``
|
|
24
|
+
installation scheme, which maps to ``<prefix>/share/seanerstack``.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
source = _source_share_path()
|
|
28
|
+
if source.is_dir():
|
|
29
|
+
return source.resolve()
|
|
30
|
+
|
|
31
|
+
installed = _installed_share_path()
|
|
32
|
+
if installed.is_dir():
|
|
33
|
+
return installed
|
|
34
|
+
|
|
35
|
+
raise RuntimeError(
|
|
36
|
+
"could not locate Seanerstack shared data; expected "
|
|
37
|
+
f"{installed}"
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def repository_path() -> Path:
|
|
42
|
+
path = share_path() / "spack_repo" / "seanerstack"
|
|
43
|
+
if not path.is_dir():
|
|
44
|
+
raise RuntimeError(f"Seanerstack Spack repository is missing: {path}")
|
|
45
|
+
return path
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def environments_path() -> Path:
|
|
49
|
+
path = share_path() / "environments"
|
|
50
|
+
if not path.is_dir():
|
|
51
|
+
raise RuntimeError(f"Seanerstack environment directory is missing: {path}")
|
|
52
|
+
return path
|
seanerstack/recipe.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
import ast
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True)
|
|
11
|
+
class Directive:
|
|
12
|
+
name: str
|
|
13
|
+
args: tuple[Any, ...]
|
|
14
|
+
kwargs: dict[str, Any]
|
|
15
|
+
lineno: int
|
|
16
|
+
end_lineno: int
|
|
17
|
+
source: str
|
|
18
|
+
comment: str | None = None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass(frozen=True)
|
|
22
|
+
class Recipe:
|
|
23
|
+
path: Path
|
|
24
|
+
class_name: str
|
|
25
|
+
docstring: str | None
|
|
26
|
+
attributes: dict[str, Any]
|
|
27
|
+
directives: tuple[Directive, ...]
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def git(self) -> str | None:
|
|
31
|
+
value = self.attributes.get("git")
|
|
32
|
+
return value if isinstance(value, str) else None
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def url(self) -> str | None:
|
|
36
|
+
value = self.attributes.get("url")
|
|
37
|
+
return value if isinstance(value, str) else None
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def seanerstack_resource(self) -> str | None:
|
|
41
|
+
value = self.attributes.get("seanerstack_resource")
|
|
42
|
+
return value if isinstance(value, str) else None
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _literal(node: ast.AST) -> Any:
|
|
46
|
+
try:
|
|
47
|
+
return ast.literal_eval(node)
|
|
48
|
+
except (ValueError, TypeError):
|
|
49
|
+
return ast.unparse(node)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _preceding_comment(lines: list[str], lineno: int) -> str | None:
|
|
53
|
+
index = lineno - 2
|
|
54
|
+
comments: list[str] = []
|
|
55
|
+
while index >= 0:
|
|
56
|
+
stripped = lines[index].strip()
|
|
57
|
+
if stripped.startswith("#"):
|
|
58
|
+
comments.append(stripped[1:].strip())
|
|
59
|
+
index -= 1
|
|
60
|
+
continue
|
|
61
|
+
if not stripped and comments:
|
|
62
|
+
break
|
|
63
|
+
break
|
|
64
|
+
if not comments:
|
|
65
|
+
return None
|
|
66
|
+
return " ".join(reversed(comments))
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def parse_recipe(path: Path) -> Recipe:
|
|
70
|
+
source = path.read_text(encoding="utf-8")
|
|
71
|
+
lines = source.splitlines()
|
|
72
|
+
tree = ast.parse(source, filename=str(path))
|
|
73
|
+
|
|
74
|
+
classes = [node for node in tree.body if isinstance(node, ast.ClassDef)]
|
|
75
|
+
if not classes:
|
|
76
|
+
raise ValueError(f"{path}: no package class found")
|
|
77
|
+
cls = classes[0]
|
|
78
|
+
|
|
79
|
+
attributes: dict[str, Any] = {}
|
|
80
|
+
directives: list[Directive] = []
|
|
81
|
+
for node in cls.body:
|
|
82
|
+
if isinstance(node, ast.Assign) and len(node.targets) == 1 and isinstance(node.targets[0], ast.Name):
|
|
83
|
+
attributes[node.targets[0].id] = _literal(node.value)
|
|
84
|
+
continue
|
|
85
|
+
if not isinstance(node, ast.Expr) or not isinstance(node.value, ast.Call):
|
|
86
|
+
continue
|
|
87
|
+
call = node.value
|
|
88
|
+
if not isinstance(call.func, ast.Name):
|
|
89
|
+
continue
|
|
90
|
+
directives.append(
|
|
91
|
+
Directive(
|
|
92
|
+
name=call.func.id,
|
|
93
|
+
args=tuple(_literal(arg) for arg in call.args),
|
|
94
|
+
kwargs={kw.arg: _literal(kw.value) for kw in call.keywords if kw.arg},
|
|
95
|
+
lineno=node.lineno,
|
|
96
|
+
end_lineno=node.end_lineno or node.lineno,
|
|
97
|
+
source=ast.get_source_segment(source, node) or ast.unparse(node),
|
|
98
|
+
comment=_preceding_comment(lines, node.lineno),
|
|
99
|
+
)
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
return Recipe(
|
|
103
|
+
path=path,
|
|
104
|
+
class_name=cls.name,
|
|
105
|
+
docstring=ast.get_docstring(cls),
|
|
106
|
+
attributes=attributes,
|
|
107
|
+
directives=tuple(directives),
|
|
108
|
+
)
|
seanerstack/secrets.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from getpass import getpass
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
import sys
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass
|
|
12
|
+
class SecretStore:
|
|
13
|
+
path: Path | None
|
|
14
|
+
password_file: Path | None = None
|
|
15
|
+
password_stdin: bool = False
|
|
16
|
+
_data: dict[str, Any] | None = None
|
|
17
|
+
|
|
18
|
+
def _password(self) -> str:
|
|
19
|
+
if self.password_stdin:
|
|
20
|
+
value = sys.stdin.readline()
|
|
21
|
+
elif self.password_file is not None:
|
|
22
|
+
value = self.password_file.read_text(encoding="utf-8")
|
|
23
|
+
else:
|
|
24
|
+
value = getpass("Vault password: ")
|
|
25
|
+
if value.endswith("\r\n"):
|
|
26
|
+
value = value[:-2]
|
|
27
|
+
elif value.endswith("\n"):
|
|
28
|
+
value = value[:-1]
|
|
29
|
+
if not value:
|
|
30
|
+
raise ValueError("vault password must not be empty")
|
|
31
|
+
return value
|
|
32
|
+
|
|
33
|
+
def _load(self) -> dict[str, Any]:
|
|
34
|
+
if self.path is None:
|
|
35
|
+
raise ValueError("resource requires credentials; provide --secrets-file FILE")
|
|
36
|
+
if self._data is None:
|
|
37
|
+
try:
|
|
38
|
+
import keyloom
|
|
39
|
+
except ImportError as error:
|
|
40
|
+
raise RuntimeError("keyloom is required to read --secrets-file") from error
|
|
41
|
+
try:
|
|
42
|
+
data = keyloom.load(path=self.path, password=self._password())
|
|
43
|
+
except keyloom.KeyloomError as error:
|
|
44
|
+
# keyloom prefixes its messages with the file name.
|
|
45
|
+
raise ValueError(str(error)) from error
|
|
46
|
+
if not isinstance(data, dict):
|
|
47
|
+
raise ValueError(f"{self.path}: decrypted secrets root must be a mapping")
|
|
48
|
+
self._data = data
|
|
49
|
+
return self._data
|
|
50
|
+
|
|
51
|
+
def has_credentials(self, name: str) -> bool:
|
|
52
|
+
credentials = self._load().get("credentials")
|
|
53
|
+
return isinstance(credentials, dict) and isinstance(credentials.get(name), dict)
|
|
54
|
+
|
|
55
|
+
def credentials(self, name: str) -> dict[str, Any]:
|
|
56
|
+
data = self._load()
|
|
57
|
+
credentials = data.get("credentials", {}).get(name) if isinstance(data.get("credentials"), dict) else None
|
|
58
|
+
if not isinstance(credentials, dict):
|
|
59
|
+
raise ValueError(f"credentials {name!r} not found in {self.path}")
|
|
60
|
+
return dict(credentials)
|