-
Notifications
You must be signed in to change notification settings - Fork 45
/
setup.py
359 lines (288 loc) · 10.9 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
#
# This source file is part of the EdgeDB open source project.
#
# Copyright 2008-present MagicStack Inc. and the EdgeDB authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import sys
if sys.version_info < (3, 8):
raise RuntimeError('edgedb requires Python 3.8 or greater')
import os
import os.path
import pathlib
import re
import subprocess
# We use vanilla build_ext, to avoid importing Cython via
# the setuptools version.
from distutils import extension as distutils_extension
from distutils.command import build_ext as distutils_build_ext
import setuptools
from setuptools.command import build_py as setuptools_build_py
from setuptools.command import sdist as setuptools_sdist
CYTHON_DEPENDENCY = 'Cython(>=3.0.11,<3.1.0)'
# Minimal dependencies required to test edgedb.
TEST_DEPENDENCIES = [
# pycodestyle is a dependency of flake8, but it must be frozen because
# their combination breaks too often
# (example breakage: https://gitlab.com/pycqa/flake8/issues/427)
'pycodestyle~=2.11.1',
'pyflakes~=3.2.0',
'flake8-bugbear~=24.4.26',
'flake8~=7.0.0',
'uvloop>=0.15.1; platform_system != "Windows"',
]
# Dependencies required to build documentation.
DOC_DEPENDENCIES = [
'sphinx~=4.2.0',
'sphinxcontrib-asyncio~=0.3.0',
'sphinx_rtd_theme~=1.0.0',
]
AI_DEPENDENCIES = [
'httpx~=0.27.0',
'httpx-sse~=0.4.0',
]
EXTRA_DEPENDENCIES = {
'ai': AI_DEPENDENCIES,
'docs': DOC_DEPENDENCIES,
'test': TEST_DEPENDENCIES,
# Dependencies required to develop edgedb.
'dev': [
CYTHON_DEPENDENCY,
'pytest>=3.6.0',
] + DOC_DEPENDENCIES + TEST_DEPENDENCIES
}
CFLAGS = ['-O2']
LDFLAGS = []
SYSTEM = sys.platform
if SYSTEM != 'win32':
CFLAGS.extend(['-std=gnu99', '-fsigned-char', '-Wall',
'-Wsign-compare', '-Wconversion',
# See also: https://github.com/cython/cython/issues/5240
'-Wno-error=incompatible-pointer-types',
])
if SYSTEM == 'darwin':
# Lots of warnings from the standard library on macOS 10.14
CFLAGS.extend(['-Wno-nullability-completeness'])
_ROOT = pathlib.Path(__file__).parent
with open(str(_ROOT / 'README.rst')) as f:
readme = f.read()
with open(str(_ROOT / 'gel' / '_version.py')) as f:
for line in f:
if line.startswith('__version__ ='):
_, _, version = line.partition('=')
VERSION = version.strip(" \n'\"")
break
else:
raise RuntimeError(
'unable to read the version from gel/_version.py')
if (_ROOT / '.git').is_dir() and 'dev' in VERSION:
# This is a git checkout, use git to
# generate a precise version.
def git_commitish():
env = {}
v = os.environ.get('PATH')
if v is not None:
env['PATH'] = v
git = subprocess.run(['git', 'rev-parse', 'HEAD'], env=env,
cwd=str(_ROOT), stdout=subprocess.PIPE)
if git.returncode == 0:
commitish = git.stdout.strip().decode('ascii')
else:
commitish = 'unknown'
return commitish
VERSION += '+' + git_commitish()[:7]
class VersionMixin:
def _fix_version(self, filename):
# Replace edgedb.__version__ with the actual version
# of the distribution (possibly inferred from git).
with open(str(filename)) as f:
content = f.read()
version_re = r"(.*__version__\s*=\s*)'[^']+'(.*)"
repl = r"\1'{}'\2".format(self.distribution.metadata.version)
content = re.sub(version_re, repl, content)
with open(str(filename), 'w') as f:
f.write(content)
class sdist(setuptools_sdist.sdist, VersionMixin):
def make_release_tree(self, base_dir, files):
super().make_release_tree(base_dir, files)
self._fix_version(pathlib.Path(base_dir) / 'edgedb' / '_version.py')
class build_py(setuptools_build_py.build_py, VersionMixin):
def build_module(self, module, module_file, package):
outfile, copied = super().build_module(module, module_file, package)
if module == '_version' and package == 'edgedb':
self._fix_version(outfile)
return outfile, copied
class build_ext(distutils_build_ext.build_ext):
user_options = distutils_build_ext.build_ext.user_options + [
('cython-always', None,
'run cythonize() even if .c files are present'),
('cython-annotate', None,
'Produce a colorized HTML version of the Cython source.'),
('cython-directives=', None,
'Cython compiler directives'),
]
def initialize_options(self):
# initialize_options() may be called multiple times on the
# same command object, so make sure not to override previously
# set options.
if getattr(self, '_initialized', False):
return
super(build_ext, self).initialize_options()
if os.environ.get('EDGEDB_DEBUG'):
self.cython_always = True
self.cython_annotate = True
self.cython_directives = "linetrace=True"
self.define = 'PG_DEBUG,CYTHON_TRACE,CYTHON_TRACE_NOGIL'
self.debug = True
else:
self.cython_always = False
self.cython_annotate = None
self.cython_directives = None
self.debug = False
def finalize_options(self):
# finalize_options() may be called multiple times on the
# same command object, so make sure not to override previously
# set options.
if getattr(self, '_initialized', False):
return
need_cythonize = self.cython_always
cfiles = {}
for extension in self.distribution.ext_modules:
for i, sfile in enumerate(extension.sources):
if sfile.endswith('.pyx'):
prefix, ext = os.path.splitext(sfile)
cfile = prefix + '.c'
if os.path.exists(cfile) and not self.cython_always:
extension.sources[i] = cfile
else:
if os.path.exists(cfile):
cfiles[cfile] = os.path.getmtime(cfile)
else:
cfiles[cfile] = 0
need_cythonize = True
if need_cythonize:
import pkg_resources
# Double check Cython presence in case setup_requires
# didn't go into effect (most likely because someone
# imported Cython before setup_requires injected the
# correct egg into sys.path.
try:
import Cython
except ImportError:
raise RuntimeError(
'please install {} to compile edgedb from source'.format(
CYTHON_DEPENDENCY))
cython_dep = pkg_resources.Requirement.parse(CYTHON_DEPENDENCY)
if Cython.__version__ not in cython_dep:
raise RuntimeError(
'edgedb requires {}, got Cython=={}'.format(
CYTHON_DEPENDENCY, Cython.__version__
))
from Cython.Build import cythonize
directives = {
'language_level': '3'
}
if self.cython_directives:
for directive in self.cython_directives.split(','):
k, _, v = directive.partition('=')
if v.lower() == 'false':
v = False
if v.lower() == 'true':
v = True
directives[k] = v
self.distribution.ext_modules[:] = cythonize(
self.distribution.ext_modules,
compiler_directives=directives,
annotate=self.cython_annotate)
super(build_ext, self).finalize_options()
INCLUDE_DIRS = [
'gel/pgproto/',
'gel/datatypes',
]
setup_requires = []
if (not (_ROOT / 'edgedb' / 'protocol' / 'protocol.c').exists() or
'--cython-always' in sys.argv):
# No Cython output, require Cython to build.
setup_requires.append(CYTHON_DEPENDENCY)
with open(str(_ROOT / 'README.rst')) as f:
readme = f.read()
setuptools.setup(
name='edgedb',
version=VERSION,
description='EdgeDB Python driver',
long_description=readme,
platforms=['macOS', 'POSIX', 'Windows'],
author='MagicStack Inc',
author_email='[email protected]',
url='https://github.com/edgedb/edgedb-python',
license='Apache License, Version 2.0',
packages=setuptools.find_packages(),
provides=['edgedb', 'gel'],
zip_safe=False,
include_package_data=True,
package_data={
'edgedb': ['py.typed'],
'gel': ['py.typed'],
},
ext_modules=[
distutils_extension.Extension(
"gel.pgproto.pgproto",
["gel/pgproto/pgproto.pyx"],
extra_compile_args=CFLAGS,
extra_link_args=LDFLAGS),
distutils_extension.Extension(
"gel.datatypes.datatypes",
["gel/datatypes/args.c",
"gel/datatypes/record_desc.c",
"gel/datatypes/namedtuple.c",
"gel/datatypes/object.c",
"gel/datatypes/hash.c",
"gel/datatypes/repr.c",
"gel/datatypes/comp.c",
"gel/datatypes/datatypes.pyx"],
extra_compile_args=CFLAGS,
extra_link_args=LDFLAGS),
distutils_extension.Extension(
"gel.protocol.protocol",
["gel/protocol/protocol.pyx"],
extra_compile_args=CFLAGS,
extra_link_args=LDFLAGS,
include_dirs=INCLUDE_DIRS),
distutils_extension.Extension(
"gel.protocol.asyncio_proto",
["gel/protocol/asyncio_proto.pyx"],
extra_compile_args=CFLAGS,
extra_link_args=LDFLAGS,
include_dirs=INCLUDE_DIRS),
distutils_extension.Extension(
"gel.protocol.blocking_proto",
["gel/protocol/blocking_proto.pyx"],
extra_compile_args=CFLAGS,
extra_link_args=LDFLAGS,
include_dirs=INCLUDE_DIRS),
],
cmdclass={'build_ext': build_ext},
python_requires=">=3.8",
install_requires=[
'certifi>=2021.5.30; platform_system == "Windows"',
],
extras_require=EXTRA_DEPENDENCIES,
setup_requires=setup_requires,
entry_points={
"console_scripts": [
"edgedb-py=gel.codegen.cli:main",
"gel-py=gel.codegen.cli:main",
]
}
)