From f37f0b0aedacd0be3cf087778836da31e7b16e18 Mon Sep 17 00:00:00 2001 From: HugoGranstrom <5092565+HugoGranstrom@users.noreply.github.com> Date: Tue, 11 May 2021 11:35:06 +0200 Subject: [PATCH] make downloading work on Windows. Unzipping doesn't work though --- install/torch_installer.nim | 49 ++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/install/torch_installer.nim b/install/torch_installer.nim index ed95980..66b4326 100644 --- a/install/torch_installer.nim +++ b/install/torch_installer.nim @@ -8,8 +8,11 @@ import std/[asyncdispatch, httpclient, strformat, strutils, os], + #zippy/ziparchives, zip/zipfiles +{.passl: "-lz".} + type Acceleration* = enum Cpu = "cpu" @@ -26,17 +29,17 @@ type proc getProjectDir(): string {.compileTime.} = currentSourcePath.rsplit(DirSep, 1)[0] -proc onProgressChanged(total, progress, speed: BiggestInt) {.async.} = +proc onProgressChanged(total, progress, speed: BiggestInt) = echo &"Downloaded {progress} of {total}" echo &"Current rate: {speed.float64 / (1000*1000):4.3f} MiBi/s" # TODO the unit is neither MB or Mb or MiBi ??? -proc downloadTo(url, targetDir, filename: string) {.async.} = - var client = newAsyncHttpClient() +proc downloadTo(url, targetDir, filename: string) = + var client = newHttpClient() defer: client.close() client.onProgressChanged = onProgressChanged echo "Starting download of \"", url, '\"' echo "Storing temporary into: \"", targetDir, '\"' - await client.downloadFile(url, targetDir / filename) + client.downloadFile(url, targetDir / filename) proc getUrlAndFilename(version = "1.8.1", accel = Cuda111, abi = Cpp11): tuple[url, filename: string] = result.filename = "libtorch-" @@ -47,8 +50,11 @@ proc getUrlAndFilename(version = "1.8.1", accel = Cuda111, abi = Cpp11): tuple[u result.filename &= &"%2B{accel}" result.filename &= ".zip" elif defined(windows): + let abi = Cpp doAssert abi == Cpp, "LibTorch for Windows does not support the C++11 ABI" - result.filename &= &"-win-{abi}-{version}.zip" + result.filename &= &"win-{abi}-{version}" + result.filename &= &"%2B{accel}" + result.filename &= ".zip" elif defined(osx): doAssert accel == Cpu, "LibTorch for MacOS does not support GPU acceleration" result.filename &= &"macos-{version}.zip" @@ -56,7 +62,38 @@ proc getUrlAndFilename(version = "1.8.1", accel = Cuda111, abi = Cpp11): tuple[u result.url = &"https://download.pytorch.org/libtorch/{accel}/{result.filename}" proc downloadLibTorch(url, targetDir, filename: string) = - waitFor url.downloadTo(targetDir, filename) + if not fileExists(targetDir / "libtorch.zip"): + url.downloadTo(targetDir, "libtorch.zip") + else: + echo "File is already downloaded" + +#[ proc uncompress(targetDir, filename: string, delete = false) = + let tmpZip = targetDir / filename + var tmpDir = getTempDir() / "libtorch_temp_download" + var i = 0 + while dirExists(tmpDir & $i): + inc(i) + tmpDir = tmpDir & $i + echo "Tempdir: ", tmpDir + let folderName = filename[0 ..< ^4] # remove .zip + let targetDir = targetDir# / "test" + #removeDir(tmpDir) + #createDir(tmpDir) + echo "Decompressing \"", tmpZip, "\" and storing into \"", targetDir, "\"" + extractAll(tmpZip, tmpDir) + echo "Extraction done! Now copying from temp" + removeDir(targetDir / "libtorch") # remove old install + echo "Removed old folder" + copyDir(tmpDir, targetDir) + echo "Copy done!" + removeDir(tmpDir) + echo "Done." + + if delete: + echo "Deleting \"", tmpZip, "\"" + removeFile(tmpZip) + else: + echo "Not deleting \"", tmpZip, "\"" ]# proc uncompress(targetDir, filename: string, delete = false) = var z: ZipArchive