MCPcopy
hub / github.com/saltstack/salt / download_file

Function download_file

tools/utils/__init__.py:361–408  ·  view source on GitHub ↗
(
    ctx: Context,
    url: str,
    dest: pathlib.Path,
    auth: tuple[str, str] | None = None,
    headers: dict[str, str] | None = None,
)

Source from the content-addressed store, hash-verified

359
360
361def download_file(
362 ctx: Context,
363 url: str,
364 dest: pathlib.Path,
365 auth: tuple[str, str] | None = None,
366 headers: dict[str, str] | None = None,
367) -> pathlib.Path:
368 ctx.info(f"Downloading {dest.name!r} @ {url} ...")
369 if sys.platform == "win32":
370 # We don't want to use curl on Windows, it doesn't work
371 curl = None
372 else:
373 curl = shutil.which("curl")
374 if curl is not None:
375 command = [curl, "-sS", "-L"]
376 if headers:
377 for key, value in headers.items():
378 command.extend(["-H", f"{key}: {value}"])
379 command.extend(["-o", str(dest), url])
380 ret = ctx.run(*command)
381 if ret.returncode:
382 ctx.error(f"Failed to download {url}")
383 ctx.exit(1)
384 return dest
385 wget = shutil.which("wget")
386 if wget is not None:
387 with ctx.chdir(dest.parent):
388 command = [wget, "--no-verbose"]
389 if headers:
390 for key, value in headers.items():
391 command.append(f"--header={key}: {value}")
392 command.append(url)
393 ret = ctx.run(*command)
394 if ret.returncode:
395 ctx.error(f"Failed to download {url}")
396 ctx.exit(1)
397 return dest
398 # NOTE the stream=True parameter below
399 with ctx.web as web:
400 if headers:
401 web.headers.update(headers)
402 with web.get(url, stream=True, auth=auth) as r:
403 r.raise_for_status()
404 with dest.open("wb") as f:
405 for chunk in r.iter_content(chunk_size=8192):
406 if chunk:
407 f.write(chunk)
408 return dest
409
410
411def get_platform_and_arch_from_slug(slug: str) -> tuple[str, str]:

Callers

nothing calls this directly

Calls 11

exitMethod · 0.80
infoMethod · 0.45
itemsMethod · 0.45
extendMethod · 0.45
runMethod · 0.45
errorMethod · 0.45
appendMethod · 0.45
updateMethod · 0.45
getMethod · 0.45
openMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected