setup.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/static"
  11. "github.com/langgenius/dify-sandbox/internal/core/runner"
  12. python_dependencies "github.com/langgenius/dify-sandbox/internal/core/runner/python/dependencies"
  13. "github.com/langgenius/dify-sandbox/internal/core/runner/types"
  14. "github.com/langgenius/dify-sandbox/internal/utils/log"
  15. )
  16. //go:embed python.so
  17. var python_lib []byte
  18. const (
  19. LIB_PATH = "/var/sandbox/sandbox-python"
  20. LIB_NAME = "python.so"
  21. )
  22. func init() {
  23. releaseLibBinary()
  24. }
  25. func releaseLibBinary() {
  26. log.Info("initializing python runner environment...")
  27. // remove the old lib
  28. if _, err := os.Stat(path.Join(LIB_PATH, LIB_NAME)); err == nil {
  29. err := os.Remove(path.Join(LIB_PATH, LIB_NAME))
  30. if err != nil {
  31. log.Panic(fmt.Sprintf("failed to remove %s", path.Join(LIB_PATH, LIB_NAME)))
  32. }
  33. }
  34. err := os.MkdirAll(LIB_PATH, 0755)
  35. if err != nil {
  36. log.Panic(fmt.Sprintf("failed to create %s", LIB_PATH))
  37. }
  38. err = os.WriteFile(path.Join(LIB_PATH, LIB_NAME), python_lib, 0755)
  39. if err != nil {
  40. log.Panic(fmt.Sprintf("failed to write %s", path.Join(LIB_PATH, LIB_NAME)))
  41. }
  42. log.Info("python runner environment initialized")
  43. }
  44. func checkLibAvaliable() bool {
  45. if _, err := os.Stat(path.Join(LIB_PATH, LIB_NAME)); err != nil {
  46. return false
  47. }
  48. return true
  49. }
  50. func ExtractOnelineDepency(dependency string) (string, string) {
  51. delimiters := []string{"==", ">=", "<=", "~="}
  52. for _, delimiter := range delimiters {
  53. if strings.Contains(dependency, delimiter) {
  54. parts := strings.Split(dependency, delimiter)
  55. if len(parts) >= 2 {
  56. return parts[0], parts[1]
  57. } else if len(parts) == 1 {
  58. return parts[0], ""
  59. } else if len(parts) == 0 {
  60. return "", ""
  61. }
  62. }
  63. }
  64. preg := regexp.MustCompile(`([a-zA-Z0-9_-]+)`)
  65. if preg.MatchString(dependency) {
  66. return dependency, ""
  67. }
  68. return "", ""
  69. }
  70. func InstallDependencies(requirements string) error {
  71. if requirements == "" {
  72. return nil
  73. }
  74. runner := runner.TempDirRunner{}
  75. return runner.WithTempDir("/", []string{}, func(root_path string) error {
  76. defer os.Remove(root_path)
  77. defer os.RemoveAll(root_path)
  78. // create a requirements file
  79. err := os.WriteFile(path.Join(root_path, "requirements.txt"), []byte(requirements), 0644)
  80. if err != nil {
  81. log.Error("failed to create requirements.txt")
  82. return nil
  83. }
  84. // install dependencies
  85. pipMirrorURL := static.GetDifySandboxGlobalConfigurations().PythonPipMirrorURL
  86. // Create the base command
  87. args := []string{"install", "-r", "requirements.txt"}
  88. if pipMirrorURL != "" {
  89. // If a mirror URL is provided, include it in the command arguments
  90. args = append(args, "-i", pipMirrorURL)
  91. }
  92. cmd := exec.Command("pip3", args...)
  93. reader, err := cmd.StdoutPipe()
  94. if err != nil {
  95. log.Panic("failed to get stdout pipe of pip3")
  96. }
  97. defer reader.Close()
  98. err = cmd.Start()
  99. if err != nil {
  100. log.Error("failed to start pip3")
  101. return nil
  102. }
  103. for {
  104. buf := make([]byte, 1024)
  105. n, err := reader.Read(buf)
  106. if err != nil {
  107. break
  108. }
  109. log.Info(string(buf[:n]))
  110. }
  111. status := cmd.Wait()
  112. if status != nil {
  113. log.Error("failed to install dependencies")
  114. return nil
  115. }
  116. // split the requirements
  117. requirements = strings.ReplaceAll(requirements, "\r\n", "\n")
  118. requirements = strings.ReplaceAll(requirements, "\r", "\n")
  119. lines := strings.Split(requirements, "\n")
  120. for _, line := range lines {
  121. package_name, version := ExtractOnelineDepency(line)
  122. if package_name == "" {
  123. continue
  124. }
  125. python_dependencies.SetupDependency(package_name, version)
  126. log.Info("Python dependency installed: %s %s", package_name, version)
  127. }
  128. return nil
  129. })
  130. }
  131. func ListDependencies() []types.Dependency {
  132. return python_dependencies.ListDependencies()
  133. }
  134. func RefreshDependencies() []types.Dependency {
  135. log.Info("updating python dependencies...")
  136. dependencies := static.GetRunnerDependencies()
  137. err := InstallDependencies(dependencies.PythonRequirements)
  138. if err != nil {
  139. log.Error("failed to update python dependencies: %v", err)
  140. }
  141. log.Info("python dependencies updated")
  142. return python_dependencies.ListDependencies()
  143. }