windows_appinstaller_update.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """Check for an App Installer update for the running MSIX package.
  2. The out-of-store desktop installs are App Installer owned: the OS registered
  3. the .appinstaller URI as the package's update source at install, so the
  4. OS can tell us whether a newer package version is available. The desktop
  5. shows its own prompt + tears down before the OS applies the swap.
  6. Run with the BUNDLED payload python (the winrt package ships there):
  7. <payload>/tools/<python-entry>/python.exe -P -m hermes_cli.windows_appinstaller_update
  8. Exit codes:
  9. 0 no update available, or this process has no package identity
  10. 2 update available (the caller decides whether to prompt/teardown)
  11. 1 error, including a missing registered App Installer update source
  12. API shape (pywinrt projection, verified against the installed winrt 3.2.1):
  13. ``Package.current`` is a static PROPERTY; the update check is the instance
  14. method ``Package.check_update_availability_async`` — ``PackageManager`` has
  15. no such method. ``PackageUpdateAvailability`` members (verified live in the
  16. installed projection): UNKNOWN=0, NO_UPDATES=1, AVAILABLE=2, REQUIRED=3,
  17. ERROR=4. Only the specific "no package identity" HRESULT (0x80073D54) means
  18. not-packaged; every other failure is an unknown, never a "no update".
  19. """
  20. import json
  21. import sys
  22. # HRESULT 0x80073D54 — "The process has no package identity." The one
  23. # failure that positively identifies a non-packaged (dev) run.
  24. _NO_PACKAGE_IDENTITY_HRESULT = -2147009196
  25. def _load_projection():
  26. from winrt.windows.applicationmodel import Package, PackageUpdateAvailability
  27. return Package, PackageUpdateAvailability
  28. def main() -> int:
  29. try:
  30. Package, PackageUpdateAvailability = _load_projection()
  31. except ImportError as exc:
  32. # The winrt module is absent (older payload without the dep): the
  33. # update check cannot run. Report unknown, not "no update".
  34. print(json.dumps({"available": None, "error": f"winrt import failed: {exc}"}))
  35. return 1
  36. # Property access, not a call: a packaged process gets the Package
  37. # instance; a dev run raises the package-identity HRESULT.
  38. try:
  39. package = Package.current
  40. except OSError as exc:
  41. if getattr(exc, "winerror", None) == _NO_PACKAGE_IDENTITY_HRESULT:
  42. print(json.dumps({"available": False, "reason": "not-packaged"}))
  43. return 0
  44. print(json.dumps({"available": None, "error": f"package identity failed: {exc}"}))
  45. return 1
  46. except Exception as exc:
  47. print(json.dumps({"available": None, "error": f"package identity failed: {exc}"}))
  48. return 1
  49. try:
  50. source = package.get_app_installer_info()
  51. if source is None:
  52. print(json.dumps({
  53. "available": None,
  54. "reason": "no-app-installer-source",
  55. "error": "No App Installer update source is registered. Install Hermes through its .appinstaller file to enable updates.",
  56. }))
  57. return 1
  58. source_uri = source.uri.absolute_uri
  59. result = package.check_update_availability_async().get()
  60. availability = PackageUpdateAvailability(result.availability)
  61. except Exception as exc:
  62. print(json.dumps({"available": None, "error": f"check failed: {exc}"}))
  63. return 1
  64. # Only AVAILABLE / REQUIRED mean "a newer package exists". NO_UPDATES is
  65. # the one honest "no". UNKNOWN and ERROR are NOT "no update" — reporting
  66. # them as such would suppress a real update prompt — so they surface as
  67. # an unknown with the availability name as the extended error.
  68. if availability == PackageUpdateAvailability.NO_UPDATES:
  69. print(json.dumps({"available": False, "availability": availability.name, "source_uri": source_uri}))
  70. return 0
  71. if availability in (PackageUpdateAvailability.AVAILABLE, PackageUpdateAvailability.REQUIRED):
  72. print(json.dumps({"available": True, "availability": availability.name, "source_uri": source_uri}))
  73. return 2
  74. print(json.dumps({"available": None, "error": f"availability unknown: {availability.name}"}))
  75. return 1
  76. if __name__ == "__main__":
  77. sys.exit(main())