command.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. # PYTHON_ARGCOMPLETE_OK
  2. """The root `jupyter` command.
  3. This does nothing other than dispatch to subcommands or output path info.
  4. """
  5. # Copyright (c) Jupyter Development Team.
  6. # Distributed under the terms of the Modified BSD License.
  7. from __future__ import annotations
  8. import argparse
  9. import errno
  10. import json
  11. import os
  12. import site
  13. import sys
  14. import sysconfig
  15. from pathlib import Path
  16. from shutil import which
  17. from subprocess import Popen
  18. from typing import Any
  19. from . import paths
  20. from .version import __version__
  21. class JupyterParser(argparse.ArgumentParser):
  22. """A Jupyter argument parser."""
  23. @property
  24. def epilog(self) -> str:
  25. """Add subcommands to epilog on request
  26. Avoids searching PATH for subcommands unless help output is requested.
  27. """
  28. subcommands: str = " ".join(list_subcommands())
  29. return f"Available subcommands: {subcommands}"
  30. @epilog.setter
  31. def epilog(self, x: Any) -> None:
  32. """Ignore epilog set in Parser.__init__"""
  33. def argcomplete(self) -> None:
  34. """Trigger auto-completion, if enabled"""
  35. try:
  36. import argcomplete # noqa: PLC0415
  37. argcomplete.autocomplete(self)
  38. except ImportError:
  39. pass
  40. def jupyter_parser() -> JupyterParser:
  41. """Create a jupyter parser object."""
  42. parser = JupyterParser(
  43. description="Jupyter: Interactive Computing",
  44. )
  45. group = parser.add_mutually_exclusive_group(required=False)
  46. # don't use argparse's version action because it prints to stderr on py2
  47. group.add_argument(
  48. "--version", action="store_true", help="show the versions of core jupyter packages and exit"
  49. )
  50. subcommand_action = group.add_argument(
  51. "subcommand", type=str, nargs="?", help="the subcommand to launch"
  52. )
  53. # For argcomplete, supply all known subcommands
  54. subcommand_action.completer = lambda *args, **kwargs: list_subcommands() # type: ignore[attr-defined] # noqa: ARG005
  55. group.add_argument("--config-dir", action="store_true", help="show Jupyter config dir")
  56. group.add_argument("--data-dir", action="store_true", help="show Jupyter data dir")
  57. group.add_argument("--runtime-dir", action="store_true", help="show Jupyter runtime dir")
  58. group.add_argument(
  59. "--paths",
  60. action="store_true",
  61. help="show all Jupyter paths. Add --json for machine-readable format.",
  62. )
  63. parser.add_argument("--json", action="store_true", help="output paths as machine-readable json")
  64. parser.add_argument("--debug", action="store_true", help="output debug information about paths")
  65. return parser
  66. def list_subcommands() -> list[str]:
  67. """List all jupyter subcommands
  68. searches PATH for `jupyter-name`
  69. Returns a list of jupyter's subcommand names, without the `jupyter-` prefix.
  70. Nested children (e.g. jupyter-sub-subsub) are not included.
  71. """
  72. subcommand_tuples = set()
  73. # construct a set of `('foo', 'bar') from `jupyter-foo-bar`
  74. for d in _path_with_self():
  75. try:
  76. bin_paths = list(Path(d).iterdir())
  77. except OSError:
  78. continue
  79. for path in bin_paths:
  80. name = path.name
  81. if name.startswith("jupyter-"):
  82. if sys.platform.startswith("win"):
  83. # remove file-extension on Windows
  84. name = path.stem
  85. subcommand_tuples.add(tuple(name.split("-")[1:]))
  86. # build a set of subcommand strings, excluding subcommands whose parents are defined
  87. subcommands = set()
  88. # Only include `jupyter-foo-bar` if `jupyter-foo` is not already present
  89. for sub_tup in subcommand_tuples:
  90. if not any(sub_tup[:i] in subcommand_tuples for i in range(1, len(sub_tup))):
  91. subcommands.add("-".join(sub_tup))
  92. return sorted(subcommands)
  93. def _execvp(cmd: str, argv: list[str]) -> None:
  94. """execvp, except on Windows where it uses Popen
  95. Python provides execvp on Windows, but its behavior is problematic (Python bug#9148).
  96. """
  97. if sys.platform.startswith("win"):
  98. # PATH is ignored when shell=False,
  99. # so rely on shutil.which
  100. cmd_path = which(cmd)
  101. if cmd_path is None:
  102. msg = f"{cmd!r} not found"
  103. raise OSError(msg, errno.ENOENT)
  104. p = Popen([cmd_path, *argv[1:]]) # noqa: S603
  105. # Don't raise KeyboardInterrupt in the parent process.
  106. # Set this after spawning, to avoid subprocess inheriting handler.
  107. import signal # noqa: PLC0415
  108. signal.signal(signal.SIGINT, signal.SIG_IGN)
  109. p.wait()
  110. sys.exit(p.returncode)
  111. else:
  112. os.execvp(cmd, argv) # noqa: S606
  113. def _jupyter_abspath(subcommand: str) -> str:
  114. """This method get the abspath of a specified jupyter-subcommand with no
  115. changes on ENV.
  116. """
  117. # get env PATH with self
  118. search_path = os.pathsep.join(_path_with_self())
  119. # get the abs path for the jupyter-<subcommand>
  120. jupyter_subcommand = f"jupyter-{subcommand}"
  121. abs_path = which(jupyter_subcommand, path=search_path)
  122. if abs_path is None:
  123. msg = f"\nJupyter command `{jupyter_subcommand}` not found."
  124. raise Exception(msg)
  125. if not os.access(abs_path, os.X_OK):
  126. msg = f"\nJupyter command `{jupyter_subcommand}` is not executable."
  127. raise Exception(msg)
  128. return abs_path
  129. def _path_with_self() -> list[str]:
  130. """Put `jupyter`'s dir at the front of PATH
  131. Ensures that /path/to/jupyter subcommand
  132. will do /path/to/jupyter-subcommand
  133. even if /other/jupyter-subcommand is ahead of it on PATH
  134. """
  135. path_list = (os.environ.get("PATH") or os.defpath).split(os.pathsep)
  136. # Insert the "scripts" directory for this Python installation
  137. # This allows the "jupyter" command to be relocated, while still
  138. # finding subcommands that have been installed in the default
  139. # location.
  140. # We put the scripts directory at the *end* of PATH, so that
  141. # if the user explicitly overrides a subcommand, that override
  142. # still takes effect.
  143. try:
  144. bindir = sysconfig.get_path("scripts")
  145. except KeyError:
  146. # The Python environment does not specify a "scripts" location
  147. pass
  148. else:
  149. path_list.append(bindir)
  150. scripts = [sys.argv[0]]
  151. if Path(scripts[0]).is_symlink():
  152. # include realpath, if `jupyter` is a symlink
  153. scripts.append(os.path.realpath(scripts[0]))
  154. for script in scripts:
  155. bindir = str(Path(script).parent)
  156. if Path(bindir).is_dir() and os.access(script, os.X_OK): # only if it's a script
  157. # ensure executable's dir is on PATH
  158. # avoids missing subcommands when jupyter is run via absolute path
  159. path_list.insert(0, bindir)
  160. return path_list
  161. def _evaluate_argcomplete(parser: JupyterParser) -> list[str]:
  162. """If argcomplete is enabled, trigger autocomplete or return current words
  163. If the first word looks like a subcommand, return the current command
  164. that is attempting to be completed so that the subcommand can evaluate it;
  165. otherwise auto-complete using the main parser.
  166. """
  167. try:
  168. # traitlets >= 5.8 provides some argcomplete support,
  169. # use helper methods to jump to argcomplete
  170. from traitlets.config.argcomplete_config import ( # noqa: PLC0415
  171. get_argcomplete_cwords,
  172. increment_argcomplete_index,
  173. )
  174. cwords = get_argcomplete_cwords()
  175. if cwords and len(cwords) > 1 and not cwords[1].startswith("-"):
  176. # If first completion word looks like a subcommand,
  177. # increment word from which to start handling arguments
  178. increment_argcomplete_index()
  179. return cwords
  180. # Otherwise no subcommand, directly autocomplete and exit
  181. parser.argcomplete()
  182. except ImportError:
  183. # traitlets >= 5.8 not available, just try to complete this without
  184. # worrying about subcommands
  185. parser.argcomplete()
  186. msg = "Control flow should not reach end of autocomplete()"
  187. raise AssertionError(msg)
  188. def main() -> None:
  189. """The command entry point."""
  190. parser = jupyter_parser()
  191. argv = sys.argv
  192. subcommand = None
  193. if "_ARGCOMPLETE" in os.environ:
  194. argv = _evaluate_argcomplete(parser)
  195. subcommand = argv[1]
  196. elif len(argv) > 1 and not argv[1].startswith("-"):
  197. # Don't parse if a subcommand is given
  198. # Avoids argparse gobbling up args passed to subcommand, such as `-h`.
  199. subcommand = argv[1]
  200. else:
  201. args, _opts = parser.parse_known_args()
  202. subcommand = args.subcommand
  203. if args.version:
  204. print("Selected Jupyter core packages...")
  205. for package in [
  206. "IPython",
  207. "ipykernel",
  208. "ipywidgets",
  209. "jupyter_client",
  210. "jupyter_core",
  211. "jupyter_server",
  212. "jupyterlab",
  213. "nbclient",
  214. "nbconvert",
  215. "nbformat",
  216. "notebook",
  217. "qtconsole",
  218. "traitlets",
  219. ]:
  220. try:
  221. if package == "jupyter_core": # We're already here
  222. version = __version__
  223. else:
  224. mod = __import__(package)
  225. version = mod.__version__
  226. except ImportError:
  227. version = "not installed"
  228. print(f"{package:<17}:", version)
  229. return
  230. if args.json and not args.paths:
  231. sys.exit("--json is only used with --paths")
  232. if args.debug and not args.paths:
  233. sys.exit("--debug is only used with --paths")
  234. if args.debug and args.json:
  235. sys.exit("--debug cannot be used with --json")
  236. if args.config_dir:
  237. print(paths.jupyter_config_dir())
  238. return
  239. if args.data_dir:
  240. print(paths.jupyter_data_dir())
  241. return
  242. if args.runtime_dir:
  243. print(paths.jupyter_runtime_dir())
  244. return
  245. if args.paths:
  246. data = {}
  247. data["runtime"] = [paths.jupyter_runtime_dir()]
  248. data["config"] = paths.jupyter_config_path()
  249. data["data"] = paths.jupyter_path()
  250. if args.json:
  251. print(json.dumps(data))
  252. else:
  253. if args.debug:
  254. env = os.environ
  255. if paths.use_platform_dirs():
  256. print(
  257. "JUPYTER_PLATFORM_DIRS is set to a true value, so we use platformdirs to find platform-specific directories"
  258. )
  259. else:
  260. print(
  261. "JUPYTER_PLATFORM_DIRS is set to a false value, or is not set, so we use hardcoded legacy paths for platform-specific directories"
  262. )
  263. if paths.prefer_environment_over_user():
  264. print(
  265. "JUPYTER_PREFER_ENV_PATH is set to a true value, or JUPYTER_PREFER_ENV_PATH is not set and we detected a virtual environment, making the environment-level path preferred over the user-level path for data and config"
  266. )
  267. else:
  268. print(
  269. "JUPYTER_PREFER_ENV_PATH is set to a false value, or JUPYTER_PREFER_ENV_PATH is not set and we did not detect a virtual environment, making the user-level path preferred over the environment-level path for data and config"
  270. )
  271. # config path list
  272. if env.get("JUPYTER_NO_CONFIG"):
  273. print(
  274. "JUPYTER_NO_CONFIG is set, making the config path list only a single temporary directory"
  275. )
  276. else:
  277. print(
  278. "JUPYTER_NO_CONFIG is not set, so we use the full path list for config"
  279. )
  280. if env.get("JUPYTER_CONFIG_PATH"):
  281. print(
  282. f"JUPYTER_CONFIG_PATH is set to '{env.get('JUPYTER_CONFIG_PATH')}', which is prepended to the config path list (unless JUPYTER_NO_CONFIG is set)"
  283. )
  284. else:
  285. print(
  286. "JUPYTER_CONFIG_PATH is not set, so we do not prepend anything to the config paths"
  287. )
  288. if env.get("JUPYTER_CONFIG_DIR"):
  289. print(
  290. f"JUPYTER_CONFIG_DIR is set to '{env.get('JUPYTER_CONFIG_DIR')}', overriding the default user-level config directory"
  291. )
  292. else:
  293. print(
  294. "JUPYTER_CONFIG_DIR is not set, so we use the default user-level config directory"
  295. )
  296. if site.ENABLE_USER_SITE:
  297. print(
  298. f"Python's site.ENABLE_USER_SITE is True, so we add the user site directory '{site.getuserbase()}'"
  299. )
  300. else:
  301. print(
  302. f"Python's site.ENABLE_USER_SITE is not True, so we do not add the Python site user directory '{site.getuserbase()}'"
  303. )
  304. # data path list
  305. if env.get("JUPYTER_PATH"):
  306. print(
  307. f"JUPYTER_PATH is set to '{env.get('JUPYTER_PATH')}', which is prepended to the data paths"
  308. )
  309. else:
  310. print(
  311. "JUPYTER_PATH is not set, so we do not prepend anything to the data paths"
  312. )
  313. if env.get("JUPYTER_DATA_DIR"):
  314. print(
  315. f"JUPYTER_DATA_DIR is set to '{env.get('JUPYTER_DATA_DIR')}', overriding the default user-level data directory"
  316. )
  317. else:
  318. print(
  319. "JUPYTER_DATA_DIR is not set, so we use the default user-level data directory"
  320. )
  321. # runtime directory
  322. if env.get("JUPYTER_RUNTIME_DIR"):
  323. print(
  324. f"JUPYTER_RUNTIME_DIR is set to '{env.get('JUPYTER_RUNTIME_DIR')}', overriding the default runtime directory"
  325. )
  326. else:
  327. print(
  328. "JUPYTER_RUNTIME_DIR is not set, so we use the default runtime directory"
  329. )
  330. print()
  331. for name in sorted(data):
  332. path = data[name]
  333. print(f"{name}:")
  334. for p in path:
  335. print(" " + p)
  336. return
  337. if not subcommand:
  338. parser.print_help(file=sys.stderr)
  339. sys.exit("\nPlease specify a subcommand or one of the optional arguments.")
  340. try:
  341. command = _jupyter_abspath(subcommand)
  342. except Exception as e:
  343. parser.print_help(file=sys.stderr)
  344. # special-case alias of "jupyter help" to "jupyter --help"
  345. if subcommand == "help":
  346. return
  347. sys.exit(str(e))
  348. try:
  349. _execvp(command, [command, *argv[2:]])
  350. except OSError as e:
  351. sys.exit(f"Error executing Jupyter command {subcommand!r}: {e}")
  352. if __name__ == "__main__":
  353. main()