-
Notifications
You must be signed in to change notification settings - Fork 9
/
setup.py
155 lines (129 loc) · 4.91 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
import argparse
import errno
import glob
import io
import logging
import os
import re
import shutil
import subprocess
import sys
import tarfile
import tempfile
import zipfile
import time
from itertools import chain
from itertools import takewhile
import urllib.error
import urllib.parse
import urllib.request
logger = logging.getLogger(__name__)
SUPPORTED_PYTHONS = [(3, 6), (3, 7), (3, 8), (3, 9), (3, 10)]
SUPPORTED_BAZEL = (3, 2, 0)
ROOT_DIR = os.path.dirname(__file__)
install_requires = []
# Calls Bazel in PATH, falling back to the standard user installatation path
# (~/.bazel/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, ".bazel", "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 move_file(target_dir, filename):
source = filename
destination = os.path.join(target_dir, filename.split('/')[-1])
# 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):
print("Copying {} to {}.".format(source, 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)
def build():
# no support windows
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)
bazel_env = dict(os.environ, PYTHON3_BIN_PATH=sys.executable)
version_info = bazel_invoke(subprocess.check_output, ["--version"])
bazel_version_str = version_info.rstrip().decode("utf-8").split(" ", 1)[1]
bazel_version_split = bazel_version_str.split(".")
bazel_version_digits = [
"".join(takewhile(str.isdigit, s)) for s in bazel_version_split
]
bazel_version = tuple(map(int, bazel_version_digits))
if bazel_version < SUPPORTED_BAZEL:
logger.warning("Expected Bazel version {} but found {}".format(
".".join(map(str, SUPPORTED_BAZEL)), bazel_version_str))
bazel_targets = ["//pygloo:all"]
return bazel_invoke(
subprocess.check_call,
["build", "--verbose_failures", "--"] + bazel_targets,
env=bazel_env)
def pip_run(build_ext):
build()
files_to_include = ["./bazel-bin/pygloo/pygloo.so"]
for filename in files_to_include:
move_file(build_ext.build_lib, filename)
if __name__ == "__main__":
import setuptools
import setuptools.command.build_ext
from setuptools.command.install import install
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
class InstallPlatlib(install):
def finalize_options(self):
install.finalize_options(self)
if self.distribution.has_ext_modules():
self.install_lib = self.install_platlib
with open(os.path.join(ROOT_DIR, "README.md"), encoding="utf-8") as f:
long_description = f.read()
setuptools.setup(
name="pygloo",
version="0.2.0",
author="Ray Team",
author_email="[email protected]",
description=("A python binding for gloo"),
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/ray-project/pygloo",
classifiers=[
'Programming Language :: Python :: 3',
'Topic :: Scientific/Engineering :: Artificial Intelligence'
],
keywords=("collective communication"),
packages=setuptools.find_packages(),
cmdclass={"build_ext": build_ext,
"install": InstallPlatlib},
# The BinaryDistribution argument triggers build_ext.
distclass=BinaryDistribution,
install_requires=install_requires,
setup_requires=["wheel"],
include_package_data=True,
zip_safe=False,
license="Apache 2.0"
)