dependency.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """Building the app and initializing all prerequisites."""
  2. from __future__ import annotations
  3. import os
  4. import re
  5. import subprocess
  6. import sys
  7. from reflex import constants
  8. from reflex.utils import console
  9. def generate_requirements():
  10. """Generate a requirements.txt file based on the current environment."""
  11. # Run the command and get the output
  12. result = subprocess.run(
  13. [sys.executable, "-m", "pipdeptree", "--warn", "silence"],
  14. capture_output=True,
  15. text=True,
  16. )
  17. # Filter the output lines using a regular expression
  18. lines = result.stdout.split("\n")
  19. filtered_lines = [line for line in lines if re.match(r"^\w+", line)]
  20. # Write the filtered lines to requirements.txt
  21. with open("requirements.txt", "w") as f:
  22. for line in filtered_lines:
  23. f.write(line + "\n")
  24. def check_requirements():
  25. """Check if the requirements are installed."""
  26. if not os.path.exists(constants.RequirementsTxt.FILE):
  27. console.warn("It seems like there's no requirements.txt in your project.")
  28. response = console.ask(
  29. "Would you like us to auto-generate one based on your current environment?",
  30. choices=["y", "n"],
  31. )
  32. if response == "y":
  33. generate_requirements()
  34. else:
  35. console.error(
  36. "Please create a requirements.txt file in your project's root directory and try again."
  37. )
  38. exit()