Allow overriding the Subversion prefix by setting the SVN_PREFIX environment variable.
[jelmer/subvertpy.git] / setup.py
1 #!/usr/bin/env python
2 # Setup file for bzr-svn
3 # Copyright (C) 2005-2008 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
12 # Build instructions for Windows:
13 # * Install the SVN dev kit ZIP for Windows from
14 #   http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=91
15 #   At time of writing, this was svn-win32-1.4.6_dev.zip
16 # * Find the SVN binary ZIP file with the binaries for your dev kit.
17 #   At time of writing, this was svn-win32-1.4.6.zip
18 #   Unzip this in the *same directory* as the dev kit - README.txt will be
19 #   overwritten, but that is all. This is the default location the .ZIP file
20 #   will suggest (ie, the directory embedded in both .zip files are the same)
21 # * Set SVN_DEV to point at this directory.
22 # * Install the APR BDB and INTL packages - see README.txt from the devkit
23 # * Set SVN_BDB and SVN_LIBINTL to point at these dirs.
24 #
25 #  To install into a particular bzr location, use:
26 #  % python setup.py install --install-lib=c:\root\of\bazaar
27
28 class CommandException(Exception):
29     """Encapsulate exit status of apr-config execution"""
30     def __init__(self, msg, cmd, arg, status, val):
31         self.message = msg % (cmd, val)
32         super(CommandException, self).__init__(self.message)
33         self.cmd = cmd
34         self.arg = arg
35         self.status = status
36     def not_found(self):
37         return os.WIFEXITED(self.status) and os.WEXITSTATUS(self.status) == 127
38
39 def run_cmd(cmd, arg):
40     """Run specified command with given arguments, handling status"""
41     f = os.popen("'%s' %s" % (cmd, arg))
42     dir = f.read().rstrip("\n")
43     status = f.close()
44     if status is None:
45         return dir
46     if os.WIFEXITED(status):
47         code = os.WEXITSTATUS(status)
48         if code == 0:
49             return dir
50         raise CommandException("%s exited with status %d",
51                                cmd, arg, status, code)
52     if os.WIFSIGNALED(status):
53         signal = os.WTERMSIG(status)
54         raise CommandException("%s killed by signal %d",
55                                cmd, arg, status, signal)
56     raise CommandException("%s terminated abnormally (%d)",
57                            cmd, arg, status, status)
58
59 def apr_config(arg):
60     apr_config_cmd = os.getenv("APR_CONFIG")
61     if apr_config_cmd is None:
62         cmds = ["apr-config", "apr-1-config", "/usr/local/apr/bin/apr-config",
63                 "/usr/local/bin/apr-config"]
64         for cmd in cmds:
65             try:
66                 res = run_cmd(cmd, arg)
67                 apr_config_cmd = cmd
68                 break
69             except CommandException, e:
70                 if not e.not_found():
71                     raise
72         else:
73             raise Exception("apr-config not found."
74                             " Please set APR_CONFIG environment variable")
75     else:
76         res = run_cmd(apr_config_cmd, arg)
77     return res
78
79 def apr_build_data():
80     """Determine the APR header file location."""
81     includedir = apr_config("--includedir")
82     if not os.path.isdir(includedir):
83         raise Exception("APR development headers not found")
84     ldflags = filter(lambda x: x != "", apr_config("--link-ld").split(" "))
85     return (includedir, ldflags)
86
87 def svn_build_data():
88     """Determine the Subversion header file location."""
89     svn_prefix = os.getenv("SVN_PREFIX")
90     if svn_prefix is None:
91         basedirs = ["/usr/local", "/usr"]
92         for basedir in basedirs:
93             includedir = os.path.join(basedir, "include/subversion-1")
94             if os.path.isdir(includedir):
95                 svn_prefix = basedir
96                 break
97     if svn_prefix is not None:
98         return ([os.path.join(basedir, "include/subversion-1")], 
99                 [os.path.join(basedir, "lib")], [])
100     raise Exception("Subversion development files not found. "
101                     "Please set SVN_PREFIX environment variable. ")
102
103 # Windows versions - we use environment variables to locate the directories
104 # and hard-code a list of libraries.
105 if os.name == "nt":
106     # just clobber the functions above we can't use
107     # for simplicitly, everything is done in the 'svn' one
108     def apr_build_data():
109         return '.', ''
110
111     def svn_build_data():
112         # environment vars for the directories we need.
113         svn_dev_dir = os.environ.get("SVN_DEV")
114         if not svn_dev_dir or not os.path.isdir(svn_dev_dir):
115             raise Exception(
116                 "Please set SVN_DEV to the location of the svn development "
117                 "packages.\nThese can be downloaded from:\n"
118                 "http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=91")
119         svn_bdb_dir = os.environ.get("SVN_BDB")
120         if not svn_bdb_dir or not os.path.isdir(svn_bdb_dir):
121             raise Exception(
122                 "Please set SVN_BDB to the location of the svn BDB packages "
123                 "- see README.txt in the SV_DEV dir")
124         svn_libintl_dir = os.environ.get("SVN_LIBINTL")
125         if not svn_libintl_dir or not os.path.isdir(svn_libintl_dir):
126             raise Exception(
127                 "Please set SVN_LIBINTL to the location of the svn libintl "
128                 "packages - see README.txt in the SV_DEV dir")
129
130         includes = [
131             # apr dirs.
132             os.path.join(svn_dev_dir, r"include\apr"),
133             os.path.join(svn_dev_dir, r"include\apr-utils"),
134             os.path.join(svn_dev_dir, r"include\apr-iconv"),
135             # svn dirs.
136             os.path.join(svn_dev_dir, "include"), 
137         ]
138         lib_dirs = [
139             os.path.join(svn_dev_dir, "lib"),
140             os.path.join(svn_dev_dir, "lib", "apr"),
141             os.path.join(svn_dev_dir, "lib", "apr-iconv"),
142             os.path.join(svn_dev_dir, "lib", "apr-util"),
143             os.path.join(svn_dev_dir, "lib", "neon"),
144             os.path.join(svn_bdb_dir, "lib"),
145             os.path.join(svn_libintl_dir, "lib"),
146         ]
147         libs = """libapr libapriconv libaprutil libneon
148                   libsvn_subr-1 libsvn_client-1 libsvn_ra-1
149                   libsvn_ra_dav-1 libsvn_ra_local-1 libsvn_ra_svn-1
150                   libsvn_repos-1 libsvn_wc-1 libsvn_delta-1 libsvn_diff-1
151                   libsvn_fs-1 libsvn_repos-1 libsvn_fs_fs-1 libsvn_fs_base-1
152                   intl3_svn
153                   libdb44 xml
154                   advapi32 shell32 ws2_32 zlibstat
155                """.split()
156
157         return includes, lib_dirs, libs,
158
159 (apr_includedir, apr_ldflags) = apr_build_data()
160 (svn_includedirs, svn_libdirs, extra_libs) = svn_build_data()
161
162 def SvnExtension(name, *args, **kwargs):
163     kwargs["include_dirs"] = [apr_includedir] + svn_includedirs
164     kwargs["library_dirs"] = svn_libdirs
165     kwargs["extra_link_args"] = apr_ldflags
166     if os.name == 'nt':
167         # on windows, just ignore and overwrite the libraries!
168         kwargs["libraries"] = extra_libs
169         # APR needs WIN32 defined.
170         kwargs["define_macros"] = [("WIN32", None)]
171     return Extension("bzrlib.plugins.svn.%s" % name, *args, **kwargs)
172
173 # On Windows, we install the apr binaries too.
174 class install_lib_with_dlls(install_lib):
175     def _get_dlls(self):
176         # return a list of of (FQ-in-name, relative-out-name) tuples.
177         ret = []
178         apr_bins = "libaprutil.dll libapriconv.dll libapr.dll".split()
179         look_dirs = os.environ.get("PATH","").split(os.pathsep)
180         look_dirs.insert(0, os.path.join(os.environ["SVN_DEV"], "bin"))
181     
182         for bin in apr_bins:
183             for look in look_dirs:
184                 f = os.path.join(look, bin)
185                 if os.path.isfile(f):
186                     target = os.path.join(self.install_dir, "bzrlib",
187                                           "plugins", "svn", bin)
188                     ret.append((f, target))
189                     break
190             else:
191                 log.warn("Could not find required DLL %r to include", bin)
192                 log.debug("(looked in %s)", look_dirs)
193         return ret
194
195     def run(self):
196         install_lib.run(self)
197         # the apr binaries.
198         if os.name == 'nt':
199             # On Windows we package up the apr dlls with the plugin.
200             for s, d in self._get_dlls():
201                 self.copy_file(s, d)
202
203     def get_outputs(self):
204         ret = install_lib.get_outputs()
205         if os.name == 'nt':
206             ret.extend([info[1] for info in self._get_dlls()])
207         return ret
208
209 setup(name='bzr-svn',
210       description='Support for Subversion branches in Bazaar',
211       keywords='plugin bzr svn',
212       version='0.4.11',
213       url='http://bazaar-vcs.org/BzrForeignBranches/Subversion',
214       download_url='http://bazaar-vcs.org/BzrSvn',
215       license='GPL',
216       author='Jelmer Vernooij',
217       author_email='jelmer@samba.org',
218       long_description="""
219       This plugin adds support for branching off and 
220       committing to Subversion repositories from 
221       Bazaar.
222       """,
223       package_dir={'bzrlib.plugins.svn':'.', 
224                    'bzrlib.plugins.svn.tests':'tests'},
225       packages=['bzrlib.plugins.svn', 
226                 'bzrlib.plugins.svn.mapping3', 
227                 'bzrlib.plugins.svn.tests'],
228       ext_modules=[
229           SvnExtension("client", ["client.c", "editor.c", "util.c", "ra.c", "wc.c"], libraries=["svn_client-1", "svn_subr-1"]), 
230           SvnExtension("ra", ["ra.c", "util.c", "editor.c"], libraries=["svn_ra-1", "svn_delta-1", "svn_subr-1"]),
231           SvnExtension("repos", ["repos.c", "util.c"], libraries=["svn_repos-1", "svn_subr-1"]),
232           SvnExtension("wc", ["wc.c", "util.c", "editor.c"], libraries=["svn_wc-1", "svn_subr-1"]),
233           ],
234       cmdclass = { 'install_lib': install_lib_with_dlls },
235       )