Start on 0.8.3.
[jelmer/subvertpy.git] / setup.py
1 #!/usr/bin/env python
2 # Setup file for subvertpy
3 # Copyright (C) 2005-2010 Jelmer Vernooij <jelmer@samba.org>
4
5 from distutils.core import setup
6 from distutils.extension import Extension
7 from distutils.command.install_lib import install_lib
8 from distutils import log
9 import sys
10 import os
11 import re
12
13 # Build instructions for Windows:
14 # * Install the SVN dev kit ZIP for Windows from
15 #   http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=91
16 #   At time of writing, this was svn-win32-1.4.6_dev.zip
17 # * Find the SVN binary ZIP file with the binaries for your dev kit.
18 #   At time of writing, this was svn-win32-1.4.6.zip
19 #   Unzip this in the *same directory* as the dev kit - README.txt will be
20 #   overwritten, but that is all. This is the default location the .ZIP file
21 #   will suggest (ie, the directory embedded in both .zip files are the same)
22 # * Set SVN_DEV to point at this directory.
23 # * Install the APR BDB and INTL packages - see README.txt from the devkit
24 # * Set SVN_BDB and SVN_LIBINTL to point at these dirs.
25
26 class CommandException(Exception):
27     """Encapsulate exit status of command execution"""
28     def __init__(self, msg, cmd, arg, status, val):
29         self.message = msg % (cmd, val)
30         Exception.__init__(self, self.message)
31         self.cmd = cmd
32         self.arg = arg
33         self.status = status
34     def not_found(self):
35         return os.WIFEXITED(self.status) and os.WEXITSTATUS(self.status) == 127
36
37
38 def split_shell_results(line):
39     return [s for s in line.split(" ") if s != ""]
40
41
42 def run_cmd(cmd, arg):
43     """Run specified command with given arguments, handling status"""
44     f = os.popen("'%s' %s" % (cmd, arg))
45     dir = f.read().rstrip("\n")
46     status = f.close()
47     if status is None:
48         return dir
49     if os.WIFEXITED(status):
50         code = os.WEXITSTATUS(status)
51         if code == 0:
52             return dir
53         raise CommandException("%s exited with status %d",
54                                cmd, arg, status, code)
55     if os.WIFSIGNALED(status):
56         signal = os.WTERMSIG(status)
57         raise CommandException("%s killed by signal %d",
58                                cmd, arg, status, signal)
59     raise CommandException("%s terminated abnormally (%d)",
60                            cmd, arg, status, status)
61
62
63 def config_value(command, arg):
64     cmds = [command] + [os.path.join(p, command) for p in ["/usr/local/apr/bin/", "/opt/local/bin/"]]
65     for cmd in cmds:
66         try:
67             return run_cmd(cmd, arg)
68         except CommandException, e:
69             if not e.not_found():
70                 raise
71     else:
72         raise Exception("apr-config not found."
73                         " Please set APR_CONFIG environment variable")
74
75
76 def apr_config(arg):
77     config_cmd = os.getenv("APR_CONFIG")
78     if config_cmd is None:
79         return config_value("apr-1-config", arg)
80     else:
81         return run_cmd(config_cmd, arg)
82
83
84 def apu_config(arg):
85     config_cmd = os.getenv("APU_CONFIG")
86     if config_cmd is None:
87         return config_value("apu-1-config", arg)
88     else:
89         return run_cmd(config_cmd, arg)
90
91
92 def apr_build_data():
93     """Determine the APR header file location."""
94     includedir = apr_config("--includedir")
95     if not os.path.isdir(includedir):
96         raise Exception("APR development headers not found")
97     extra_link_flags = apr_config("--link-ld --libs")
98     return (includedir, split_shell_results(extra_link_flags))
99
100
101 def apu_build_data():
102     """Determine the APR util header file location."""
103     includedir = apu_config("--includedir")
104     if not os.path.isdir(includedir):
105         raise Exception("APR util development headers not found")
106     extra_link_flags = apu_config("--link-ld --libs")
107     return (includedir, split_shell_results(extra_link_flags))
108
109
110 def svn_build_data():
111     """Determine the Subversion header file location."""
112     if "SVN_HEADER_PATH" in os.environ and "SVN_LIBRARY_PATH" in os.environ:
113         return ([os.getenv("SVN_HEADER_PATH")], [os.getenv("SVN_LIBRARY_PATH")], [], [])
114     svn_prefix = os.getenv("SVN_PREFIX")
115     if svn_prefix is None:
116         basedirs = ["/usr/local", "/usr"]
117         for basedir in basedirs:
118             includedir = os.path.join(basedir, "include/subversion-1")
119             if os.path.isdir(includedir):
120                 svn_prefix = basedir
121                 break
122     if svn_prefix is not None:
123         return ([os.path.join(svn_prefix, "include/subversion-1")], 
124                 [os.path.join(svn_prefix, "lib")], [], [])
125     raise Exception("Subversion development files not found. "
126                     "Please set SVN_PREFIX or (SVN_LIBRARY_PATH and SVN_HEADER_PATH) environment variable. ")
127
128 def is_keychain_provider_available():
129     """
130     Checks for the availability of the Keychain simple authentication provider in Subversion by compiling a simple test program.
131     """
132     abd = apr_build_data()
133     sbd = svn_build_data()
134     gcc_command_args = ['gcc'] + ['-I' + inc for inc in sbd[0]] + ['-L' + lib for lib in sbd[1]] + ['-I' + abd[0], '-lsvn_subr-1', '-x', 'c', '-']
135     (gcc_in, gcc_out, gcc_err) = os.popen3(gcc_command_args)
136     gcc_in.write("""
137 #include <svn_auth.h>
138 int main(int argc, const char* arv[]) {
139     svn_auth_get_keychain_simple_provider(NULL, NULL);
140 }
141 """)
142     gcc_in.close()
143     gcc_out.read()
144     return (gcc_out.close() is None)
145
146
147 class VersionQuery(object):
148
149     def __init__(self, filename):
150         self.filename = filename
151         f = file(filename, "rU")
152         try:
153             self.text = f.read()
154         finally:
155             f.close()
156
157     def grep(self, what):
158         m = re.search(r"^#define\s+%s\s+(\d+)\s*$" % (what,), self.text, re.MULTILINE)
159         if not m:
160             raise Exception("Definition for %s was not found in file %s." % (what, self.filename))
161         return int(m.group(1))
162
163 # Windows versions - we use environment variables to locate the directories
164 # and hard-code a list of libraries.
165 if os.name == "nt":
166     def get_apr_version():
167         apr_version_file = os.path.join(os.environ["SVN_DEV"], r"include\apr\apr_version.h")
168         if not os.path.isfile(apr_version_file):
169             raise Exception(
170                 "Please check that your SVN_DEV location is correct.\n"
171                 "Unable to find required apr\\apr_version.h file.")
172         query = VersionQuery(apr_version_file)
173         return query.grep("APR_MAJOR_VERSION"), query.grep("APR_MINOR_VERSION"), query.grep("APR_PATCH_VERSION")
174
175     def get_svn_version():
176         svn_version_file = os.path.join(os.environ["SVN_DEV"], r"include\svn_version.h")
177         if not os.path.isfile(svn_version_file):
178             raise Exception(
179                 "Please check that your SVN_DEV location is correct.\n"
180                 "Unable to find required svn_version.h file.")
181         query = VersionQuery(svn_version_file)
182         return query.grep("SVN_VER_MAJOR"), query.grep("SVN_VER_MINOR"), query.grep("SVN_VER_PATCH")
183
184     # just clobber the functions above we can't use
185     # for simplicitly, everything is done in the 'svn' one
186     def apr_build_data():
187         return '.', []
188
189     def apu_build_data():
190         return '.', []
191
192     def svn_build_data():
193         # environment vars for the directories we need.
194         svn_dev_dir = os.environ.get("SVN_DEV")
195         if not svn_dev_dir or not os.path.isdir(svn_dev_dir):
196             raise Exception(
197                 "Please set SVN_DEV to the location of the svn development "
198                 "packages.\nThese can be downloaded from:\n"
199                 "http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=91")
200         svn_bdb_dir = os.environ.get("SVN_BDB")
201         if not svn_bdb_dir or not os.path.isdir(svn_bdb_dir):
202             raise Exception(
203                 "Please set SVN_BDB to the location of the svn BDB packages "
204                 "- see README.txt in the SVN_DEV dir")
205         svn_libintl_dir = os.environ.get("SVN_LIBINTL")
206         if not svn_libintl_dir or not os.path.isdir(svn_libintl_dir):
207             raise Exception(
208                 "Please set SVN_LIBINTL to the location of the svn libintl "
209                 "packages - see README.txt in the SVN_DEV dir")
210
211         svn_version = get_svn_version()
212         apr_version = get_apr_version()
213
214         includes = [
215             # apr dirs.
216             os.path.join(svn_dev_dir, r"include\apr"),
217             os.path.join(svn_dev_dir, r"include\apr-util"),
218             os.path.join(svn_dev_dir, r"include\apr-iconv"),
219             # svn dirs.
220             os.path.join(svn_dev_dir, "include"), 
221         ]
222         lib_dirs = [
223             os.path.join(svn_dev_dir, "lib"),
224             os.path.join(svn_dev_dir, "lib", "apr"),
225             os.path.join(svn_dev_dir, "lib", "apr-iconv"),
226             os.path.join(svn_dev_dir, "lib", "apr-util"),
227             os.path.join(svn_dev_dir, "lib", "neon"),
228             os.path.join(svn_bdb_dir, "lib"),
229             os.path.join(svn_libintl_dir, "lib"),
230         ]
231         aprlibs = """libapr libapriconv libaprutil""".split()
232         if apr_version[0] == 1:
233             aprlibs = [aprlib + "-1" for aprlib in aprlibs]
234         elif apr_version[0] > 1:
235             raise Exception(
236                 "You have apr version %d.%d.%d.\n"
237                 "This setup only knows how to build with 0.*.* or 1.*.*." % apr_version)
238         libs = """libneon libsvn_subr-1 libsvn_client-1 libsvn_ra-1
239                   libsvn_ra_dav-1 libsvn_ra_local-1 libsvn_ra_svn-1
240                   libsvn_repos-1 libsvn_wc-1 libsvn_delta-1 libsvn_diff-1
241                   libsvn_fs-1 libsvn_repos-1 libsvn_fs_fs-1 libsvn_fs_base-1
242                   intl3_svn
243                   libdb44 xml
244                   advapi32 shell32 ws2_32 zlibstat
245                """.split()
246         if svn_version >= (1,5,0):
247             # Since 1.5.0 libsvn_ra_dav-1 was removed
248             libs.remove("libsvn_ra_dav-1")
249
250         return includes, lib_dirs, [], aprlibs+libs,
251
252 (apr_includedir, apr_link_flags) = apr_build_data()
253 (apu_includedir, apu_link_flags) = apu_build_data()
254 (svn_includedirs, svn_libdirs, svn_link_flags, extra_libs) = svn_build_data()
255
256 def SvnExtension(name, *args, **kwargs):
257     kwargs["include_dirs"] = [apr_includedir, apu_includedir] + svn_includedirs + ["subvertpy"]
258     kwargs["library_dirs"] = svn_libdirs
259     # Note that the apr-util link flags are not included here, as
260     # subvertpy only uses some apr util constants but does not use
261     # the library directly.
262     kwargs["extra_link_args"] = apr_link_flags + svn_link_flags
263     if os.name == 'nt':
264         # on windows, just ignore and overwrite the libraries!
265         kwargs["libraries"] = extra_libs
266         # APR needs WIN32 defined.
267         kwargs["define_macros"] = [("WIN32", None)]
268     if sys.platform == 'darwin':
269         # on Mac OS X, we need to check for Keychain availability
270         if is_keychain_provider_available():
271             if "define_macros" not in kwargs:
272                 kwargs["define_macros"] = []
273             kwargs["define_macros"].extend((('DARWIN', None), ('SVN_KEYCHAIN_PROVIDER_AVAILABLE', '1')))
274     return Extension(name, *args, **kwargs)
275
276
277 # On Windows, we install the apr binaries too.
278 class install_lib_with_dlls(install_lib):
279     def _get_dlls(self):
280         # return a list of of (FQ-in-name, relative-out-name) tuples.
281         ret = []
282         apr_bins = [libname + ".dll" for libname in extra_libs if libname.startswith("libapr")]
283         if get_svn_version() >= (1,5,0):
284             # Since 1.5.0 these libraries became shared
285             apr_bins += """libsvn_client-1.dll libsvn_delta-1.dll libsvn_diff-1.dll
286                            libsvn_fs-1.dll libsvn_ra-1.dll libsvn_repos-1.dll
287                            libsvn_subr-1.dll libsvn_wc-1.dll libsasl.dll""".split()
288         apr_bins += """intl3_svn.dll libdb44.dll libeay32.dll ssleay32.dll""".split()
289         look_dirs = os.environ.get("PATH","").split(os.pathsep)
290         look_dirs.insert(0, os.path.join(os.environ["SVN_DEV"], "bin"))
291     
292         for bin in apr_bins:
293             for look in look_dirs:
294                 f = os.path.join(look, bin)
295                 if os.path.isfile(f):
296                     target = os.path.join(self.install_dir, "subvertpy", bin)
297                     ret.append((f, target))
298                     break
299             else:
300                 log.warn("Could not find required DLL %r to include", bin)
301                 log.debug("(looked in %s)", look_dirs)
302         return ret
303
304     def run(self):
305         install_lib.run(self)
306         # the apr binaries.
307         if os.name == 'nt':
308             # On Windows we package up the apr dlls with the plugin.
309             for s, d in self._get_dlls():
310                 self.copy_file(s, d)
311
312     def get_outputs(self):
313         ret = install_lib.get_outputs(self)
314         if os.name == 'nt':
315             ret.extend([info[1] for info in self._get_dlls()])
316         return ret
317
318
319 def source_path(filename):
320     return os.path.join("subvertpy", filename)
321
322
323 def subvertpy_modules():
324     return [
325         SvnExtension("subvertpy.client", [source_path(n) for n in ("client.c", "editor.c", "util.c", "_ra.c", "wc.c")], libraries=["svn_client-1", "svn_subr-1", "svn_ra-1", "svn_wc-1"]), 
326         SvnExtension("subvertpy._ra", [source_path(n) for n in ("_ra.c", "util.c", "editor.c")], libraries=["svn_ra-1", "svn_delta-1", "svn_subr-1"]),
327         SvnExtension("subvertpy.repos", [source_path(n) for n in ("repos.c", "util.c")], libraries=["svn_repos-1", "svn_subr-1", "svn_fs-1"]),
328         SvnExtension("subvertpy.wc", [source_path(n) for n in ("wc.c", "util.c", "editor.c")], libraries=["svn_wc-1", "svn_subr-1"])
329         ]
330
331
332 subvertpy_version = (0, 8, 3)
333 subvertpy_version_string = ".".join(map(str, subvertpy_version))
334
335
336 if __name__ == "__main__":
337     setup(name='subvertpy',
338           description='Alternative Python bindings for Subversion',
339           keywords='svn subvertpy subversion bindings',
340           version=subvertpy_version_string,
341           url='http://samba.org/~jelmer/subvertpy',
342           download_url="http://samba.org/~jelmer/subvertpy/subvertpy-%s.tar.gz" % subvertpy_version_string,
343           license='LGPLv2.1 or later',
344           author='Jelmer Vernooij',
345           author_email='jelmer@samba.org',
346           long_description="""
347           Alternative Python bindings for Subversion. The goal is to have complete, portable and "Pythonic" Python bindings. 
348           """,
349           packages=['subvertpy', 'subvertpy.tests'],
350           ext_modules=subvertpy_modules(),
351           scripts=['bin/subvertpy-fast-export'],
352           cmdclass = { 'install_lib': install_lib_with_dlls },
353           )