prerequisites.py 49 KB

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