Files
hermes-agent/utils/__init__.py
BearHuddleston 554944092a fix(gateway): publish a no-link RoomLink secret with a no-clobber rename on Windows
The no-hard-link fallback held a flock across its exists() check and its
os.replace, but skipped the lock where fcntl is missing. That is Windows,
where a FAT/exFAT or SMB home refuses os.link, so two first users there
could both pass the check, both replace, and keep different signing keys.

The fallback moves into utils.files.publish_new_file: Windows publishes
with os.rename, which never replaces an existing file (FileExistsError),
and POSIX keeps the sibling flock. The first-use race test now also widens
the check-to-publish window and runs on every OS lane.
2026-11-28 01:58:02 -06:00

104 lines
2.6 KiB
Python

"""Small helpers that several top-level packages share and that know nothing about Hermes as a product.
- ``utils.files``: files on disk: atomic writes and replaces, temp files, directory sync,
cleanup, file mode and owner, file signatures for caches, JSON and YAML file helpers.
- ``utils.urls``: base URL hostname, path and origin, and domain matching.
- ``utils.env``: truthy strings, numeric env vars, and proxy env var normalization.
Callers import from ``utils``; tests patch a name in the module that defines it.
"""
from utils.env import (
env_float,
env_int,
env_var_enabled,
is_truthy_value,
normalize_proxy_env_vars,
normalize_proxy_url,
sanitize_no_proxy_entries,
TRUTHY_STRINGS,
)
from utils.files import (
atomic_json_write,
atomic_replace,
atomic_roundtrip_yaml_save,
atomic_roundtrip_yaml_update,
atomic_write_bytes,
atomic_write_text,
atomic_yaml_write,
content_digest,
default_new_file_mode,
fast_safe_load,
file_cache_version,
file_signature,
fsync_directory,
load_yaml_file_readonly,
logger,
mkstemp_beside,
mtime_is_racy,
preserve_file_mode,
preserve_file_owner,
publish_new_file,
racy_content_digest,
RACY_MTIME_WINDOW_NS,
read_json_or_empty,
restore_file_mode,
restore_file_owner,
rmtree_readonly,
safe_json_loads,
unlink_files_older_than,
warn_if_credential_file_broadly_readable,
)
from utils.urls import (
base_url_host_matches,
base_url_hostname,
base_url_origin,
base_url_path,
same_base_url_origin,
)
__all__ = [
"RACY_MTIME_WINDOW_NS",
"TRUTHY_STRINGS",
"atomic_json_write",
"atomic_replace",
"atomic_roundtrip_yaml_save",
"atomic_roundtrip_yaml_update",
"atomic_write_bytes",
"atomic_write_text",
"atomic_yaml_write",
"base_url_host_matches",
"base_url_hostname",
"base_url_origin",
"base_url_path",
"content_digest",
"default_new_file_mode",
"env_float",
"env_int",
"env_var_enabled",
"fast_safe_load",
"file_cache_version",
"file_signature",
"fsync_directory",
"is_truthy_value",
"load_yaml_file_readonly",
"logger",
"mkstemp_beside",
"mtime_is_racy",
"normalize_proxy_env_vars",
"normalize_proxy_url",
"preserve_file_mode",
"preserve_file_owner",
"publish_new_file",
"racy_content_digest",
"read_json_or_empty",
"restore_file_mode",
"restore_file_owner",
"rmtree_readonly",
"safe_json_loads",
"same_base_url_origin",
"sanitize_no_proxy_entries",
"unlink_files_older_than",
"warn_if_credential_file_broadly_readable",
]