forked from pycurl/pycurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinbuild.py
197 lines (179 loc) · 8.11 KB
/
winbuild.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
# Bootstrap python binary:
# http://www.python.org/ftp/python/3.3.4/python-3.3.4.msi
# http://www.python.org/ftp/python/3.3.4/python-3.3.4.amd64.msi
# msvc9/vs2008 express:
# http://go.microsoft.com/?linkid=7729279
# msvc10/vs2010 express:
# http://go.microsoft.com/?linkid=9709949
# work directory for downloading dependencies and building everything
root = 'c:/dev/build-pycurl'
# where msysgit is installed
git_root = 'c:/program files/git'
# which versions of python to build against
python_versions = ['2.6.6', '2.7.6', '3.2.5', '3.3.5', '3.4.1']
# where pythons are installed
python_path_template = 'c:/python%s/python'
vc_paths = {
# where msvc 9 is installed, for python 2.6 through 3.2
'vc9': 'c:/program files/microsoft visual studio 9.0',
# where msvc 10 is installed, for python 3.3
'vc10': 'c:/program files/microsoft visual studio 10.0',
}
# whether to link libcurl against zlib
use_zlib = True
# which version of zlib to use, will be downloaded from internet
zlib_version = '1.2.8'
# which version of libcurl to use, will be downloaded from the internet
libcurl_version = '7.37.0'
# pycurl version to build, we should know this ourselves
pycurl_version = '7.19.5'
import os, os.path, sys, subprocess, shutil, contextlib
archives_path = os.path.join(root, 'archives')
state_path = os.path.join(root, 'state')
git_bin_path = os.path.join(git_root, 'bin')
git_path = os.path.join(git_bin_path, 'git')
rm_path = os.path.join(git_bin_path, 'rm')
tar_path = os.path.join(git_bin_path, 'tar')
for key in vc_paths:
vc_paths[key] = {
'root': vc_paths[key],
'vsvars': os.path.join(vc_paths[key], 'common7/tools/vsvars32.bat'),
}
python_vc_versions = {
'2.6': 'vc9',
'2.7': 'vc9',
'3.2': 'vc9',
'3.3': 'vc10',
'3.4': 'vc10',
}
vc_versions = vc_paths.keys()
try:
from urllib.request import urlopen
except ImportError:
from urllib import urlopen
def fetch(url, archive=None):
if archive is None:
archive = os.path.basename(url)
if not os.path.exists(archive):
sys.stdout.write("Fetching %s\n" % url)
io = urlopen(url)
with open('.tmp.%s' % archive, 'wb') as f:
while True:
chunk = io.read(65536)
if len(chunk) == 0:
break
f.write(chunk)
os.rename('.tmp.%s' % archive, archive)
@contextlib.contextmanager
def in_dir(dir):
old_cwd = os.getcwd()
try:
os.chdir(dir)
yield
finally:
os.chdir(old_cwd)
@contextlib.contextmanager
def step(step_fn, args, target_dir):
step = step_fn.__name__
if args:
step += '-' + '-'.join(args)
if not os.path.exists(state_path):
os.makedirs(state_path)
state_file_path = os.path.join(state_path, step)
if not os.path.exists(state_file_path) or not os.path.exists(target_dir):
step_fn(*args)
with open(state_file_path, 'w') as f:
pass
def untar(basename):
if os.path.exists(basename):
shutil.rmtree(basename)
subprocess.check_call([tar_path, 'xf', '%s.tar.gz' % basename])
def rename_for_vc(basename, vc_version):
suffixed_dir = '%s-%s' % (basename, vc_version)
if os.path.exists(suffixed_dir):
shutil.rmtree(suffixed_dir)
os.rename(basename, suffixed_dir)
return suffixed_dir
def build():
os.environ['PATH'] += ";%s" % git_bin_path
if not os.path.exists(archives_path):
os.makedirs(archives_path)
with in_dir(archives_path):
def build_zlib(vc_version):
fetch('http://downloads.sourceforge.net/project/libpng/zlib/%s/zlib-%s.tar.gz' % (zlib_version, zlib_version))
untar('zlib-%s' % zlib_version)
zlib_dir = rename_for_vc('zlib-%s' % zlib_version, vc_version)
with in_dir(zlib_dir):
with open('doit.bat', 'w') as f:
f.write("call \"%s\"\n" % vc_paths[vc_version]['vsvars'])
f.write("nmake /f win32/Makefile.msc\n")
subprocess.check_call(['doit.bat'])
def build_curl(vc_version):
fetch('http://curl.haxx.se/download/curl-%s.tar.gz' % libcurl_version)
untar('curl-%s' % libcurl_version)
curl_dir = rename_for_vc('curl-%s' % libcurl_version, vc_version)
with in_dir(os.path.join(curl_dir, 'winbuild')):
with open('doit.bat', 'w') as f:
f.write("call \"%s\"\n" % vc_paths[vc_version]['vsvars'])
f.write("set include=%%include%%;%s\n" % os.path.join(archives_path, 'zlib-%s-%s' % (zlib_version, vc_version)))
f.write("set lib=%%lib%%;%s\n" % os.path.join(archives_path, 'zlib-%s-%s' % (zlib_version, vc_version)))
if use_zlib:
extra_options = ' WITH_ZLIB=dll'
else:
extra_options = ''
f.write("nmake /f Makefile.vc mode=dll ENABLE_IDN=no%s\n" % extra_options)
subprocess.check_call(['doit.bat'])
for vc_version in vc_versions:
if use_zlib:
step(build_zlib, (vc_version,), 'zlib-%s-%s' % (zlib_version, vc_version))
step(build_curl, (vc_version,), 'curl-%s-%s' % (libcurl_version, vc_version))
def prepare_pycurl():
#fetch('http://pycurl.sourceforge.net/download/pycurl-%s.tar.gz' % pycurl_version)
if os.path.exists('pycurl-%s' % pycurl_version):
#shutil.rmtree('pycurl-%s' % pycurl_version)
subprocess.check_call([rm_path, '-rf', 'pycurl-%s' % pycurl_version])
#subprocess.check_call([tar_path, 'xf', 'pycurl-%s.tar.gz' % pycurl_version])
shutil.copytree('c:/dev/pycurl', 'pycurl-%s' % pycurl_version)
def build_pycurl(python_version, target):
python_path = python_path_template % python_version.replace('.', '')
vc_version = python_vc_versions[python_version]
with in_dir(os.path.join('pycurl-%s' % pycurl_version)):
if use_zlib:
libcurl_build_name = 'libcurl-vc-x86-release-dll-zlib-dll-ipv6-sspi-spnego-winssl'
else:
libcurl_build_name = 'libcurl-vc-x86-release-dll-ipv6-sspi-spnego-winssl'
curl_dir = '../curl-%s-%s/builds/%s' % (libcurl_version, vc_version, libcurl_build_name)
if not os.path.exists('build/lib.win32-%s' % python_version):
# exists for building additional targets for the same python version
os.makedirs('build/lib.win32-%s' % python_version)
shutil.copy(os.path.join(curl_dir, 'bin', 'libcurl.dll'), 'build/lib.win32-%s' % python_version)
with open('doit.bat', 'w') as f:
f.write("call \"%s\"\n" % vc_paths[vc_version]['vsvars'])
f.write("%s setup.py docstrings\n" % (python_path,))
f.write("%s setup.py %s --curl-dir=%s --use-libcurl-dll\n" % (python_path, target, curl_dir))
subprocess.check_call(['doit.bat'])
if target == 'bdist':
os.rename('dist/pycurl-%s.win32.zip' % pycurl_version, 'dist/pycurl-%s.win32-py%s.zip' % (pycurl_version, python_version))
prepare_pycurl()
python_releases = ['.'.join(version.split('.')[:2]) for version in python_versions]
for python_version in python_releases:
for target in ['bdist', 'bdist_wininst', 'bdist_msi']:
build_pycurl(python_version, target)
def download_pythons():
try:
from urllib.request import urlopen
except NameError:
from urllib import urlopen
for version in python_versions:
if os.path.exists(os.path.join(archives_path, 'python-%s.msi')):
continue
print('Downloading %s' % version)
url = 'http://python.org/ftp/python/%s/python-%s.msi' % (version, version)
io = urlopen(url)
data = io.read()
with open(os.path.join(archives_path, 'python-%s.msi' % version), 'wb') as f:
f.write(data)
if len(sys.argv) > 1 and sys.argv[1] == 'download':
download_pythons()
else:
build()