defaults.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """Optional packages the default install carries, and the user's opt-outs.
  2. A ``default`` package (see ``Package.default``) joins the installers' PM stage,
  3. a bare ``hermes pm install`` and ``hermes update`` on every target it builds
  4. for. The user can decline one (``install.sh --skip-browser`` /
  5. ``--skip-computer-use``, ``install.ps1 -SkipBrowser`` / ``-SkipComputerUse``,
  6. ``hermes pm install --without NAME``). The choice
  7. is recorded per installation beside PM's other install state, so a later
  8. update or bare install never re-adds it. An explicit
  9. ``hermes pm install NAME`` clears it.
  10. Stdlib-only apart from PM's own modules: the installers run this before any
  11. application dependency exists.
  12. """
  13. from __future__ import annotations
  14. import json
  15. from collections.abc import Iterable
  16. from pathlib import Path
  17. DECLINED_FILENAME = "declined-packages.json"
  18. # A retired default whose opt-out carries over to the package that took its
  19. # place: `--without agent-browser` declined the browser, which is Chromium now.
  20. # Read through on every load, so a record written by an older installer keeps
  21. # skipping the browser. The installers still pass the old name on purpose: the
  22. # stable PM they install by default accepts only `agent-browser`.
  23. RETIRED_DEFAULTS = {"agent-browser": "chromium"}
  24. def canonical_default_name(name: str) -> str:
  25. return RETIRED_DEFAULTS.get(name, name)
  26. def declined_path(project_root: Path | None = None) -> Path:
  27. from pm.environments import install_state_dir
  28. from pm.paths import repo_root
  29. return install_state_dir(repo_root() if project_root is None else Path(project_root)) / DECLINED_FILENAME
  30. def declined(project_root: Path | None = None) -> frozenset[str]:
  31. try:
  32. data = json.loads(declined_path(project_root).read_text(encoding="utf-8-sig"))
  33. except FileNotFoundError:
  34. return frozenset()
  35. except (OSError, ValueError) as exc:
  36. # Silently treating a damaged record as "nothing declined" would
  37. # re-add a package the user explicitly refused.
  38. from pm.package import InstallError
  39. raise InstallError("defaults", f"cannot read {declined_path(project_root)}: {exc}",
  40. "fix or delete the file, then retry") from exc
  41. names = data.get("declined") if isinstance(data, dict) else None
  42. return frozenset(canonical_default_name(name) for name in names or () if isinstance(name, str))
  43. def record_declined(*, add: Iterable[str] = (), remove: Iterable[str] = (),
  44. project_root: Path | None = None) -> frozenset[str]:
  45. """Update the recorded opt-outs; returns the new set. Writes only on change."""
  46. from pm.filesystem import durable_write_bytes
  47. current = declined(project_root)
  48. updated = (current | {canonical_default_name(name) for name in add}) - set(remove)
  49. if updated != current:
  50. body = json.dumps({"schema": 1, "declined": sorted(updated)}, indent=2) + "\n"
  51. durable_write_bytes(declined_path(project_root), body.encode("utf-8"))
  52. return frozenset(updated)
  53. def default_package_names() -> list[str]:
  54. """Every package that may be declined: the ``default`` ones, any target."""
  55. from pm.registry import all_packages, get_package
  56. return [name for name in all_packages() if get_package(name).default and not get_package(name).internal]
  57. def default_packages(names: list[str], *, target: str | None = None,
  58. declined_names: frozenset[str] | None = None) -> list[str]:
  59. """The optional defaults among ``names`` this install should carry.
  60. Excludes targets the package has no build for (its ``gaps``) and packages
  61. the user declined. ``names`` is the lockfile's package list.
  62. """
  63. from pm.registry import get_package
  64. from pm.store import current_target
  65. target = current_target() if target is None else target
  66. refused = declined() if declined_names is None else declined_names
  67. selected = []
  68. for name in names:
  69. try:
  70. package = get_package(name)
  71. except KeyError:
  72. continue # Lockfile/definition skew mid-update; drift() skips these too.
  73. if package.default and not package.internal and name not in refused \
  74. and package.missing_reason(target) is None:
  75. selected.append(name)
  76. return selected