lib.ec.secret_store.KvFile: Rename private symbols
KvFile contains symbol names "addr" or "entry" which look better defined than they are. I prefer sticking to well-defined names like "path". "path" in itself is ambiguous and makes it necessary to understand its context, but so be it: At least it's used uniformly throughout the codebase and makes it even more obvious that the function it's used in needs to bear a meaningful name. Which is what this commit does, along with renaming some member variables to more self-explanatory variants. In detail, __addr() composes a host path from the URI path and the store's relpath, __resolve() strips the store's suffixes, __entry_kv() and __is_entry() operate on a config path that has a .jw-secret file, __subtree holds the path component of the URI, and __raw holds the context under the FcCache. The local variables config, entry, real, and blob do not name the things they hold either. - The __addr() method becomes __host_path(), its parameter is named relpath. - The __resolve() method becomes __strip_suffix(). - The __entry_kv() and __is_entry() methods become __compound_kv() and __is_compound(), their parameters are named config_path. - The __subtree and __raw members become __root and __inner. - The locals config, entry, real, and blob become config_path, compound, host_path, and secret_path. Update the error messages and the comments that name the old symbols to use the new ones. Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0 Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
ca56f38daa
commit
1c27a40d48
1 changed files with 103 additions and 99 deletions
|
|
@ -31,30 +31,30 @@ class KvFile(Base):
|
|||
if uri is None:
|
||||
uri = f'kvfile://{ctx.uri.hostname or "target"}'
|
||||
super().__init__(uri = uri)
|
||||
self.__raw = ctx
|
||||
self.__inner = ctx
|
||||
self.__distro: Distro | None = distro
|
||||
self.___config_paths: list[str] | None = None
|
||||
self.__subtree = self.uri.path.lstrip('/')
|
||||
self.__ctx = FcCache(ctx)
|
||||
self.__root = self.uri.path.lstrip('/')
|
||||
self.__ctx = FcCache(self.__inner)
|
||||
|
||||
@override
|
||||
async def _open(self) -> None:
|
||||
await self.__raw.open()
|
||||
await self.__inner.open()
|
||||
|
||||
@override
|
||||
async def _close(self) -> None:
|
||||
await self.__raw.close()
|
||||
await self.__inner.close()
|
||||
|
||||
def __addr(self, path: str) -> str:
|
||||
path = self._unchroot(path)
|
||||
if not path:
|
||||
return self.__subtree
|
||||
if not self.__subtree:
|
||||
return path
|
||||
return f'{self.__subtree}/{path}'
|
||||
def __host_path(self, relpath: str) -> str:
|
||||
relpath = self._unchroot(relpath)
|
||||
if not relpath:
|
||||
return self.__root
|
||||
if not self.__root:
|
||||
return relpath
|
||||
return f'{self.__root}/{relpath}'
|
||||
|
||||
@staticmethod
|
||||
def __resolve(path: str) -> tuple[str, bool]:
|
||||
def __strip_suffix(path: str) -> tuple[str, bool]:
|
||||
if path.endswith(SECRET_SUFFIX):
|
||||
return path[:-len(SECRET_SUFFIX)], False
|
||||
if path.endswith(BLOB_SUFFIX):
|
||||
|
|
@ -67,7 +67,7 @@ class KvFile(Base):
|
|||
from ...Distro import Distro
|
||||
from ...ExecContext import ExecContext
|
||||
|
||||
if not isinstance(self.__raw, ExecContext):
|
||||
if not isinstance(self.__inner, ExecContext):
|
||||
raise ValueError(
|
||||
'KvFile: a distro must be given when the host'
|
||||
' context is not an exec context'
|
||||
|
|
@ -76,7 +76,7 @@ class KvFile(Base):
|
|||
|
||||
pkg_filter_str = os.environ.get('JW_DEFAULT_PKG_FILTER')
|
||||
self.__distro = await Distro.instantiate(
|
||||
ec = self.__raw,
|
||||
ec = self.__inner,
|
||||
default_pkg_filter = (
|
||||
PackageFilterString(pkg_filter_str) if pkg_filter_str else None
|
||||
),
|
||||
|
|
@ -91,7 +91,7 @@ class KvFile(Base):
|
|||
# per-level presence checks and the walk's node stats
|
||||
# hit memory: the root, the candidate files of both
|
||||
# kinds, the directories they live in, and the file
|
||||
# names a suffixed address would probe those
|
||||
# names a suffixed path would probe those
|
||||
# directories by
|
||||
warm = ['/']
|
||||
warm += [f'/{p}{SECRET_SUFFIX}' for p in self.___config_paths]
|
||||
|
|
@ -107,27 +107,27 @@ class KvFile(Base):
|
|||
assert self.___config_paths is not None
|
||||
return self.___config_paths
|
||||
|
||||
async def __entry_kv(self, entry: str) -> dict[str, str]:
|
||||
path = f'/{entry.lstrip("/")}{SECRET_SUFFIX}'
|
||||
async def __compound_kv(self, config_path: str) -> dict[str, str]:
|
||||
path = f'/{config_path.lstrip("/")}{SECRET_SUFFIX}'
|
||||
if not await self.__ctx.file_exists(path):
|
||||
raise FileNotFoundError(entry)
|
||||
raise FileNotFoundError(config_path)
|
||||
ret = await self.__ctx.get(path)
|
||||
return parse_kv(ret.stdout)
|
||||
|
||||
async def __is_entry(self, path: str) -> bool:
|
||||
# -- The entry file on the host; KvFile has no default tree.
|
||||
# The path is host-relative, as from __addr()
|
||||
if not path:
|
||||
async def __is_compound(self, config_path: str) -> bool:
|
||||
# -- The compound file on the host; KvFile has no default tree.
|
||||
# The path is host-relative, as from __host_path()
|
||||
if not config_path:
|
||||
return False
|
||||
return await self.__ctx.file_exists(f'/{path}{SECRET_SUFFIX}')
|
||||
return await self.__ctx.file_exists(f'/{config_path}{SECRET_SUFFIX}')
|
||||
|
||||
@override
|
||||
async def _stat(self, path: str, follow_symlinks: bool) -> StatResult:
|
||||
path = self.__addr(path)
|
||||
config, is_blob = self.__resolve(path)
|
||||
path = self.__host_path(path)
|
||||
config_path, is_blob = self.__strip_suffix(path)
|
||||
|
||||
async def __stat_of(real: str, ftype: FileType) -> StatResult:
|
||||
st = await self.__ctx.stat(real, follow_symlinks)
|
||||
async def __stat_of(host_path: str, ftype: FileType) -> StatResult:
|
||||
st = await self.__ctx.stat(host_path, follow_symlinks)
|
||||
return self._stat_result(
|
||||
st.owner,
|
||||
st.group,
|
||||
|
|
@ -137,21 +137,22 @@ class KvFile(Base):
|
|||
size = st.size,
|
||||
)
|
||||
|
||||
if not config:
|
||||
if not config_path:
|
||||
return await __stat_of('/', FT_DIR)
|
||||
if is_blob:
|
||||
real = f'/{config}{BLOB_SUFFIX}'
|
||||
if not await self.__ctx.file_exists(real):
|
||||
secret_path = f'/{config_path}{BLOB_SUFFIX}'
|
||||
if not await self.__ctx.file_exists(secret_path):
|
||||
raise FileNotFoundError(errno.ENOENT, f'stat: {path}')
|
||||
return await __stat_of(real, FT_SECRET_FILE)
|
||||
if await self.__is_entry(config):
|
||||
return await __stat_of(f'/{config}{SECRET_SUFFIX}', FT_SECRET)
|
||||
parent, sep, key = config.rpartition('/')
|
||||
return await __stat_of(secret_path, FT_SECRET_FILE)
|
||||
if await self.__is_compound(config_path):
|
||||
return await __stat_of(f'/{config_path}{SECRET_SUFFIX}', FT_SECRET)
|
||||
parent, sep, key = config_path.rpartition('/')
|
||||
if sep and key:
|
||||
parent, _ = self.__resolve(parent)
|
||||
if await self.__is_entry(parent) and key in await self.__entry_kv(parent):
|
||||
parent, _ = self.__strip_suffix(parent)
|
||||
if await self.__is_compound(parent) and \
|
||||
key in await self.__compound_kv(parent):
|
||||
# -- The type is synthesized, the mode the one of
|
||||
# the entry file
|
||||
# the compound file
|
||||
st = await self.__ctx.stat(f'/{parent}{SECRET_SUFFIX}', follow_symlinks)
|
||||
return self._stat_result(
|
||||
st.owner,
|
||||
|
|
@ -160,40 +161,40 @@ class KvFile(Base):
|
|||
FT_SECRET_KEY,
|
||||
mode = st.mode,
|
||||
)
|
||||
blob = f'/{config}{BLOB_SUFFIX}'
|
||||
if await self.__ctx.file_exists(blob):
|
||||
return await __stat_of(blob, FT_SECRET_FILE)
|
||||
if await self.__ctx.is_dir(f'/{config}'):
|
||||
return await __stat_of(f'/{config}', FT_DIR)
|
||||
secret_path = f'/{config_path}{BLOB_SUFFIX}'
|
||||
if await self.__ctx.file_exists(secret_path):
|
||||
return await __stat_of(secret_path, FT_SECRET_FILE)
|
||||
if await self.__ctx.is_dir(f'/{config_path}'):
|
||||
return await __stat_of(f'/{config_path}', FT_DIR)
|
||||
raise FileNotFoundError(errno.ENOENT, f'stat: {path}')
|
||||
|
||||
@override
|
||||
async def _is_dir(self, path: str, follow_symlinks: bool) -> bool:
|
||||
path = self.__addr(path)
|
||||
config, is_blob = self.__resolve(path)
|
||||
if not config:
|
||||
path = self.__host_path(path)
|
||||
config_path, is_blob = self.__strip_suffix(path)
|
||||
if not config_path:
|
||||
return True
|
||||
parent, sep, _ = config.rpartition('/')
|
||||
parent, sep, _ = config_path.rpartition('/')
|
||||
if sep:
|
||||
parent, _ = self.__resolve(parent)
|
||||
if await self.__is_entry(parent):
|
||||
parent, _ = self.__strip_suffix(parent)
|
||||
if await self.__is_compound(parent):
|
||||
return False
|
||||
if await self.__is_entry(config):
|
||||
if await self.__is_compound(config_path):
|
||||
return True
|
||||
if is_blob:
|
||||
return False
|
||||
return await self.__ctx.is_dir(f'/{config}')
|
||||
return await self.__ctx.is_dir(f'/{config_path}')
|
||||
|
||||
@override
|
||||
async def _readdir(self, path: str) -> Iterable[str]:
|
||||
path = self.__addr(path)
|
||||
config, is_blob = self.__resolve(path)
|
||||
if config and await self.__is_entry(config):
|
||||
return (await self.__entry_kv(config)).keys()
|
||||
if is_blob and await self.__ctx.file_exists(f'/{config}{BLOB_SUFFIX}'):
|
||||
path = self.__host_path(path)
|
||||
config_path, is_blob = self.__strip_suffix(path)
|
||||
if config_path and await self.__is_compound(config_path):
|
||||
return (await self.__compound_kv(config_path)).keys()
|
||||
if is_blob and await self.__ctx.file_exists(f'/{config_path}{BLOB_SUFFIX}'):
|
||||
raise NotADirectoryError(path)
|
||||
children: set[str] = set()
|
||||
prefix = f'{config}/' if config else ''
|
||||
prefix = f'{config_path}/' if config_path else ''
|
||||
for cfg in await self.__config_paths():
|
||||
if not cfg.startswith(prefix):
|
||||
continue
|
||||
|
|
@ -209,15 +210,15 @@ class KvFile(Base):
|
|||
children.add(label)
|
||||
continue
|
||||
# -- A direct child lists under the on-host file name of
|
||||
# the file that backs it, entry and blob alike
|
||||
# the file that backs it, compound and blob alike
|
||||
if await self.__ctx.file_exists(f'/{cfg}{SECRET_SUFFIX}'):
|
||||
children.add(f'{label}{SECRET_SUFFIX}')
|
||||
if await self.__ctx.file_exists(f'/{cfg}{BLOB_SUFFIX}'):
|
||||
children.add(f'{label}{BLOB_SUFFIX}')
|
||||
if config and not children:
|
||||
if config_path and not children:
|
||||
# -- A branch the host lacks is absent; a real directory
|
||||
# with no secret children lists empty
|
||||
if not await self.__ctx.is_dir(f'/{config}'):
|
||||
if not await self.__ctx.is_dir(f'/{config_path}'):
|
||||
raise FileNotFoundError(path)
|
||||
return children
|
||||
|
||||
|
|
@ -225,23 +226,24 @@ class KvFile(Base):
|
|||
async def _get(
|
||||
self, path: str, wd: str | None, throw: bool, verbose: bool | None, title: str
|
||||
) -> Result:
|
||||
path = self.__addr(path)
|
||||
config, is_blob = self.__resolve(path)
|
||||
path = self.__host_path(path)
|
||||
config_path, is_blob = self.__strip_suffix(path)
|
||||
if is_blob:
|
||||
real = f'/{config}{BLOB_SUFFIX}'
|
||||
if not await self.__ctx.file_exists(real):
|
||||
secret_path = f'/{config_path}{BLOB_SUFFIX}'
|
||||
if not await self.__ctx.file_exists(secret_path):
|
||||
raise FileNotFoundError(path)
|
||||
return await self.__ctx.get(real)
|
||||
real = f'/{config}'
|
||||
if await self.__ctx.file_exists(real + SECRET_SUFFIX):
|
||||
return await self.__ctx.get(real + SECRET_SUFFIX)
|
||||
if await self.__ctx.file_exists(real + BLOB_SUFFIX):
|
||||
return await self.__ctx.get(real + BLOB_SUFFIX)
|
||||
if '/' not in config:
|
||||
return await self.__ctx.get(secret_path)
|
||||
secret_path = f'/{config_path}{SECRET_SUFFIX}'
|
||||
if await self.__ctx.file_exists(secret_path):
|
||||
return await self.__ctx.get(secret_path)
|
||||
secret_path = f'/{config_path}{BLOB_SUFFIX}'
|
||||
if await self.__ctx.file_exists(secret_path):
|
||||
return await self.__ctx.get(secret_path)
|
||||
if '/' not in config_path:
|
||||
raise FileNotFoundError(path)
|
||||
entry, key = config.rsplit('/', 1)
|
||||
entry, _ = self.__resolve(entry)
|
||||
kv = await self.__entry_kv(entry)
|
||||
compound, key = config_path.rsplit('/', 1)
|
||||
compound, _ = self.__strip_suffix(compound)
|
||||
kv = await self.__compound_kv(compound)
|
||||
try:
|
||||
return Result(stdout = kv[key].encode())
|
||||
except KeyError:
|
||||
|
|
@ -249,27 +251,29 @@ class KvFile(Base):
|
|||
|
||||
@override
|
||||
async def _mkdir(self, path: str, mode: int, ftype: FileType) -> None:
|
||||
path = self.__addr(path)
|
||||
config, _ = self.__resolve(path)
|
||||
real = f'/{config}'
|
||||
path = self.__host_path(path)
|
||||
config_path, _ = self.__strip_suffix(path)
|
||||
host_path = f'/{config_path}'
|
||||
if ftype.is_subtype(FT_COMPOUND):
|
||||
# -- An entry: materialize the empty entry file
|
||||
if await self.__ctx.file_exists(real + SECRET_SUFFIX):
|
||||
# -- A compound: materialize the empty compound file
|
||||
if await self.__ctx.file_exists(host_path + SECRET_SUFFIX):
|
||||
return
|
||||
if not await self.__ctx.is_dir(os.path.dirname(real)):
|
||||
if not await self.__ctx.is_dir(os.path.dirname(host_path)):
|
||||
raise OSError(
|
||||
errno.EROFS, f'mkdir: "{path}" is below a missing directory'
|
||||
)
|
||||
await self.__ctx.put(real + SECRET_SUFFIX, b'', mode = 0o400, atomic = True)
|
||||
await self.__ctx.put(
|
||||
host_path + SECRET_SUFFIX, b'', mode = 0o400, atomic = True
|
||||
)
|
||||
elif ftype.is_subtype(FT_DIR):
|
||||
# -- A branch: a real directory
|
||||
if await self.__ctx.is_dir(real):
|
||||
if await self.__ctx.is_dir(host_path):
|
||||
return
|
||||
if not await self.__ctx.is_dir(os.path.dirname(real)):
|
||||
if not await self.__ctx.is_dir(os.path.dirname(host_path)):
|
||||
raise OSError(
|
||||
errno.EROFS, f'mkdir: "{path}" is below a missing directory'
|
||||
)
|
||||
await self.__ctx.mkdir(real, mode)
|
||||
await self.__ctx.mkdir(host_path, mode)
|
||||
else:
|
||||
raise ValueError(f'mkdir: "{path}" is not a directory type')
|
||||
|
||||
|
|
@ -288,16 +292,16 @@ class KvFile(Base):
|
|||
atomic: bool,
|
||||
ftype: FileType,
|
||||
) -> Result:
|
||||
path = self.__addr(path)
|
||||
config, is_blob = self.__resolve(path)
|
||||
path = self.__host_path(path)
|
||||
config_path, is_blob = self.__strip_suffix(path)
|
||||
if ftype.is_subtype(FT_SECRET):
|
||||
if is_blob:
|
||||
raise ValueError(
|
||||
f'put: "{path}" names a blob file, but the ftype is an entry'
|
||||
f'put: "{path}" names a blob file, but the ftype is a compound'
|
||||
)
|
||||
m = 0o400 if mode is None else int(mode, 8)
|
||||
return await self.__ctx.put(
|
||||
f'/{config}{SECRET_SUFFIX}',
|
||||
f'/{config_path}{SECRET_SUFFIX}',
|
||||
content,
|
||||
owner = owner,
|
||||
group = group,
|
||||
|
|
@ -309,26 +313,26 @@ class KvFile(Base):
|
|||
# on-host file name
|
||||
m = 0o400 if mode is None else int(mode, 8)
|
||||
return await self.__ctx.put(
|
||||
f'/{config}{BLOB_SUFFIX}',
|
||||
f'/{config_path}{BLOB_SUFFIX}',
|
||||
content,
|
||||
owner = owner,
|
||||
group = group,
|
||||
mode = m,
|
||||
atomic = True,
|
||||
)
|
||||
entry, sep, key = config.rpartition('/')
|
||||
if not sep or not entry or not key or '/' in key:
|
||||
raise ValueError(f'put: "{path}" is not an entry/key path')
|
||||
entry, _ = self.__resolve(entry)
|
||||
real_entry = f'/{entry}'
|
||||
compound, sep, key = config_path.rpartition('/')
|
||||
if not sep or not compound or not key or '/' in key:
|
||||
raise ValueError(f'put: "{path}" is not a compound/key path')
|
||||
compound, _ = self.__strip_suffix(compound)
|
||||
real_compound = f'/{compound}'
|
||||
if ftype.is_subtype(FT_SECRET_KEY):
|
||||
if not await self.__ctx.file_exists(f'{real_entry}{SECRET_SUFFIX}'):
|
||||
raise OSError(errno.EROFS, f'put: "{entry}" has no entry file')
|
||||
kv = await self.__entry_kv(entry)
|
||||
if not await self.__ctx.file_exists(f'{real_compound}{SECRET_SUFFIX}'):
|
||||
raise OSError(errno.EROFS, f'put: "{compound}" has no compound file')
|
||||
kv = await self.__compound_kv(compound)
|
||||
kv[key] = content.decode()
|
||||
m = 0o400 if mode is None else int(mode, 8)
|
||||
return await self.__ctx.put(
|
||||
f'{real_entry}{SECRET_SUFFIX}',
|
||||
f'{real_compound}{SECRET_SUFFIX}',
|
||||
serialize_kv(kv),
|
||||
owner = owner,
|
||||
group = group,
|
||||
|
|
@ -339,4 +343,4 @@ class KvFile(Base):
|
|||
|
||||
@override
|
||||
async def _unlink(self, path: str) -> None:
|
||||
await self.__ctx.unlink(f'/{self.__addr(path)}')
|
||||
await self.__ctx.unlink(f'/{self.__host_path(path)}')
|
||||
|
|
|
|||
Loading…
Reference in a new issue