2
0

pip_install.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import os
  2. import subprocess
  3. def write_log(output, name, type):
  4. script_dir = os.path.dirname((__file__))
  5. with open(
  6. os.path.join(script_dir, "..", "..", "logs", f"{type}-{name}.log"), "w"
  7. ) as outfile:
  8. outfile.write("\n".join(output))
  9. def pip_install_wrapper(requirements, name):
  10. def fn():
  11. output = []
  12. for line in _pip_install(requirements, name):
  13. output.append(line)
  14. yield "<br />".join(output)
  15. write_log(output, name, type="pip-install")
  16. return fn
  17. def pip_uninstall_wrapper(package_name, name):
  18. def fn():
  19. output = []
  20. for line in _pip_uninstall(package_name, name):
  21. output.append(line)
  22. yield "<br />".join(output)
  23. write_log(output, name, type="pip-uninstall")
  24. return fn
  25. def _pip_install(requirements, name):
  26. # process = subprocess.Popen(
  27. # f"uv pip install {requirements}",
  28. # shell=True,
  29. # stdout=subprocess.PIPE,
  30. # stderr=subprocess.STDOUT,
  31. # universal_newlines=True,
  32. # )
  33. # # Stream the output to the console
  34. # for line in process.stdout: # type: ignore
  35. # print(line, end="")
  36. # yield line
  37. # # Wait for the process to finish
  38. # process.wait()
  39. # # Check if the process was successful
  40. # if process.returncode == 0:
  41. # print(f"Successfully installed {name}")
  42. # yield f"Successfully installed {name}, please restart the webui"
  43. # else:
  44. # print(f"Failed to install {name}")
  45. # yield f"Failed to install {name}"
  46. try:
  47. print(f"Installing {name} dependencies...")
  48. yield from pip_shell(f"pip install {requirements}")
  49. # yield from pip_shell(f"uv pip install {requirements}")
  50. print(f"Successfully installed {name} dependencies")
  51. yield f"Successfully installed {name} dependencies"
  52. except Exception:
  53. print(f"Failed to install {name} dependencies")
  54. yield f"Failed to install {name} dependencies"
  55. def _pip_uninstall(package_name, name):
  56. try:
  57. print(f"Uninstalling {name} ({package_name})...")
  58. yield from pip_shell(f"pip uninstall -y {package_name}")
  59. # yield from pip_shell(f"uv pip uninstall {package_name}")
  60. print(f"Successfully uninstalled {name} ({package_name})")
  61. yield f"Successfully uninstalled {name} ({package_name})"
  62. except Exception:
  63. print(f"Failed to uninstall {name} ({package_name})")
  64. yield f"Failed to uninstall {name} ({package_name})"
  65. def pip_shell(command):
  66. process = subprocess.Popen(
  67. command,
  68. shell=True,
  69. stdout=subprocess.PIPE,
  70. stderr=subprocess.STDOUT,
  71. universal_newlines=True,
  72. )
  73. # Stream the output to the console
  74. for line in process.stdout: # type: ignore
  75. print(line, end="")
  76. yield line
  77. # Wait for the process to finish
  78. process.wait()
  79. if process.returncode != 0:
  80. raise Exception(f"Failed to run {command}")