utils.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. """Notebook related utilities"""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. from __future__ import annotations
  5. import errno
  6. import importlib.util
  7. import os
  8. import socket
  9. import sys
  10. import warnings
  11. from _frozen_importlib_external import _NamespacePath
  12. from contextlib import contextmanager
  13. from pathlib import Path
  14. from typing import TYPE_CHECKING, Any, NewType
  15. from urllib.parse import (
  16. SplitResult,
  17. quote,
  18. unquote,
  19. urlparse,
  20. urlsplit,
  21. urlunsplit,
  22. )
  23. from urllib.parse import (
  24. urljoin as _urljoin,
  25. )
  26. from urllib.request import pathname2url as _pathname2url
  27. from jupyter_core.utils import ensure_async as _ensure_async
  28. from packaging.version import Version
  29. from tornado.httpclient import AsyncHTTPClient, HTTPClient, HTTPRequest, HTTPResponse
  30. from tornado.netutil import Resolver
  31. if TYPE_CHECKING:
  32. from collections.abc import Generator, Sequence
  33. ApiPath = NewType("ApiPath", str)
  34. # Re-export
  35. urljoin = _urljoin
  36. pathname2url = _pathname2url
  37. ensure_async = _ensure_async
  38. def url_path_join(*pieces: str) -> str:
  39. """Join components of url into a relative url
  40. Use to prevent double slash when joining subpath. This will leave the
  41. initial and final / in place
  42. """
  43. initial = pieces[0].startswith("/")
  44. final = pieces[-1].endswith("/")
  45. stripped = [s.strip("/") for s in pieces]
  46. result = "/".join(s for s in stripped if s)
  47. if initial:
  48. result = "/" + result
  49. if final:
  50. result = result + "/"
  51. if result == "//":
  52. result = "/"
  53. return result
  54. def url_is_absolute(url: str) -> bool:
  55. """Determine whether a given URL is absolute"""
  56. return urlparse(url).path.startswith("/")
  57. def path2url(path: str) -> str:
  58. """Convert a local file path to a URL"""
  59. pieces = [quote(p) for p in path.split(os.sep)]
  60. # preserve trailing /
  61. if pieces[-1] == "":
  62. pieces[-1] = "/"
  63. url = url_path_join(*pieces)
  64. return url
  65. def url2path(url: str) -> str:
  66. """Convert a URL to a local file path"""
  67. pieces = [unquote(p) for p in url.split("/")]
  68. path = os.path.join(*pieces)
  69. return path
  70. def url_escape(path: str) -> str:
  71. """Escape special characters in a URL path
  72. Turns '/foo bar/' into '/foo%20bar/'
  73. """
  74. parts = path.split("/")
  75. return "/".join([quote(p) for p in parts])
  76. def url_unescape(path: str) -> str:
  77. """Unescape special characters in a URL path
  78. Turns '/foo%20bar/' into '/foo bar/'
  79. """
  80. return "/".join([unquote(p) for p in path.split("/")])
  81. def samefile_simple(path: str, other_path: str) -> bool:
  82. """
  83. Fill in for os.path.samefile when it is unavailable (Windows+py2).
  84. Do a case-insensitive string comparison in this case
  85. plus comparing the full stat result (including times)
  86. because Windows + py2 doesn't support the stat fields
  87. needed for identifying if it's the same file (st_ino, st_dev).
  88. Only to be used if os.path.samefile is not available.
  89. Parameters
  90. ----------
  91. path : str
  92. representing a path to a file
  93. other_path : str
  94. representing a path to another file
  95. Returns
  96. -------
  97. same: Boolean that is True if both path and other path are the same
  98. """
  99. path_stat = os.stat(path)
  100. other_path_stat = os.stat(other_path)
  101. return path.lower() == other_path.lower() and path_stat == other_path_stat
  102. def to_os_path(path: ApiPath, root: str = "") -> str:
  103. """Convert an API path to a filesystem path
  104. If given, root will be prepended to the path.
  105. root must be a filesystem path already.
  106. """
  107. parts = str(path).strip("/").split("/")
  108. parts = [p for p in parts if p != ""] # remove duplicate splits
  109. path_ = os.path.join(root, *parts)
  110. return os.path.normpath(path_)
  111. def to_api_path(os_path: str, root: str = "") -> ApiPath:
  112. """Convert a filesystem path to an API path
  113. If given, root will be removed from the path.
  114. root must be a filesystem path already.
  115. """
  116. if os_path.startswith(root):
  117. os_path = os_path[len(root) :]
  118. parts = os_path.strip(os.path.sep).split(os.path.sep)
  119. parts = [p for p in parts if p != ""] # remove duplicate splits
  120. path = "/".join(parts)
  121. return ApiPath(path)
  122. def check_version(v: str, check: str) -> bool:
  123. """check version string v >= check
  124. If dev/prerelease tags result in TypeError for string-number comparison,
  125. it is assumed that the dependency is satisfied.
  126. Users on dev branches are responsible for keeping their own packages up to date.
  127. """
  128. try:
  129. return bool(Version(v) >= Version(check))
  130. except TypeError:
  131. return True
  132. # Copy of IPython.utils.process.check_pid:
  133. def _check_pid_win32(pid: int) -> bool:
  134. import ctypes
  135. # OpenProcess returns 0 if no such process (of ours) exists
  136. # positive int otherwise
  137. return bool(ctypes.windll.kernel32.OpenProcess(1, 0, pid)) # type:ignore[attr-defined]
  138. def _check_pid_posix(pid: int) -> bool:
  139. """Copy of IPython.utils.process.check_pid"""
  140. try:
  141. os.kill(pid, 0)
  142. except OSError as err:
  143. if err.errno == errno.ESRCH:
  144. return False
  145. elif err.errno == errno.EPERM:
  146. # Don't have permission to signal the process - probably means it exists
  147. return True
  148. raise
  149. else:
  150. return True
  151. if sys.platform == "win32":
  152. check_pid = _check_pid_win32
  153. else:
  154. check_pid = _check_pid_posix
  155. async def run_sync_in_loop(maybe_async):
  156. """**DEPRECATED**: Use ``ensure_async`` from jupyter_core instead."""
  157. warnings.warn(
  158. "run_sync_in_loop is deprecated since Jupyter Server 2.0, use 'ensure_async' from jupyter_core instead",
  159. DeprecationWarning,
  160. stacklevel=2,
  161. )
  162. return ensure_async(maybe_async)
  163. def urlencode_unix_socket_path(socket_path: str) -> str:
  164. """Encodes a UNIX socket path string from a socket path for the `http+unix` URI form."""
  165. return socket_path.replace("/", "%2F")
  166. def urldecode_unix_socket_path(socket_path: str) -> str:
  167. """Decodes a UNIX sock path string from an encoded sock path for the `http+unix` URI form."""
  168. return socket_path.replace("%2F", "/")
  169. def urlencode_unix_socket(socket_path: str) -> str:
  170. """Encodes a UNIX socket URL from a socket path for the `http+unix` URI form."""
  171. return "http+unix://%s" % urlencode_unix_socket_path(socket_path)
  172. def unix_socket_in_use(socket_path: str) -> bool:
  173. """Checks whether a UNIX socket path on disk is in use by attempting to connect to it."""
  174. if not os.path.exists(socket_path):
  175. return False
  176. try:
  177. sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  178. sock.connect(socket_path)
  179. except OSError:
  180. return False
  181. else:
  182. return True
  183. finally:
  184. sock.close()
  185. @contextmanager
  186. def _request_for_tornado_client(
  187. urlstring: str, method: str = "GET", body: Any = None, headers: Any = None
  188. ) -> Generator[HTTPRequest, None, None]:
  189. """A utility that provides a context that handles
  190. HTTP, HTTPS, and HTTP+UNIX request.
  191. Creates a tornado HTTPRequest object with a URL
  192. that tornado's HTTPClients can accept.
  193. If the request is made to a unix socket, temporarily
  194. configure the AsyncHTTPClient to resolve the URL
  195. and connect to the proper socket.
  196. """
  197. parts = urlsplit(urlstring)
  198. if parts.scheme in ["http", "https"]:
  199. pass
  200. elif parts.scheme == "http+unix":
  201. # If unix socket, mimic HTTP.
  202. parts = SplitResult(
  203. scheme="http",
  204. netloc=parts.netloc,
  205. path=parts.path,
  206. query=parts.query,
  207. fragment=parts.fragment,
  208. )
  209. class UnixSocketResolver(Resolver):
  210. """A resolver that routes HTTP requests to unix sockets
  211. in tornado HTTP clients.
  212. Due to constraints in Tornados' API, the scheme of the
  213. must be `http` (not `http+unix`). Applications should replace
  214. the scheme in URLS before making a request to the HTTP client.
  215. """
  216. def initialize(self, resolver):
  217. self.resolver = resolver
  218. def close(self):
  219. self.resolver.close()
  220. async def resolve(self, host, port, *args, **kwargs):
  221. return [(socket.AF_UNIX, urldecode_unix_socket_path(host))]
  222. resolver = UnixSocketResolver(resolver=Resolver())
  223. AsyncHTTPClient.configure(None, resolver=resolver)
  224. else:
  225. msg = "Unknown URL scheme."
  226. raise Exception(msg)
  227. # Yield the request for the given client.
  228. url = urlunsplit(parts)
  229. request = HTTPRequest(url, method=method, body=body, headers=headers, validate_cert=False)
  230. yield request
  231. def fetch(
  232. urlstring: str, method: str = "GET", body: Any = None, headers: Any = None
  233. ) -> HTTPResponse:
  234. """
  235. Send a HTTP, HTTPS, or HTTP+UNIX request
  236. to a Tornado Web Server. Returns a tornado HTTPResponse.
  237. """
  238. with _request_for_tornado_client(
  239. urlstring, method=method, body=body, headers=headers
  240. ) as request:
  241. response = HTTPClient(AsyncHTTPClient).fetch(request)
  242. return response
  243. async def async_fetch(
  244. urlstring: str, method: str = "GET", body: Any = None, headers: Any = None, io_loop: Any = None
  245. ) -> HTTPResponse:
  246. """
  247. Send an asynchronous HTTP, HTTPS, or HTTP+UNIX request
  248. to a Tornado Web Server. Returns a tornado HTTPResponse.
  249. """
  250. with _request_for_tornado_client(
  251. urlstring, method=method, body=body, headers=headers
  252. ) as request:
  253. response = await AsyncHTTPClient(io_loop).fetch(request)
  254. return response
  255. def is_namespace_package(namespace: str) -> bool | None:
  256. """Is the provided namespace a Python Namespace Package (PEP420).
  257. https://www.python.org/dev/peps/pep-0420/#specification
  258. Returns `None` if module is not importable.
  259. """
  260. # NOTE: using submodule_search_locations because the loader can be None
  261. try:
  262. spec = importlib.util.find_spec(namespace)
  263. except ValueError: # spec is not set - see https://docs.python.org/3/library/importlib.html#importlib.util.find_spec
  264. return None
  265. if not spec:
  266. # e.g. module not installed
  267. return None
  268. return isinstance(spec.submodule_search_locations, _NamespacePath)
  269. def filefind(filename: str, path_dirs: Sequence[str]) -> str:
  270. """Find a file by looking through a sequence of paths.
  271. For use in FileFindHandler.
  272. Iterates through a sequence of paths looking for a file and returns
  273. the full, absolute path of the first occurrence of the file.
  274. Absolute paths are not accepted for inputs.
  275. This function does not automatically try any paths,
  276. such as the cwd or the user's home directory.
  277. Parameters
  278. ----------
  279. filename : str
  280. The filename to look for. Must be a relative path.
  281. path_dirs : sequence of str
  282. The sequence of paths to look in for the file.
  283. Walk through each element and join with ``filename``.
  284. Only after ensuring the path resolves within the directory is it checked for existence.
  285. Returns
  286. -------
  287. Raises :exc:`OSError` or returns absolute path to file.
  288. """
  289. file_path = Path(filename)
  290. # If the input is an absolute path, reject it
  291. if file_path.is_absolute():
  292. msg = f"{filename} is absolute, filefind only accepts relative paths."
  293. raise OSError(msg)
  294. for path_str in path_dirs:
  295. path = Path(path_str).absolute()
  296. test_path = path / file_path
  297. # os.path.abspath resolves '..', but Path.absolute() doesn't
  298. # Path.resolve() does, but traverses symlinks, which we don't want
  299. test_path = Path(os.path.abspath(test_path))
  300. if not test_path.is_relative_to(path):
  301. # points outside root, e.g. via `filename='../foo'`
  302. continue
  303. # make sure we don't call is_file before we know it's a file within a prefix
  304. # GHSA-hrw6-wg82-cm62 - can leak password hash on windows.
  305. if test_path.is_file():
  306. return os.path.abspath(test_path)
  307. msg = f"File {filename!r} does not exist in any of the search paths: {path_dirs!r}"
  308. raise OSError(msg)
  309. def import_item(name: str) -> Any:
  310. """Import and return ``bar`` given the string ``foo.bar``.
  311. Calling ``bar = import_item("foo.bar")`` is the functional equivalent of
  312. executing the code ``from foo import bar``.
  313. Parameters
  314. ----------
  315. name : str
  316. The fully qualified name of the module/package being imported.
  317. Returns
  318. -------
  319. mod : module object
  320. The module that was imported.
  321. """
  322. parts = name.rsplit(".", 1)
  323. if len(parts) == 2:
  324. # called with 'foo.bar....'
  325. package, obj = parts
  326. module = __import__(package, fromlist=[obj])
  327. try:
  328. pak = getattr(module, obj)
  329. except AttributeError as e:
  330. raise ImportError("No module named %s" % obj) from e
  331. return pak
  332. else:
  333. # called with un-dotted string
  334. return __import__(parts[0])
  335. class JupyterServerAuthWarning(RuntimeWarning):
  336. """Emitted when authentication configuration issue is detected.
  337. Intended for filtering out expected warnings in tests, including
  338. downstream tests, rather than for users to silence this warning.
  339. """