1
0

prerequisites.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. """Everything related to fetching or initializing build prerequisites."""
  2. from __future__ import annotations
  3. import glob
  4. import json
  5. import os
  6. import platform
  7. import re
  8. import subprocess
  9. import sys
  10. import tempfile
  11. import threading
  12. from datetime import datetime
  13. from fileinput import FileInput
  14. from pathlib import Path
  15. from types import ModuleType
  16. from typing import Optional
  17. import httpx
  18. import typer
  19. from alembic.util.exc import CommandError
  20. from packaging import version
  21. from redis import Redis
  22. from reflex import constants, model
  23. from reflex.config import get_config
  24. from reflex.utils import console, path_ops
  25. IS_WINDOWS = platform.system() == "Windows"
  26. def check_node_version():
  27. """Check the version of Node.js.
  28. Returns:
  29. Whether the version of Node.js is valid.
  30. """
  31. try:
  32. # Run the node -v command and capture the output
  33. result = subprocess.run(
  34. [constants.NODE_PATH, "-v"],
  35. stdout=subprocess.PIPE,
  36. stderr=subprocess.PIPE,
  37. )
  38. # The output will be in the form "vX.Y.Z", but version.parse() can handle it
  39. current_version = version.parse(result.stdout.decode())
  40. # Compare the version numbers
  41. return (
  42. current_version >= version.parse(constants.NODE_VERSION_MIN)
  43. if IS_WINDOWS
  44. else current_version == version.parse(constants.NODE_VERSION)
  45. )
  46. except Exception:
  47. return False
  48. def get_bun_version() -> Optional[version.Version]:
  49. """Get the version of bun.
  50. Returns:
  51. The version of bun.
  52. """
  53. try:
  54. # Run the bun -v command and capture the output
  55. result = subprocess.run(
  56. [constants.BUN_PATH, "-v"],
  57. stdout=subprocess.PIPE,
  58. stderr=subprocess.PIPE,
  59. )
  60. return version.parse(result.stdout.decode().strip())
  61. except Exception:
  62. return None
  63. def get_windows_package_manager() -> str:
  64. """Get the package manager for windows.
  65. Returns:
  66. The path to the package manager for windows.
  67. Raises:
  68. FileNotFoundError: If bun or npm is not installed.
  69. """
  70. npm_path = path_ops.which("npm")
  71. if npm_path is None:
  72. raise FileNotFoundError("Reflex requires npm to be installed on Windows.")
  73. return npm_path
  74. def get_install_package_manager() -> str:
  75. """Get the package manager executable for installation.
  76. currently on unix systems, bun is used for installation only.
  77. Returns:
  78. The path to the package manager.
  79. """
  80. get_config()
  81. # On Windows, we use npm instead of bun.
  82. if platform.system() == "Windows":
  83. return get_windows_package_manager()
  84. # On other platforms, we use bun.
  85. return constants.BUN_PATH
  86. def get_package_manager() -> str:
  87. """Get the package manager executable for running app.
  88. currently on unix systems, npm is used for running the app only.
  89. Returns:
  90. The path to the package manager.
  91. """
  92. get_config()
  93. if platform.system() == "Windows":
  94. return get_windows_package_manager()
  95. return constants.NPM_PATH
  96. def get_app() -> ModuleType:
  97. """Get the app module based on the default config.
  98. Returns:
  99. The app based on the default config.
  100. """
  101. config = get_config()
  102. module = ".".join([config.app_name, config.app_name])
  103. sys.path.insert(0, os.getcwd())
  104. return __import__(module, fromlist=(constants.APP_VAR,))
  105. def get_redis() -> Optional[Redis]:
  106. """Get the redis client.
  107. Returns:
  108. The redis client.
  109. """
  110. config = get_config()
  111. if config.redis_url is None:
  112. return None
  113. redis_url, redis_port = config.redis_url.split(":")
  114. print("Using redis at", config.redis_url)
  115. return Redis(host=redis_url, port=int(redis_port), db=0)
  116. def get_production_backend_url() -> str:
  117. """Get the production backend URL.
  118. Returns:
  119. The production backend URL.
  120. """
  121. config = get_config()
  122. return constants.PRODUCTION_BACKEND_URL.format(
  123. username=config.username,
  124. app_name=config.app_name,
  125. )
  126. def get_default_app_name() -> str:
  127. """Get the default app name.
  128. The default app name is the name of the current directory.
  129. Returns:
  130. The default app name.
  131. Raises:
  132. Exit: if the app directory name is reflex.
  133. """
  134. app_name = os.getcwd().split(os.path.sep)[-1].replace("-", "_")
  135. # Make sure the app is not named "reflex".
  136. if app_name == constants.MODULE_NAME:
  137. console.print(
  138. f"[red]The app directory cannot be named [bold]{constants.MODULE_NAME}."
  139. )
  140. raise typer.Exit()
  141. return app_name
  142. def create_config(app_name: str):
  143. """Create a new rxconfig file.
  144. Args:
  145. app_name: The name of the app.
  146. """
  147. # Import here to avoid circular imports.
  148. from reflex.compiler import templates
  149. config_name = f"{re.sub(r'[^a-zA-Z]', '', app_name).capitalize()}Config"
  150. with open(constants.CONFIG_FILE, "w") as f:
  151. f.write(templates.RXCONFIG.render(app_name=app_name, config_name=config_name))
  152. def initialize_gitignore():
  153. """Initialize the template .gitignore file."""
  154. # The files to add to the .gitignore file.
  155. files = constants.DEFAULT_GITIGNORE
  156. # Subtract current ignored files.
  157. if os.path.exists(constants.GITIGNORE_FILE):
  158. with open(constants.GITIGNORE_FILE, "r") as f:
  159. files |= set([line.strip() for line in f.readlines()])
  160. # Write files to the .gitignore file.
  161. with open(constants.GITIGNORE_FILE, "w") as f:
  162. f.write(f"{(path_ops.join(sorted(files))).lstrip()}")
  163. def initialize_app_directory(app_name: str, template: constants.Template):
  164. """Initialize the app directory on reflex init.
  165. Args:
  166. app_name: The name of the app.
  167. template: The template to use.
  168. """
  169. console.log("Initializing the app directory.")
  170. path_ops.cp(os.path.join(constants.TEMPLATE_DIR, "apps", template.value), app_name)
  171. path_ops.mv(
  172. os.path.join(app_name, template.value + ".py"),
  173. os.path.join(app_name, app_name + constants.PY_EXT),
  174. )
  175. path_ops.cp(constants.ASSETS_TEMPLATE_DIR, constants.APP_ASSETS_DIR)
  176. def initialize_web_directory():
  177. """Initialize the web directory on reflex init."""
  178. console.log("Initializing the web directory.")
  179. path_ops.cp(constants.WEB_TEMPLATE_DIR, constants.WEB_DIR)
  180. path_ops.mkdir(constants.WEB_ASSETS_DIR)
  181. # update nextJS config based on rxConfig
  182. next_config_file = os.path.join(constants.WEB_DIR, constants.NEXT_CONFIG_FILE)
  183. with open(next_config_file, "r") as file:
  184. lines = file.readlines()
  185. for i, line in enumerate(lines):
  186. if "compress:" in line:
  187. new_line = line.replace(
  188. "true", "true" if get_config().next_compression else "false"
  189. )
  190. lines[i] = new_line
  191. with open(next_config_file, "w") as file:
  192. file.writelines(lines)
  193. # Write the current version of distributed reflex package to a REFLEX_JSON."""
  194. with open(constants.REFLEX_JSON, "w") as f:
  195. reflex_json = {"version": constants.VERSION}
  196. json.dump(reflex_json, f, ensure_ascii=False)
  197. def initialize_bun():
  198. """Check that bun requirements are met, and install if not."""
  199. if IS_WINDOWS:
  200. # Bun is not supported on Windows.
  201. return
  202. # Check the bun version.
  203. if get_bun_version() != version.parse(constants.BUN_VERSION):
  204. remove_existing_bun_installation()
  205. install_bun()
  206. def remove_existing_bun_installation():
  207. """Remove existing bun installation."""
  208. if os.path.exists(constants.BUN_PATH):
  209. path_ops.rm(constants.BUN_ROOT_PATH)
  210. def initialize_node():
  211. """Validate nodejs have install or not."""
  212. if not check_node_version():
  213. install_node()
  214. def download_and_run(url: str, *args, **env):
  215. """Download and run a script.
  216. Args:
  217. url: The url of the script.
  218. args: The arguments to pass to the script.
  219. env: The environment variables to use.
  220. Raises:
  221. Exit: if installation failed
  222. """
  223. # Download the script
  224. response = httpx.get(url)
  225. if response.status_code != httpx.codes.OK:
  226. response.raise_for_status()
  227. # Save the script to a temporary file.
  228. script = tempfile.NamedTemporaryFile()
  229. with open(script.name, "w") as f:
  230. f.write(response.text)
  231. # Run the script.
  232. env = {
  233. **os.environ,
  234. **env,
  235. }
  236. result = subprocess.run(["bash", f.name, *args], env=env)
  237. if result.returncode != 0:
  238. raise typer.Exit(code=result.returncode)
  239. def install_node():
  240. """Install nvm and nodejs for use by Reflex.
  241. Independent of any existing system installations.
  242. Raises:
  243. Exit: if installation failed
  244. """
  245. # NVM is not supported on Windows.
  246. if IS_WINDOWS:
  247. console.print(
  248. f"[red]Node.js version {constants.NODE_VERSION} or higher is required to run Reflex."
  249. )
  250. raise typer.Exit()
  251. # Create the nvm directory and install.
  252. path_ops.mkdir(constants.NVM_DIR)
  253. env = {**os.environ, "NVM_DIR": constants.NVM_DIR}
  254. download_and_run(constants.NVM_INSTALL_URL, **env)
  255. # Install node.
  256. # We use bash -c as we need to source nvm.sh to use nvm.
  257. result = subprocess.run(
  258. [
  259. "bash",
  260. "-c",
  261. f". {constants.NVM_DIR}/nvm.sh && nvm install {constants.NODE_VERSION}",
  262. ],
  263. env=env,
  264. )
  265. if result.returncode != 0:
  266. raise typer.Exit(code=result.returncode)
  267. def install_bun():
  268. """Install bun onto the user's system.
  269. Raises:
  270. FileNotFoundError: If required packages are not found.
  271. """
  272. # Bun is not supported on Windows.
  273. if IS_WINDOWS:
  274. return
  275. # Skip if bun is already installed.
  276. if os.path.exists(constants.BUN_PATH):
  277. return
  278. # Check if unzip is installed
  279. unzip_path = path_ops.which("unzip")
  280. if unzip_path is None:
  281. raise FileNotFoundError("Reflex requires unzip to be installed.")
  282. # Run the bun install script.
  283. download_and_run(
  284. constants.BUN_INSTALL_URL,
  285. f"bun-v{constants.BUN_VERSION}",
  286. BUN_INSTALL=constants.BUN_ROOT_PATH,
  287. )
  288. def install_frontend_packages():
  289. """Installs the base and custom frontend packages."""
  290. # Install the frontend packages.
  291. console.rule("[bold]Installing frontend packages")
  292. # Install the base packages.
  293. subprocess.run(
  294. [get_install_package_manager(), "install"],
  295. cwd=constants.WEB_DIR,
  296. stdout=subprocess.PIPE,
  297. )
  298. # Install the app packages.
  299. packages = get_config().frontend_packages
  300. if len(packages) > 0:
  301. subprocess.run(
  302. [get_install_package_manager(), "add", *packages],
  303. cwd=constants.WEB_DIR,
  304. stdout=subprocess.PIPE,
  305. )
  306. def check_initialized(frontend: bool = True):
  307. """Check that the app is initialized.
  308. Args:
  309. frontend: Whether to check if the frontend is initialized.
  310. Raises:
  311. Exit: If the app is not initialized.
  312. """
  313. has_config = os.path.exists(constants.CONFIG_FILE)
  314. has_reflex_dir = IS_WINDOWS or os.path.exists(constants.REFLEX_DIR)
  315. has_web_dir = not frontend or os.path.exists(constants.WEB_DIR)
  316. # Check if the app is initialized.
  317. if not (has_config and has_reflex_dir and has_web_dir):
  318. console.print(
  319. f"[red]The app is not initialized. Run [bold]{constants.MODULE_NAME} init[/bold] first."
  320. )
  321. raise typer.Exit()
  322. # Check that the template is up to date.
  323. if frontend and not is_latest_template():
  324. console.print(
  325. "[red]The base app template has updated. Run [bold]reflex init[/bold] again."
  326. )
  327. raise typer.Exit()
  328. # Print a warning for Windows users.
  329. if IS_WINDOWS:
  330. console.print(
  331. "[yellow][WARNING] We strongly advise using Windows Subsystem for Linux (WSL) for optimal performance with reflex."
  332. )
  333. def is_latest_template() -> bool:
  334. """Whether the app is using the latest template.
  335. Returns:
  336. Whether the app is using the latest template.
  337. """
  338. if not os.path.exists(constants.REFLEX_JSON):
  339. return False
  340. with open(constants.REFLEX_JSON) as f: # type: ignore
  341. app_version = json.load(f)["version"]
  342. return app_version == constants.VERSION
  343. def initialize_frontend_dependencies():
  344. """Initialize all the frontend dependencies."""
  345. # Create the reflex directory.
  346. path_ops.mkdir(constants.REFLEX_DIR)
  347. # Install the frontend dependencies.
  348. threads = [
  349. threading.Thread(target=initialize_bun),
  350. threading.Thread(target=initialize_node),
  351. ]
  352. for thread in threads:
  353. thread.start()
  354. # Set up the web directory.
  355. initialize_web_directory()
  356. # Wait for the threads to finish.
  357. for thread in threads:
  358. thread.join()
  359. def check_admin_settings():
  360. """Check if admin settings are set and valid for logging in cli app."""
  361. admin_dash = get_config().admin_dash
  362. current_time = datetime.now()
  363. if admin_dash:
  364. if not admin_dash.models:
  365. console.print(
  366. f"[yellow][Admin Dashboard][/yellow] :megaphone: Admin dashboard enabled, but no models defined in [bold magenta]rxconfig.py[/bold magenta]. Time: {current_time}"
  367. )
  368. else:
  369. console.print(
  370. f"[yellow][Admin Dashboard][/yellow] Admin enabled, building admin dashboard. Time: {current_time}"
  371. )
  372. console.print(
  373. "Admin dashboard running at: [bold green]http://localhost:8000/admin[/bold green]"
  374. )
  375. def check_db_initialized() -> bool:
  376. """Check if the database migrations are initialized.
  377. Returns:
  378. True if alembic is initialized (or if database is not used).
  379. """
  380. if get_config().db_url is not None and not Path(constants.ALEMBIC_CONFIG).exists():
  381. console.print(
  382. "[red]Database is not initialized. Run [bold]reflex db init[/bold] first."
  383. )
  384. return False
  385. return True
  386. def check_schema_up_to_date():
  387. """Check if the sqlmodel metadata matches the current database schema."""
  388. if get_config().db_url is None or not Path(constants.ALEMBIC_CONFIG).exists():
  389. return
  390. with model.Model.get_db_engine().connect() as connection:
  391. try:
  392. if model.Model.alembic_autogenerate(
  393. connection=connection,
  394. write_migration_scripts=False,
  395. ):
  396. console.print(
  397. "[red]Detected database schema changes. Run [bold]reflex db makemigrations[/bold] "
  398. "to generate migration scripts.",
  399. )
  400. except CommandError as command_error:
  401. if "Target database is not up to date." in str(command_error):
  402. console.print(
  403. f"[red]{command_error} Run [bold]reflex db migrate[/bold] to update database."
  404. )
  405. def migrate_to_reflex():
  406. """Migration from Pynecone to Reflex."""
  407. # Check if the old config file exists.
  408. if not os.path.exists(constants.OLD_CONFIG_FILE):
  409. return
  410. # Ask the user if they want to migrate.
  411. action = console.ask(
  412. "Pynecone project detected. Automatically upgrade to Reflex?",
  413. choices=["y", "n"],
  414. )
  415. if action == "n":
  416. return
  417. # Rename pcconfig to rxconfig.
  418. console.print(
  419. f"[bold]Renaming {constants.OLD_CONFIG_FILE} to {constants.CONFIG_FILE}"
  420. )
  421. os.rename(constants.OLD_CONFIG_FILE, constants.CONFIG_FILE)
  422. # Find all python files in the app directory.
  423. file_pattern = os.path.join(get_config().app_name, "**/*.py")
  424. file_list = glob.glob(file_pattern, recursive=True)
  425. # Add the config file to the list of files to be migrated.
  426. file_list.append(constants.CONFIG_FILE)
  427. # Migrate all files.
  428. updates = {
  429. "Pynecone": "Reflex",
  430. "pynecone as pc": "reflex as rx",
  431. "pynecone.io": "reflex.dev",
  432. "pynecone": "reflex",
  433. "pc.": "rx.",
  434. "pcconfig": "rxconfig",
  435. }
  436. for file_path in file_list:
  437. with FileInput(file_path, inplace=True) as file:
  438. for line in file:
  439. for old, new in updates.items():
  440. line = line.replace(old, new)
  441. print(line, end="")