| 1 |
#!/usr/bin/python3 |
| 2 |
# |
| 3 |
# Copyright (c) 2019 Martin Storsjo |
| 4 |
# |
| 5 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 6 |
# purpose with or without fee is hereby granted, provided that the above |
| 7 |
# copyright notice and this permission notice appear in all copies. |
| 8 |
# |
| 9 |
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 |
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 16 |
|
| 17 |
import argparse |
| 18 |
import functools |
| 19 |
import hashlib |
| 20 |
import os |
| 21 |
import multiprocessing.pool |
| 22 |
try: |
| 23 |
import simplejson |
| 24 |
except ModuleNotFoundError: |
| 25 |
import json as simplejson |
| 26 |
import six |
| 27 |
import shutil |
| 28 |
import socket |
| 29 |
import subprocess |
| 30 |
import sys |
| 31 |
import tempfile |
| 32 |
import zipfile |
| 33 |
|
| 34 |
def getArgsParser(): |
| 35 |
parser = argparse.ArgumentParser(description = "Download and install Visual Studio") |
| 36 |
parser.add_argument("--manifest", metavar="manifest", help="A predownloaded manifest file") |
| 37 |
parser.add_argument("--save-manifest", const=True, action="store_const", help="Store the downloaded manifest to a file") |
| 38 |
parser.add_argument("--major", default=17, metavar="version", help="The major version to download (defaults to 17)") |
| 39 |
parser.add_argument("--preview", dest="type", default="release", const="pre", action="store_const", help="Download the preview version instead of the release version") |
| 40 |
parser.add_argument("--cache", metavar="dir", help="Directory to use as a persistent cache for downloaded files") |
| 41 |
parser.add_argument("--dest", metavar="dir", help="Directory to install into") |
| 42 |
parser.add_argument("package", metavar="package", help="Package to install. If omitted, installs the default command line tools.", nargs="*") |
| 43 |
parser.add_argument("--ignore", metavar="component", help="Package to skip", action="append") |
| 44 |
parser.add_argument("--accept-license", const=True, action="store_const", help="Don't prompt for accepting the license") |
| 45 |
parser.add_argument("--print-version", const=True, action="store_const", help="Stop after fetching the manifest") |
| 46 |
parser.add_argument("--list-workloads", const=True, action="store_const", help="List high level workloads") |
| 47 |
parser.add_argument("--list-components", const=True, action="store_const", help="List available components") |
| 48 |
parser.add_argument("--list-packages", const=True, action="store_const", help="List all individual packages, regardless of type") |
| 49 |
parser.add_argument("--include-optional", const=True, action="store_const", help="Include all optional dependencies") |
| 50 |
parser.add_argument("--skip-recommended", const=True, action="store_const", help="Don't include recommended dependencies") |
| 51 |
parser.add_argument("--print-deps-tree", const=True, action="store_const", help="Print a tree of resolved dependencies for the given selection") |
| 52 |
parser.add_argument("--print-reverse-deps", const=True, action="store_const", help="Print a tree of packages that depend on the given selection") |
| 53 |
parser.add_argument("--print-selection", const=True, action="store_const", help="Print a list of the individual packages that are selected to be installed") |
| 54 |
parser.add_argument("--only-download", const=True, action="store_const", help="Stop after downloading package files") |
| 55 |
parser.add_argument("--only-unpack", const=True, action="store_const", help="Unpack the selected packages and keep all files, in the layout they are unpacked, don't restructure and prune files other than what's needed for MSVC CLI tools") |
| 56 |
parser.add_argument("--keep-unpack", const=True, action="store_const", help="Keep the unpacked files that aren't otherwise selected as needed output") |
| 57 |
parser.add_argument("--msvc-version", metavar="version", help="Install a specific MSVC toolchain version") |
| 58 |
parser.add_argument("--sdk-version", metavar="version", help="Install a specific Windows SDK version") |
| 59 |
return parser |
| 60 |
|
| 61 |
def setPackageSelectionMSVC16(args, packages, userversion, sdk, toolversion, defaultPackages): |
| 62 |
if findPackage(packages, "Microsoft.VisualStudio.Component.VC." + toolversion + ".x86.x64", None, warn=False): |
| 63 |
args.package.extend(["Win10SDK_" + sdk, "Microsoft.VisualStudio.Component.VC." + toolversion + ".x86.x64", "Microsoft.VisualStudio.Component.VC." + toolversion + ".ARM", "Microsoft.VisualStudio.Component.VC." + toolversion + ".ARM64"]) |
| 64 |
else: |
| 65 |
# Options for toolchains for specific versions. The latest version in |
| 66 |
# each manifest isn't available as a pinned version though, so if that |
| 67 |
# version is requested, try the default version. |
| 68 |
print("Didn't find exact version packages for " + userversion + ", assuming this is provided by the default/latest version") |
| 69 |
args.package.extend(defaultPackages) |
| 70 |
|
| 71 |
def setPackageSelectionMSVC15(args, packages, userversion, sdk, toolversion, defaultPackages): |
| 72 |
if findPackage(packages, "Microsoft.VisualStudio.Component.VC.Tools." + toolversion, None, warn=False): |
| 73 |
args.package.extend(["Win10SDK_" + sdk, "Microsoft.VisualStudio.Component.VC.Tools." + toolversion]) |
| 74 |
else: |
| 75 |
# Options for toolchains for specific versions. The latest version in |
| 76 |
# each manifest isn't available as a pinned version though, so if that |
| 77 |
# version is requested, try the default version. |
| 78 |
print("Didn't find exact version packages for " + userversion + ", assuming this is provided by the default/latest version") |
| 79 |
args.package.extend(defaultPackages) |
| 80 |
|
| 81 |
def setPackageSelection(args, packages): |
| 82 |
# If no packages are selected, install these versionless packages, which |
| 83 |
# gives the latest/recommended version for the current manifest. |
| 84 |
defaultPackages = ["Microsoft.VisualStudio.Workload.VCTools", "Microsoft.VisualStudio.Component.VC.Tools.ARM", "Microsoft.VisualStudio.Component.VC.Tools.ARM64"] |
| 85 |
|
| 86 |
# Note, that in the manifest for MSVC version X.Y, only version X.Y-1 |
| 87 |
# exists with a package name like "Microsoft.VisualStudio.Component.VC." |
| 88 |
# + toolversion + ".x86.x64". |
| 89 |
if args.msvc_version == "16.0": |
| 90 |
setPackageSelectionMSVC16(args, packages, args.msvc_version, "10.0.17763", "14.20", defaultPackages) |
| 91 |
elif args.msvc_version == "16.1": |
| 92 |
setPackageSelectionMSVC16(args, packages, args.msvc_version, "10.0.18362", "14.21", defaultPackages) |
| 93 |
elif args.msvc_version == "16.2": |
| 94 |
setPackageSelectionMSVC16(args, packages, args.msvc_version, "10.0.18362", "14.22", defaultPackages) |
| 95 |
elif args.msvc_version == "16.3": |
| 96 |
setPackageSelectionMSVC16(args, packages, args.msvc_version, "10.0.18362", "14.23", defaultPackages) |
| 97 |
elif args.msvc_version == "16.4": |
| 98 |
setPackageSelectionMSVC16(args, packages, args.msvc_version, "10.0.18362", "14.24", defaultPackages) |
| 99 |
elif args.msvc_version == "16.5": |
| 100 |
setPackageSelectionMSVC16(args, packages, args.msvc_version, "10.0.18362", "14.25", defaultPackages) |
| 101 |
elif args.msvc_version == "16.6": |
| 102 |
setPackageSelectionMSVC16(args, packages, args.msvc_version, "10.0.18362", "14.26", defaultPackages) |
| 103 |
elif args.msvc_version == "16.7": |
| 104 |
setPackageSelectionMSVC16(args, packages, args.msvc_version, "10.0.18362", "14.27", defaultPackages) |
| 105 |
elif args.msvc_version == "16.8": |
| 106 |
setPackageSelectionMSVC16(args, packages, args.msvc_version, "10.0.18362", "14.28", defaultPackages) |
| 107 |
elif args.msvc_version == "16.9": |
| 108 |
setPackageSelectionMSVC16(args, packages, args.msvc_version, "10.0.19041", "14.28.16.9", defaultPackages) |
| 109 |
elif args.msvc_version == "16.10": |
| 110 |
setPackageSelectionMSVC16(args, packages, args.msvc_version, "10.0.19041", "14.29.16.10", defaultPackages) |
| 111 |
elif args.msvc_version == "16.11": |
| 112 |
setPackageSelectionMSVC16(args, packages, args.msvc_version, "10.0.19041", "14.29.16.11", defaultPackages) |
| 113 |
elif args.msvc_version == "17.0": |
| 114 |
setPackageSelectionMSVC16(args, packages, args.msvc_version, "10.0.19041", "14.30.17.0", defaultPackages) |
| 115 |
|
| 116 |
elif args.msvc_version == "15.4": |
| 117 |
setPackageSelectionMSVC15(args, packages, args.msvc_version, "10.0.16299", "14.11", defaultPackages) |
| 118 |
elif args.msvc_version == "15.5": |
| 119 |
setPackageSelectionMSVC15(args, packages, args.msvc_version, "10.0.16299", "14.12", defaultPackages) |
| 120 |
elif args.msvc_version == "15.6": |
| 121 |
setPackageSelectionMSVC15(args, packages, args.msvc_version, "10.0.16299", "14.13", defaultPackages) |
| 122 |
elif args.msvc_version == "15.7": |
| 123 |
setPackageSelectionMSVC15(args, packages, args.msvc_version, "10.0.17134", "14.14", defaultPackages) |
| 124 |
elif args.msvc_version == "15.8": |
| 125 |
setPackageSelectionMSVC15(args, packages, args.msvc_version, "10.0.17134", "14.15", defaultPackages) |
| 126 |
elif args.msvc_version == "15.9": |
| 127 |
setPackageSelectionMSVC15(args, packages, args.msvc_version, "10.0.17763", "14.16", defaultPackages) |
| 128 |
elif args.msvc_version != None: |
| 129 |
print("Unsupported MSVC toolchain version " + args.msvc_version) |
| 130 |
sys.exit(1) |
| 131 |
|
| 132 |
if len(args.package) == 0: |
| 133 |
args.package = defaultPackages |
| 134 |
|
| 135 |
if args.sdk_version != None: |
| 136 |
for key in packages: |
| 137 |
if key.startswith("win10sdk") or key.startswith("win11sdk"): |
| 138 |
base = key[0:8] |
| 139 |
sdkname = base + "_" + args.sdk_version |
| 140 |
if key == sdkname: |
| 141 |
args.package.append(key) |
| 142 |
else: |
| 143 |
args.ignore.append(key) |
| 144 |
p = packages[key][0] |
| 145 |
|
| 146 |
def lowercaseIgnores(args): |
| 147 |
ignore = [] |
| 148 |
if args.ignore != None: |
| 149 |
for i in args.ignore: |
| 150 |
ignore.append(i.lower()) |
| 151 |
args.ignore = ignore |
| 152 |
|
| 153 |
def getManifest(args): |
| 154 |
if args.manifest == None: |
| 155 |
url = "https://aka.ms/vs/%s/%s/channel" % (args.major, args.type) |
| 156 |
print("Fetching %s" % (url)) |
| 157 |
manifest = simplejson.loads(six.moves.urllib.request.urlopen(url).read()) |
| 158 |
print("Got toplevel manifest for %s" % (manifest["info"]["productDisplayVersion"])) |
| 159 |
for item in manifest["channelItems"]: |
| 160 |
if "type" in item and item["type"] == "Manifest": |
| 161 |
args.manifest = item["payloads"][0]["url"] |
| 162 |
if args.manifest == None: |
| 163 |
print("Unable to find an intaller manifest!") |
| 164 |
sys.exit(1) |
| 165 |
|
| 166 |
if not args.manifest.startswith("http"): |
| 167 |
args.manifest = "file:" + args.manifest |
| 168 |
|
| 169 |
manifestdata = six.moves.urllib.request.urlopen(args.manifest).read() |
| 170 |
manifest = simplejson.loads(manifestdata) |
| 171 |
print("Loaded installer manifest for %s" % (manifest["info"]["productDisplayVersion"])) |
| 172 |
|
| 173 |
if args.save_manifest: |
| 174 |
filename = "%s.manifest" % (manifest["info"]["productDisplayVersion"]) |
| 175 |
if os.path.isfile(filename): |
| 176 |
oldfile = open(filename, "rb").read() |
| 177 |
if oldfile != manifestdata: |
| 178 |
print("Old saved manifest in \"%s\" differs from newly downloaded one, not overwriting!" % (filename)) |
| 179 |
else: |
| 180 |
print("Old saved manifest in \"%s\" is still current" % (filename)) |
| 181 |
else: |
| 182 |
f = open(filename, "wb") |
| 183 |
f.write(manifestdata) |
| 184 |
f.close() |
| 185 |
print("Saved installer manifest to \"%s\"" % (filename)) |
| 186 |
|
| 187 |
return manifest |
| 188 |
|
| 189 |
def prioritizePackage(a, b): |
| 190 |
if "chip" in a and "chip" in b: |
| 191 |
ax64 = a["chip"].lower() == "x64" |
| 192 |
bx64 = b["chip"].lower() == "x64" |
| 193 |
if ax64 and not bx64: |
| 194 |
return -1 |
| 195 |
elif bx64 and not ax64: |
| 196 |
return 1 |
| 197 |
if "language" in a and "language" in b: |
| 198 |
aeng = a["language"].lower().startswith("en-") |
| 199 |
beng = b["language"].lower().startswith("en-") |
| 200 |
if aeng and not beng: |
| 201 |
return -1 |
| 202 |
if beng and not aeng: |
| 203 |
return 1 |
| 204 |
return 0 |
| 205 |
|
| 206 |
def getPackages(manifest): |
| 207 |
packages = {} |
| 208 |
for p in manifest["packages"]: |
| 209 |
id = p["id"].lower() |
| 210 |
if not id in packages: |
| 211 |
packages[id] = [] |
| 212 |
packages[id].append(p) |
| 213 |
for key in packages: |
| 214 |
packages[key] = sorted(packages[key], key=functools.cmp_to_key(prioritizePackage)) |
| 215 |
return packages |
| 216 |
|
| 217 |
def listPackageType(packages, type): |
| 218 |
if type != None: |
| 219 |
type = type.lower() |
| 220 |
ids = [] |
| 221 |
for key in packages: |
| 222 |
p = packages[key][0] |
| 223 |
if type == None: |
| 224 |
ids.append(p["id"]) |
| 225 |
elif "type" in p and p["type"].lower() == type: |
| 226 |
ids.append(p["id"]) |
| 227 |
for id in sorted(ids): |
| 228 |
print(id) |
| 229 |
|
| 230 |
def findPackage(packages, id, chip, warn=True): |
| 231 |
origid = id |
| 232 |
id = id.lower() |
| 233 |
candidates = None |
| 234 |
if not id in packages: |
| 235 |
if warn: |
| 236 |
print("WARNING: %s not found" % (origid)) |
| 237 |
return None |
| 238 |
candidates = packages[id] |
| 239 |
if chip != None: |
| 240 |
chip = chip.lower() |
| 241 |
for a in candidates: |
| 242 |
if "chip" in a and a["chip"].lower() == chip: |
| 243 |
return a |
| 244 |
return candidates[0] |
| 245 |
|
| 246 |
def printDepends(packages, target, deptype, chip, indent, args): |
| 247 |
chipstr = "" |
| 248 |
if chip != None: |
| 249 |
chipstr = " (" + chip + ")" |
| 250 |
deptypestr = "" |
| 251 |
if deptype != "": |
| 252 |
deptypestr = " (" + deptype + ")" |
| 253 |
ignorestr = "" |
| 254 |
ignore = False |
| 255 |
if target.lower() in args.ignore: |
| 256 |
ignorestr = " (Ignored)" |
| 257 |
ignore = True |
| 258 |
print(indent + target + chipstr + deptypestr + ignorestr) |
| 259 |
if deptype == "Optional" and not args.include_optional: |
| 260 |
return |
| 261 |
if deptype == "Recommended" and args.skip_recommended: |
| 262 |
return |
| 263 |
if ignore: |
| 264 |
return |
| 265 |
p = findPackage(packages, target, chip) |
| 266 |
if p == None: |
| 267 |
return |
| 268 |
if "dependencies" in p: |
| 269 |
deps = p["dependencies"] |
| 270 |
for key in deps: |
| 271 |
dep = deps[key] |
| 272 |
type = "" |
| 273 |
if "type" in dep: |
| 274 |
type = dep["type"] |
| 275 |
chip = None |
| 276 |
if "chip" in dep: |
| 277 |
chip = dep["chip"] |
| 278 |
printDepends(packages, key, type, chip, indent + " ", args) |
| 279 |
|
| 280 |
def printReverseDepends(packages, target, deptype, indent, args): |
| 281 |
deptypestr = "" |
| 282 |
if deptype != "": |
| 283 |
deptypestr = " (" + deptype + ")" |
| 284 |
print(indent + target + deptypestr) |
| 285 |
if deptype == "Optional" and not args.include_optional: |
| 286 |
return |
| 287 |
if deptype == "Recommended" and args.skip_recommended: |
| 288 |
return |
| 289 |
target = target.lower() |
| 290 |
for key in packages: |
| 291 |
p = packages[key][0] |
| 292 |
if "dependencies" in p: |
| 293 |
deps = p["dependencies"] |
| 294 |
for k in deps: |
| 295 |
if k.lower() != target: |
| 296 |
continue |
| 297 |
dep = deps[k] |
| 298 |
type = "" |
| 299 |
if "type" in dep: |
| 300 |
type = dep["type"] |
| 301 |
printReverseDepends(packages, p["id"], type, indent + " ", args) |
| 302 |
|
| 303 |
def getPackageKey(p): |
| 304 |
packagekey = p["id"] |
| 305 |
if "version" in p: |
| 306 |
packagekey = packagekey + "-" + p["version"] |
| 307 |
if "chip" in p: |
| 308 |
packagekey = packagekey + "-" + p["chip"] |
| 309 |
return packagekey |
| 310 |
|
| 311 |
def aggregateDepends(packages, included, target, chip, args): |
| 312 |
if target.lower() in args.ignore: |
| 313 |
return [] |
| 314 |
p = findPackage(packages, target, chip) |
| 315 |
if p == None: |
| 316 |
return [] |
| 317 |
packagekey = getPackageKey(p) |
| 318 |
if packagekey in included: |
| 319 |
return [] |
| 320 |
ret = [p] |
| 321 |
included[packagekey] = True |
| 322 |
if "dependencies" in p: |
| 323 |
deps = p["dependencies"] |
| 324 |
for key in deps: |
| 325 |
dep = deps[key] |
| 326 |
if "type" in dep: |
| 327 |
deptype = dep["type"] |
| 328 |
if deptype == "Optional" and not args.include_optional: |
| 329 |
continue |
| 330 |
if deptype == "Recommended" and args.skip_recommended: |
| 331 |
continue |
| 332 |
chip = None |
| 333 |
if "chip" in dep: |
| 334 |
chip = dep["chip"] |
| 335 |
ret.extend(aggregateDepends(packages, included, key, chip, args)) |
| 336 |
return ret |
| 337 |
|
| 338 |
def getSelectedPackages(packages, args): |
| 339 |
ret = [] |
| 340 |
included = {} |
| 341 |
for i in args.package: |
| 342 |
ret.extend(aggregateDepends(packages, included, i, None, args)) |
| 343 |
return ret |
| 344 |
|
| 345 |
def sumInstalledSize(l): |
| 346 |
sum = 0 |
| 347 |
for p in l: |
| 348 |
if "installSizes" in p: |
| 349 |
sizes = p["installSizes"] |
| 350 |
for location in sizes: |
| 351 |
sum = sum + sizes[location] |
| 352 |
return sum |
| 353 |
|
| 354 |
def sumDownloadSize(l): |
| 355 |
sum = 0 |
| 356 |
for p in l: |
| 357 |
if "payloads" in p: |
| 358 |
for payload in p["payloads"]: |
| 359 |
if "size" in payload: |
| 360 |
sum = sum + payload["size"] |
| 361 |
return sum |
| 362 |
|
| 363 |
def formatSize(s): |
| 364 |
if s > 900*1024*1024: |
| 365 |
return "%.1f GB" % (s/(1024*1024*1024)) |
| 366 |
if s > 900*1024: |
| 367 |
return "%.1f MB" % (s/(1024*1024)) |
| 368 |
if s > 1024: |
| 369 |
return "%.1f KB" % (s/1024) |
| 370 |
return "%d bytes" % (s) |
| 371 |
|
| 372 |
def printPackageList(l): |
| 373 |
for p in sorted(l, key=lambda p: p["id"]): |
| 374 |
s = p["id"] |
| 375 |
if "type" in p: |
| 376 |
s = s + " (" + p["type"] + ")" |
| 377 |
if "chip" in p: |
| 378 |
s = s + " (" + p["chip"] + ")" |
| 379 |
if "language" in p: |
| 380 |
s = s + " (" + p["language"] + ")" |
| 381 |
s = s + " " + formatSize(sumInstalledSize([p])) |
| 382 |
print(s) |
| 383 |
|
| 384 |
def makedirs(dir): |
| 385 |
try: |
| 386 |
os.makedirs(dir) |
| 387 |
except OSError: |
| 388 |
pass |
| 389 |
|
| 390 |
def sha256File(file): |
| 391 |
sha256Hash = hashlib.sha256() |
| 392 |
with open(file, "rb") as f: |
| 393 |
for byteBlock in iter(lambda: f.read(4096), b""): |
| 394 |
sha256Hash.update(byteBlock) |
| 395 |
return sha256Hash.hexdigest() |
| 396 |
|
| 397 |
def getPayloadName(payload): |
| 398 |
name = payload["fileName"] |
| 399 |
if "\\" in name: |
| 400 |
name = name.split("\\")[-1] |
| 401 |
if "/" in name: |
| 402 |
name = name.split("/")[-1] |
| 403 |
return name |
| 404 |
|
| 405 |
def downloadPackages(selected, cache, allowHashMismatch = False): |
| 406 |
pool = multiprocessing.Pool(5) |
| 407 |
tasks = [] |
| 408 |
makedirs(cache) |
| 409 |
for p in selected: |
| 410 |
if not "payloads" in p: |
| 411 |
continue |
| 412 |
dir = os.path.join(cache, getPackageKey(p)) |
| 413 |
makedirs(dir) |
| 414 |
for payload in p["payloads"]: |
| 415 |
name = getPayloadName(payload) |
| 416 |
destname = os.path.join(dir, name) |
| 417 |
fileid = os.path.join(getPackageKey(p), name) |
| 418 |
args = (payload, destname, fileid, allowHashMismatch) |
| 419 |
tasks.append(pool.apply_async(_downloadPayload, args)) |
| 420 |
|
| 421 |
downloaded = sum(task.get() for task in tasks) |
| 422 |
pool.close() |
| 423 |
print("Downloaded %s in total" % (formatSize(downloaded))) |
| 424 |
|
| 425 |
def _downloadPayload(payload, destname, fileid, allowHashMismatch): |
| 426 |
attempts = 5 |
| 427 |
for attempt in range(attempts): |
| 428 |
try: |
| 429 |
if os.access(destname, os.F_OK): |
| 430 |
if "sha256" in payload: |
| 431 |
if sha256File(destname).lower() != payload["sha256"].lower(): |
| 432 |
six.print_("Incorrect existing file %s, removing" % (fileid), flush=True) |
| 433 |
os.remove(destname) |
| 434 |
else: |
| 435 |
six.print_("Using existing file %s" % (fileid), flush=True) |
| 436 |
return 0 |
| 437 |
else: |
| 438 |
return 0 |
| 439 |
size = 0 |
| 440 |
if "size" in payload: |
| 441 |
size = payload["size"] |
| 442 |
six.print_("Downloading %s (%s)" % (fileid, formatSize(size)), flush=True) |
| 443 |
six.moves.urllib.request.urlretrieve(payload["url"], destname) |
| 444 |
if "sha256" in payload: |
| 445 |
if sha256File(destname).lower() != payload["sha256"].lower(): |
| 446 |
if allowHashMismatch: |
| 447 |
six.print_("WARNING: Incorrect hash for downloaded file %s" % (fileid), flush=True) |
| 448 |
else: |
| 449 |
raise Exception("Incorrect hash for downloaded file %s, aborting" % fileid) |
| 450 |
return size |
| 451 |
except Exception as e: |
| 452 |
if attempt == attempts - 1: |
| 453 |
raise |
| 454 |
six.print_("%s: %s" % (type(e).__name__, e), flush=True) |
| 455 |
|
| 456 |
def mergeTrees(src, dest): |
| 457 |
if not os.path.isdir(src): |
| 458 |
return |
| 459 |
if not os.path.isdir(dest): |
| 460 |
shutil.move(src, dest) |
| 461 |
return |
| 462 |
names = os.listdir(src) |
| 463 |
destnames = {} |
| 464 |
for n in os.listdir(dest): |
| 465 |
destnames[n.lower()] = n |
| 466 |
for n in names: |
| 467 |
srcname = os.path.join(src, n) |
| 468 |
destname = os.path.join(dest, n) |
| 469 |
if os.path.isdir(srcname): |
| 470 |
if os.path.isdir(destname): |
| 471 |
mergeTrees(srcname, destname) |
| 472 |
elif n.lower() in destnames: |
| 473 |
mergeTrees(srcname, os.path.join(dest, destnames[n.lower()])) |
| 474 |
else: |
| 475 |
shutil.move(srcname, destname) |
| 476 |
else: |
| 477 |
shutil.move(srcname, destname) |
| 478 |
|
| 479 |
def unzipFiltered(zip, dest): |
| 480 |
tmp = os.path.join(dest, "extract") |
| 481 |
for f in zip.infolist(): |
| 482 |
name = six.moves.urllib.parse.unquote(f.filename) |
| 483 |
if "/" in name: |
| 484 |
sep = name.rfind("/") |
| 485 |
dir = os.path.join(dest, name[0:sep]) |
| 486 |
makedirs(dir) |
| 487 |
extracted = zip.extract(f, tmp) |
| 488 |
shutil.move(extracted, os.path.join(dest, name)) |
| 489 |
shutil.rmtree(tmp) |
| 490 |
|
| 491 |
def unpackVsix(file, dest, listing): |
| 492 |
temp = os.path.join(dest, "vsix") |
| 493 |
makedirs(temp) |
| 494 |
with zipfile.ZipFile(file, 'r') as zip: |
| 495 |
unzipFiltered(zip, temp) |
| 496 |
with open(listing, "w") as f: |
| 497 |
for n in zip.namelist(): |
| 498 |
f.write(n + "\n") |
| 499 |
contents = os.path.join(temp, "Contents") |
| 500 |
if os.access(contents, os.F_OK): |
| 501 |
mergeTrees(contents, dest) |
| 502 |
shutil.rmtree(temp) |
| 503 |
|
| 504 |
def unpackWin10SDK(src, payloads, dest): |
| 505 |
# We could try to unpack only the MSIs we need here. |
| 506 |
# Note, this extracts some files into Program Files/..., and some |
| 507 |
# files directly in the root unpack directory. The files we need |
| 508 |
# are under Program Files/... though. |
| 509 |
for payload in payloads: |
| 510 |
name = getPayloadName(payload) |
| 511 |
if name.endswith(".msi"): |
| 512 |
print("Extracting " + name) |
| 513 |
srcfile = os.path.join(src, name) |
| 514 |
if sys.platform == "win32": |
| 515 |
cmd = ["msiexec", "/a", srcfile, "/qn", "TARGETDIR=" + os.path.abspath(dest)] |
| 516 |
else: |
| 517 |
cmd = ["msiextract", "-C", dest, srcfile] |
| 518 |
with open(os.path.join(dest, "WinSDK-" + getPayloadName(payload) + "-listing.txt"), "w") as log: |
| 519 |
subprocess.check_call(cmd, stdout=log) |
| 520 |
|
| 521 |
def extractPackages(selected, cache, dest): |
| 522 |
makedirs(dest) |
| 523 |
for p in selected: |
| 524 |
type = p["type"] |
| 525 |
dir = os.path.join(cache, getPackageKey(p)) |
| 526 |
if type == "Component" or type == "Workload" or type == "Group": |
| 527 |
continue |
| 528 |
if type == "Vsix": |
| 529 |
print("Unpacking " + p["id"]) |
| 530 |
for payload in p["payloads"]: |
| 531 |
unpackVsix(os.path.join(dir, getPayloadName(payload)), dest, os.path.join(dest, getPackageKey(p) + "-listing.txt")) |
| 532 |
elif p["id"].startswith("Win10SDK") or p["id"].startswith("Win11SDK"): |
| 533 |
print("Unpacking " + p["id"]) |
| 534 |
unpackWin10SDK(dir, p["payloads"], dest) |
| 535 |
else: |
| 536 |
print("Skipping unpacking of " + p["id"] + " of type " + type) |
| 537 |
|
| 538 |
def moveVCSDK(unpack, dest): |
| 539 |
# Move the VC and Program Files\Windows Kits\10 directories |
| 540 |
# out from the unpack directory, allowing the rest of unpacked |
| 541 |
# files to be removed. |
| 542 |
makedirs(os.path.join(dest, "kits")) |
| 543 |
mergeTrees(os.path.join(unpack, "VC"), os.path.join(dest, "VC")) |
| 544 |
kitsPath = unpack |
| 545 |
# msiexec extracts to Windows Kits rather than Program Files\Windows Kits |
| 546 |
if sys.platform != "win32": |
| 547 |
kitsPath = os.path.join(kitsPath, "Program Files") |
| 548 |
kitsPath = os.path.join(kitsPath, "Windows Kits", "10") |
| 549 |
mergeTrees(kitsPath, os.path.join(dest, "kits", "10")) |
| 550 |
# The DIA SDK isn't necessary for normal use, but can be used when e.g. |
| 551 |
# compiling LLVM. |
| 552 |
mergeTrees(os.path.join(unpack, "DIA SDK"), os.path.join(dest, "DIA SDK")) |
| 553 |
|
| 554 |
if __name__ == "__main__": |
| 555 |
parser = getArgsParser() |
| 556 |
args = parser.parse_args() |
| 557 |
lowercaseIgnores(args) |
| 558 |
|
| 559 |
socket.setdefaulttimeout(15) |
| 560 |
|
| 561 |
packages = getPackages(getManifest(args)) |
| 562 |
|
| 563 |
if args.print_version: |
| 564 |
sys.exit(0) |
| 565 |
|
| 566 |
if not args.accept_license: |
| 567 |
response = six.moves.input("Do you accept the license at " + findPackage(packages, "Microsoft.VisualStudio.Product.BuildTools", None)["localizedResources"][0]["license"] + " (yes/no)? ") |
| 568 |
while response != "yes" and response != "no": |
| 569 |
response = six.moves.input("Do you accept the license? Answer \"yes\" or \"no\": ") |
| 570 |
if response == "no": |
| 571 |
sys.exit(0) |
| 572 |
|
| 573 |
setPackageSelection(args, packages) |
| 574 |
|
| 575 |
if args.list_components or args.list_workloads or args.list_packages: |
| 576 |
if args.list_components: |
| 577 |
listPackageType(packages, "Component") |
| 578 |
if args.list_workloads: |
| 579 |
listPackageType(packages, "Workload") |
| 580 |
if args.list_packages: |
| 581 |
listPackageType(packages, None) |
| 582 |
sys.exit(0) |
| 583 |
|
| 584 |
if args.print_deps_tree: |
| 585 |
for i in args.package: |
| 586 |
printDepends(packages, i, "", None, "", args) |
| 587 |
sys.exit(0) |
| 588 |
|
| 589 |
if args.print_reverse_deps: |
| 590 |
for i in args.package: |
| 591 |
printReverseDepends(packages, i, "", "", args) |
| 592 |
sys.exit(0) |
| 593 |
|
| 594 |
selected = getSelectedPackages(packages, args) |
| 595 |
|
| 596 |
if args.print_selection: |
| 597 |
printPackageList(selected) |
| 598 |
|
| 599 |
print("Selected %d packages, for a total download size of %s, install size of %s" % (len(selected), formatSize(sumDownloadSize(selected)), formatSize(sumInstalledSize(selected)))) |
| 600 |
|
| 601 |
if args.print_selection: |
| 602 |
sys.exit(0) |
| 603 |
|
| 604 |
tempcache = None |
| 605 |
if args.cache != None: |
| 606 |
cache = os.path.abspath(args.cache) |
| 607 |
else: |
| 608 |
cache = tempfile.mkdtemp(prefix="vsinstall-") |
| 609 |
tempcache = cache |
| 610 |
|
| 611 |
if not args.only_download and args.dest == None: |
| 612 |
print("No destination directory set!") |
| 613 |
sys.exit(1) |
| 614 |
|
| 615 |
try: |
| 616 |
downloadPackages(selected, cache, allowHashMismatch=args.only_download) |
| 617 |
if args.only_download: |
| 618 |
sys.exit(0) |
| 619 |
|
| 620 |
dest = os.path.abspath(args.dest) |
| 621 |
|
| 622 |
if args.only_unpack: |
| 623 |
unpack = dest |
| 624 |
else: |
| 625 |
unpack = os.path.join(dest, "unpack") |
| 626 |
|
| 627 |
extractPackages(selected, cache, unpack) |
| 628 |
|
| 629 |
if not args.only_unpack: |
| 630 |
moveVCSDK(unpack, dest) |
| 631 |
if not args.keep_unpack: |
| 632 |
shutil.rmtree(unpack) |
| 633 |
finally: |
| 634 |
if tempcache != None: |
| 635 |
shutil.rmtree(tempcache) |