]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - setup.py
setup: always create a support dir and populate it with a site-packages and add same...
[tahoe-lafs/tahoe-lafs.git] / setup.py
1 #! /usr/bin/env python
2
3 # Allmydata Tahoe -- secure, distributed storage grid
4 #
5 # Copyright (C) 2008 Allmydata, Inc.
6 #
7 # This file is part of tahoe.
8 #
9 # See the docs/about.html file for licensing information.
10
11 import os, re, shutil, stat, subprocess, sys, zipfile
12
13 ##### sys.path management
14
15 def pylibdir(prefixdir):
16     pyver = "python%d.%d" % (sys.version_info[:2])
17     if sys.platform == "win32":
18         return os.path.join(prefixdir, "Lib", "site-packages")
19     else:
20         return os.path.join(prefixdir, "lib", pyver, "site-packages")
21
22 basedir = os.path.dirname(os.path.abspath(__file__))
23 supportlib = pylibdir(os.path.join(basedir, "support"))
24
25 prefixdirs = [] # argh!  horrible kludge to work-around setuptools #54
26 for i in range(len(sys.argv)):
27     arg = sys.argv[i]
28     if arg.startswith("--prefix="):
29         prefixdirs.append(arg[len("--prefix="):])
30     if arg == "--prefix":
31         if len(sys.argv) > i+1:
32             prefixdirs.append(sys.argv[i+1])
33
34     if arg.startswith("develop") or arg.startswith("build") or arg.startswith("test"): # argh! horrible kludge to workaround setuptools #17
35         if sys.platform == "linux2":
36             # workaround for tahoe #229 / setuptools #17, on debian
37             sys.argv.extend(["--site-dirs", "/var/lib/python-support/python%d.%d" % (sys.version_info[:2])])
38         elif sys.platform == "darwin":
39             # this probably only applies to leopard 10.5, possibly only 10.5.5
40             sd = "/System/Library/Frameworks/Python.framework/Versions/%d.%d/Extras/lib/python" % (sys.version_info[:2])
41             sys.argv.extend(["--site-dirs", sd])
42
43 if not prefixdirs:
44     prefixdirs.append("support")
45
46 for prefixdir in prefixdirs:
47     libdir = pylibdir(prefixdir)
48     try:
49         os.makedirs(libdir)
50     except EnvironmentError, le:
51         # Okay, maybe the dir was already there.
52         pass
53     sys.path.append(libdir)
54     pp = os.environ.get('PYTHONPATH','').split(os.pathsep)
55     pp.append(libdir)
56     os.environ['PYTHONPATH'] = os.pathsep.join(pp)
57
58 try:
59     from ez_setup import use_setuptools
60 except ImportError:
61     pass
62 else:
63     # This invokes our own customized version of ez_setup.py to make sure that
64     # setuptools >= v0.6c8 (a.k.a. v0.6-final) is installed.
65
66     # setuptools < v0.6c8 doesn't handle eggs which get installed into the CWD
67     # as a result of being transitively depended on in a setup_requires, but
68     # then are needed for the installed code to run, i.e. in an
69     # install_requires.
70     use_setuptools(download_delay=0, min_version="0.6c10dev")
71
72 from setuptools import find_packages, setup
73 from setuptools.command import sdist
74 from distutils.core import Command
75 from pkg_resources import require
76
77 # Make the dependency-version-requirement, which is used by the Makefile at
78 # build-time, also available to the app at runtime:
79 import shutil
80 shutil.copyfile("_auto_deps.py", os.path.join("src", "allmydata", "_auto_deps.py"))
81
82 trove_classifiers=[
83     "Development Status :: 5 - Production/Stable",
84     "Environment :: Console",
85     "Environment :: Web Environment",
86     "License :: OSI Approved :: GNU General Public License (GPL)",
87     "License :: DFSG approved",
88     "License :: Other/Proprietary License",
89     "Intended Audience :: Developers",
90     "Intended Audience :: End Users/Desktop",
91     "Intended Audience :: System Administrators",
92     "Operating System :: Microsoft",
93     "Operating System :: Microsoft :: Windows",
94     "Operating System :: Microsoft :: Windows :: Windows NT/2000",
95     "Operating System :: Unix",
96     "Operating System :: POSIX :: Linux",
97     "Operating System :: POSIX",
98     "Operating System :: MacOS :: MacOS X",
99     "Operating System :: OS Independent",
100     "Natural Language :: English",
101     "Programming Language :: C",
102     "Programming Language :: Python",
103     "Programming Language :: Python :: 2",
104     "Programming Language :: Python :: 2.4",
105     "Programming Language :: Python :: 2.5",
106     "Programming Language :: Python :: 2.6",
107     "Topic :: Utilities",
108     "Topic :: System :: Systems Administration",
109     "Topic :: System :: Filesystems",
110     "Topic :: System :: Distributed Computing",
111     "Topic :: Software Development :: Libraries",
112     "Topic :: Communications :: Usenet News",
113     "Topic :: System :: Archiving :: Backup",
114     "Topic :: System :: Archiving :: Mirroring",
115     "Topic :: System :: Archiving",
116     ]
117
118
119 VERSIONFILE = "src/allmydata/_version.py"
120 verstr = "unknown"
121 try:
122     verstrline = open(VERSIONFILE, "rt").read()
123 except EnvironmentError:
124     pass # Okay, there is no version file.
125 else:
126     VSRE = r"^verstr = ['\"]([^'\"]*)['\"]"
127     mo = re.search(VSRE, verstrline, re.M)
128     if mo:
129         verstr = mo.group(1)
130     else:
131         print "unable to find version in %s" % (VERSIONFILE,)
132         raise RuntimeError("if %s.py exists, it is required to be well-formed" % (VERSIONFILE,))
133
134 LONG_DESCRIPTION=\
135 """Welcome to the Tahoe project, a secure, decentralized, fault-tolerant
136 filesystem.  All of the source code is available under a Free Software, Open
137 Source licence.
138
139 This filesystem is encrypted and spread over multiple peers in such a way that
140 it remains available even when some of the peers are unavailable,
141 malfunctioning, or malicious."""
142
143
144 setup_requires = []
145
146 # Nevow requires Twisted to setup, but doesn't declare that requirement in a way that enables
147 # setuptools to satisfy that requirement before Nevow's setup.py tried to "import twisted".
148 # Fortunately we require setuptools_trial to setup and setuptools_trial requires Twisted to
149 # install, so hopefully everything will work out until the Nevow issue is fixed:
150 # http://divmod.org/trac/ticket/2629
151 # setuptools_trial is needed if you want "./setup.py trial" or "./setup.py test" to execute the
152 # tests (and in order to make sure Twisted is installed early enough -- see the paragraph
153 # above).
154 # http://pypi.python.org/pypi/setuptools_trial
155 setup_requires.extend(['setuptools_trial'])
156
157 # darcsver is needed if you want "./setup.py darcsver" to write a new version stamp in
158 # src/allmydata/_version.py, with a version number derived from darcs history.
159 # http://pypi.python.org/pypi/darcsver
160 setup_requires.append('darcsver >= 1.1.5')
161
162 if 'trial' in sys.argv[1:] or 'test' in sys.argv[1:]:
163     # Cygwin requires the poll reactor to work at all.  Linux requires the poll reactor to avoid
164     # bug #402 (twisted bug #3218).  In general, the poll reactor is better than the select
165     # reactor, but it is not available on all platforms.  According to exarkun on IRC, it is
166     # available but buggy on some versions of Mac OS X, so just because you can install it
167     # doesn't mean we want to use it on every platform.
168     if sys.platform in ("linux2", "cygwin"):
169         if not [a for a in sys.argv if a.startswith("--reactor")]:
170             sys.argv.append("--reactor=poll")
171
172 # setuptools_darcs is required to produce complete distributions (such as with
173 # "sdist" or "bdist_egg"), unless there is a PKG-INFO file present which shows
174 # that this is itself a source distribution.
175 # http://pypi.python.org/pypi/setuptools_darcs
176 if not os.path.exists('PKG-INFO'):
177     setup_requires.append('setuptools_darcs >= 1.1.0')
178
179 class ShowSupportLib(Command):
180     user_options = []
181     def initialize_options(self):
182         pass
183     def finalize_options(self):
184         pass
185     def run(self):
186         # TODO: --quiet suppresses the 'running show_supportlib' message.
187         # Find a way to do this all the time.
188         print supportlib # TODO windowsy
189
190 class ShowPythonPath(Command):
191     user_options = []
192     def initialize_options(self):
193         pass
194     def finalize_options(self):
195         pass
196     def run(self):
197         # TODO: --quiet suppresses the 'running show_supportlib' message.
198         # Find a way to do this all the time.
199         print "PYTHONPATH=%s" % os.environ.get("PYTHONPATH", '')
200
201 class RunWithPythonPath(Command):
202     description = "Run a subcommand with PYTHONPATH set appropriately"
203
204     user_options = [ ("python", "p",
205                       "Treat command string as arguments to a python executable"),
206                      ("command=", "c", "Command to be run"),
207                      ("directory=", "d", "Directory to run the command in"),
208                      ]
209     boolean_options = ["python"]
210
211     def initialize_options(self):
212         self.command = None
213         self.python = False
214         self.directory = None
215     def finalize_options(self):
216         pass
217     def run(self):
218         oldpp = os.environ.get("PYTHONPATH", "").split(os.pathsep)
219         if oldpp == [""]:
220             # grr silly split() behavior
221             oldpp = []
222         os.environ['PYTHONPATH'] = os.pathsep.join(oldpp + [supportlib,])
223
224         # We must require the command to be safe to split on
225         # whitespace, and have --python and --directory to make it
226         # easier to achieve this.
227
228         command = []
229         if self.python:
230             command.append(sys.executable)
231         if self.command:
232             command.extend(self.command.split())
233         if not command:
234             raise RuntimeError("The --command argument is mandatory")
235         if self.directory:
236             os.chdir(self.directory)
237         if self.verbose:
238             print "command =", " ".join(command)
239         rc = subprocess.call(command)
240         sys.exit(rc)
241
242 class CheckAutoDeps(Command):
243     user_options = []
244     def initialize_options(self):
245         pass
246     def finalize_options(self):
247         pass
248     def run(self):
249         import _auto_deps
250         _auto_deps.require_auto_deps()
251
252
253 class MakeExecutable(Command):
254     user_options = []
255     def initialize_options(self):
256         pass
257     def finalize_options(self):
258         pass
259     def run(self):
260         bin_tahoe_template = os.path.join("bin", "tahoe-script.template")
261
262         # Create the 'tahoe-script.py' file under the 'bin' directory. The 'tahoe-script.py'
263         # file is exactly the same as the 'tahoe-script.template' script except that the shebang
264         # line is rewritten to use our sys.executable for the interpreter. On Windows, create a
265         # tahoe.exe will execute it.  On non-Windows, make a symlink to it from 'tahoe'.  The
266         # tahoe.exe will be copied from the setuptools egg's cli.exe and this will work from a
267         # zip-safe and non-zip-safe setuptools egg.
268         f = open(bin_tahoe_template, "rU")
269         script_lines = f.readlines()
270         f.close()
271         script_lines[0] = "#!%s\n" % sys.executable
272         tahoe_script = os.path.join("bin", "tahoe-script.py")
273         f = open(tahoe_script, "w")
274         for line in script_lines:
275             f.write(line)
276         f.close()
277         if sys.platform == "win32":
278             setuptools_egg = require("setuptools")[0].location
279             if os.path.isfile(setuptools_egg):
280                 z = zipfile.ZipFile(setuptools_egg, 'r')
281                 for filename in z.namelist():
282                     if 'cli.exe' in filename:
283                         cli_exe = z.read(filename)
284             else:
285                 cli_exe = os.path.join(setuptools_egg, 'setuptools', 'cli.exe')
286             tahoe_exe = os.path.join("bin", "tahoe.exe")
287             if os.path.isfile(setuptools_egg):
288                 f = open(tahoe_exe, 'wb')
289                 f.write(cli_exe)
290                 f.close()
291             else:
292                 shutil.copy(cli_exe, tahoe_exe)
293         else:
294             try:
295                 os.remove(os.path.join('bin', 'tahoe'))
296             except:
297                 # okay, probably it was already gone
298                 pass
299             os.symlink('tahoe-script.py', os.path.join('bin', 'tahoe'))
300
301         # chmod +x bin/tahoe-script.py
302         old_mode = stat.S_IMODE(os.stat(tahoe_script)[stat.ST_MODE])
303         new_mode = old_mode | (stat.S_IXUSR | stat.S_IRUSR |
304                                stat.S_IXGRP | stat.S_IRGRP |
305                                stat.S_IXOTH | stat.S_IROTH )
306         os.chmod(tahoe_script, new_mode)
307
308 class MySdist(sdist.sdist):
309     """ A hook in the sdist command so that we can determine whether this the
310     tarball should be 'SUMO' or not, i.e. whether or not to include the
311     external dependency tarballs. Note that we always include
312     misc/dependencies/* in the tarball; --sumo controls whether tahoe-deps/*
313     is included as well.
314     """
315
316     user_options = sdist.sdist.user_options + \
317         [('sumo', 's',
318           "create a 'sumo' sdist which includes the contents of tahoe-deps/*"),
319          ]
320     boolean_options = ['sumo']
321
322     def initialize_options(self):
323         sdist.sdist.initialize_options(self)
324         self.sumo = False
325
326     def make_distribution(self):
327         # add our extra files to the list just before building the
328         # tarball/zipfile. We override make_distribution() instead of run()
329         # because setuptools.command.sdist.run() does not lend itself to
330         # easy/robust subclassing (the code we need to add goes right smack
331         # in the middle of a 12-line method). If this were the distutils
332         # version, we'd override get_file_list().
333
334         if self.sumo:
335             # If '--sumo' was specified, include tahoe-deps/* in the sdist.
336             # We assume that the user has fetched the tahoe-deps.tar.gz
337             # tarball and unpacked it already.
338             self.filelist.extend([os.path.join("tahoe-deps", fn)
339                                   for fn in os.listdir("tahoe-deps")])
340             # In addition, we want the tarball/zipfile to have -SUMO in the
341             # name, and the unpacked directory to have -SUMO too. The easiest
342             # way to do this is to patch self.distribution and override the
343             # get_fullname() method. (an alternative is to modify
344             # self.distribution.metadata.version, but that also affects the
345             # contents of PKG-INFO).
346             fullname = self.distribution.get_fullname()
347             def get_fullname():
348                 return fullname + "-SUMO"
349             self.distribution.get_fullname = get_fullname
350
351         return sdist.sdist.make_distribution(self)
352
353 # Tahoe's dependencies are managed by the find_links= entry in setup.cfg and
354 # the _auto_deps.install_requires list, which is used in the call to setup()
355 # at the end of this file
356 from _auto_deps import install_requires
357
358 setup(name='allmydata-tahoe',
359       version=verstr,
360       description='secure, decentralized, fault-tolerant filesystem',
361       long_description=LONG_DESCRIPTION,
362       author='the allmydata.org Tahoe project',
363       author_email='tahoe-dev@allmydata.org',
364       url='http://allmydata.org/',
365       license='GNU GPL',
366       cmdclass={"show_supportlib": ShowSupportLib,
367                 "show_pythonpath": ShowPythonPath,
368                 "run_with_pythonpath": RunWithPythonPath,
369                 "check_auto_deps": CheckAutoDeps,
370                 "make_executable": MakeExecutable,
371                 "sdist": MySdist,
372                 },
373       package_dir = {'':'src'},
374       packages=find_packages("src"),
375       classifiers=trove_classifiers,
376       test_suite="allmydata.test",
377       install_requires=install_requires,
378       include_package_data=True,
379       setup_requires=setup_requires,
380       entry_points = { 'console_scripts': [ 'tahoe = allmydata.scripts.runner:run' ] },
381       zip_safe=False, # We prefer unzipped for easier access.
382       )