prerequisites.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. """Everything related to fetching or initializing build prerequisites."""
  2. from __future__ import annotations
  3. import json
  4. import os
  5. import platform
  6. import subprocess
  7. import sys
  8. from pathlib import Path
  9. from types import ModuleType
  10. from typing import Optional
  11. import typer
  12. from redis import Redis
  13. from pynecone import constants
  14. from pynecone.config import get_config
  15. from pynecone.utils import console, path_ops
  16. def check_node_version(min_version):
  17. """Check the version of Node.js.
  18. Args:
  19. min_version: The minimum version of Node.js required.
  20. Returns:
  21. Whether the version of Node.js is high enough.
  22. """
  23. try:
  24. # Run the node -v command and capture the output
  25. result = subprocess.run(
  26. ["node", "-v"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
  27. )
  28. # The output will be in the form "vX.Y.Z", so we can split it on the "v" character and take the second part
  29. version = result.stdout.decode().strip().split("v")[1]
  30. # Compare the version numbers
  31. return version.split(".") >= min_version.split(".")
  32. except Exception:
  33. return False
  34. def get_bun_version() -> str:
  35. """Get the version of bun.
  36. Returns:
  37. The version of bun.
  38. Raises:
  39. FileNotFoundError: If bun is not installed.
  40. """
  41. try:
  42. # Run the bun -v command and capture the output
  43. result = subprocess.run(
  44. ["bun", "-v"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
  45. )
  46. version = result.stdout.decode().strip()
  47. return version
  48. except Exception:
  49. raise FileNotFoundError("Pynecone requires bun to be installed.") from None
  50. def get_package_manager() -> str:
  51. """Get the package manager executable.
  52. Returns:
  53. The path to the package manager.
  54. Raises:
  55. FileNotFoundError: If bun or npm is not installed.
  56. Exit: If the app directory is invalid.
  57. """
  58. config = get_config()
  59. # Check that the node version is valid.
  60. if not check_node_version(constants.MIN_NODE_VERSION):
  61. console.print(
  62. f"[red]Node.js version {constants.MIN_NODE_VERSION} or higher is required to run Pynecone."
  63. )
  64. raise typer.Exit()
  65. # On Windows, we use npm instead of bun.
  66. if platform.system() == "Windows" or config.disable_bun:
  67. npm_path = path_ops.which("npm")
  68. if npm_path is None:
  69. raise FileNotFoundError("Pynecone requires npm to be installed on Windows.")
  70. return npm_path
  71. # On other platforms, we use bun.
  72. return os.path.expandvars(get_config().bun_path)
  73. def get_app() -> ModuleType:
  74. """Get the app module based on the default config.
  75. Returns:
  76. The app based on the default config.
  77. """
  78. config = get_config()
  79. module = ".".join([config.app_name, config.app_name])
  80. sys.path.insert(0, os.getcwd())
  81. return __import__(module, fromlist=(constants.APP_VAR,))
  82. def get_redis() -> Optional[Redis]:
  83. """Get the redis client.
  84. Returns:
  85. The redis client.
  86. """
  87. config = get_config()
  88. if config.redis_url is None:
  89. return None
  90. redis_url, redis_port = config.redis_url.split(":")
  91. print("Using redis at", config.redis_url)
  92. return Redis(host=redis_url, port=int(redis_port), db=0)
  93. def get_production_backend_url() -> str:
  94. """Get the production backend URL.
  95. Returns:
  96. The production backend URL.
  97. """
  98. config = get_config()
  99. return constants.PRODUCTION_BACKEND_URL.format(
  100. username=config.username,
  101. app_name=config.app_name,
  102. )
  103. def get_default_app_name() -> str:
  104. """Get the default app name.
  105. The default app name is the name of the current directory.
  106. Returns:
  107. The default app name.
  108. """
  109. return os.getcwd().split(os.path.sep)[-1].replace("-", "_")
  110. def create_config(app_name: str):
  111. """Create a new pcconfig file.
  112. Args:
  113. app_name: The name of the app.
  114. """
  115. # Import here to avoid circular imports.
  116. from pynecone.compiler import templates
  117. with open(constants.CONFIG_FILE, "w") as f:
  118. f.write(templates.PCCONFIG.format(app_name=app_name))
  119. def create_web_directory(root: Path) -> str:
  120. """Creates a web directory in the given root directory
  121. and returns the path to the directory.
  122. Args:
  123. root (Path): The root directory of the project.
  124. Returns:
  125. The path to the web directory.
  126. """
  127. web_dir = str(root / constants.WEB_DIR)
  128. path_ops.cp(constants.WEB_TEMPLATE_DIR, web_dir, overwrite=False)
  129. return web_dir
  130. def initialize_gitignore():
  131. """Initialize the template .gitignore file."""
  132. # The files to add to the .gitignore file.
  133. files = constants.DEFAULT_GITIGNORE
  134. # Subtract current ignored files.
  135. if os.path.exists(constants.GITIGNORE_FILE):
  136. with open(constants.GITIGNORE_FILE, "r") as f:
  137. files -= set(f.read().splitlines())
  138. # Add the new files to the .gitignore file.
  139. with open(constants.GITIGNORE_FILE, "a") as f:
  140. f.write(path_ops.join(files))
  141. def initialize_app_directory(app_name: str):
  142. """Initialize the app directory on pc init.
  143. Args:
  144. app_name: The name of the app.
  145. """
  146. console.log("Initializing the app directory.")
  147. path_ops.cp(constants.APP_TEMPLATE_DIR, app_name)
  148. path_ops.mv(
  149. os.path.join(app_name, constants.APP_TEMPLATE_FILE),
  150. os.path.join(app_name, app_name + constants.PY_EXT),
  151. )
  152. path_ops.cp(constants.ASSETS_TEMPLATE_DIR, constants.APP_ASSETS_DIR)
  153. def initialize_web_directory():
  154. """Initialize the web directory on pc init."""
  155. console.log("Initializing the web directory.")
  156. path_ops.rm(os.path.join(constants.WEB_TEMPLATE_DIR, constants.NODE_MODULES))
  157. path_ops.rm(os.path.join(constants.WEB_TEMPLATE_DIR, constants.PACKAGE_LOCK))
  158. path_ops.cp(constants.WEB_TEMPLATE_DIR, constants.WEB_DIR)
  159. def install_bun():
  160. """Install bun onto the user's system.
  161. Raises:
  162. FileNotFoundError: If the required packages are not installed.
  163. """
  164. if get_bun_version() in constants.INVALID_BUN_VERSIONS:
  165. console.print(
  166. f"[red]Bun version {get_bun_version()} is not supported by Pynecone. Please downgrade to bun version {constants.MIN_BUN_VERSION} or upgrade to {constants.MAX_BUN_VERSION} or higher."
  167. )
  168. return
  169. # Bun is not supported on Windows.
  170. if platform.system() == "Windows":
  171. console.log("Skipping bun installation on Windows.")
  172. return
  173. # Only install if bun is not already installed.
  174. if not os.path.exists(get_package_manager()):
  175. console.log("Installing bun...")
  176. # Check if curl is installed
  177. curl_path = path_ops.which("curl")
  178. if curl_path is None:
  179. raise FileNotFoundError("Pynecone requires curl to be installed.")
  180. # Check if unzip is installed
  181. unzip_path = path_ops.which("unzip")
  182. if unzip_path is None:
  183. raise FileNotFoundError("Pynecone requires unzip to be installed.")
  184. os.system(constants.INSTALL_BUN)
  185. def install_frontend_packages(web_dir: str):
  186. """Installs the base and custom frontend packages
  187. into the given web directory.
  188. Args:
  189. web_dir (str): The directory where the frontend code is located.
  190. """
  191. # Install the frontend packages.
  192. console.rule("[bold]Installing frontend packages")
  193. # Install the base packages.
  194. subprocess.run(
  195. [get_package_manager(), "install"],
  196. cwd=web_dir,
  197. stdout=subprocess.PIPE,
  198. )
  199. # Install the app packages.
  200. packages = get_config().frontend_packages
  201. if len(packages) > 0:
  202. subprocess.run(
  203. [get_package_manager(), "add", *packages],
  204. cwd=web_dir,
  205. stdout=subprocess.PIPE,
  206. )
  207. def is_initialized() -> bool:
  208. """Check whether the app is initialized.
  209. Returns:
  210. Whether the app is initialized in the current directory.
  211. """
  212. return os.path.exists(constants.CONFIG_FILE) and os.path.exists(constants.WEB_DIR)
  213. def is_latest_template() -> bool:
  214. """Whether the app is using the latest template.
  215. Returns:
  216. Whether the app is using the latest template.
  217. """
  218. with open(constants.PCVERSION_TEMPLATE_FILE) as f: # type: ignore
  219. template_version = json.load(f)["version"]
  220. if not os.path.exists(constants.PCVERSION_APP_FILE):
  221. return False
  222. with open(constants.PCVERSION_APP_FILE) as f: # type: ignore
  223. app_version = json.load(f)["version"]
  224. return app_version >= template_version