prerequisites.py 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. """Everything related to fetching or initializing build prerequisites."""
  2. from __future__ import annotations
  3. import glob
  4. import importlib
  5. import inspect
  6. import json
  7. import os
  8. import platform
  9. import random
  10. import re
  11. import shutil
  12. import stat
  13. import sys
  14. import tempfile
  15. import zipfile
  16. from datetime import datetime
  17. from fileinput import FileInput
  18. from pathlib import Path
  19. from types import ModuleType
  20. from typing import Callable, Optional
  21. import httpx
  22. import pkg_resources
  23. import typer
  24. from alembic.util.exc import CommandError
  25. from packaging import version
  26. from redis import Redis as RedisSync
  27. from redis.asyncio import Redis
  28. import reflex
  29. from reflex import constants, model
  30. from reflex.base import Base
  31. from reflex.compiler import templates
  32. from reflex.config import Config, get_config
  33. from reflex.utils import console, path_ops, processes
  34. CURRENTLY_INSTALLING_NODE = False
  35. class Template(Base):
  36. """A template for a Reflex app."""
  37. name: str
  38. description: str
  39. code_url: str
  40. demo_url: str
  41. def check_latest_package_version(package_name: str):
  42. """Check if the latest version of the package is installed.
  43. Args:
  44. package_name: The name of the package.
  45. """
  46. try:
  47. # Get the latest version from PyPI
  48. current_version = pkg_resources.get_distribution(package_name).version
  49. url = f"https://pypi.org/pypi/{package_name}/json"
  50. response = httpx.get(url)
  51. latest_version = response.json()["info"]["version"]
  52. if (
  53. version.parse(current_version) < version.parse(latest_version)
  54. and not get_or_set_last_reflex_version_check_datetime()
  55. ):
  56. # only show a warning when the host version is outdated and
  57. # the last_version_check_datetime is not set in reflex.json
  58. console.warn(
  59. f"Your version ({current_version}) of {package_name} is out of date. Upgrade to {latest_version} with 'pip install {package_name} --upgrade'"
  60. )
  61. except Exception:
  62. pass
  63. def get_or_set_last_reflex_version_check_datetime():
  64. """Get the last time a check was made for the latest reflex version.
  65. This is typically useful for cases where the host reflex version is
  66. less than that on Pypi.
  67. Returns:
  68. The last version check datetime.
  69. """
  70. if not os.path.exists(constants.Reflex.JSON):
  71. return None
  72. # Open and read the file
  73. with open(constants.Reflex.JSON, "r") as file:
  74. data: dict = json.load(file)
  75. last_version_check_datetime = data.get("last_version_check_datetime")
  76. if not last_version_check_datetime:
  77. data.update({"last_version_check_datetime": str(datetime.now())})
  78. path_ops.update_json_file(constants.Reflex.JSON, data)
  79. return last_version_check_datetime
  80. def check_node_version() -> bool:
  81. """Check the version of Node.js.
  82. Returns:
  83. Whether the version of Node.js is valid.
  84. """
  85. current_version = get_node_version()
  86. if current_version:
  87. # Compare the version numbers
  88. return (
  89. current_version >= version.parse(constants.Node.MIN_VERSION)
  90. if constants.IS_WINDOWS
  91. else current_version == version.parse(constants.Node.VERSION)
  92. )
  93. return False
  94. def get_node_version() -> version.Version | None:
  95. """Get the version of node.
  96. Returns:
  97. The version of node.
  98. """
  99. node_path = path_ops.get_node_path()
  100. if node_path is None:
  101. return None
  102. try:
  103. result = processes.new_process([node_path, "-v"], run=True)
  104. # The output will be in the form "vX.Y.Z", but version.parse() can handle it
  105. return version.parse(result.stdout) # type: ignore
  106. except (FileNotFoundError, TypeError):
  107. return None
  108. def get_fnm_version() -> version.Version | None:
  109. """Get the version of fnm.
  110. Returns:
  111. The version of FNM.
  112. """
  113. try:
  114. result = processes.new_process([constants.Fnm.EXE, "--version"], run=True)
  115. return version.parse(result.stdout.split(" ")[1]) # type: ignore
  116. except (FileNotFoundError, TypeError):
  117. return None
  118. except version.InvalidVersion as e:
  119. console.warn(
  120. f"The detected fnm version ({e.args[0]}) is not valid. Defaulting to None."
  121. )
  122. return None
  123. def get_bun_version() -> version.Version | None:
  124. """Get the version of bun.
  125. Returns:
  126. The version of bun.
  127. """
  128. try:
  129. # Run the bun -v command and capture the output
  130. result = processes.new_process([get_config().bun_path, "-v"], run=True)
  131. return version.parse(result.stdout) # type: ignore
  132. except FileNotFoundError:
  133. return None
  134. except version.InvalidVersion as e:
  135. console.warn(
  136. f"The detected bun version ({e.args[0]}) is not valid. Defaulting to None."
  137. )
  138. return None
  139. def get_install_package_manager() -> str | None:
  140. """Get the package manager executable for installation.
  141. Currently on unix systems, bun is used for installation only.
  142. Returns:
  143. The path to the package manager.
  144. """
  145. # On Windows, we use npm instead of bun.
  146. if constants.IS_WINDOWS:
  147. return get_package_manager()
  148. # On other platforms, we use bun.
  149. return get_config().bun_path
  150. def get_package_manager() -> str | None:
  151. """Get the package manager executable for running app.
  152. Currently on unix systems, npm is used for running the app only.
  153. Returns:
  154. The path to the package manager.
  155. """
  156. npm_path = path_ops.get_npm_path()
  157. if npm_path is not None:
  158. npm_path = str(Path(npm_path).resolve())
  159. return npm_path
  160. def get_app(reload: bool = False) -> ModuleType:
  161. """Get the app module based on the default config.
  162. Args:
  163. reload: Re-import the app module from disk
  164. Returns:
  165. The app based on the default config.
  166. Raises:
  167. RuntimeError: If the app name is not set in the config.
  168. """
  169. os.environ[constants.RELOAD_CONFIG] = str(reload)
  170. config = get_config()
  171. if not config.app_name:
  172. raise RuntimeError(
  173. "Cannot get the app module because `app_name` is not set in rxconfig! "
  174. "If this error occurs in a reflex test case, ensure that `get_app` is mocked."
  175. )
  176. module = config.module
  177. sys.path.insert(0, os.getcwd())
  178. app = __import__(module, fromlist=(constants.CompileVars.APP,))
  179. if reload:
  180. from reflex.state import reload_state_module
  181. # Reset rx.State subclasses to avoid conflict when reloading.
  182. reload_state_module(module=module)
  183. # Reload the app module.
  184. importlib.reload(app)
  185. return app
  186. def get_compiled_app(reload: bool = False) -> ModuleType:
  187. """Get the app module based on the default config after first compiling it.
  188. Args:
  189. reload: Re-import the app module from disk
  190. Returns:
  191. The compiled app based on the default config.
  192. """
  193. app_module = get_app(reload=reload)
  194. app = getattr(app_module, constants.CompileVars.APP)
  195. # For py3.8 and py3.9 compatibility when redis is used, we MUST add any decorator pages
  196. # before compiling the app in a thread to avoid event loop error (REF-2172).
  197. app._apply_decorated_pages()
  198. app.compile_()
  199. return app_module
  200. def get_redis() -> Redis | None:
  201. """Get the asynchronous redis client.
  202. Returns:
  203. The asynchronous redis client.
  204. """
  205. if isinstance((redis_url_or_options := parse_redis_url()), str):
  206. return Redis.from_url(redis_url_or_options)
  207. elif isinstance(redis_url_or_options, dict):
  208. return Redis(**redis_url_or_options)
  209. return None
  210. def get_redis_sync() -> RedisSync | None:
  211. """Get the synchronous redis client.
  212. Returns:
  213. The synchronous redis client.
  214. """
  215. if isinstance((redis_url_or_options := parse_redis_url()), str):
  216. return RedisSync.from_url(redis_url_or_options)
  217. elif isinstance(redis_url_or_options, dict):
  218. return RedisSync(**redis_url_or_options)
  219. return None
  220. def parse_redis_url() -> str | dict | None:
  221. """Parse the REDIS_URL in config if applicable.
  222. Returns:
  223. If redis-py syntax, return the URL as it is. Otherwise, return the host/port/db as a dict.
  224. """
  225. config = get_config()
  226. if not config.redis_url:
  227. return None
  228. if config.redis_url.startswith(("redis://", "rediss://", "unix://")):
  229. return config.redis_url
  230. console.deprecate(
  231. feature_name="host[:port] style redis urls",
  232. reason="redis-py url syntax is now being used",
  233. deprecation_version="0.3.6",
  234. removal_version="0.5.0",
  235. )
  236. redis_url, has_port, redis_port = config.redis_url.partition(":")
  237. if not has_port:
  238. redis_port = 6379
  239. console.info(f"Using redis at {config.redis_url}")
  240. return dict(host=redis_url, port=int(redis_port), db=0)
  241. def get_production_backend_url() -> str:
  242. """Get the production backend URL.
  243. Returns:
  244. The production backend URL.
  245. """
  246. config = get_config()
  247. return constants.PRODUCTION_BACKEND_URL.format(
  248. username=config.username,
  249. app_name=config.app_name,
  250. )
  251. def validate_app_name(app_name: str | None = None) -> str:
  252. """Validate the app name.
  253. The default app name is the name of the current directory.
  254. Args:
  255. app_name: the name passed by user during reflex init
  256. Returns:
  257. The app name after validation.
  258. Raises:
  259. Exit: if the app directory name is reflex or if the name is not standard for a python package name.
  260. """
  261. app_name = (
  262. app_name if app_name else os.getcwd().split(os.path.sep)[-1].replace("-", "_")
  263. )
  264. # Make sure the app is not named "reflex".
  265. if app_name == constants.Reflex.MODULE_NAME:
  266. console.error(
  267. f"The app directory cannot be named [bold]{constants.Reflex.MODULE_NAME}[/bold]."
  268. )
  269. raise typer.Exit(1)
  270. # Make sure the app name is standard for a python package name.
  271. if not re.match(r"^[a-zA-Z][a-zA-Z0-9_]*$", app_name):
  272. console.error(
  273. "The app directory name must start with a letter and can contain letters, numbers, and underscores."
  274. )
  275. raise typer.Exit(1)
  276. return app_name
  277. def create_config(app_name: str):
  278. """Create a new rxconfig file.
  279. Args:
  280. app_name: The name of the app.
  281. """
  282. # Import here to avoid circular imports.
  283. from reflex.compiler import templates
  284. config_name = f"{re.sub(r'[^a-zA-Z]', '', app_name).capitalize()}Config"
  285. with open(constants.Config.FILE, "w") as f:
  286. console.debug(f"Creating {constants.Config.FILE}")
  287. f.write(templates.RXCONFIG.render(app_name=app_name, config_name=config_name))
  288. def initialize_gitignore(
  289. gitignore_file: str = constants.GitIgnore.FILE,
  290. files_to_ignore: set[str] = constants.GitIgnore.DEFAULTS,
  291. ):
  292. """Initialize the template .gitignore file.
  293. Args:
  294. gitignore_file: The .gitignore file to create.
  295. files_to_ignore: The files to add to the .gitignore file.
  296. """
  297. # Combine with the current ignored files.
  298. if os.path.exists(gitignore_file):
  299. with open(gitignore_file, "r") as f:
  300. files_to_ignore |= set([line.strip() for line in f.readlines()])
  301. # Write files to the .gitignore file.
  302. with open(gitignore_file, "w", newline="\n") as f:
  303. console.debug(f"Creating {gitignore_file}")
  304. f.write(f"{(path_ops.join(sorted(files_to_ignore))).lstrip()}")
  305. def initialize_requirements_txt():
  306. """Initialize the requirements.txt file.
  307. If absent, generate one for the user.
  308. If the requirements.txt does not have reflex as dependency,
  309. generate a requirement pinning current version and append to
  310. the requirements.txt file.
  311. """
  312. fp = Path(constants.RequirementsTxt.FILE)
  313. encoding = "utf-8"
  314. if not fp.exists():
  315. fp.touch()
  316. else:
  317. # Detect the encoding of the original file
  318. import charset_normalizer
  319. charset_matches = charset_normalizer.from_path(fp)
  320. maybe_charset_match = charset_matches.best()
  321. if maybe_charset_match is None:
  322. console.debug(f"Unable to detect encoding for {fp}, exiting.")
  323. return
  324. encoding = maybe_charset_match.encoding
  325. console.debug(f"Detected encoding for {fp} as {encoding}.")
  326. try:
  327. other_requirements_exist = False
  328. with open(fp, "r", encoding=encoding) as f:
  329. for req in f.readlines():
  330. # Check if we have a package name that is reflex
  331. if re.match(r"^reflex[^a-zA-Z0-9]", req):
  332. console.debug(f"{fp} already has reflex as dependency.")
  333. return
  334. other_requirements_exist = True
  335. with open(fp, "a", encoding=encoding) as f:
  336. preceding_newline = "\n" if other_requirements_exist else ""
  337. f.write(
  338. f"{preceding_newline}{constants.RequirementsTxt.DEFAULTS_STUB}{constants.Reflex.VERSION}\n"
  339. )
  340. except Exception:
  341. console.info(f"Unable to check {fp} for reflex dependency.")
  342. def initialize_app_directory(
  343. app_name: str,
  344. template_name: str = constants.Templates.DEFAULT,
  345. template_code_dir_name: str | None = None,
  346. template_dir: Path | None = None,
  347. ):
  348. """Initialize the app directory on reflex init.
  349. Args:
  350. app_name: The name of the app.
  351. template_name: The name of the template to use.
  352. template_code_dir_name: The name of the code directory in the template.
  353. template_dir: The directory of the template source files.
  354. Raises:
  355. Exit: If template_name, template_code_dir_name, template_dir combination is not supported.
  356. """
  357. console.log("Initializing the app directory.")
  358. # By default, use the blank template from local assets.
  359. if template_name == constants.Templates.DEFAULT:
  360. if template_code_dir_name is not None or template_dir is not None:
  361. console.error(
  362. f"Only {template_name=} should be provided, got {template_code_dir_name=}, {template_dir=}."
  363. )
  364. raise typer.Exit(1)
  365. template_code_dir_name = constants.Templates.Dirs.CODE
  366. template_dir = Path(constants.Templates.Dirs.BASE, "apps", template_name)
  367. else:
  368. if template_code_dir_name is None or template_dir is None:
  369. console.error(
  370. f"For `{template_name}` template, `template_code_dir_name` and `template_dir` should both be provided."
  371. )
  372. raise typer.Exit(1)
  373. console.debug(f"Using {template_name=} {template_dir=} {template_code_dir_name=}.")
  374. # Remove all pyc and __pycache__ dirs in template directory.
  375. for pyc_file in template_dir.glob("**/*.pyc"):
  376. pyc_file.unlink()
  377. for pycache_dir in template_dir.glob("**/__pycache__"):
  378. pycache_dir.rmdir()
  379. for file in template_dir.iterdir():
  380. # Copy the file to current directory but keep the name the same.
  381. path_ops.cp(str(file), file.name)
  382. # Rename the template app to the app name.
  383. path_ops.mv(template_code_dir_name, app_name)
  384. path_ops.mv(
  385. os.path.join(app_name, template_name + constants.Ext.PY),
  386. os.path.join(app_name, app_name + constants.Ext.PY),
  387. )
  388. # Fix up the imports.
  389. path_ops.find_replace(
  390. app_name,
  391. f"from {template_name}",
  392. f"from {app_name}",
  393. )
  394. def get_project_hash(raise_on_fail: bool = False) -> int | None:
  395. """Get the project hash from the reflex.json file if the file exists.
  396. Args:
  397. raise_on_fail: Whether to raise an error if the file does not exist.
  398. Returns:
  399. project_hash: The app hash.
  400. """
  401. if not os.path.exists(constants.Reflex.JSON) and not raise_on_fail:
  402. return None
  403. # Open and read the file
  404. with open(constants.Reflex.JSON, "r") as file:
  405. data = json.load(file)
  406. return data.get("project_hash")
  407. def initialize_web_directory():
  408. """Initialize the web directory on reflex init."""
  409. console.log("Initializing the web directory.")
  410. # Re-use the hash if one is already created, so we don't over-write it when running reflex init
  411. project_hash = get_project_hash()
  412. path_ops.cp(constants.Templates.Dirs.WEB_TEMPLATE, constants.Dirs.WEB)
  413. initialize_package_json()
  414. path_ops.mkdir(constants.Dirs.WEB_ASSETS)
  415. update_next_config()
  416. # Initialize the reflex json file.
  417. init_reflex_json(project_hash=project_hash)
  418. def _compile_package_json():
  419. return templates.PACKAGE_JSON.render(
  420. scripts={
  421. "dev": constants.PackageJson.Commands.DEV,
  422. "export": constants.PackageJson.Commands.EXPORT,
  423. "export_sitemap": constants.PackageJson.Commands.EXPORT_SITEMAP,
  424. "prod": constants.PackageJson.Commands.PROD,
  425. },
  426. dependencies=constants.PackageJson.DEPENDENCIES,
  427. dev_dependencies=constants.PackageJson.DEV_DEPENDENCIES,
  428. )
  429. def initialize_package_json():
  430. """Render and write in .web the package.json file."""
  431. output_path = constants.PackageJson.PATH
  432. code = _compile_package_json()
  433. with open(output_path, "w") as file:
  434. file.write(code)
  435. def init_reflex_json(project_hash: int | None):
  436. """Write the hash of the Reflex project to a REFLEX_JSON.
  437. Re-use the hash if one is already created, therefore do not
  438. overwrite it every time we run the reflex init command
  439. .
  440. Args:
  441. project_hash: The app hash.
  442. """
  443. if project_hash is not None:
  444. console.debug(f"Project hash is already set to {project_hash}.")
  445. else:
  446. # Get a random project hash.
  447. project_hash = random.getrandbits(128)
  448. console.debug(f"Setting project hash to {project_hash}.")
  449. # Write the hash and version to the reflex json file.
  450. reflex_json = {
  451. "version": constants.Reflex.VERSION,
  452. "project_hash": project_hash,
  453. }
  454. path_ops.update_json_file(constants.Reflex.JSON, reflex_json)
  455. def update_next_config(export=False):
  456. """Update Next.js config from Reflex config.
  457. Args:
  458. export: if the method run during reflex export.
  459. """
  460. next_config_file = os.path.join(constants.Dirs.WEB, constants.Next.CONFIG_FILE)
  461. next_config = _update_next_config(get_config(), export=export)
  462. with open(next_config_file, "w") as file:
  463. file.write(next_config)
  464. file.write("\n")
  465. def _update_next_config(config, export=False):
  466. next_config = {
  467. "basePath": config.frontend_path or "",
  468. "compress": config.next_compression,
  469. "reactStrictMode": True,
  470. "trailingSlash": True,
  471. }
  472. if export:
  473. next_config["output"] = "export"
  474. next_config["distDir"] = constants.Dirs.STATIC
  475. next_config_json = re.sub(r'"([^"]+)"(?=:)', r"\1", json.dumps(next_config))
  476. return f"module.exports = {next_config_json};"
  477. def remove_existing_bun_installation():
  478. """Remove existing bun installation."""
  479. console.debug("Removing existing bun installation.")
  480. if os.path.exists(get_config().bun_path):
  481. path_ops.rm(constants.Bun.ROOT_PATH)
  482. def download_and_run(url: str, *args, show_status: bool = False, **env):
  483. """Download and run a script.
  484. Args:
  485. url: The url of the script.
  486. args: The arguments to pass to the script.
  487. show_status: Whether to show the status of the script.
  488. env: The environment variables to use.
  489. """
  490. # Download the script
  491. console.debug(f"Downloading {url}")
  492. response = httpx.get(url)
  493. if response.status_code != httpx.codes.OK:
  494. response.raise_for_status()
  495. # Save the script to a temporary file.
  496. script = tempfile.NamedTemporaryFile()
  497. with open(script.name, "w") as f:
  498. f.write(response.text)
  499. # Run the script.
  500. env = {**os.environ, **env}
  501. process = processes.new_process(["bash", f.name, *args], env=env)
  502. show = processes.show_status if show_status else processes.show_logs
  503. show(f"Installing {url}", process)
  504. def download_and_extract_fnm_zip():
  505. """Download and run a script.
  506. Raises:
  507. Exit: If an error occurs while downloading or extracting the FNM zip.
  508. """
  509. # Download the zip file
  510. url = constants.Fnm.INSTALL_URL
  511. console.debug(f"Downloading {url}")
  512. fnm_zip_file = os.path.join(constants.Fnm.DIR, f"{constants.Fnm.FILENAME}.zip")
  513. # Function to download and extract the FNM zip release.
  514. try:
  515. # Download the FNM zip release.
  516. # TODO: show progress to improve UX
  517. with httpx.stream("GET", url, follow_redirects=True) as response:
  518. response.raise_for_status()
  519. with open(fnm_zip_file, "wb") as output_file:
  520. for chunk in response.iter_bytes():
  521. output_file.write(chunk)
  522. # Extract the downloaded zip file.
  523. with zipfile.ZipFile(fnm_zip_file, "r") as zip_ref:
  524. zip_ref.extractall(constants.Fnm.DIR)
  525. console.debug("FNM package downloaded and extracted successfully.")
  526. except Exception as e:
  527. console.error(f"An error occurred while downloading fnm package: {e}")
  528. raise typer.Exit(1) from e
  529. finally:
  530. # Clean up the downloaded zip file.
  531. path_ops.rm(fnm_zip_file)
  532. def install_node():
  533. """Install fnm and nodejs for use by Reflex.
  534. Independent of any existing system installations.
  535. """
  536. if not constants.Fnm.FILENAME:
  537. # fnm only support Linux, macOS and Windows distros.
  538. console.debug("")
  539. return
  540. # Skip installation if check_node_version() checks out
  541. if check_node_version():
  542. console.debug("Skipping node installation as it is already installed.")
  543. return
  544. path_ops.mkdir(constants.Fnm.DIR)
  545. if not os.path.exists(constants.Fnm.EXE):
  546. download_and_extract_fnm_zip()
  547. if constants.IS_WINDOWS:
  548. # Install node
  549. fnm_exe = Path(constants.Fnm.EXE).resolve()
  550. fnm_dir = Path(constants.Fnm.DIR).resolve()
  551. process = processes.new_process(
  552. [
  553. "powershell",
  554. "-Command",
  555. f'& "{fnm_exe}" install {constants.Node.VERSION} --fnm-dir "{fnm_dir}"',
  556. ],
  557. )
  558. else: # All other platforms (Linux, MacOS).
  559. # Add execute permissions to fnm executable.
  560. os.chmod(constants.Fnm.EXE, stat.S_IXUSR)
  561. # Install node.
  562. # Specify arm64 arch explicitly for M1s and M2s.
  563. architecture_arg = (
  564. ["--arch=arm64"]
  565. if platform.system() == "Darwin" and platform.machine() == "arm64"
  566. else []
  567. )
  568. process = processes.new_process(
  569. [
  570. constants.Fnm.EXE,
  571. "install",
  572. *architecture_arg,
  573. constants.Node.VERSION,
  574. "--fnm-dir",
  575. constants.Fnm.DIR,
  576. ],
  577. )
  578. processes.show_status("Installing node", process)
  579. def install_bun():
  580. """Install bun onto the user's system.
  581. Raises:
  582. FileNotFoundError: If required packages are not found.
  583. """
  584. # Bun is not supported on Windows.
  585. if constants.IS_WINDOWS:
  586. console.debug("Skipping bun installation on Windows.")
  587. return
  588. # Skip if bun is already installed.
  589. if os.path.exists(get_config().bun_path) and get_bun_version() == version.parse(
  590. constants.Bun.VERSION
  591. ):
  592. console.debug("Skipping bun installation as it is already installed.")
  593. return
  594. # if unzip is installed
  595. unzip_path = path_ops.which("unzip")
  596. if unzip_path is None:
  597. raise FileNotFoundError("Reflex requires unzip to be installed.")
  598. # Run the bun install script.
  599. download_and_run(
  600. constants.Bun.INSTALL_URL,
  601. f"bun-v{constants.Bun.VERSION}",
  602. BUN_INSTALL=constants.Bun.ROOT_PATH,
  603. )
  604. def _write_cached_procedure_file(payload: str, cache_file: str):
  605. with open(cache_file, "w") as f:
  606. f.write(payload)
  607. def _read_cached_procedure_file(cache_file: str) -> str | None:
  608. if os.path.exists(cache_file):
  609. with open(cache_file, "r") as f:
  610. return f.read()
  611. return None
  612. def _clear_cached_procedure_file(cache_file: str):
  613. if os.path.exists(cache_file):
  614. os.remove(cache_file)
  615. def cached_procedure(cache_file: str, payload_fn: Callable[..., str]):
  616. """Decorator to cache the runs of a procedure on disk. Procedures should not have
  617. a return value.
  618. Args:
  619. cache_file: The file to store the cache payload in.
  620. payload_fn: Function that computes cache payload from function args
  621. Returns:
  622. The decorated function.
  623. """
  624. def _inner_decorator(func):
  625. def _inner(*args, **kwargs):
  626. payload = _read_cached_procedure_file(cache_file)
  627. new_payload = payload_fn(*args, **kwargs)
  628. if payload != new_payload:
  629. _clear_cached_procedure_file(cache_file)
  630. func(*args, **kwargs)
  631. _write_cached_procedure_file(new_payload, cache_file)
  632. return _inner
  633. return _inner_decorator
  634. @cached_procedure(
  635. cache_file=os.path.join(
  636. constants.Dirs.WEB, "reflex.install_frontend_packages.cached"
  637. ),
  638. payload_fn=lambda p, c: f"{repr(sorted(list(p)))},{c.json()}",
  639. )
  640. def install_frontend_packages(packages: set[str], config: Config):
  641. """Installs the base and custom frontend packages.
  642. Args:
  643. packages: A list of package names to be installed.
  644. config: The config object.
  645. Example:
  646. >>> install_frontend_packages(["react", "react-dom"], get_config())
  647. """
  648. # Install the base packages.
  649. process = processes.new_process(
  650. [get_install_package_manager(), "install", "--loglevel", "silly"],
  651. cwd=constants.Dirs.WEB,
  652. shell=constants.IS_WINDOWS,
  653. )
  654. processes.show_status("Installing base frontend packages", process)
  655. if config.tailwind is not None:
  656. # install tailwind and tailwind plugins as dev dependencies.
  657. process = processes.new_process(
  658. [
  659. get_install_package_manager(),
  660. "add",
  661. "-d",
  662. constants.Tailwind.VERSION,
  663. *((config.tailwind or {}).get("plugins", [])),
  664. ],
  665. cwd=constants.Dirs.WEB,
  666. shell=constants.IS_WINDOWS,
  667. )
  668. processes.show_status("Installing tailwind", process)
  669. # Install custom packages defined in frontend_packages
  670. if len(packages) > 0:
  671. process = processes.new_process(
  672. [get_install_package_manager(), "add", *packages],
  673. cwd=constants.Dirs.WEB,
  674. shell=constants.IS_WINDOWS,
  675. )
  676. processes.show_status(
  677. "Installing frontend packages from config and components", process
  678. )
  679. def needs_reinit(frontend: bool = True) -> bool:
  680. """Check if an app needs to be reinitialized.
  681. Args:
  682. frontend: Whether to check if the frontend is initialized.
  683. Returns:
  684. Whether the app needs to be reinitialized.
  685. Raises:
  686. Exit: If the app is not initialized.
  687. """
  688. if not os.path.exists(constants.Config.FILE):
  689. console.error(
  690. f"{constants.Config.FILE} not found. Run [bold]{constants.Reflex.MODULE_NAME} init[/bold] first."
  691. )
  692. raise typer.Exit(1)
  693. # Make sure the .reflex directory exists.
  694. if not os.path.exists(constants.Reflex.DIR):
  695. return True
  696. # Make sure the .web directory exists in frontend mode.
  697. if frontend and not os.path.exists(constants.Dirs.WEB):
  698. return True
  699. if constants.IS_WINDOWS:
  700. console.warn(
  701. """Windows Subsystem for Linux (WSL) is recommended for improving initial install times."""
  702. )
  703. # No need to reinitialize if the app is already initialized.
  704. return False
  705. def is_latest_template() -> bool:
  706. """Whether the app is using the latest template.
  707. Returns:
  708. Whether the app is using the latest template.
  709. """
  710. if not os.path.exists(constants.Reflex.JSON):
  711. return False
  712. with open(constants.Reflex.JSON) as f: # type: ignore
  713. app_version = json.load(f)["version"]
  714. return app_version == constants.Reflex.VERSION
  715. def validate_bun():
  716. """Validate bun if a custom bun path is specified to ensure the bun version meets requirements.
  717. Raises:
  718. Exit: If custom specified bun does not exist or does not meet requirements.
  719. """
  720. # if a custom bun path is provided, make sure its valid
  721. # This is specific to non-FHS OS
  722. bun_path = get_config().bun_path
  723. if bun_path != constants.Bun.DEFAULT_PATH:
  724. console.info(f"Using custom Bun path: {bun_path}")
  725. bun_version = get_bun_version()
  726. if not bun_version:
  727. console.error(
  728. "Failed to obtain bun version. Make sure the specified bun path in your config is correct."
  729. )
  730. raise typer.Exit(1)
  731. elif bun_version < version.parse(constants.Bun.MIN_VERSION):
  732. console.error(
  733. f"Reflex requires bun version {constants.Bun.VERSION} or higher to run, but the detected version is "
  734. f"{bun_version}. If you have specified a custom bun path in your config, make sure to provide one "
  735. f"that satisfies the minimum version requirement."
  736. )
  737. raise typer.Exit(1)
  738. def validate_frontend_dependencies(init=True):
  739. """Validate frontend dependencies to ensure they meet requirements.
  740. Args:
  741. init: whether running `reflex init`
  742. Raises:
  743. Exit: If the package manager is invalid.
  744. """
  745. if not init:
  746. # we only need to validate the package manager when running app.
  747. # `reflex init` will install the deps anyway(if applied).
  748. package_manager = get_package_manager()
  749. if not package_manager:
  750. console.error(
  751. "Could not find NPM package manager. Make sure you have node installed."
  752. )
  753. raise typer.Exit(1)
  754. if not check_node_version():
  755. node_version = get_node_version()
  756. console.error(
  757. f"Reflex requires node version {constants.Node.MIN_VERSION} or higher to run, but the detected version is {node_version}",
  758. )
  759. raise typer.Exit(1)
  760. if constants.IS_WINDOWS:
  761. return
  762. if init:
  763. # we only need bun for package install on `reflex init`.
  764. validate_bun()
  765. def ensure_reflex_installation_id() -> Optional[int]:
  766. """Ensures that a reflex distinct id has been generated and stored in the reflex directory.
  767. Returns:
  768. Distinct id.
  769. """
  770. try:
  771. initialize_reflex_user_directory()
  772. installation_id_file = os.path.join(constants.Reflex.DIR, "installation_id")
  773. installation_id = None
  774. if os.path.exists(installation_id_file):
  775. try:
  776. with open(installation_id_file, "r") as f:
  777. installation_id = int(f.read())
  778. except Exception:
  779. # If anything goes wrong at all... just regenerate.
  780. # Like what? Examples:
  781. # - file not exists
  782. # - file not readable
  783. # - content not parseable as an int
  784. pass
  785. if installation_id is None:
  786. installation_id = random.getrandbits(128)
  787. with open(installation_id_file, "w") as f:
  788. f.write(str(installation_id))
  789. # If we get here, installation_id is definitely set
  790. return installation_id
  791. except Exception as e:
  792. console.debug(f"Failed to ensure reflex installation id: {e}")
  793. return None
  794. def initialize_reflex_user_directory():
  795. """Initialize the reflex user directory."""
  796. # Create the reflex directory.
  797. path_ops.mkdir(constants.Reflex.DIR)
  798. def initialize_frontend_dependencies():
  799. """Initialize all the frontend dependencies."""
  800. # validate dependencies before install
  801. validate_frontend_dependencies()
  802. # Avoid warning about Node installation while we're trying to install it.
  803. global CURRENTLY_INSTALLING_NODE
  804. CURRENTLY_INSTALLING_NODE = True
  805. # Install the frontend dependencies.
  806. processes.run_concurrently(install_node, install_bun)
  807. CURRENTLY_INSTALLING_NODE = False
  808. # Set up the web directory.
  809. initialize_web_directory()
  810. def check_db_initialized() -> bool:
  811. """Check if the database migrations are initialized.
  812. Returns:
  813. True if alembic is initialized (or if database is not used).
  814. """
  815. if get_config().db_url is not None and not Path(constants.ALEMBIC_CONFIG).exists():
  816. console.error(
  817. "Database is not initialized. Run [bold]reflex db init[/bold] first."
  818. )
  819. return False
  820. return True
  821. def check_schema_up_to_date():
  822. """Check if the sqlmodel metadata matches the current database schema."""
  823. if get_config().db_url is None or not Path(constants.ALEMBIC_CONFIG).exists():
  824. return
  825. with model.Model.get_db_engine().connect() as connection:
  826. try:
  827. if model.Model.alembic_autogenerate(
  828. connection=connection,
  829. write_migration_scripts=False,
  830. ):
  831. console.error(
  832. "Detected database schema changes. Run [bold]reflex db makemigrations[/bold] "
  833. "to generate migration scripts.",
  834. )
  835. except CommandError as command_error:
  836. if "Target database is not up to date." in str(command_error):
  837. console.error(
  838. f"{command_error} Run [bold]reflex db migrate[/bold] to update database."
  839. )
  840. def prompt_for_template(templates: list[Template]) -> str:
  841. """Prompt the user to specify a template.
  842. Args:
  843. templates: The templates to choose from.
  844. Returns:
  845. The template name the user selects.
  846. """
  847. # Show the user the URLs of each template to preview.
  848. console.print("\nGet started with a template:")
  849. # Prompt the user to select a template.
  850. id_to_name = {
  851. str(idx): f"{template.name} ({template.demo_url}) - {template.description}"
  852. for idx, template in enumerate(templates)
  853. }
  854. for id in range(len(id_to_name)):
  855. console.print(f"({id}) {id_to_name[str(id)]}")
  856. template = console.ask(
  857. "Which template would you like to use?",
  858. choices=[str(i) for i in range(len(id_to_name))],
  859. show_choices=False,
  860. default="0",
  861. )
  862. # Return the template.
  863. return templates[int(template)].name
  864. def should_show_rx_chakra_migration_instructions() -> bool:
  865. """Should we show the migration instructions for rx.chakra.* => rx.*?.
  866. Returns:
  867. bool: True if we should show the migration instructions.
  868. """
  869. if os.getenv("REFLEX_PROMPT_MIGRATE_TO_RX_CHAKRA") == "yes":
  870. return True
  871. if not Path(constants.Config.FILE).exists():
  872. # They are running reflex init for the first time.
  873. return False
  874. existing_init_reflex_version = None
  875. reflex_json = Path(constants.Dirs.REFLEX_JSON)
  876. if reflex_json.exists():
  877. with reflex_json.open("r") as f:
  878. data = json.load(f)
  879. existing_init_reflex_version = data.get("version", None)
  880. if existing_init_reflex_version is None:
  881. # They clone a reflex app from git for the first time.
  882. # That app may or may not be 0.4 compatible.
  883. # So let's just show these instructions THIS TIME.
  884. return True
  885. if constants.Reflex.VERSION < "0.4":
  886. return False
  887. else:
  888. return existing_init_reflex_version < "0.4"
  889. def show_rx_chakra_migration_instructions():
  890. """Show the migration instructions for rx.chakra.* => rx.*."""
  891. console.log(
  892. "Prior to reflex 0.4.0, rx.* components are based on Chakra UI. They are now based on Radix UI. To stick to Chakra UI, use rx.chakra.*."
  893. )
  894. console.log("")
  895. console.log(
  896. "[bold]Run `reflex script keep-chakra` to automatically update your app."
  897. )
  898. console.log("")
  899. console.log(
  900. "For more details, please see https://reflex.dev/blog/2024-02-16-reflex-v0.4.0/"
  901. )
  902. def migrate_to_rx_chakra():
  903. """Migrate rx.button => r.chakra.button, etc."""
  904. file_pattern = os.path.join(get_config().app_name, "**/*.py")
  905. file_list = glob.glob(file_pattern, recursive=True)
  906. # Populate with all rx.<x> components that have been moved to rx.chakra.<x>
  907. patterns = {
  908. rf"\brx\.{name}\b": f"rx.chakra.{name}"
  909. for name in _get_rx_chakra_component_to_migrate()
  910. }
  911. for file_path in file_list:
  912. with FileInput(file_path, inplace=True) as file:
  913. for _line_num, line in enumerate(file):
  914. for old, new in patterns.items():
  915. line = re.sub(old, new, line)
  916. print(line, end="")
  917. def _get_rx_chakra_component_to_migrate() -> set[str]:
  918. from reflex.components.chakra import ChakraComponent
  919. rx_chakra_names = set(dir(reflex.chakra))
  920. names_to_migrate = set()
  921. # whitelist names will always be rewritten as rx.chakra.<x>
  922. whitelist = {
  923. "ColorModeIcon",
  924. "MultiSelect",
  925. "MultiSelectOption",
  926. "color_mode_icon",
  927. "multi_select",
  928. "multi_select_option",
  929. }
  930. for rx_chakra_name in sorted(rx_chakra_names):
  931. if rx_chakra_name.startswith("_"):
  932. continue
  933. rx_chakra_object = getattr(reflex.chakra, rx_chakra_name)
  934. try:
  935. if (
  936. inspect.ismethod(rx_chakra_object)
  937. and inspect.isclass(rx_chakra_object.__self__)
  938. and issubclass(rx_chakra_object.__self__, ChakraComponent)
  939. ):
  940. names_to_migrate.add(rx_chakra_name)
  941. elif inspect.isclass(rx_chakra_object) and issubclass(
  942. rx_chakra_object, ChakraComponent
  943. ):
  944. names_to_migrate.add(rx_chakra_name)
  945. elif rx_chakra_name in whitelist:
  946. names_to_migrate.add(rx_chakra_name)
  947. except Exception:
  948. raise
  949. return names_to_migrate
  950. def migrate_to_reflex():
  951. """Migration from Pynecone to Reflex."""
  952. # Check if the old config file exists.
  953. if not os.path.exists(constants.Config.PREVIOUS_FILE):
  954. return
  955. # Ask the user if they want to migrate.
  956. action = console.ask(
  957. "Pynecone project detected. Automatically upgrade to Reflex?",
  958. choices=["y", "n"],
  959. )
  960. if action == "n":
  961. return
  962. # Rename pcconfig to rxconfig.
  963. console.log(
  964. f"[bold]Renaming {constants.Config.PREVIOUS_FILE} to {constants.Config.FILE}"
  965. )
  966. os.rename(constants.Config.PREVIOUS_FILE, constants.Config.FILE)
  967. # Find all python files in the app directory.
  968. file_pattern = os.path.join(get_config().app_name, "**/*.py")
  969. file_list = glob.glob(file_pattern, recursive=True)
  970. # Add the config file to the list of files to be migrated.
  971. file_list.append(constants.Config.FILE)
  972. # Migrate all files.
  973. updates = {
  974. "Pynecone": "Reflex",
  975. "pynecone as pc": "reflex as rx",
  976. "pynecone.io": "reflex.dev",
  977. "pynecone": "reflex",
  978. "pc.": "rx.",
  979. "pcconfig": "rxconfig",
  980. }
  981. for file_path in file_list:
  982. with FileInput(file_path, inplace=True) as file:
  983. for line in file:
  984. for old, new in updates.items():
  985. line = line.replace(old, new)
  986. print(line, end="")
  987. def fetch_app_templates() -> dict[str, Template]:
  988. """Fetch the list of app templates from the Reflex backend server.
  989. Returns:
  990. The name and download URL as a dictionary.
  991. """
  992. config = get_config()
  993. if not config.cp_backend_url:
  994. console.info(
  995. "Skip fetching App templates. No backend URL is specified in the config."
  996. )
  997. return {}
  998. try:
  999. response = httpx.get(
  1000. f"{config.cp_backend_url}{constants.Templates.APP_TEMPLATES_ROUTE}"
  1001. )
  1002. response.raise_for_status()
  1003. return {
  1004. template["name"]: Template.parse_obj(template)
  1005. for template in response.json()
  1006. }
  1007. except httpx.HTTPError as ex:
  1008. console.info(f"Failed to fetch app templates: {ex}")
  1009. return {}
  1010. except (TypeError, KeyError, json.JSONDecodeError) as tkje:
  1011. console.info(f"Unable to process server response for app templates: {tkje}")
  1012. return {}
  1013. def create_config_init_app_from_remote_template(
  1014. app_name: str,
  1015. template_url: str,
  1016. ):
  1017. """Create new rxconfig and initialize app using a remote template.
  1018. Args:
  1019. app_name: The name of the app.
  1020. template_url: The path to the template source code as a zip file.
  1021. Raises:
  1022. Exit: If any download, file operations fail or unexpected zip file format.
  1023. """
  1024. # Create a temp directory for the zip download.
  1025. try:
  1026. temp_dir = tempfile.mkdtemp()
  1027. except OSError as ose:
  1028. console.error(f"Failed to create temp directory for download: {ose}")
  1029. raise typer.Exit(1) from ose
  1030. # Use httpx GET with redirects to download the zip file.
  1031. zip_file_path = Path(temp_dir) / "template.zip"
  1032. try:
  1033. # Note: following redirects can be risky. We only allow this for reflex built templates at the moment.
  1034. response = httpx.get(template_url, follow_redirects=True)
  1035. console.debug(f"Server responded download request: {response}")
  1036. response.raise_for_status()
  1037. except httpx.HTTPError as he:
  1038. console.error(f"Failed to download the template: {he}")
  1039. raise typer.Exit(1) from he
  1040. try:
  1041. with open(zip_file_path, "wb") as f:
  1042. f.write(response.content)
  1043. console.debug(f"Downloaded the zip to {zip_file_path}")
  1044. except OSError as ose:
  1045. console.error(f"Unable to write the downloaded zip to disk {ose}")
  1046. raise typer.Exit(1) from ose
  1047. # Create a temp directory for the zip extraction.
  1048. try:
  1049. unzip_dir = Path(tempfile.mkdtemp())
  1050. except OSError as ose:
  1051. console.error(f"Failed to create temp directory for extracting zip: {ose}")
  1052. raise typer.Exit(1) from ose
  1053. try:
  1054. zipfile.ZipFile(zip_file_path).extractall(path=unzip_dir)
  1055. # The zip file downloaded from github looks like:
  1056. # repo-name-branch/**/*, so we need to remove the top level directory.
  1057. if len(subdirs := os.listdir(unzip_dir)) != 1:
  1058. console.error(f"Expected one directory in the zip, found {subdirs}")
  1059. raise typer.Exit(1)
  1060. template_dir = unzip_dir / subdirs[0]
  1061. console.debug(f"Template folder is located at {template_dir}")
  1062. except Exception as uze:
  1063. console.error(f"Failed to unzip the template: {uze}")
  1064. raise typer.Exit(1) from uze
  1065. # Move the rxconfig file here first.
  1066. path_ops.mv(str(template_dir / constants.Config.FILE), constants.Config.FILE)
  1067. new_config = get_config(reload=True)
  1068. # Get the template app's name from rxconfig in case it is different than
  1069. # the source code repo name on github.
  1070. template_name = new_config.app_name
  1071. create_config(app_name)
  1072. initialize_app_directory(
  1073. app_name,
  1074. template_name=template_name,
  1075. template_code_dir_name=template_name,
  1076. template_dir=template_dir,
  1077. )
  1078. # Clean up the temp directories.
  1079. shutil.rmtree(temp_dir)
  1080. shutil.rmtree(unzip_dir)
  1081. def initialize_app(app_name: str, template: str | None = None):
  1082. """Initialize the app either from a remote template or a blank app. If the config file exists, it is considered as reinit.
  1083. Args:
  1084. app_name: The name of the app.
  1085. template: The name of the template to use.
  1086. Raises:
  1087. Exit: If template is directly provided in the command flag and is invalid.
  1088. """
  1089. # Local imports to avoid circular imports.
  1090. from reflex.utils import telemetry
  1091. # Check if the app is already initialized.
  1092. if os.path.exists(constants.Config.FILE):
  1093. telemetry.send("reinit")
  1094. return
  1095. # Get the available templates
  1096. templates: dict[str, Template] = fetch_app_templates()
  1097. # Prompt for a template if not provided.
  1098. if template is None and len(templates) > 0:
  1099. template = prompt_for_template(list(templates.values()))
  1100. elif template is None:
  1101. template = constants.Templates.DEFAULT
  1102. assert template is not None
  1103. # If the blank template is selected, create a blank app.
  1104. if template == constants.Templates.DEFAULT:
  1105. # Default app creation behavior: a blank app.
  1106. create_config(app_name)
  1107. initialize_app_directory(app_name)
  1108. else:
  1109. # Fetch App templates from the backend server.
  1110. console.debug(f"Available templates: {templates}")
  1111. # If user selects a template, it needs to exist.
  1112. if template in templates:
  1113. template_url = templates[template].code_url
  1114. else:
  1115. # Check if the template is a github repo.
  1116. if template.startswith("https://github.com"):
  1117. template_url = f"{template.strip('/')}/archive/main.zip"
  1118. else:
  1119. console.error(f"Template `{template}` not found.")
  1120. raise typer.Exit(1)
  1121. create_config_init_app_from_remote_template(
  1122. app_name=app_name,
  1123. template_url=template_url,
  1124. )
  1125. telemetry.send("init")