env.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package python
  2. import (
  3. _ "embed"
  4. "os"
  5. "os/exec"
  6. "path"
  7. "github.com/langgenius/dify-sandbox/internal/core/runner"
  8. "github.com/langgenius/dify-sandbox/internal/static"
  9. "github.com/langgenius/dify-sandbox/internal/utils/log"
  10. )
  11. //go:embed env.sh
  12. var env_script string
  13. func PreparePythonDependenciesEnv() error {
  14. config := static.GetDifySandboxGlobalConfigurations()
  15. runner := runner.TempDirRunner{}
  16. err := runner.WithTempDir("/", []string{}, func(root_path string) error {
  17. err := os.WriteFile(path.Join(root_path, "env.sh"), []byte(env_script), 0755)
  18. if err != nil {
  19. return err
  20. }
  21. for _, lib_path := range config.PythonLibPaths {
  22. // check if the lib path is available
  23. if _, err := os.Stat(lib_path); err != nil {
  24. log.Warn("python lib path %s is not available", lib_path)
  25. continue
  26. }
  27. exec_cmd := exec.Command(
  28. "bash",
  29. path.Join(root_path, "env.sh"),
  30. lib_path,
  31. LIB_PATH,
  32. )
  33. exec_cmd.Stderr = os.Stderr
  34. if err := exec_cmd.Run(); err != nil {
  35. return err
  36. }
  37. }
  38. os.RemoveAll(root_path)
  39. os.Remove(root_path)
  40. return nil
  41. })
  42. return err
  43. }