assets.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package gpu
  2. import (
  3. "fmt"
  4. "log/slog"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "strings"
  9. "github.com/jmorganca/ollama/version"
  10. )
  11. func AssetsDir() (string, error) {
  12. home, err := os.UserHomeDir()
  13. if err != nil {
  14. return "", err
  15. }
  16. baseDir := filepath.Join(home, ".ollama", "assets")
  17. libDirs, err := os.ReadDir(baseDir)
  18. if err == nil {
  19. for _, d := range libDirs {
  20. if d.Name() == version.Version {
  21. continue
  22. }
  23. // Special case the rocm dependencies, which are handled by the installer
  24. if d.Name() == "rocm" {
  25. continue
  26. }
  27. slog.Debug("stale lib detected, cleaning up " + d.Name())
  28. err = os.RemoveAll(filepath.Join(baseDir, d.Name()))
  29. if err != nil {
  30. slog.Warn(fmt.Sprintf("unable to clean up stale library %s: %s", filepath.Join(baseDir, d.Name()), err))
  31. }
  32. }
  33. }
  34. return filepath.Join(baseDir, version.Version), nil
  35. }
  36. func UpdatePath(dir string) {
  37. if runtime.GOOS == "windows" {
  38. tmpDir := filepath.Dir(dir)
  39. pathComponents := strings.Split(os.Getenv("PATH"), ";")
  40. i := 0
  41. for _, comp := range pathComponents {
  42. if strings.EqualFold(comp, dir) {
  43. return
  44. }
  45. // Remove any other prior paths to our temp dir
  46. if !strings.HasPrefix(strings.ToLower(comp), strings.ToLower(tmpDir)) {
  47. pathComponents[i] = comp
  48. i++
  49. }
  50. }
  51. newPath := strings.Join(append([]string{dir}, pathComponents...), ";")
  52. slog.Info(fmt.Sprintf("Updating PATH to %s", newPath))
  53. os.Setenv("PATH", newPath)
  54. }
  55. // linux and darwin rely on rpath
  56. }