Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

273 lignes
7.1 KiB

  1. # imports - third party imports
  2. import click
  3. @click.command("init", help="Initialize a new bench instance in the specified path")
  4. @click.argument("path")
  5. @click.option(
  6. "--version",
  7. "--xhiveframework-branch",
  8. "xhiveframework_branch",
  9. default=None,
  10. help="Clone a particular branch of xhiveframework",
  11. )
  12. @click.option(
  13. "--ignore-exist", is_flag=True, default=False, help="Ignore if Bench instance exists."
  14. )
  15. @click.option(
  16. "--python", type=str, default="python3", help="Path to Python Executable."
  17. )
  18. @click.option(
  19. "--apps_path", default=None, help="path to json files with apps to install after init"
  20. )
  21. @click.option("--xhiveframework-path", default=None, help="path to xhiveframework repo")
  22. @click.option("--clone-from", default=None, help="copy repos from path")
  23. @click.option(
  24. "--clone-without-update", is_flag=True, help="copy repos from path without update"
  25. )
  26. @click.option("--no-procfile", is_flag=True, help="Do not create a Procfile")
  27. @click.option(
  28. "--no-backups",
  29. is_flag=True,
  30. help="Do not set up automatic periodic backups for all sites on this bench",
  31. )
  32. @click.option(
  33. "--skip-redis-config-generation",
  34. is_flag=True,
  35. help="Skip redis config generation if already specifying the common-site-config file",
  36. )
  37. @click.option("--skip-assets", is_flag=True, default=False, help="Do not build assets")
  38. @click.option("--install-app", help="Install particular app after initialization")
  39. @click.option("--verbose", is_flag=True, help="Verbose output during install")
  40. @click.option(
  41. "--dev",
  42. is_flag=True,
  43. default=False,
  44. help="Enable developer mode and install development dependencies.",
  45. )
  46. def init(
  47. path,
  48. apps_path,
  49. xhiveframework_path,
  50. xhiveframework_branch,
  51. no_procfile,
  52. no_backups,
  53. clone_from,
  54. verbose,
  55. skip_redis_config_generation,
  56. clone_without_update,
  57. ignore_exist=False,
  58. skip_assets=False,
  59. python="python3",
  60. install_app=None,
  61. dev=False,
  62. ):
  63. import os
  64. from bench.utils import log
  65. from bench.utils.system import init
  66. if not ignore_exist and os.path.exists(path):
  67. log(f"Bench instance already exists at {path}", level=2)
  68. return
  69. try:
  70. init(
  71. path,
  72. apps_path=apps_path, # can be used from --config flag? Maybe config file could have more info?
  73. no_procfile=no_procfile,
  74. no_backups=no_backups,
  75. xhiveframework_path=xhiveframework_path,
  76. xhiveframework_branch=xhiveframework_branch,
  77. install_app=install_app,
  78. clone_from=clone_from,
  79. skip_redis_config_generation=skip_redis_config_generation,
  80. clone_without_update=clone_without_update,
  81. skip_assets=skip_assets,
  82. python=python,
  83. verbose=verbose,
  84. dev=dev,
  85. )
  86. log(f"Bench {path} initialized", level=1)
  87. except SystemExit:
  88. raise
  89. except Exception:
  90. import shutil
  91. import time
  92. from bench.utils import get_traceback
  93. # add a sleep here so that the traceback of other processes doesnt overlap with the prompts
  94. time.sleep(1)
  95. print(get_traceback())
  96. log(f"There was a problem while creating {path}", level=2)
  97. if click.confirm("Do you want to rollback these changes?", abort=True):
  98. log(f'Rolling back Bench "{path}"')
  99. if os.path.exists(path):
  100. shutil.rmtree(path)
  101. @click.command("drop")
  102. @click.argument("path")
  103. def drop(path):
  104. from bench.bench import Bench
  105. from bench.exceptions import BenchNotFoundError, ValidationError
  106. bench = Bench(path)
  107. if not bench.exists:
  108. raise BenchNotFoundError(f"Bench {bench.name} does not exist")
  109. if bench.sites:
  110. raise ValidationError("Cannot remove non-empty bench directory")
  111. bench.drop()
  112. print("Bench dropped")
  113. @click.command(
  114. ["get", "get-app"],
  115. help="Clone an app from the internet or filesystem and set it up in your bench",
  116. )
  117. @click.argument("name", nargs=-1) # Dummy argument for backward compatibility
  118. @click.argument("git-url")
  119. @click.option("--branch", default=None, help="branch to checkout")
  120. @click.option("--overwrite", is_flag=True, default=False)
  121. @click.option("--skip-assets", is_flag=True, default=False, help="Do not build assets")
  122. @click.option(
  123. "--soft-link",
  124. is_flag=True,
  125. default=False,
  126. help="Create a soft link to git repo instead of clone.",
  127. )
  128. @click.option(
  129. "--init-bench", is_flag=True, default=False, help="Initialize Bench if not in one"
  130. )
  131. @click.option(
  132. "--resolve-deps",
  133. is_flag=True,
  134. default=False,
  135. help="Resolve dependencies before installing app",
  136. )
  137. @click.option(
  138. "--cache-key",
  139. type=str,
  140. default=None,
  141. help="Caches get-app artifacts if provided (only first 10 chars is used)",
  142. )
  143. @click.option(
  144. "--compress-artifacts",
  145. is_flag=True,
  146. default=False,
  147. help="Whether to gzip get-app artifacts that are to be cached",
  148. )
  149. def get_app(
  150. git_url,
  151. branch,
  152. name=None,
  153. overwrite=False,
  154. skip_assets=False,
  155. soft_link=False,
  156. init_bench=False,
  157. resolve_deps=False,
  158. cache_key=None,
  159. compress_artifacts=False,
  160. ):
  161. "clone an app from the internet and set it up in your bench"
  162. from bench.app import get_app
  163. get_app(
  164. git_url,
  165. branch=branch,
  166. skip_assets=skip_assets,
  167. overwrite=overwrite,
  168. soft_link=soft_link,
  169. init_bench=init_bench,
  170. resolve_deps=resolve_deps,
  171. cache_key=cache_key,
  172. compress_artifacts=compress_artifacts,
  173. )
  174. @click.command("new-app", help="Create a new Xhiveframework application under apps folder")
  175. @click.option(
  176. "--no-git",
  177. is_flag=True,
  178. flag_value="--no-git",
  179. help="Do not initialize git repository for the app (available in Xhiveframework v14+)",
  180. )
  181. @click.argument("app-name")
  182. def new_app(app_name, no_git=None):
  183. from bench.app import new_app
  184. new_app(app_name, no_git)
  185. @click.command(
  186. ["remove", "rm", "remove-app"],
  187. help=(
  188. "Completely remove app from bench and re-build assets if not installed on any site"
  189. ),
  190. )
  191. @click.option("--no-backup", is_flag=True, help="Do not backup app before removing")
  192. @click.option("--force", is_flag=True, help="Force remove app")
  193. @click.argument("app-name")
  194. def remove_app(app_name, no_backup=False, force=False):
  195. from bench.bench import Bench
  196. bench = Bench(".")
  197. bench.uninstall(app_name, no_backup=no_backup, force=force)
  198. @click.command("exclude-app", help="Exclude app from updating")
  199. @click.argument("app_name")
  200. def exclude_app_for_update(app_name):
  201. from bench.app import add_to_excluded_apps_txt
  202. add_to_excluded_apps_txt(app_name)
  203. @click.command("include-app", help="Include app for updating")
  204. @click.argument("app_name")
  205. def include_app_for_update(app_name):
  206. "Include app from updating"
  207. from bench.app import remove_from_excluded_apps_txt
  208. remove_from_excluded_apps_txt(app_name)
  209. @click.command(
  210. "pip",
  211. context_settings={"ignore_unknown_options": True, "help_option_names": []},
  212. help="For pip help use `bench pip help [COMMAND]` or `bench pip [COMMAND] -h`",
  213. )
  214. @click.argument("args", nargs=-1)
  215. @click.pass_context
  216. def pip(ctx, args):
  217. "Run pip commands in bench env"
  218. import os
  219. from bench.utils.bench import get_env_cmd
  220. env_py = get_env_cmd("python")
  221. os.execv(env_py, (env_py, "-m", "pip") + args)
  222. @click.command(
  223. "validate-dependencies",
  224. help="Validates that all requirements specified in xhiveframework-dependencies are met curently.",
  225. )
  226. @click.pass_context
  227. def validate_dependencies(ctx):
  228. "Validate all specified xhiveframework-dependencies."
  229. from bench.bench import Bench
  230. from bench.app import App
  231. bench = Bench(".")
  232. for app_name in bench.apps:
  233. app = App(app_name, bench=bench)
  234. app.validate_app_dependencies(throw=True)