Download retries
This commit is contained in:
parent
7c9d826998
commit
73fec6753f
1 changed files with 9 additions and 5 deletions
|
|
@ -76,7 +76,7 @@ proc getNimteropCacheDir(): string =
|
||||||
result = getNimcacheDir() / "nimterop"
|
result = getNimcacheDir() / "nimterop"
|
||||||
|
|
||||||
proc execAction*(cmd: string, retry = 0, die = true, cache = false,
|
proc execAction*(cmd: string, retry = 0, die = true, cache = false,
|
||||||
cacheKey = ""): tuple[output: string, ret: int] =
|
cacheKey = "", onRetry: proc() = nil): tuple[output: string, ret: int] =
|
||||||
## Execute an external command - supported at compile time
|
## Execute an external command - supported at compile time
|
||||||
##
|
##
|
||||||
## Checks if command exits successfully before returning. If not, an
|
## Checks if command exits successfully before returning. If not, an
|
||||||
|
|
@ -129,7 +129,9 @@ proc execAction*(cmd: string, retry = 0, die = true, cache = false,
|
||||||
# On failure, retry or die as requested
|
# On failure, retry or die as requested
|
||||||
if result.ret != 0:
|
if result.ret != 0:
|
||||||
if retry > 0:
|
if retry > 0:
|
||||||
sleep(1000)
|
if not onRetry.isNil:
|
||||||
|
onRetry()
|
||||||
|
sleep(500)
|
||||||
result = execAction(cmd, retry = retry - 1, die, cache, cacheKey)
|
result = execAction(cmd, retry = retry - 1, die, cache, cacheKey)
|
||||||
elif die:
|
elif die:
|
||||||
doAssert false, "Command failed: " & $result.ret & "\ncmd: " & ccmd &
|
doAssert false, "Command failed: " & $result.ret & "\ncmd: " & ccmd &
|
||||||
|
|
@ -344,16 +346,17 @@ proc extractTar*(tarfile, outdir: string, quiet = false) =
|
||||||
if name.len != 0:
|
if name.len != 0:
|
||||||
rmFile(outdir / name)
|
rmFile(outdir / name)
|
||||||
|
|
||||||
proc downloadUrl*(url, outdir: string, quiet = false) =
|
proc downloadUrl*(url, outdir: string, quiet = false, retry = 1) =
|
||||||
## Download a file using `curl` or `wget` (or `powershell` on Windows) to the specified directory
|
## Download a file using `curl` or `wget` (or `powershell` on Windows) to the specified directory
|
||||||
##
|
##
|
||||||
## If an archive file, it is automatically extracted after download.
|
## If an archive file, it is automatically extracted after download.
|
||||||
let
|
let
|
||||||
file = url.extractFilename()
|
file = url.extractFilename()
|
||||||
|
filePath = outdir / file
|
||||||
ext = file.splitFile().ext.toLowerAscii()
|
ext = file.splitFile().ext.toLowerAscii()
|
||||||
archives = @[".zip", ".xz", ".gz", ".bz2", ".tgz", ".tar"]
|
archives = @[".zip", ".xz", ".gz", ".bz2", ".tgz", ".tar"]
|
||||||
|
|
||||||
if not (ext in archives and fileExists(outdir/file)):
|
if not (ext in archives and fileExists(filePath)):
|
||||||
if not quiet:
|
if not quiet:
|
||||||
echo "# Downloading " & file
|
echo "# Downloading " & file
|
||||||
mkDir(outdir)
|
mkDir(outdir)
|
||||||
|
|
@ -368,7 +371,8 @@ proc downloadUrl*(url, outdir: string, quiet = false) =
|
||||||
cmd = "powershell [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; wget $# -OutFile $#"
|
cmd = "powershell [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; wget $# -OutFile $#"
|
||||||
else:
|
else:
|
||||||
doAssert false, "No download tool available - curl, wget"
|
doAssert false, "No download tool available - curl, wget"
|
||||||
discard execAction(cmd % [url.quoteShell, (outdir/file).sanitizePath], retry = 3)
|
discard execAction(cmd % [url.quoteShell, (filePath).sanitizePath], retry = 3,
|
||||||
|
onRetry = proc() = rmFile(filePath))
|
||||||
|
|
||||||
if ext == ".zip":
|
if ext == ".zip":
|
||||||
extractZip(file, outdir, quiet)
|
extractZip(file, outdir, quiet)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue