Prepare release candidate.
[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     if "SVN_HEADER_PATH" in os.environ and "SVN_LIBRARY_PATH" in os.environ:
90         return ([os.getenv("SVN_HEADER_PATH")], [os.getenv("SVN_LIBRARY_PATH")], [])
91     svn_prefix = os.getenv("SVN_PREFIX")
92     if svn_prefix is None:
93         basedirs = ["/usr/local", "/usr"]
94         for basedir in basedirs:
95             includedir = os.path.join(basedir, "include/subversion-1")
96             if os.path.isdir(includedir):
97                 svn_prefix = basedir
98                 break
99     if svn_prefix is not None:
100         return ([os.path.join(svn_prefix, "include/subversion-1")], 
101                 [os.path.join(svn_prefix, "lib")], [])
102     raise Exception("Subversion development files not found. "
103                     "Please set SVN_PREFIX or (SVN_LIBRARY_PATH and SVN_HEADER_PATH) environment variable. ")
104
105 # Windows versions - we use environment variables to locate the directories
106 # and hard-code a list of libraries.
107 if os.name == "nt":
108     # just clobber the functions above we can't use
109     # for simplicitly, everything is done in the 'svn' one
110     def apr_build_data():
111         return '.', ''
112
113     def svn_build_data():
114         # environment vars for the directories we need.
115         svn_dev_dir = os.environ.get("SVN_DEV")
116         if not svn_dev_dir or not os.path.isdir(svn_dev_dir):
117             raise Exception(
118                 "Please set SVN_DEV to the location of the svn development "
119                 "packages.\nThese can be downloaded from:\n"
120                 "http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=91")
121         svn_bdb_dir = os.environ.get("SVN_BDB")
122         if not svn_bdb_dir or not os.path.isdir(svn_bdb_dir):
123             raise Exception(
124                 "Please set SVN_BDB to the location of the svn BDB packages "
125                 "- see README.txt in the SV_DEV dir")
126         svn_libintl_dir = os.environ.get("SVN_LIBINTL")
127         if not svn_libintl_dir or not os.path.isdir(svn_libintl_dir):
128             raise Exception(
129                 "Please set SVN_LIBINTL to the location of the svn libintl "
130                 "packages - see README.txt in the SV_DEV dir")
131
132         includes = [
133             # apr dirs.
134             os.path.join(svn_dev_dir, r"include\apr"),
135             os.path.join(svn_dev_dir, r"include\apr-utils"),
136             os.path.join(svn_dev_dir, r"include\apr-iconv"),
137             # svn dirs.
138             os.path.join(svn_dev_dir, "include"), 
139         ]
140         lib_dirs = [
141             os.path.join(svn_dev_dir, "lib"),
142             os.path.join(svn_dev_dir, "lib", "apr"),
143             os.path.join(svn_dev_dir, "lib", "apr-iconv"),
144             os.path.join(svn_dev_dir, "lib", "apr-util"),
145             os.path.join(svn_dev_dir, "lib", "neon"),
146             os.path.join(svn_bdb_dir, "lib"),
147             os.path.join(svn_libintl_dir, "lib"),
148         ]
149         libs = """libapr libapriconv libaprutil libneon
150                   libsvn_subr-1 libsvn_client-1 libsvn_ra-1
151                   libsvn_ra_dav-1 libsvn_ra_local-1 libsvn_ra_svn-1
152                   libsvn_repos-1 libsvn_wc-1 libsvn_delta-1 libsvn_diff-1
153                   libsvn_fs-1 libsvn_repos-1 libsvn_fs_fs-1 libsvn_fs_base-1
154                   intl3_svn
155                   libdb44 xml
156                   advapi32 shell32 ws2_32 zlibstat
157                """.split()
158
159         return includes, lib_dirs, libs,
160
161 (apr_includedir, apr_ldflags) = apr_build_data()
162 (svn_includedirs, svn_libdirs, extra_libs) = svn_build_data()
163
164 def SvnExtension(name, *args, **kwargs):
165     kwargs["include_dirs"] = [apr_includedir] + svn_includedirs
166     kwargs["library_dirs"] = svn_libdirs
167     kwargs["extra_link_args"] = apr_ldflags
168     if os.name == 'nt':
169         # on windows, just ignore and overwrite the libraries!
170         kwargs["libraries"] = extra_libs
171         # APR needs WIN32 defined.
172         kwargs["define_macros"] = [("WIN32", None)]
173     return Extension("bzrlib.plugins.svn.%s" % name, *args, **kwargs)
174
175 # On Windows, we install the apr binaries too.
176 class install_lib_with_dlls(install_lib):
177     def _get_dlls(self):
178         # return a list of of (FQ-in-name, relative-out-name) tuples.
179         ret = []
180         apr_bins = "libaprutil.dll libapriconv.dll libapr.dll".split()
181         look_dirs = os.environ.get("PATH","").split(os.pathsep)
182         look_dirs.insert(0, os.path.join(os.environ["SVN_DEV"], "bin"))
183     
184         for bin in apr_bins:
185             for look in look_dirs:
186                 f = os.path.join(look, bin)
187                 if os.path.isfile(f):
188                     target = os.path.join(self.install_dir, "bzrlib",
189                                           "plugins", "svn", bin)
190                     ret.append((f, target))
191                     break
192             else:
193                 log.warn("Could not find required DLL %r to include", bin)
194                 log.debug("(looked in %s)", look_dirs)
195         return ret
196
197     def run(self):
198         install_lib.run(self)
199         # the apr binaries.
200         if os.name == 'nt':
201             # On Windows we package up the apr dlls with the plugin.
202             for s, d in self._get_dlls():
203                 self.copy_file(s, d)
204
205     def get_outputs(self):
206         ret = install_lib.get_outputs()
207         if os.name == 'nt':
208             ret.extend([info[1] for info in self._get_dlls()])
209         return ret
210
211 setup(name='bzr-svn',
212       description='Support for Subversion branches in Bazaar',
213       keywords='plugin bzr svn',
214       version='0.4.11~rc1',
215       url='http://bazaar-vcs.org/BzrForeignBranches/Subversion',
216       download_url='http://bazaar-vcs.org/BzrSvn',
217       license='GPL',
218       author='Jelmer Vernooij',
219       author_email='jelmer@samba.org',
220       long_description="""
221       This plugin adds support for branching off and 
222       committing to Subversion repositories from 
223       Bazaar.
224       """,
225       package_dir={'bzrlib.plugins.svn':'.', 
226                    'bzrlib.plugins.svn.tests':'tests'},
227       packages=['bzrlib.plugins.svn', 
228                 'bzrlib.plugins.svn.mapping3', 
229                 'bzrlib.plugins.svn.tests'],
230       ext_modules=[
231           SvnExtension("client", ["client.c", "editor.c", "util.c", "ra.c", "wc.c"], libraries=["svn_client-1", "svn_subr-1"]), 
232           SvnExtension("ra", ["ra.c", "util.c", "editor.c"], libraries=["svn_ra-1", "svn_delta-1", "svn_subr-1"]),
233           SvnExtension("repos", ["repos.c", "util.c"], libraries=["svn_repos-1", "svn_subr-1"]),
234           SvnExtension("wc", ["wc.c", "util.c", "editor.c"], libraries=["svn_wc-1", "svn_subr-1"]),
235           ],
236       cmdclass = { 'install_lib': install_lib_with_dlls },
237       )