-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsstore_package_resolution.py
More file actions
252 lines (203 loc) · 7.83 KB
/
Copy pathmsstore_package_resolution.py
File metadata and controls
252 lines (203 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python3
"""Package selection and install-order helpers for MSStoreHelper."""
import os
import re
from package_ingress import (
INSTALLABLE_PACKAGE_EXTENSIONS,
PackageIngressError,
validate_package_filename,
)
INSTALLABLE_EXTENSIONS = INSTALLABLE_PACKAGE_EXTENSIONS
ROLE_ORDER = {
"net_native_framework": 10,
"net_native_runtime": 20,
"vclibs": 30,
"ui_xaml": 40,
"windows_app_runtime": 50,
"app": 100,
}
ROLE_LABELS = {
"net_native_framework": ".NET Native Framework",
"net_native_runtime": ".NET Native Runtime",
"vclibs": "VCLibs",
"ui_xaml": "UI.Xaml",
"windows_app_runtime": "Windows App Runtime",
"app": "App",
}
DEPENDENCY_PREFIXES = (
("net_native_framework", ("microsoft.net.native.framework",)),
("net_native_runtime", ("microsoft.net.native.runtime",)),
("vclibs", ("microsoft.vclibs",)),
("ui_xaml", ("microsoft.ui.xaml",)),
(
"windows_app_runtime",
("microsoft.windowsappruntime", "microsoft.winapp.runtime"),
),
)
def package_identity(filename):
"""Return the AppX package identity name from a Store filename."""
base = os.path.basename(filename)
stem, _ = os.path.splitext(base)
return stem.split("_", 1)[0]
def package_role(filename):
identity = package_identity(filename).lower()
for role, prefixes in DEPENDENCY_PREFIXES:
if any(identity.startswith(prefix) for prefix in prefixes):
return role
return "app"
def package_role_label(filename):
return ROLE_LABELS.get(package_role(filename), "App")
def is_dependency_package(package):
return package_role(package["FileName"]) != "app"
def package_extension(package):
filename = package["FileName"]
ext = os.path.splitext(filename)[1].lower()
if ext:
return ext
file_type = package.get("FileType", "").lower()
return f".{file_type}" if file_type else ""
def is_installable_package(package):
try:
validate_package_filename(package["FileName"])
except (KeyError, PackageIngressError):
return False
ext = package_extension(package)
if ext not in INSTALLABLE_EXTENSIONS:
return False
return not package.get("IsEncrypted", False)
def package_architecture(package):
arch = package.get("Architecture")
if arch:
return str(arch).lower()
lower = package["FileName"].lower()
if "_arm64_" in lower:
return "arm64"
if "_x64_" in lower or "_amd64_" in lower:
return "x64"
if "_x86_" in lower:
return "x86"
if "_arm_" in lower:
return "arm"
return "neutral"
def is_bundle_package(package):
if package.get("IsBundle"):
return True
return package_extension(package) in {".appxbundle", ".msixbundle"}
def is_arch_compatible(package, target_arch):
arch = package_architecture(package)
if is_bundle_package(package):
return True
accepted = {target_arch.lower(), "neutral"}
if is_dependency_package(package) and target_arch.lower() == "x64":
accepted.add("x86")
if is_dependency_package(package) and target_arch.lower() == "arm64":
accepted.update({"arm", "x86"})
return arch in accepted
def package_version_tuple(filename):
"""Parse the package version from Name_version_arch_publisher.ext."""
stem, _ = os.path.splitext(os.path.basename(filename))
parts = stem.split("_")
for part in parts[1:]:
if re.fullmatch(r"\d+(?:\.\d+){1,4}", part):
return tuple(int(piece) for piece in part.split("."))
matches = re.findall(r"\d+(?:\.\d+){1,4}", stem)
if not matches:
return ()
return tuple(int(piece) for piece in matches[-1].split("."))
def version_tuple_from_text(text):
match = re.search(r"\d+(?:\.\d+){1,4}", str(text or ""))
if not match:
return ()
return tuple(int(piece) for piece in match.group(0).split("."))
def format_version_tuple(version_tuple):
return ".".join(str(piece) for piece in version_tuple) if version_tuple else "unknown"
def compare_version_tuples(left, right):
width = max(len(left), len(right))
padded_left = tuple(left) + (0,) * (width - len(left))
padded_right = tuple(right) + (0,) * (width - len(right))
if padded_left == padded_right:
return 0
return 1 if padded_left > padded_right else -1
def installed_version_satisfies_package(package, installed_version):
available = package_version_tuple(package["FileName"])
installed = version_tuple_from_text(installed_version)
if not installed or not available:
return False
return compare_version_tuples(installed, available) >= 0
def signature_info_is_valid_microsoft(signature_info):
status = str(signature_info.get("Status", "")).lower()
signer = str(signature_info.get("Signer", "")).strip()
revocation_state = str(
signature_info.get("RevocationState", "")
).strip().lower()
status_valid = status in {"valid", "0"}
microsoft_signers = {
"CN=Microsoft Corporation",
(
"CN=Microsoft Corporation, O=Microsoft Corporation, "
"L=Redmond, S=Washington, C=US"
),
(
"CN=Microsoft Windows, O=Microsoft Corporation, "
"L=Redmond, S=Washington, C=US"
),
}
return (
status_valid
and signature_info.get("ChainValid") is True
and revocation_state in {"checked", "offline"}
and signer in microsoft_signers
)
def _candidate_score(package, target_arch, prefer_exact_arch=False):
arch = package_architecture(package)
if prefer_exact_arch:
return (
1 if arch == target_arch.lower() else 0,
package_version_tuple(package["FileName"]),
1 if is_bundle_package(package) else 0,
1 if arch == "neutral" else 0,
)
return (
package_version_tuple(package["FileName"]),
1 if is_bundle_package(package) else 0,
1 if arch == target_arch.lower() else 0,
1 if arch == "neutral" else 0,
)
def select_recommended_packages(packages, target_arch, prefer_exact_arch=False):
"""Pick best installable packages, including dependency frameworks."""
best_by_identity = {}
for package in packages:
if not is_installable_package(package):
continue
if not is_arch_compatible(package, target_arch):
continue
identity = package_identity(package["FileName"]).lower()
key = (
identity,
package_architecture(package),
) if is_dependency_package(package) else identity
current = best_by_identity.get(key)
if current is None or _candidate_score(package, target_arch, prefer_exact_arch) > _candidate_score(current, target_arch, prefer_exact_arch):
best_by_identity[key] = package
return order_packages_for_install(best_by_identity.values(), target_arch)
def order_packages_for_install(packages, target_arch):
"""Install frameworks before app packages while preserving arch preference."""
return sorted(
packages,
key=lambda package: (
ROLE_ORDER.get(package_role(package["FileName"]), ROLE_ORDER["app"]),
package_identity(package["FileName"]).lower(),
package_architecture(package) != target_arch.lower(),
package_version_tuple(package["FileName"]),
package["FileName"].lower(),
),
)
def annotate_package(package):
"""Add resolver metadata used by the GUI without changing API shape."""
role = package_role(package["FileName"])
package["PackageRole"] = role
package["PackageRoleLabel"] = ROLE_LABELS.get(role, "App")
package["InstallOrder"] = ROLE_ORDER.get(role, ROLE_ORDER["app"])
package["PackageIdentity"] = package_identity(package["FileName"])
package["AvailableVersion"] = format_version_tuple(package_version_tuple(package["FileName"]))
return package