setup.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package python
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path"
  8. "regexp"
  9. "strings"
  10. "github.com/langgenius/dify-sandbox/internal/core/runner"
  11. python_dependencies "github.com/langgenius/dify-sandbox/internal/core/runner/python/dependencies"
  12. "github.com/langgenius/dify-sandbox/internal/core/runner/types"
  13. "github.com/langgenius/dify-sandbox/internal/utils/log"
  14. )
  15. //go:embed python.so
  16. var python_lib []byte
  17. const (
  18. LIB_PATH = "/var/sandbox/sandbox-python"
  19. LIB_NAME = "python.so"
  20. )
  21. func init() {
  22. releaseLibBinary()
  23. }
  24. func releaseLibBinary() {
  25. log.Info("initializing python runner environment...")
  26. os.RemoveAll(LIB_PATH)
  27. os.Remove(LIB_PATH)
  28. err := os.MkdirAll(LIB_PATH, 0755)
  29. if err != nil {
  30. log.Panic(fmt.Sprintf("failed to create %s", LIB_PATH))
  31. }
  32. err = os.WriteFile(path.Join(LIB_PATH, LIB_NAME), python_lib, 0755)
  33. if err != nil {
  34. log.Panic(fmt.Sprintf("failed to write %s", path.Join(LIB_PATH, LIB_NAME)))
  35. }
  36. log.Info("python runner environment initialized")
  37. }
  38. func checkLibAvaliable() bool {
  39. if _, err := os.Stat(path.Join(LIB_PATH, LIB_NAME)); err != nil {
  40. return false
  41. }
  42. return true
  43. }
  44. func ExtractOnelineDepency(dependency string) (string, string) {
  45. delimiters := []string{"==", ">=", "<=", "~="}
  46. for _, delimiter := range delimiters {
  47. if strings.Contains(dependency, delimiter) {
  48. parts := strings.Split(dependency, delimiter)
  49. if len(parts) >= 2 {
  50. return parts[0], parts[1]
  51. } else if len(parts) == 1 {
  52. return parts[0], ""
  53. } else if len(parts) == 0 {
  54. return "", ""
  55. }
  56. }
  57. }
  58. preg := regexp.MustCompile(`([a-zA-Z0-9_-]+)`)
  59. if preg.MatchString(dependency) {
  60. return dependency, ""
  61. }
  62. return "", ""
  63. }
  64. func InstallDependencies(requirements string) error {
  65. if requirements == "" {
  66. return nil
  67. }
  68. runner := runner.TempDirRunner{}
  69. return runner.WithTempDir([]string{}, func(root_path string) error {
  70. defer os.Remove(root_path)
  71. defer os.RemoveAll(root_path)
  72. // create a requirements file
  73. err := os.WriteFile(path.Join(root_path, "requirements.txt"), []byte(requirements), 0644)
  74. if err != nil {
  75. log.Error("failed to create requirements.txt")
  76. return nil
  77. }
  78. // install dependencies
  79. cmd := exec.Command("pip3", "install", "-r", "requirements.txt")
  80. reader, err := cmd.StdoutPipe()
  81. if err != nil {
  82. log.Panic("failed to get stdout pipe of pip3")
  83. }
  84. defer reader.Close()
  85. err = cmd.Start()
  86. if err != nil {
  87. log.Error("failed to start pip3")
  88. return nil
  89. }
  90. for {
  91. buf := make([]byte, 1024)
  92. n, err := reader.Read(buf)
  93. if err != nil {
  94. break
  95. }
  96. log.Info(string(buf[:n]))
  97. }
  98. status := cmd.Wait()
  99. if status != nil {
  100. log.Error("failed to install dependencies")
  101. return nil
  102. }
  103. // split the requirements
  104. requirements = strings.ReplaceAll(requirements, "\r\n", "\n")
  105. requirements = strings.ReplaceAll(requirements, "\r", "\n")
  106. lines := strings.Split(requirements, "\n")
  107. for _, line := range lines {
  108. package_name, version := ExtractOnelineDepency(line)
  109. if package_name == "" {
  110. continue
  111. }
  112. python_dependencies.SetupDependency(package_name, version, fmt.Sprintf("import %s", package_name))
  113. log.Info("Python dependency installed: %s %s", package_name, version)
  114. }
  115. return nil
  116. })
  117. }
  118. func ListDependencies() []types.Dependency {
  119. return python_dependencies.ListDependencies()
  120. }