make-msix-layout.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python3
  2. """Builds the MSIX package layout (the folder `makeappx pack /d` packs) for the Microsoft Store build (round 165n).
  3. Usage:
  4. scripts/make-msix-layout.py --version 0.1.20[-alpha] --bin <folder with LYNX.exe> --out <layout folder>
  5. [--identity-name N] [--publisher P] [--publisher-display D] [--display-name L]
  6. [--test-identity]
  7. [--extra file ...]
  8. The identity defaults come from the environment (MSIX_IDENTITY_NAME, MSIX_PUBLISHER, MSIX_PUBLISHER_DISPLAY: the
  9. repository variables, copied from Partner Center › Product identity) and fall back to the same values; MSIX_DISPLAY_NAME
  10. is the app name reserved in Partner Center (default "LYNX SHARE"; Start menu, taskbar and the app itself say LYNX SHARE too). --test-identity
  11. appends Windows' "unsigned package" OID to the publisher, so `Add-AppxPackage -AllowUnsigned` installs the package
  12. for a test (it can then never clash with the Store-signed one). See docs/STORE.md.
  13. """
  14. import argparse
  15. import os
  16. import re
  17. import shutil
  18. import sys
  19. ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  20. # LYNX's identity in Partner Center (Product identity; family name KA1WARE.LYNXSHARE_sj8hjwabzpdr8, Store ID
  21. # 9NQRFBFDCX6N): the defaults, so a local package matches the release workflow's (whose repository variables hold
  22. # the same values)
  23. DEFAULT_NAME = "KA1WARE.LYNXSHARE"
  24. DEFAULT_PUBLISHER = "CN=BACBB786-A595-4AC3-9807-A70EAA268E4C"
  25. DEFAULT_DISPLAY = "KA1WARE"
  26. UNSIGNED_OID = "OID.2.25.311729368913984317654407730594956997722=1"
  27. APP_FILES = ("LYNX.exe", "lynx-asio.dll", "onnxruntime.dll", "LYNX-bridge32.exe") # (lynx-asio32.dll is for registering with 32-bit hosts: not in the package)
  28. def msix_version(version, store_build=0):
  29. """'0.1.20' / '0.1.20-alpha' -> '0.1.200.0'. The Store reserves the fourth part (it must be 0), so a repackaged
  30. build of the same release (a resubmission after a failed certification) needs a higher third part: the third part
  31. is patch * 10 + store_build (0..9), which keeps every later release above every rebuild (0.1.209 < 0.1.210).
  32. 0.1.20 shipped as 0.1.20.0, below all of these. Each part 0..65535."""
  33. m = re.match(r"^(\d+)\.(\d+)\.(\d+)(?:[-+].*)?$", version.strip())
  34. if not m:
  35. raise ValueError(f"not a version: {version!r}")
  36. if not 0 <= store_build <= 9:
  37. raise ValueError(f"store build must be 0..9: {store_build!r}")
  38. major, minor, patch = (int(p) for p in m.groups())
  39. parts = [major, minor, patch * 10 + store_build]
  40. if any(p > 65535 for p in parts):
  41. raise ValueError(f"version part above 65535: {version!r}")
  42. return ".".join(str(p) for p in parts) + ".0"
  43. def xml_escape(text):
  44. return text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace('"', "&quot;")
  45. def main(argv):
  46. ap = argparse.ArgumentParser()
  47. ap.add_argument("--version", required=True)
  48. ap.add_argument("--bin", required=True, help="folder with LYNX.exe (and lynx-asio.dll)")
  49. ap.add_argument("--out", required=True)
  50. ap.add_argument("--identity-name", default=os.environ.get("MSIX_IDENTITY_NAME") or DEFAULT_NAME)
  51. ap.add_argument("--publisher", default=os.environ.get("MSIX_PUBLISHER") or DEFAULT_PUBLISHER)
  52. ap.add_argument("--publisher-display", default=os.environ.get("MSIX_PUBLISHER_DISPLAY") or DEFAULT_DISPLAY)
  53. ap.add_argument("--display-name", default=os.environ.get("MSIX_DISPLAY_NAME") or "LYNX SHARE")
  54. ap.add_argument("--store-build", type=int, default=int(os.environ.get("MSIX_STORE_BUILD") or 0),
  55. help="0..9: repackaged build of the same release (see msix_version)")
  56. ap.add_argument("--test-identity", action="store_true")
  57. ap.add_argument("--loose", action="store_true",
  58. help="also write unqualified logo names, for Add-AppxPackage -Register without makepri's resources.pri")
  59. ap.add_argument("--extra", nargs="*", default=[], help="more files for the package root (Demo.lynx, LICENSE...)")
  60. a = ap.parse_args(argv)
  61. if not re.match(r"^[A-Za-z0-9.\-]{3,50}$", a.identity_name):
  62. raise SystemExit(f"bad identity name {a.identity_name!r} (3-50 of A-Z a-z 0-9 . -)")
  63. if not a.publisher.startswith("CN="):
  64. raise SystemExit(f"publisher must be a distinguished name starting with CN=: {a.publisher!r}")
  65. publisher = a.publisher + (", " + UNSIGNED_OID if a.test_identity and UNSIGNED_OID not in a.publisher else "")
  66. if os.path.exists(a.out):
  67. shutil.rmtree(a.out)
  68. os.makedirs(a.out)
  69. for name in APP_FILES:
  70. src = os.path.join(a.bin, name)
  71. if os.path.exists(src):
  72. shutil.copy2(src, a.out)
  73. elif name == "LYNX.exe":
  74. raise SystemExit(f"missing {src}")
  75. for extra in a.extra:
  76. if os.path.isdir(extra): # a folder (the third-party licence texts, round 190nk) keeps its name
  77. shutil.copytree(extra, os.path.join(a.out, os.path.basename(os.path.normpath(extra))), dirs_exist_ok=True)
  78. else:
  79. shutil.copy2(extra, a.out)
  80. assets = os.path.join(a.out, "Assets")
  81. shutil.copytree(os.path.join(ROOT, "resources", "msix", "Assets"), assets)
  82. if a.loose:
  83. for base in ("Square44x44Logo", "Square150x150Logo", "Wide310x150Logo", "SmallTile", "LargeTile", "StoreLogo", "ProjectFile"):
  84. shutil.copy2(os.path.join(assets, base + ".scale-100.png"), os.path.join(assets, base + ".png"))
  85. with open(os.path.join(ROOT, "resources", "msix", "AppxManifest.xml.in"), encoding="utf-8") as f:
  86. manifest = f.read()
  87. fields = {
  88. "@IDENTITY_NAME@": a.identity_name,
  89. "@PUBLISHER@": publisher,
  90. "@PUBLISHER_DISPLAY@": a.publisher_display,
  91. "@VERSION@": msix_version(a.version, a.store_build),
  92. "@DISPLAY_NAME@": a.display_name,
  93. }
  94. for key, value in fields.items():
  95. manifest = manifest.replace(key, xml_escape(value))
  96. if "@" in re.sub(r"<!--.*?-->", "", manifest, flags=re.S):
  97. raise SystemExit("unfilled field left in the manifest")
  98. with open(os.path.join(a.out, "AppxManifest.xml"), "w", encoding="utf-8", newline="\r\n") as f:
  99. f.write(manifest)
  100. print(f"layout {a.out}: {a.identity_name} {fields['@VERSION@']} publisher={publisher!r}")
  101. return 0
  102. if __name__ == "__main__":
  103. sys.exit(main(sys.argv[1:]))