setup.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. import argparse
  2. import errno
  3. import glob
  4. import io
  5. import logging
  6. import os
  7. import re
  8. import shutil
  9. import subprocess
  10. import sys
  11. import tarfile
  12. import tempfile
  13. import zipfile
  14. from itertools import chain
  15. from itertools import takewhile
  16. import urllib.error
  17. import urllib.parse
  18. import urllib.request
  19. logger = logging.getLogger(__name__)
  20. SUPPORTED_PYTHONS = [(3, 6), (3, 7), (3, 8), (3, 9)]
  21. SUPPORTED_BAZEL = (3, 2, 0)
  22. ROOT_DIR = os.path.dirname(__file__)
  23. BUILD_JAVA = os.getenv("RAY_INSTALL_JAVA") == "1"
  24. INSTALL_CPP = os.getenv("RAY_INSTALL_CPP") == "1"
  25. PICKLE5_SUBDIR = os.path.join("ray", "pickle5_files")
  26. THIRDPARTY_SUBDIR = os.path.join("ray", "thirdparty_files")
  27. CLEANABLE_SUBDIRS = [PICKLE5_SUBDIR, THIRDPARTY_SUBDIR]
  28. exe_suffix = ".exe" if sys.platform == "win32" else ""
  29. # .pyd is the extension Python requires on Windows for shared libraries.
  30. # https://docs.python.org/3/faq/windows.html#is-a-pyd-file-the-same-as-a-dll
  31. pyd_suffix = ".pyd" if sys.platform == "win32" else ".so"
  32. pickle5_url = ("https://github.com/pitrou/pickle5-backport/archive/"
  33. "c0c1a158f59366696161e0dffdd10cfe17601372.tar.gz")
  34. # Ideally, we could include these files by putting them in a
  35. # MANIFEST.in or using the package_data argument to setup, but the
  36. # MANIFEST.in gets applied at the very beginning when setup.py runs
  37. # before these files have been created, so we have to move the files
  38. # manually.
  39. # NOTE: The lists below must be kept in sync with ray/BUILD.bazel.
  40. ray_files = [
  41. "ray/core/src/ray/thirdparty/redis/src/redis-server" + exe_suffix,
  42. "ray/core/src/ray/gcs/redis_module/libray_redis_module.so",
  43. "ray/_raylet" + pyd_suffix,
  44. "ray/core/src/ray/gcs/gcs_server" + exe_suffix,
  45. "ray/core/src/ray/raylet/raylet" + exe_suffix,
  46. "ray/streaming/_streaming.so",
  47. ]
  48. if BUILD_JAVA or os.path.exists(
  49. os.path.join(ROOT_DIR, "ray/jars/ray_dist.jar")):
  50. ray_files.append("ray/jars/ray_dist.jar")
  51. if INSTALL_CPP:
  52. ray_files.append("ray/core/src/ray/cpp/default_worker")
  53. # C++ API library and project template files.
  54. ray_files += [
  55. os.path.join(dirpath, filename)
  56. for dirpath, dirnames, filenames in os.walk("ray/cpp")
  57. for filename in filenames
  58. ]
  59. # These are the directories where automatically generated Python protobuf
  60. # bindings are created.
  61. generated_python_directories = [
  62. "ray/core/generated",
  63. "ray/streaming/generated",
  64. ]
  65. ray_files.append("ray/nightly-wheels.yaml")
  66. # Autoscaler files.
  67. ray_files += [
  68. "ray/autoscaler/aws/defaults.yaml",
  69. "ray/autoscaler/azure/defaults.yaml",
  70. "ray/autoscaler/_private/_azure/azure-vm-template.json",
  71. "ray/autoscaler/_private/_azure/azure-config-template.json",
  72. "ray/autoscaler/gcp/defaults.yaml",
  73. "ray/autoscaler/local/defaults.yaml",
  74. "ray/autoscaler/kubernetes/defaults.yaml",
  75. "ray/autoscaler/_private/_kubernetes/kubectl-rsync.sh",
  76. "ray/autoscaler/staroid/defaults.yaml",
  77. "ray/autoscaler/ray-schema.json",
  78. ]
  79. # Dashboard files.
  80. ray_files += [
  81. os.path.join(dirpath, filename) for dirpath, dirnames, filenames in
  82. os.walk("ray/new_dashboard/client/build") for filename in filenames
  83. ]
  84. # If you're adding dependencies for ray extras, please
  85. # also update the matching section of requirements/requirements.txt
  86. # in this directory
  87. extras = {
  88. "default": ["colorful"],
  89. "serve": ["uvicorn", "requests", "starlette", "fastapi"],
  90. "tune": ["pandas", "tabulate", "tensorboardX>=1.9"],
  91. "k8s": ["kubernetes"],
  92. "observability": [
  93. "opentelemetry-api==1.1.0", "opentelemetry-sdk==1.1.0",
  94. "opentelemetry-exporter-otlp==1.1.0"
  95. ]
  96. }
  97. if sys.version_info >= (3, 7, 0):
  98. extras["k8s"].append("kopf")
  99. extras["rllib"] = extras["tune"] + [
  100. "dm_tree",
  101. "gym",
  102. "lz4",
  103. "opencv-python-headless<=4.3.0.36",
  104. "pyyaml",
  105. "scipy",
  106. ]
  107. extras["all"] = list(set(chain.from_iterable(extras.values())))
  108. # These are the main dependencies for users of ray. This list
  109. # should be carefully curated. If you change it, please reflect
  110. # the change in the matching section of requirements/requirements.txt
  111. install_requires = [
  112. # TODO(alex) Pin the version once this PR is
  113. # included in the stable release.
  114. # https://github.com/aio-libs/aiohttp/pull/4556#issuecomment-679228562
  115. "aiohttp",
  116. "aiohttp_cors",
  117. "aioredis < 2",
  118. "click >= 7.0",
  119. "colorama",
  120. "dataclasses; python_version < '3.7'",
  121. "filelock",
  122. "gpustat",
  123. "grpcio >= 1.28.1",
  124. "jsonschema",
  125. "msgpack >= 1.0.0, < 2.0.0",
  126. "numpy >= 1.16; python_version < '3.9'",
  127. "numpy >= 1.19.3; python_version >= '3.9'",
  128. "protobuf >= 3.15.3",
  129. "py-spy >= 0.2.0",
  130. "pydantic >= 1.8",
  131. "pyyaml",
  132. "requests",
  133. "redis >= 3.5.0",
  134. "opencensus",
  135. "prometheus_client >= 0.7.1",
  136. ]
  137. def is_native_windows_or_msys():
  138. """Check to see if we are running on native Windows,
  139. but NOT WSL (which is seen as Linux)."""
  140. return sys.platform == "msys" or sys.platform == "win32"
  141. def is_invalid_windows_platform():
  142. # 'GCC' check is how you detect MinGW:
  143. # https://github.com/msys2/MINGW-packages/blob/abd06ca92d876b9db05dd65f27d71c4ebe2673a9/mingw-w64-python2/0410-MINGW-build-extensions-with-GCC.patch#L53
  144. platform = sys.platform
  145. ver = sys.version
  146. return platform == "msys" or (platform == "win32" and ver and "GCC" in ver)
  147. # Calls Bazel in PATH, falling back to the standard user installatation path
  148. # (~/.bazel/bin/bazel) if it isn't found.
  149. def bazel_invoke(invoker, cmdline, *args, **kwargs):
  150. home = os.path.expanduser("~")
  151. first_candidate = os.getenv("BAZEL_PATH", "bazel")
  152. candidates = [first_candidate]
  153. if sys.platform == "win32":
  154. mingw_dir = os.getenv("MINGW_DIR")
  155. if mingw_dir:
  156. candidates.append(mingw_dir + "/bin/bazel.exe")
  157. else:
  158. candidates.append(os.path.join(home, ".bazel", "bin", "bazel"))
  159. result = None
  160. for i, cmd in enumerate(candidates):
  161. try:
  162. result = invoker([cmd] + cmdline, *args, **kwargs)
  163. break
  164. except IOError:
  165. if i >= len(candidates) - 1:
  166. raise
  167. return result
  168. def download(url):
  169. try:
  170. result = urllib.request.urlopen(url).read()
  171. except urllib.error.URLError:
  172. # This fallback is necessary on Python 3.5 on macOS due to TLS 1.2.
  173. curl_args = ["curl", "-s", "-L", "-f", "-o", "-", url]
  174. result = subprocess.check_output(curl_args)
  175. return result
  176. # Installs pickle5-backport into the local subdirectory.
  177. def download_pickle5(pickle5_dir):
  178. pickle5_file = urllib.parse.unquote(
  179. urllib.parse.urlparse(pickle5_url).path)
  180. pickle5_name = re.sub("\\.tar\\.gz$", ".tgz", pickle5_file, flags=re.I)
  181. url_path_parts = os.path.splitext(pickle5_name)[0].split("/")
  182. (project, commit) = (url_path_parts[2], url_path_parts[4])
  183. pickle5_archive = download(pickle5_url)
  184. with tempfile.TemporaryDirectory() as work_dir:
  185. tf = tarfile.open(None, "r", io.BytesIO(pickle5_archive))
  186. try:
  187. tf.extractall(work_dir)
  188. finally:
  189. tf.close()
  190. src_dir = os.path.join(work_dir, project + "-" + commit)
  191. args = [sys.executable, "setup.py", "-q", "bdist_wheel"]
  192. subprocess.check_call(args, cwd=src_dir)
  193. for wheel in glob.glob(os.path.join(src_dir, "dist", "*.whl")):
  194. wzf = zipfile.ZipFile(wheel, "r")
  195. try:
  196. wzf.extractall(pickle5_dir)
  197. finally:
  198. wzf.close()
  199. def build(build_python, build_java, build_cpp):
  200. if tuple(sys.version_info[:2]) not in SUPPORTED_PYTHONS:
  201. msg = ("Detected Python version {}, which is not supported. "
  202. "Only Python {} are supported.").format(
  203. ".".join(map(str, sys.version_info[:2])),
  204. ", ".join(".".join(map(str, v)) for v in SUPPORTED_PYTHONS))
  205. raise RuntimeError(msg)
  206. if is_invalid_windows_platform():
  207. msg = ("Please use official native CPython on Windows,"
  208. " not Cygwin/MSYS/MSYS2/MinGW/etc.\n" +
  209. "Detected: {}\n at: {!r}".format(sys.version, sys.executable))
  210. raise OSError(msg)
  211. bazel_env = dict(os.environ, PYTHON3_BIN_PATH=sys.executable)
  212. if is_native_windows_or_msys():
  213. SHELL = bazel_env.get("SHELL")
  214. if SHELL:
  215. bazel_env.setdefault("BAZEL_SH", os.path.normpath(SHELL))
  216. BAZEL_SH = bazel_env["BAZEL_SH"]
  217. SYSTEMROOT = os.getenv("SystemRoot")
  218. wsl_bash = os.path.join(SYSTEMROOT, "System32", "bash.exe")
  219. if (not BAZEL_SH) and SYSTEMROOT and os.path.isfile(wsl_bash):
  220. msg = ("You appear to have Bash from WSL,"
  221. " which Bazel may invoke unexpectedly. "
  222. "To avoid potential problems,"
  223. " please explicitly set the {name!r}"
  224. " environment variable for Bazel.").format(name="BAZEL_SH")
  225. raise RuntimeError(msg)
  226. # Check if the current Python already has pickle5 (either comes with newer
  227. # Python versions, or has been installed by us before).
  228. pickle5 = None
  229. if sys.version_info >= (3, 8, 2):
  230. import pickle as pickle5
  231. else:
  232. try:
  233. import pickle5
  234. except ImportError:
  235. pass
  236. if not pickle5:
  237. download_pickle5(os.path.join(ROOT_DIR, PICKLE5_SUBDIR))
  238. # Note: We are passing in sys.executable so that we use the same
  239. # version of Python to build packages inside the build.sh script. Note
  240. # that certain flags will not be passed along such as --user or sudo.
  241. # TODO(rkn): Fix this.
  242. if not os.getenv("SKIP_THIRDPARTY_INSTALL"):
  243. pip_packages = ["psutil", "setproctitle==1.2.2"]
  244. subprocess.check_call(
  245. [
  246. sys.executable, "-m", "pip", "install", "-q",
  247. "--target=" + os.path.join(ROOT_DIR, THIRDPARTY_SUBDIR)
  248. ] + pip_packages,
  249. env=dict(os.environ, CC="gcc"))
  250. version_info = bazel_invoke(subprocess.check_output, ["--version"])
  251. bazel_version_str = version_info.rstrip().decode("utf-8").split(" ", 1)[1]
  252. bazel_version_split = bazel_version_str.split(".")
  253. bazel_version_digits = [
  254. "".join(takewhile(str.isdigit, s)) for s in bazel_version_split
  255. ]
  256. bazel_version = tuple(map(int, bazel_version_digits))
  257. if bazel_version < SUPPORTED_BAZEL:
  258. logger.warning("Expected Bazel version {} but found {}".format(
  259. ".".join(map(str, SUPPORTED_BAZEL)), bazel_version_str))
  260. bazel_targets = []
  261. bazel_targets += ["//:ray_pkg"] if build_python else []
  262. bazel_targets += ["//cpp:ray_cpp_pkg"] if build_cpp else []
  263. bazel_targets += ["//java:ray_java_pkg"] if build_java else []
  264. return bazel_invoke(
  265. subprocess.check_call,
  266. ["build", "--verbose_failures", "--"] + bazel_targets,
  267. env=bazel_env)
  268. def walk_directory(directory):
  269. file_list = []
  270. for (root, dirs, filenames) in os.walk(directory):
  271. for name in filenames:
  272. file_list.append(os.path.join(root, name))
  273. return file_list
  274. def copy_file(target_dir, filename, rootdir):
  275. # TODO(rkn): This feels very brittle. It may not handle all cases. See
  276. # https://github.com/apache/arrow/blob/master/python/setup.py for an
  277. # example.
  278. # File names can be absolute paths, e.g. from walk_directory().
  279. source = os.path.relpath(filename, rootdir)
  280. destination = os.path.join(target_dir, source)
  281. # Create the target directory if it doesn't already exist.
  282. os.makedirs(os.path.dirname(destination), exist_ok=True)
  283. if not os.path.exists(destination):
  284. if sys.platform == "win32":
  285. # Does not preserve file mode (needed to avoid read-only bit)
  286. shutil.copyfile(source, destination, follow_symlinks=True)
  287. else:
  288. # Preserves file mode (needed to copy executable bit)
  289. shutil.copy(source, destination, follow_symlinks=True)
  290. return 1
  291. return 0
  292. def find_version(*filepath):
  293. # Extract version information from filepath
  294. with open(os.path.join(ROOT_DIR, *filepath)) as fp:
  295. version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
  296. fp.read(), re.M)
  297. if version_match:
  298. return version_match.group(1)
  299. raise RuntimeError("Unable to find version string.")
  300. def pip_run(build_ext):
  301. build(True, BUILD_JAVA, True)
  302. files_to_include = list(ray_files)
  303. # We also need to install pickle5 along with Ray, so make sure that the
  304. # relevant non-Python pickle5 files get copied.
  305. pickle5_dir = os.path.join(ROOT_DIR, PICKLE5_SUBDIR)
  306. files_to_include += walk_directory(os.path.join(pickle5_dir, "pickle5"))
  307. thirdparty_dir = os.path.join(ROOT_DIR, THIRDPARTY_SUBDIR)
  308. files_to_include += walk_directory(thirdparty_dir)
  309. # Copy over the autogenerated protobuf Python bindings.
  310. for directory in generated_python_directories:
  311. for filename in os.listdir(directory):
  312. if filename[-3:] == ".py":
  313. files_to_include.append(os.path.join(directory, filename))
  314. copied_files = 0
  315. for filename in files_to_include:
  316. copied_files += copy_file(build_ext.build_lib, filename, ROOT_DIR)
  317. print("# of files copied to {}: {}".format(build_ext.build_lib,
  318. copied_files))
  319. def api_main(program, *args):
  320. parser = argparse.ArgumentParser()
  321. choices = ["build", "bazel_version", "python_versions", "clean", "help"]
  322. parser.add_argument("command", type=str, choices=choices)
  323. parser.add_argument(
  324. "-l",
  325. "--language",
  326. default="python,cpp",
  327. type=str,
  328. help="A list of languages to build native libraries. "
  329. "Supported languages include \"python\" and \"java\". "
  330. "If not specified, only the Python library will be built.")
  331. parsed_args = parser.parse_args(args)
  332. result = None
  333. if parsed_args.command == "build":
  334. kwargs = dict(build_python=False, build_java=False, build_cpp=False)
  335. for lang in parsed_args.language.split(","):
  336. if "python" in lang:
  337. kwargs.update(build_python=True)
  338. elif "java" in lang:
  339. kwargs.update(build_java=True)
  340. elif "cpp" in lang:
  341. kwargs.update(build_cpp=True)
  342. else:
  343. raise ValueError("invalid language: {!r}".format(lang))
  344. result = build(**kwargs)
  345. elif parsed_args.command == "bazel_version":
  346. print(".".join(map(str, SUPPORTED_BAZEL)))
  347. elif parsed_args.command == "python_versions":
  348. for version in SUPPORTED_PYTHONS:
  349. # NOTE: On Windows this will print "\r\n" on the command line.
  350. # Strip it out by piping to tr -d "\r".
  351. print(".".join(map(str, version)))
  352. elif parsed_args.command == "clean":
  353. def onerror(function, path, excinfo):
  354. nonlocal result
  355. if excinfo[1].errno != errno.ENOENT:
  356. msg = excinfo[1].strerror
  357. logger.error("cannot remove {}: {}".format(path, msg))
  358. result = 1
  359. for subdir in CLEANABLE_SUBDIRS:
  360. shutil.rmtree(os.path.join(ROOT_DIR, subdir), onerror=onerror)
  361. elif parsed_args.command == "help":
  362. parser.print_help()
  363. else:
  364. raise ValueError("Invalid command: {!r}".format(parsed_args.command))
  365. return result
  366. if __name__ == "__api__":
  367. api_main(*sys.argv)
  368. if __name__ == "__main__":
  369. import setuptools
  370. import setuptools.command.build_ext
  371. class build_ext(setuptools.command.build_ext.build_ext):
  372. def run(self):
  373. return pip_run(self)
  374. class BinaryDistribution(setuptools.Distribution):
  375. def has_ext_modules(self):
  376. return True
  377. setuptools.setup(
  378. name="ray",
  379. version=find_version("ray", "__init__.py"),
  380. author="Ray Team",
  381. author_email="ray-dev@googlegroups.com",
  382. description=("Ray provides a simple, universal API for building "
  383. "distributed applications."),
  384. long_description=io.open(
  385. os.path.join(ROOT_DIR, os.path.pardir, "README.rst"),
  386. "r",
  387. encoding="utf-8").read(),
  388. url="https://github.com/ray-project/ray",
  389. keywords=("ray distributed parallel machine-learning hyperparameter-tuning"
  390. "reinforcement-learning deep-learning serving python"),
  391. classifiers=[
  392. "Programming Language :: Python :: 3.6",
  393. "Programming Language :: Python :: 3.7",
  394. "Programming Language :: Python :: 3.8",
  395. "Programming Language :: Python :: 3.9",
  396. ],
  397. packages=setuptools.find_packages(),
  398. cmdclass={"build_ext": build_ext},
  399. # The BinaryDistribution argument triggers build_ext.
  400. distclass=BinaryDistribution,
  401. install_requires=install_requires,
  402. setup_requires=["cython >= 0.29.15", "wheel"],
  403. extras_require=extras,
  404. entry_points={
  405. "console_scripts": [
  406. "ray=ray.scripts.scripts:main",
  407. "rllib=ray.rllib.scripts:cli [rllib]",
  408. "tune=ray.tune.scripts:cli",
  409. "ray-operator=ray.ray_operator.operator:main",
  410. "serve=ray.serve.scripts:cli",
  411. ]
  412. },
  413. include_package_data=True,
  414. zip_safe=False,
  415. license="Apache 2.0") if __name__ == "__main__" else None