-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
setup.py
831 lines (728 loc) · 27.6 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
import argparse
import errno
import io
import logging
import os
import pathlib
import re
import shlex
import shutil
import subprocess
import sys
import urllib.error
import urllib.parse
import urllib.request
import warnings
from enum import Enum
from itertools import chain
# Workaround for setuptools_scm (used on macos) adding junk files
# https://stackoverflow.com/a/61274968/8162137
try:
import setuptools_scm.integration
setuptools_scm.integration.find_files = lambda _: []
except ImportError:
pass
logger = logging.getLogger(__name__)
SUPPORTED_PYTHONS = [(3, 9), (3, 10), (3, 11), (3, 12)]
# When the bazel version is updated, make sure to update it
# in WORKSPACE file as well.
ROOT_DIR = os.path.dirname(__file__)
BUILD_JAVA = os.getenv("RAY_INSTALL_JAVA") == "1"
SKIP_BAZEL_BUILD = os.getenv("SKIP_BAZEL_BUILD") == "1"
BAZEL_ARGS = os.getenv("BAZEL_ARGS")
BAZEL_LIMIT_CPUS = os.getenv("BAZEL_LIMIT_CPUS")
THIRDPARTY_SUBDIR = os.path.join("ray", "thirdparty_files")
RUNTIME_ENV_AGENT_THIRDPARTY_SUBDIR = os.path.join(
"ray", "_private", "runtime_env", "agent", "thirdparty_files"
)
CLEANABLE_SUBDIRS = [
THIRDPARTY_SUBDIR,
RUNTIME_ENV_AGENT_THIRDPARTY_SUBDIR,
]
# In automated builds, we do a few adjustments before building. For instance,
# the bazel environment is set up slightly differently, and symlinks are
# replaced with junctions in Windows. This variable is set e.g. in our conda-forge
# feedstock.
is_automated_build = bool(int(os.environ.get("IS_AUTOMATED_BUILD", "0")))
exe_suffix = ".exe" if sys.platform == "win32" else ""
# .pyd is the extension Python requires on Windows for shared libraries.
# https://docs.python.org/3/faq/windows.html#is-a-pyd-file-the-same-as-a-dll
pyd_suffix = ".pyd" if sys.platform == "win32" else ".so"
def find_version(*filepath):
# Extract version information from filepath
with open(os.path.join(ROOT_DIR, *filepath)) as fp:
version_match = re.search(r"^version = ['\"]([^'\"]*)['\"]", fp.read(), re.M)
if version_match:
return version_match.group(1)
raise RuntimeError("Unable to find version string.")
class SetupType(Enum):
RAY = 1
RAY_CPP = 2
class BuildType(Enum):
DEFAULT = 1
DEBUG = 2
ASAN = 3
TSAN = 4
class SetupSpec:
def __init__(
self, type: SetupType, name: str, description: str, build_type: BuildType
):
self.type: SetupType = type
self.name: str = name
version = find_version("ray", "_version.py")
# add .dbg suffix if debug mode is on.
if build_type == BuildType.DEBUG:
self.version: str = f"{version}+dbg"
elif build_type == BuildType.ASAN:
self.version: str = f"{version}+asan"
elif build_type == BuildType.TSAN:
self.version: str = f"{version}+tsan"
else:
self.version = version
self.description: str = description
self.build_type: BuildType = build_type
self.files_to_include: list = []
self.install_requires: list = []
self.extras: dict = {}
def get_packages(self):
if self.type == SetupType.RAY:
return setuptools.find_packages(exclude=("tests", "*.tests", "*.tests.*"))
else:
return []
build_type = os.getenv("RAY_DEBUG_BUILD")
if build_type == "debug":
BUILD_TYPE = BuildType.DEBUG
elif build_type == "asan":
BUILD_TYPE = BuildType.ASAN
elif build_type == "tsan":
BUILD_TYPE = BuildType.TSAN
else:
BUILD_TYPE = BuildType.DEFAULT
if os.getenv("RAY_INSTALL_CPP") == "1":
# "ray-cpp" wheel package.
setup_spec = SetupSpec(
SetupType.RAY_CPP,
"ray-cpp",
"A subpackage of Ray which provides the Ray C++ API.",
BUILD_TYPE,
)
else:
# "ray" primary wheel package.
setup_spec = SetupSpec(
SetupType.RAY,
"ray",
"Ray provides a simple, "
"universal API for building distributed applications.",
BUILD_TYPE,
)
RAY_EXTRA_CPP = True
# Disable extra cpp for the development versions.
if "dev" in setup_spec.version or os.getenv("RAY_DISABLE_EXTRA_CPP") == "1":
RAY_EXTRA_CPP = False
# Ideally, we could include these files by putting them in a
# MANIFEST.in or using the package_data argument to setup, but the
# MANIFEST.in gets applied at the very beginning when setup.py runs
# before these files have been created, so we have to move the files
# manually.
# NOTE: The lists below must be kept in sync with ray/BUILD.bazel.
ray_files = [
"ray/_raylet" + pyd_suffix,
"ray/core/src/ray/gcs/gcs_server" + exe_suffix,
"ray/core/src/ray/raylet/raylet" + exe_suffix,
]
if sys.platform == "linux":
ray_files.append("ray/core/libjemalloc.so")
if BUILD_JAVA or os.path.exists(os.path.join(ROOT_DIR, "ray/jars/ray_dist.jar")):
ray_files.append("ray/jars/ray_dist.jar")
if setup_spec.type == SetupType.RAY_CPP:
setup_spec.files_to_include += ["ray/cpp/default_worker" + exe_suffix]
# C++ API library and project template files.
setup_spec.files_to_include += [
os.path.join(dirpath, filename)
for dirpath, dirnames, filenames in os.walk("ray/cpp")
for filename in filenames
]
# These are the directories where automatically generated Python protobuf
# bindings are created.
generated_python_directories = [
"ray/core/generated",
"ray/serve/generated",
]
ray_files.append("ray/nightly-wheels.yaml")
# Autoscaler files.
ray_files += [
"ray/autoscaler/aws/defaults.yaml",
"ray/autoscaler/aws/cloudwatch/prometheus.yml",
"ray/autoscaler/aws/cloudwatch/ray_prometheus_waiter.sh",
"ray/autoscaler/azure/defaults.yaml",
"ray/autoscaler/spark/defaults.yaml",
"ray/autoscaler/_private/_azure/azure-vm-template.json",
"ray/autoscaler/_private/_azure/azure-config-template.json",
"ray/autoscaler/gcp/defaults.yaml",
"ray/autoscaler/local/defaults.yaml",
"ray/autoscaler/vsphere/defaults.yaml",
"ray/autoscaler/ray-schema.json",
]
# Dashboard files.
ray_files += [
os.path.join(dirpath, filename)
for dirpath, dirnames, filenames in os.walk("ray/dashboard/client/build")
for filename in filenames
]
# Dashboard metrics files.
ray_files += [
os.path.join(dirpath, filename)
for dirpath, dirnames, filenames in os.walk("ray/dashboard/modules/metrics/export")
for filename in filenames
]
ray_files += [
os.path.join(dirpath, filename)
for dirpath, dirnames, filenames in os.walk(
"ray/dashboard/modules/metrics/dashboards"
)
for filename in filenames
if filename.endswith(".json")
]
# html templates for notebook integration
ray_files += [
p.as_posix() for p in pathlib.Path("ray/widgets/templates/").glob("*.html.j2")
]
# If you're adding dependencies for ray extras, please
# also update the matching section of requirements/requirements.txt
# in this directory
if setup_spec.type == SetupType.RAY:
pandas_dep = "pandas >= 1.3"
numpy_dep = "numpy >= 1.20"
pyarrow_deps = [
"pyarrow >= 6.0.1",
"pyarrow <18; sys_platform == 'darwin' and platform_machine == 'x86_64'",
]
setup_spec.extras = {
"adag": [
"cupy-cuda12x; sys_platform != 'darwin'",
],
"client": [
# The Ray client needs a specific range of gRPC to work:
# Tracking issues: https://github.com/grpc/grpc/issues/33714
"grpcio != 1.56.0"
if sys.platform == "darwin"
else "grpcio",
],
"data": [
numpy_dep,
pandas_dep,
*pyarrow_deps,
"fsspec",
],
"default": [
# If adding dependencies necessary to launch the dashboard api server,
# please add it to python/ray/dashboard/optional_deps.py as well.
"aiohttp >= 3.7",
"aiohttp_cors",
"colorful",
"py-spy >= 0.2.0",
"requests",
"grpcio >= 1.32.0; python_version < '3.10'", # noqa:E501
"grpcio >= 1.42.0; python_version >= '3.10'", # noqa:E501
"opencensus",
"pydantic!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,<3",
"prometheus_client >= 0.7.1",
"smart_open",
"virtualenv >=20.0.24, !=20.21.1", # For pip runtime env.
"memray; sys_platform != 'win32'",
],
"observability": [
"opentelemetry-api",
"opentelemetry-sdk",
"opentelemetry-exporter-otlp",
],
"serve": [
"uvicorn[standard]",
"requests",
"starlette",
"fastapi",
"watchfiles",
],
"tune": [
"pandas",
"tensorboardX>=1.9",
"requests",
*pyarrow_deps,
"fsspec",
],
}
# Ray Serve depends on the Ray dashboard components.
setup_spec.extras["serve"] = list(
set(setup_spec.extras["serve"] + setup_spec.extras["default"])
)
# Ensure gRPC library exists for Ray Serve gRPC support.
setup_spec.extras["serve-grpc"] = list(
set(
setup_spec.extras["serve"]
+ [
"grpcio >= 1.32.0; python_version < '3.10'", # noqa:E501
"grpcio >= 1.42.0; python_version >= '3.10'", # noqa:E501
"pyOpenSSL",
]
)
)
if RAY_EXTRA_CPP:
setup_spec.extras["cpp"] = ["ray-cpp==" + setup_spec.version]
setup_spec.extras["rllib"] = setup_spec.extras["tune"] + [
"dm_tree",
"gymnasium==1.0.0",
"lz4",
"scikit-image",
"pyyaml",
"scipy",
"typer",
"rich",
]
setup_spec.extras["train"] = setup_spec.extras["tune"]
# Ray AI Runtime should encompass Data, Tune, and Serve.
setup_spec.extras["air"] = list(
set(
setup_spec.extras["tune"]
+ setup_spec.extras["data"]
+ setup_spec.extras["train"]
+ setup_spec.extras["serve"]
)
)
# "all" will not include "cpp" anymore. It is a big depedendency
# that most people do not need.
#
# Instead, when cpp is supported, we add a "all-cpp".
setup_spec.extras["all"] = list(
set(
chain.from_iterable([v for k, v in setup_spec.extras.items() if k != "cpp"])
)
)
if RAY_EXTRA_CPP:
setup_spec.extras["all-cpp"] = list(
set(setup_spec.extras["all"] + setup_spec.extras["cpp"])
)
# These are the main dependencies for users of ray. This list
# should be carefully curated. If you change it, please reflect
# the change in the matching section of requirements/requirements.txt
#
# NOTE: if you add any unbounded dependency, please also update
# install-core-prerelease-dependencies.sh so we can test
# new releases candidates.
if setup_spec.type == SetupType.RAY:
setup_spec.install_requires = [
"click >= 7.0",
"filelock",
"jsonschema",
"msgpack >= 1.0.0, < 2.0.0",
"packaging",
"protobuf >= 3.15.3, != 3.19.5",
"pyyaml",
"aiosignal",
"frozenlist",
"requests",
]
def is_native_windows_or_msys():
"""Check to see if we are running on native Windows,
but NOT WSL (which is seen as Linux)."""
return sys.platform == "msys" or sys.platform == "win32"
def is_invalid_windows_platform():
# 'GCC' check is how you detect MinGW:
# https://github.com/msys2/MINGW-packages/blob/abd06ca92d876b9db05dd65f27d71c4ebe2673a9/mingw-w64-python2/0410-MINGW-build-extensions-with-GCC.patch#L53
platform = sys.platform
ver = sys.version
return platform == "msys" or (platform == "win32" and ver and "GCC" in ver)
# Calls Bazel in PATH, falling back to the standard user installation path
# (~/bin/bazel) if it isn't found.
def bazel_invoke(invoker, cmdline, *args, **kwargs):
home = os.path.expanduser("~")
first_candidate = os.getenv("BAZEL_PATH", "bazel")
candidates = [first_candidate]
if sys.platform == "win32":
mingw_dir = os.getenv("MINGW_DIR")
if mingw_dir:
candidates.append(mingw_dir + "/bin/bazel.exe")
else:
candidates.append(os.path.join(home, "bin", "bazel"))
result = None
for i, cmd in enumerate(candidates):
try:
result = invoker([cmd] + cmdline, *args, **kwargs)
break
except IOError:
if i >= len(candidates) - 1:
raise
return result
def download(url):
try:
result = urllib.request.urlopen(url).read()
except urllib.error.URLError:
# This fallback is necessary on Python 3.5 on macOS due to TLS 1.2.
curl_args = ["curl", "-s", "-L", "-f", "-o", "-", url]
result = subprocess.check_output(curl_args)
return result
def patch_isdir():
"""
Python on Windows is having hard times at telling if a symlink is
a directory - it can "guess" wrong at times, which bites when
finding packages. Replace with a fixed version which unwraps links first.
"""
orig_isdir = os.path.isdir
def fixed_isdir(path):
while os.path.islink(path):
try:
link = os.readlink(path)
except OSError:
break
path = os.path.abspath(os.path.join(os.path.dirname(path), link))
return orig_isdir(path)
os.path.isdir = fixed_isdir
def replace_symlinks_with_junctions():
"""
Per default Windows requires admin access to create symlinks, while
junctions (which behave similarly) can be created by users.
This function replaces symlinks (which might be broken when checked
out without admin rights) with junctions so Ray can be built both
with and without admin access.
"""
assert is_native_windows_or_msys()
# Update this list if new symlinks are introduced to the source tree
_LINKS = {
r"ray\rllib": "../../rllib",
}
root_dir = os.path.dirname(__file__)
for link, default in _LINKS.items():
path = os.path.join(root_dir, link)
try:
out = subprocess.check_output(
"DIR /A:LD /B", shell=True, cwd=os.path.dirname(path)
)
except subprocess.CalledProcessError:
out = b""
if os.path.basename(path) in out.decode("utf8").splitlines():
logger.info(f"'{link}' is already converted to junction point")
else:
logger.info(f"Converting '{link}' to junction point...")
if os.path.isfile(path):
with open(path) as inp:
target = inp.read()
os.unlink(path)
elif os.path.isdir(path):
target = default
try:
# unlink() works on links as well as on regular files,
# and links to directories are considered directories now
os.unlink(path)
except OSError as err:
# On Windows attempt to unlink a regular directory results
# in a PermissionError with errno set to errno.EACCES.
if err.errno != errno.EACCES:
raise
# For regular directories deletion is done with rmdir call.
os.rmdir(path)
else:
raise ValueError(f"Unexpected type of entry: '{path}'")
target = os.path.abspath(os.path.join(os.path.dirname(path), target))
logger.info("Setting {} -> {}".format(link, target))
subprocess.check_call(
f'MKLINK /J "{os.path.basename(link)}" "{target}"',
shell=True,
cwd=os.path.dirname(path),
)
if is_automated_build and is_native_windows_or_msys():
# Automated replacements should only happen in automatic build
# contexts for now
patch_isdir()
replace_symlinks_with_junctions()
def build(build_python, build_java, build_cpp):
if tuple(sys.version_info[:2]) not in SUPPORTED_PYTHONS:
msg = (
"Detected Python version {}, which is not supported. "
"Only Python {} are supported."
).format(
".".join(map(str, sys.version_info[:2])),
", ".join(".".join(map(str, v)) for v in SUPPORTED_PYTHONS),
)
raise RuntimeError(msg)
if is_invalid_windows_platform():
msg = (
"Please use official native CPython on Windows,"
" not Cygwin/MSYS/MSYS2/MinGW/etc.\n"
+ "Detected: {}\n at: {!r}".format(sys.version, sys.executable)
)
raise OSError(msg)
bazel_env = dict(os.environ, PYTHON3_BIN_PATH=sys.executable)
if is_native_windows_or_msys():
SHELL = bazel_env.get("SHELL")
if SHELL:
bazel_env.setdefault("BAZEL_SH", os.path.normpath(SHELL))
BAZEL_SH = bazel_env.get("BAZEL_SH", "")
SYSTEMROOT = os.getenv("SystemRoot")
wsl_bash = os.path.join(SYSTEMROOT, "System32", "bash.exe")
if (not BAZEL_SH) and SYSTEMROOT and os.path.isfile(wsl_bash):
msg = (
"You appear to have Bash from WSL,"
" which Bazel may invoke unexpectedly. "
"To avoid potential problems,"
" please explicitly set the {name!r}"
" environment variable for Bazel."
).format(name="BAZEL_SH")
raise RuntimeError(msg)
# Note: We are passing in sys.executable so that we use the same
# version of Python to build packages inside the build.sh script. Note
# that certain flags will not be passed along such as --user or sudo.
# TODO(rkn): Fix this.
if not os.getenv("SKIP_THIRDPARTY_INSTALL"):
pip_packages = ["psutil", "setproctitle==1.2.2", "colorama"]
subprocess.check_call(
[
sys.executable,
"-m",
"pip",
"install",
"-q",
"--target=" + os.path.join(ROOT_DIR, THIRDPARTY_SUBDIR),
]
+ pip_packages,
env=dict(os.environ, CC="gcc"),
)
# runtime env agent dependenceis
runtime_env_agent_pip_packages = ["aiohttp"]
subprocess.check_call(
[
sys.executable,
"-m",
"pip",
"install",
"-q",
"--target=" + os.path.join(ROOT_DIR, RUNTIME_ENV_AGENT_THIRDPARTY_SUBDIR),
]
+ runtime_env_agent_pip_packages
)
bazel_flags = ["--verbose_failures"]
if BAZEL_ARGS:
bazel_flags.extend(shlex.split(BAZEL_ARGS))
if BAZEL_LIMIT_CPUS:
n = int(BAZEL_LIMIT_CPUS) # the value must be an int
bazel_flags.append(f"--local_cpu_resources={n}")
warnings.warn(
"Setting BAZEL_LIMIT_CPUS is deprecated and will be removed in a future"
" version. Please use BAZEL_ARGS instead.",
FutureWarning,
)
if is_automated_build:
src_dir = os.environ.get("SRC_DIR", False) or os.getcwd()
src_dir = os.path.abspath(src_dir)
if is_native_windows_or_msys():
drive = os.path.splitdrive(src_dir)[0] + "\\"
root_dir = os.path.join(drive, "bazel-root")
out_dir = os.path.join(drive, "b-o")
bazel_flags.append("--enable_runfiles=false")
else:
root_dir = os.path.join(src_dir, "..", "bazel-root")
out_dir = os.path.join(src_dir, "..", "b-o")
for d in (root_dir, out_dir):
if not os.path.exists(d):
os.makedirs(d)
bazel_precmd_flags = [
"--output_user_root=" + root_dir,
"--output_base=" + out_dir,
]
else:
bazel_precmd_flags = []
bazel_targets = []
bazel_targets += ["//:ray_pkg"] if build_python else []
bazel_targets += ["//cpp:ray_cpp_pkg"] if build_cpp else []
bazel_targets += ["//java:ray_java_pkg"] if build_java else []
if setup_spec.build_type == BuildType.DEBUG:
bazel_flags.extend(["--config", "debug"])
if setup_spec.build_type == BuildType.ASAN:
bazel_flags.extend(["--config=asan-build"])
if setup_spec.build_type == BuildType.TSAN:
bazel_flags.extend(["--config=tsan"])
return bazel_invoke(
subprocess.check_call,
bazel_precmd_flags + ["build"] + bazel_flags + ["--"] + bazel_targets,
env=bazel_env,
)
def _walk_thirdparty_dir(directory):
file_list = []
for root, dirs, filenames in os.walk(directory):
# Exclude generated bytecode cache directories and tests directories
# from vendored packages.
for exclude_dir in ["__pycache__", "tests"]:
if exclude_dir in dirs:
dirs.remove(exclude_dir)
for name in filenames:
file_list.append(os.path.join(root, name))
return file_list
def copy_file(target_dir, filename, rootdir):
# TODO(rkn): This feels very brittle. It may not handle all cases. See
# https://github.com/apache/arrow/blob/master/python/setup.py for an
# example.
# File names can be absolute paths, e.g. from _walk_thirdparty_dir().
source = os.path.relpath(filename, rootdir)
destination = os.path.join(target_dir, source)
# Create the target directory if it doesn't already exist.
os.makedirs(os.path.dirname(destination), exist_ok=True)
if not os.path.exists(destination):
if sys.platform == "win32":
# Does not preserve file mode (needed to avoid read-only bit)
shutil.copyfile(source, destination, follow_symlinks=True)
else:
# Preserves file mode (needed to copy executable bit)
shutil.copy(source, destination, follow_symlinks=True)
return 1
return 0
def add_system_dlls(dlls, target_dir):
"""
Copy any required dlls required by the c-extension module and not already
provided by python. They will end up in the wheel next to the c-extension
module which will guarentee they are available at runtime.
"""
for dll in dlls:
# Installing Visual Studio will copy the runtime dlls to system32
src = os.path.join(r"c:\Windows\system32", dll)
assert os.path.exists(src)
shutil.copy(src, target_dir)
def pip_run(build_ext):
if SKIP_BAZEL_BUILD:
build(False, False, False)
else:
build(True, BUILD_JAVA, True)
if setup_spec.type == SetupType.RAY:
setup_spec.files_to_include += ray_files
thirdparty_dir = os.path.join(ROOT_DIR, THIRDPARTY_SUBDIR)
setup_spec.files_to_include += _walk_thirdparty_dir(thirdparty_dir)
runtime_env_agent_thirdparty_dir = os.path.join(
ROOT_DIR, RUNTIME_ENV_AGENT_THIRDPARTY_SUBDIR
)
setup_spec.files_to_include += _walk_thirdparty_dir(
runtime_env_agent_thirdparty_dir
)
# Copy over the autogenerated protobuf Python bindings.
for directory in generated_python_directories:
for filename in os.listdir(directory):
if filename[-3:] == ".py":
setup_spec.files_to_include.append(
os.path.join(directory, filename)
)
copied_files = 0
for filename in setup_spec.files_to_include:
copied_files += copy_file(build_ext.build_lib, filename, ROOT_DIR)
if sys.platform == "win32":
# _raylet.pyd links to some MSVC runtime DLLS, this one may not be
# present on a user's machine. While vcruntime140.dll and
# vcruntime140_1.dll are also required, they are provided by CPython.
runtime_dlls = ["msvcp140.dll"]
add_system_dlls(runtime_dlls, os.path.join(build_ext.build_lib, "ray"))
copied_files += len(runtime_dlls)
print("# of files copied to {}: {}".format(build_ext.build_lib, copied_files))
def api_main(program, *args):
parser = argparse.ArgumentParser()
choices = ["build", "python_versions", "clean", "help"]
parser.add_argument("command", type=str, choices=choices)
parser.add_argument(
"-l",
"--language",
default="python,cpp",
type=str,
help="A list of languages to build native libraries. "
'Supported languages include "python" and "java". '
"If not specified, only the Python library will be built.",
)
parsed_args = parser.parse_args(args)
result = None
if parsed_args.command == "build":
kwargs = dict(build_python=False, build_java=False, build_cpp=False)
for lang in parsed_args.language.split(","):
if "python" in lang:
kwargs.update(build_python=True)
elif "java" in lang:
kwargs.update(build_java=True)
elif "cpp" in lang:
kwargs.update(build_cpp=True)
else:
raise ValueError("invalid language: {!r}".format(lang))
result = build(**kwargs)
elif parsed_args.command == "python_versions":
for version in SUPPORTED_PYTHONS:
# NOTE: On Windows this will print "\r\n" on the command line.
# Strip it out by piping to tr -d "\r".
print(".".join(map(str, version)))
elif parsed_args.command == "clean":
def onerror(function, path, excinfo):
nonlocal result
if excinfo[1].errno != errno.ENOENT:
msg = excinfo[1].strerror
logger.error("cannot remove {}: {}".format(path, msg))
result = 1
for subdir in CLEANABLE_SUBDIRS:
shutil.rmtree(os.path.join(ROOT_DIR, subdir), onerror=onerror)
elif parsed_args.command == "help":
parser.print_help()
else:
raise ValueError("Invalid command: {!r}".format(parsed_args.command))
return result
if __name__ == "__api__":
api_main(*sys.argv)
if __name__ == "__main__":
import setuptools
import setuptools.command.build_ext
class build_ext(setuptools.command.build_ext.build_ext):
def run(self):
return pip_run(self)
class BinaryDistribution(setuptools.Distribution):
def has_ext_modules(self):
return True
# Ensure no remaining lib files.
build_dir = os.path.join(ROOT_DIR, "build")
if os.path.isdir(build_dir):
shutil.rmtree(build_dir)
setuptools.setup(
name=setup_spec.name,
version=setup_spec.version,
author="Ray Team",
author_email="[email protected]",
description=(setup_spec.description),
long_description=io.open(
os.path.join(ROOT_DIR, os.path.pardir, "README.rst"), "r", encoding="utf-8"
).read(),
url="https://github.com/ray-project/ray",
keywords=(
"ray distributed parallel machine-learning hyperparameter-tuning"
"reinforcement-learning deep-learning serving python"
),
python_requires=">=3.9",
classifiers=[
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
],
packages=setup_spec.get_packages(),
cmdclass={"build_ext": build_ext},
# The BinaryDistribution argument triggers build_ext.
distclass=BinaryDistribution,
install_requires=setup_spec.install_requires,
setup_requires=["cython >= 0.29.32", "wheel"],
extras_require=setup_spec.extras,
entry_points={
"console_scripts": [
"ray=ray.scripts.scripts:main",
"rllib=ray.rllib.scripts:cli [rllib]",
"tune=ray.tune.cli.scripts:cli",
"serve=ray.serve.scripts:cli",
]
},
package_data={
"ray": ["includes/*.pxd", "*.pxd"],
},
include_package_data=True,
exclude_package_data={
# Empty string means "any package".
# Therefore, exclude BUILD from every package:
"": ["BUILD"],
},
zip_safe=False,
license="Apache 2.0",
) if __name__ == "__main__" else None