Module: LibTorchDownloader

Defined in:
lib/rubynetic/libtorch_downloader.rb

Constant Summary collapse

BASE_URL =
"https://download.pytorch.org/libtorch"

Class Method Summary collapse

Class Method Details

.download_and_install(os, version = "2.1.0", variant = "cpu") ⇒ Object



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
# File 'lib/rubynetic/libtorch_downloader.rb', line 8

def self.download_and_install(os, version = "2.1.0", variant = "cpu")
  url = case os
        when "macos"
          "#{BASE_URL}/#{variant}/libtorch-macos-#{version}.zip"
        when "linux"
          "#{BASE_URL}/#{variant}/libtorch-cxx11-abi-shared-with-deps-#{version}+#{variant}.zip"
        when "windows"
          "#{BASE_URL}/#{variant}/libtorch-win-shared-with-deps-#{version}.zip"
        else
          raise "❌ OS not supported: #{os}"
        end

  install_path = File.expand_path("~/libtorch")  # 📌 Основная директория установки
  temp_path = Dir.mktmpdir("libtorch_")  # 🟢 Временная директория для распаковки
  zip_file = File.join(Dir.tmpdir, "libtorch.zip")

  puts "📥 Downloading LibTorch #{version} (#{variant}) for #{os}..."
  URI.open(url) do |remote_file|
    File.open(zip_file, "wb") { |file| file.write(remote_file.read) }
  end

  puts "📦 Extracting to temp folder: #{temp_path}..."
  FileUtils.mkdir_p(temp_path)

  unless system("which unzip > /dev/null")
    raise "❌ 'unzip' command not found. Please install unzip."
  end

  system("unzip -o #{zip_file} -d #{temp_path}")

  # 📌 Определяем путь, где оказались файлы после извлечения
  extracted_dir = "#{temp_path}/libtorch"
  puts "🔍 Extracted files are in: #{extracted_dir}"

  # 📌 Исправляем структуру директорий, если файлы оказались в `libtorch/libtorch`
  if Dir.exist?("#{extracted_dir}/libtorch")
    puts "🔄 Fixing extraction structure: Moving inner libtorch/* to root"
    extracted_dir = "#{extracted_dir}/libtorch"
  end

  # 📌 Убедимся, что `install_path` существует
  FileUtils.mkdir_p(install_path)

  # 📌 Переносим файлы, но только если они существуют
  Dir.glob("#{extracted_dir}/*").each do |file|
    dest = File.join(install_path, File.basename(file))
    if File.exist?(file) || File.directory?(file)
      puts "📂 Moving: #{file} -> #{dest}"
      FileUtils.mv(file, dest)
    else
      puts "⚠️ Warning: File or directory does not exist - #{file}"
    end
  end

  puts "🛠 Setting up environment..."
  ENV["LIBTORCH_INSTALL_PATH"] = install_path

  case os
  when "macos"
    system("echo 'export LIBTORCH_INSTALL_PATH=#{install_path}' >> ~/.zshrc")
    system("echo 'export DYLD_LIBRARY_PATH=#{install_path}/lib:$DYLD_LIBRARY_PATH' >> ~/.zshrc")
  when "linux"
    system("echo 'export LIBTORCH_INSTALL_PATH=#{install_path}' >> ~/.bashrc")
    system("echo 'export LD_LIBRARY_PATH=#{install_path}/lib:$LD_LIBRARY_PATH' >> ~/.bashrc")
  when "windows"
    system("setx PATH \"#{install_path}\\lib;%PATH%\"")
  end

  puts "✅ Installation complete! LibTorch accessible at: #{install_path}"

  # Очистка временных файлов
  FileUtils.rm_rf(temp_path)
  File.delete(zip_file) if File.exist?(zip_file)
end