Skip to content

Commit e7bedb8

Browse files
committed
Make sure setup.py sdist produces correct tarballs
1 parent c31616c commit e7bedb8

File tree

2 files changed

+62
-37
lines changed

2 files changed

+62
-37
lines changed

Makefile

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
.PHONY: compile clean all distclean test debug sdist clean-libuv
2-
.PHONY: release sdist-libuv docs
1+
.PHONY: _default clean clean-libuv distclean compile debug docs test release
32

43

54
PYTHON ?= python
65

76

8-
all: compile
7+
_default: compile
98

109

1110
clean:
12-
rm -fr dist/ doc/_build/
13-
rm -fr uvloop/*.c uvloop/*.html uvloop/*.so build *.egg-info
11+
rm -fr dist/ doc/_build/ *.egg-info
12+
rm -fr build/lib.* build/temp.*
13+
rm -fr uvloop/*.c uvloop/*.html uvloop/*.so
1414
rm -fr uvloop/handles/*.html uvloop/includes/*.html
1515
find . -name '__pycache__' | xargs rm -rf
1616

@@ -19,10 +19,6 @@ clean-libuv:
1919
(cd vendor/libuv; git clean -dfX)
2020

2121

22-
sdist-libuv: clean-libuv
23-
/bin/sh vendor/libuv/autogen.sh
24-
25-
2622
distclean: clean clean-libuv
2723

2824

@@ -47,11 +43,5 @@ test:
4743
$(PYTHON) -m unittest discover -s tests
4844

4945

50-
sdist: clean compile test sdist-libuv
51-
$(PYTHON) setup.py sdist
52-
53-
54-
# Don't change "clean" to "distclean"! Otherwise "clean-libuv" will
55-
# only be called once, which will produce a broken sdist.
56-
release: clean compile test sdist-libuv
46+
release: distclean compile test
5747
$(PYTHON) setup.py sdist bdist_wheel upload

setup.py

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import re
3+
import shutil
34
import subprocess
45
import sys
56
import unittest
@@ -17,12 +18,14 @@
1718

1819

1920
from setuptools import setup, Extension
20-
from setuptools.command.build_ext import build_ext
21+
from setuptools.command.build_ext import build_ext as build_ext
22+
from setuptools.command.sdist import sdist as sdist
2123

2224

2325
VERSION = '0.6.5'
2426
CFLAGS = ['-O2']
2527
LIBUV_DIR = os.path.join(os.path.dirname(__file__), 'vendor', 'libuv')
28+
LIBUV_BUILD_DIR = os.path.join(os.path.dirname(__file__), 'build', 'libuv')
2629

2730

2831
def discover_tests():
@@ -31,7 +34,33 @@ def discover_tests():
3134
return test_suite
3235

3336

34-
class libuv_build_ext(build_ext):
37+
def _libuv_build_env():
38+
env = os.environ.copy()
39+
40+
cur_cflags = env.get('CFLAGS', '')
41+
if not re.search('-O\d', cur_cflags):
42+
cur_cflags += ' -O2'
43+
44+
env['CFLAGS'] = (cur_cflags + ' -fPIC ' + env.get('ARCHFLAGS', ''))
45+
46+
return env
47+
48+
49+
def _libuv_autogen(env):
50+
if not os.path.exists(os.path.join(LIBUV_DIR, 'configure')):
51+
subprocess.run(
52+
['/bin/sh', 'autogen.sh'], cwd=LIBUV_DIR, env=env, check=True)
53+
54+
55+
class uvloop_sdist(sdist):
56+
def run(self):
57+
# Make sure sdist archive contains configure
58+
# to avoid the dependency on autotools.
59+
_libuv_autogen(_libuv_build_env())
60+
super().run()
61+
62+
63+
class uvloop_build_ext(build_ext):
3564
user_options = build_ext.user_options + [
3665
('cython-always', None,
3766
'run cythonize() even if .c files are present'),
@@ -177,31 +206,34 @@ def _patch_cfile(self, cfile):
177206
f.write(src)
178207

179208
def build_libuv(self):
180-
env = os.environ.copy()
181-
182-
cur_cflags = env.get('CFLAGS', '')
183-
if not re.search('-O\d', cur_cflags):
184-
cur_cflags += ' -O2'
209+
env = _libuv_build_env()
185210

186-
env['CFLAGS'] = (cur_cflags + ' -fPIC ' + env.get('ARCHFLAGS', ''))
211+
# Make sure configure and friends are present in case
212+
# we are building from a git checkout.
213+
_libuv_autogen(env)
187214

188-
j_flag = '-j{}'.format(os.cpu_count() or 1)
189-
190-
if not os.path.exists(os.path.join(LIBUV_DIR, 'configure')):
191-
subprocess.run(['/bin/sh', 'autogen.sh'], cwd=LIBUV_DIR, env=env,
192-
check=True)
215+
# Copy the libuv tree to build/ so that its build
216+
# products don't pollute sdist accidentally.
217+
if os.path.exists(LIBUV_BUILD_DIR):
218+
shutil.rmtree(LIBUV_BUILD_DIR)
219+
shutil.copytree(LIBUV_DIR, LIBUV_BUILD_DIR)
193220

194221
# Sometimes pip fails to preserve the timestamps correctly,
195222
# in which case, make will try to run autotools again.
196-
subprocess.run(['touch', 'configure.ac', 'aclocal.m4',
197-
'configure', 'Makefile.am', 'Makefile.in'],
198-
cwd=LIBUV_DIR, env=env, check=True)
223+
subprocess.run(
224+
['touch', 'configure.ac', 'aclocal.m4', 'configure',
225+
'Makefile.am', 'Makefile.in'],
226+
cwd=LIBUV_BUILD_DIR, env=env, check=True)
199227

200-
subprocess.run(['./configure'], cwd=LIBUV_DIR, env=env, check=True)
228+
subprocess.run(
229+
['./configure'],
230+
cwd=LIBUV_BUILD_DIR, env=env, check=True)
201231

232+
j_flag = '-j{}'.format(os.cpu_count() or 1)
202233
c_flag = "CFLAGS={}".format(env['CFLAGS'])
203-
subprocess.run(['make', j_flag, c_flag],
204-
cwd=LIBUV_DIR, env=env, check=True)
234+
subprocess.run(
235+
['make', j_flag, c_flag],
236+
cwd=LIBUV_BUILD_DIR, env=env, check=True)
205237

206238
def build_extensions(self):
207239
if self.use_system_libuv:
@@ -212,7 +244,7 @@ def build_extensions(self):
212244
# Support macports on Mac OS X.
213245
self.compiler.add_include_dir('/opt/local/include')
214246
else:
215-
libuv_lib = os.path.join(LIBUV_DIR, '.libs', 'libuv.a')
247+
libuv_lib = os.path.join(LIBUV_BUILD_DIR, '.libs', 'libuv.a')
216248
if not os.path.exists(libuv_lib):
217249
self.build_libuv()
218250
if not os.path.exists(libuv_lib):
@@ -241,7 +273,10 @@ def build_extensions(self):
241273
platforms=['*nix'],
242274
version=VERSION,
243275
packages=['uvloop'],
244-
cmdclass={'build_ext': libuv_build_ext},
276+
cmdclass={
277+
'sdist': uvloop_sdist,
278+
'build_ext': uvloop_build_ext
279+
},
245280
ext_modules=[
246281
Extension(
247282
"uvloop.loop",

0 commit comments

Comments
 (0)