]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - setup.py
use_setuptools_trial.patch
[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, sys, stat, subprocess
12
13 ##### sys.path management
14
15 basedir = os.path.dirname(os.path.abspath(__file__))
16 pyver = "python%d.%d" % (sys.version_info[:2])
17 if sys.platform == "win32":
18     supportlib = os.path.join("support", "Lib", "site-packages")
19 else:
20     supportlib = os.path.join("support", "lib", pyver, "site-packages")
21 supportlib = os.path.join(basedir, supportlib)
22
23 def add_tahoe_paths():
24     """Modify sys.path and PYTHONPATH to include Tahoe and supporting libraries
25
26     The first step towards building Tahoe is to run::
27
28       python setup.py build_tahoe
29
30     which is the equivalent of::
31
32       mkdir -p $(BASEDIR)/support/lib/python2.5/site-packages
33        (or cygpath equivalent)
34       setup.py develop --multi-version --prefix=$(BASEDIR)/support
35
36     This installs .eggs for any dependent libraries that aren't already
37     available on the system, into support/lib/pythonN.N/site-packages (or
38     support/Lib/site-packages on windows). It also adds an .egg-link for
39     Tahoe itself into the same directory.
40
41     We add this directory to os.environ['PYTHONPATH'], so that any child
42     processes we spawn will be able to use these packages.
43
44     When the setuptools site.py sees that supportlib in PYTHONPATH, it scans
45     through it for .egg and .egg-link entries, and adds them to sys.path .
46     Since python has already processed all the site.py files by the time we
47     get here, we perform this same sort of processing ourselves: this makes
48     tahoe (and dependency libraries) available to code within setup.py
49     itself. This is used by the 'setup.py trial' subcommand, which invokes
50     trial directly rather than spawning a subprocess (this is easier than
51     locating the 'trial' executable, especially when Twisted was installed as
52     a dependent library).
53
54     We'll need to add these .eggs to sys.path before importing anything that
55     isn't a part of stdlib. All the directories that we add this way are put
56     at the start of sys.path, so they will override anything that was present
57     on the system (and perhaps found lacking by the setuptools requirements
58     expressed in _auto_deps.py).
59     """
60
61     extra_syspath_items = []
62     extra_pythonpath_items = []
63
64     extra_syspath_items.append(supportlib)
65     extra_pythonpath_items.append(supportlib)
66
67     # Since we use setuptools to populate that directory, there will be a
68     # number of .egg and .egg-link entries there. Add all of them to
69     # sys.path, since that what the setuptools site.py would do if it
70     # encountered them at process start time. Without this step, the rest of
71     # this process would be unable to use the packages installed there. We
72     # don't need to add them to PYTHONPATH, since the site.py present there
73     # will add them when the child process starts up.
74
75     if os.path.isdir(supportlib):
76         for fn in os.listdir(supportlib):
77             if fn.endswith(".egg"):
78                 extra_syspath_items.append(os.path.join(supportlib, fn))
79
80     # We also add our src/ directory, since that's where all the Tahoe code
81     # lives. This matches what site.py does when it sees the .egg-link file
82     # that is written to the support dir by an invocation of our 'setup.py
83     # develop' command.
84     extra_syspath_items.append(os.path.join(basedir, "src"))
85
86     # and we put an extra copy of everything from PYTHONPATH in front, so
87     # that it is possible to override the packages that setuptools downloads
88     # with alternate versions, by doing e.g. "PYTHONPATH=foo python setup.py
89     # trial"
90     oldpp = os.environ.get("PYTHONPATH", "").split(os.pathsep)
91     if oldpp == [""]:
92         # grr silly split() behavior
93         oldpp = []
94     extra_syspath_items = oldpp + extra_syspath_items
95
96     sys.path = extra_syspath_items + sys.path
97
98     # We also provide it to any child processes we spawn, via
99     # os.environ["PYTHONPATH"]
100     os.environ["PYTHONPATH"] = os.pathsep.join(oldpp + extra_pythonpath_items)
101
102 # add_tahoe_paths() must be called before use_setuptools() is called. I don't
103 # know why. If it isn't, then a later pkg_resources.requires(pycryptopp) call
104 # fails because an old version (in /usr/lib) was already loaded.
105 add_tahoe_paths()
106
107 try:
108     from ez_setup import use_setuptools
109 except ImportError:
110     pass
111 else:
112     # This invokes our own customized version of ez_setup.py to make sure that
113     # setuptools >= v0.6c8 (a.k.a. v0.6-final) is installed.
114
115     # setuptools < v0.6c8 doesn't handle eggs which get installed into the CWD
116     # as a result of being transitively depended on in a setup_requires, but
117     # then are needed for the installed code to run, i.e. in an
118     # install_requires.
119     use_setuptools(download_delay=0, min_version="0.6c8")
120
121 from setuptools import find_packages, setup
122 from setuptools.command import sdist
123 from distutils.core import Command
124 from setuptools_trial.setuptools_trial import TrialTest
125
126 # Make the dependency-version-requirement, which is used by the Makefile at
127 # build-time, also available to the app at runtime:
128 import shutil
129 shutil.copyfile("_auto_deps.py", os.path.join("src", "allmydata", "_auto_deps.py"))
130
131 trove_classifiers=[
132     "Development Status :: 5 - Production/Stable",
133     "Environment :: Console",
134     "Environment :: Web Environment",
135     "License :: OSI Approved :: GNU General Public License (GPL)",
136     "License :: DFSG approved",
137     "License :: Other/Proprietary License",
138     "Intended Audience :: Developers",
139     "Intended Audience :: End Users/Desktop",
140     "Intended Audience :: System Administrators",
141     "Operating System :: Microsoft",
142     "Operating System :: Microsoft :: Windows",
143     "Operating System :: Microsoft :: Windows :: Windows NT/2000",
144     "Operating System :: Unix",
145     "Operating System :: POSIX :: Linux",
146     "Operating System :: POSIX",
147     "Operating System :: MacOS :: MacOS X",
148     "Operating System :: OS Independent",
149     "Natural Language :: English",
150     "Programming Language :: C",
151     "Programming Language :: Python",
152     "Programming Language :: Python :: 2",
153     "Programming Language :: Python :: 2.4",
154     "Programming Language :: Python :: 2.5",
155     "Topic :: Utilities",
156     "Topic :: System :: Systems Administration",
157     "Topic :: System :: Filesystems",
158     "Topic :: System :: Distributed Computing",
159     "Topic :: Software Development :: Libraries",
160     "Topic :: Communications :: Usenet News",
161     "Topic :: System :: Archiving :: Backup",
162     "Topic :: System :: Archiving :: Mirroring",
163     "Topic :: System :: Archiving",
164     ]
165
166
167 VERSIONFILE = "src/allmydata/_version.py"
168 verstr = "unknown"
169 try:
170     verstrline = open(VERSIONFILE, "rt").read()
171 except EnvironmentError:
172     pass # Okay, there is no version file.
173 else:
174     VSRE = r"^verstr = ['\"]([^'\"]*)['\"]"
175     mo = re.search(VSRE, verstrline, re.M)
176     if mo:
177         verstr = mo.group(1)
178     else:
179         print "unable to find version in %s" % (VERSIONFILE,)
180         raise RuntimeError("if %s.py exists, it is required to be well-formed" % (VERSIONFILE,))
181
182 LONG_DESCRIPTION=\
183 """Welcome to the Tahoe project, a secure, decentralized, fault-tolerant
184 filesystem.  All of the source code is available under a Free Software, Open
185 Source licence.
186
187 This filesystem is encrypted and spread over multiple peers in such a way that
188 it remains available even when some of the peers are unavailable,
189 malfunctioning, or malicious."""
190
191
192 setup_requires = []
193
194 # Nevow requires Twisted to setup, but doesn't declare that requirement in a way that enables
195 # setuptools to satisfy that requirement before Nevow's setup.py tried to "import twisted".
196 setup_requires.extend(['Twisted >= 2.4.0', 'setuptools_trial'])
197
198 # darcsver is needed only if you want "./setup.py darcsver" to write a new
199 # version stamp in src/allmydata/_version.py, with a version number derived from
200 # darcs history.
201 # http://pypi.python.org/pypi/darcsver
202 if 'darcsver' in sys.argv[1:]:
203     setup_requires.append('darcsver >= 1.1.5')
204
205 # setuptools_darcs is required to produce complete distributions (such as with
206 # "sdist" or "bdist_egg"), unless there is a PKG-INFO file present which shows
207 # that this is itself a source distribution.
208 # http://pypi.python.org/pypi/setuptools_darcs
209 if not os.path.exists('PKG-INFO'):
210     setup_requires.append('setuptools_darcs >= 1.1.0')
211
212 class ShowSupportLib(Command):
213     user_options = []
214     def initialize_options(self):
215         pass
216     def finalize_options(self):
217         pass
218     def run(self):
219         # TODO: --quiet suppresses the 'running show_supportlib' message.
220         # Find a way to do this all the time.
221         print supportlib # TODO windowsy
222
223 class ShowPythonPath(Command):
224     user_options = []
225     def initialize_options(self):
226         pass
227     def finalize_options(self):
228         pass
229     def run(self):
230         # TODO: --quiet suppresses the 'running show_supportlib' message.
231         # Find a way to do this all the time.
232         print "PYTHONPATH=%s" % os.environ["PYTHONPATH"]
233
234 class RunWithPythonPath(Command):
235     description = "Run a subcommand with PYTHONPATH set appropriately"
236
237     user_options = [ ("python", "p",
238                       "Treat command string as arguments to a python executable"),
239                      ("command=", "c", "Command to be run"),
240                      ("directory=", "d", "Directory to run the command in"),
241                      ]
242     boolean_options = ["python"]
243
244     def initialize_options(self):
245         self.command = None
246         self.python = False
247         self.directory = None
248     def finalize_options(self):
249         pass
250     def run(self):
251         # os.environ['PYTHONPATH'] is already set by add_tahoe_paths, so we
252         # just need to exec() their command. We must require the command to
253         # be safe to split on whitespace, and have --python and --directory
254         # to make it easier to achieve this.
255         command = []
256         if self.python:
257             command.append(sys.executable)
258         if self.command:
259             command.extend(self.command.split())
260         if not command:
261             raise RuntimeError("The --command argument is mandatory")
262         if self.directory:
263             os.chdir(self.directory)
264         if self.verbose:
265             print "command =", " ".join(command)
266         rc = subprocess.call(command)
267         sys.exit(rc)
268
269 class CheckAutoDeps(Command):
270     user_options = []
271     def initialize_options(self):
272         pass
273     def finalize_options(self):
274         pass
275     def run(self):
276         import _auto_deps
277         _auto_deps.require_auto_deps()
278
279
280 class BuildTahoe(Command):
281     user_options = []
282     def initialize_options(self):
283         pass
284     def finalize_options(self):
285         pass
286     def run(self):
287         # chmod +x bin/tahoe
288         bin_tahoe = os.path.join("bin", "tahoe")
289         old_mode = stat.S_IMODE(os.stat(bin_tahoe)[stat.ST_MODE])
290         new_mode = old_mode | (stat.S_IXUSR | stat.S_IRUSR |
291                                stat.S_IXGRP | stat.S_IRGRP |
292                                stat.S_IXOTH | stat.S_IROTH )
293         os.chmod(bin_tahoe, new_mode)
294
295         # 'setup.py develop --multi-version --prefix SUPPORT' will complain if SUPPORTLIB is
296         # not on PYTHONPATH, because it thinks you are installing to a place
297         # that will not be searched at runtime (which is true, except that we
298         # add SUPPORTLIB to PYTHONPATH to run tests, etc). So set up
299         # PYTHONPATH now, then spawn a 'setup.py develop' command. Also, we
300         # have to create the directory ourselves.
301         if not os.path.isdir(supportlib):
302             os.makedirs(supportlib)
303
304         # command = [sys.executable, "setup.py", "develop", "--multi-version", "--prefix", "support"]
305         command = [sys.executable, "setup.py", "develop", "--prefix", "support"]
306         if sys.platform == "linux2":
307             # workaround for tahoe #229 / setuptools #17, on debian
308             command.extend(["--site-dirs", "/var/lib/python-support/" + pyver])
309         elif sys.platform == "darwin":
310             # this probably only applies to leopard 10.5, possibly only 10.5.5
311             sd = "/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python"
312             command.extend(["--site-dirs", sd])
313         print "Command:", " ".join(command)
314         rc = subprocess.call(command)
315         if rc < 0:
316             print >>sys.stderr, "'setup.py develop' terminated by signal", -rc
317             sys.exit(1)
318         elif rc > 0:
319             print >>sys.stderr, "'setup.py develop' exited with rc", rc
320             sys.exit(rc)
321
322 class Trial(TrialTest):
323     # Custom sub-class of the TrialTest class from the setuptools_trial
324     # plugin so that we can ensure certain options are set by default.
325     #
326     # Examples:
327     #  setup.py trial    # run all tests
328     #  setup.py trial -a allmydata.test.test_util   # run some tests
329     #  setup.py trial -a '--reporter=text allmydata.test.test_util' #other args
330
331
332     def initialize_options(self):
333         TrialTest.initialize_options(self)
334
335         # We want to set the reactor to 'poll', because of bug #402
336         # (twisted bug #3218).
337         if sys.platform in ("linux2", "cygwin"):
338             # poll on linux2 to avoid #402 problems with select
339             # poll on cygwin since selectreactor runs out of fds
340             self.reactor = "poll"
341
342
343 class MySdist(sdist.sdist):
344     """ A hook in the sdist command so that we can determine whether this the
345     tarball should be 'SUMO' or not, i.e. whether or not to include the
346     external dependency tarballs. Note that we always include
347     misc/dependencies/* in the tarball; --sumo controls whether tahoe-deps/*
348     is included as well.
349     """
350
351     user_options = sdist.sdist.user_options + \
352         [('sumo', 's',
353           "create a 'sumo' sdist which includes the contents of tahoe-deps/*"),
354          ]
355     boolean_options = ['sumo']
356
357     def initialize_options(self):
358         sdist.sdist.initialize_options(self)
359         self.sumo = False
360
361     def make_distribution(self):
362         # add our extra files to the list just before building the
363         # tarball/zipfile. We override make_distribution() instead of run()
364         # because setuptools.command.sdist.run() does not lend itself to
365         # easy/robust subclassing (the code we need to add goes right smack
366         # in the middle of a 12-line method). If this were the distutils
367         # version, we'd override get_file_list().
368
369         if self.sumo:
370             # If '--sumo' was specified, include tahoe-deps/* in the sdist.
371             # We assume that the user has fetched the tahoe-deps.tar.gz
372             # tarball and unpacked it already.
373             self.filelist.extend([os.path.join("tahoe-deps", fn)
374                                   for fn in os.listdir("tahoe-deps")])
375             # In addition, we want the tarball/zipfile to have -SUMO in the
376             # name, and the unpacked directory to have -SUMO too. The easiest
377             # way to do this is to patch self.distribution and override the
378             # get_fullname() method. (an alternative is to modify
379             # self.distribution.metadata.version, but that also affects the
380             # contents of PKG-INFO).
381             fullname = self.distribution.get_fullname()
382             def get_fullname():
383                 return fullname + "-SUMO"
384             self.distribution.get_fullname = get_fullname
385
386         return sdist.sdist.make_distribution(self)
387
388 # Tahoe's dependencies are managed by the find_links= entry in setup.cfg and
389 # the _auto_deps.install_requires list, which is used in the call to setup()
390 # at the end of this file
391 from _auto_deps import install_requires
392
393 setup(name='allmydata-tahoe',
394       version=verstr,
395       description='secure, decentralized, fault-tolerant filesystem',
396       long_description=LONG_DESCRIPTION,
397       author='the allmydata.org Tahoe project',
398       author_email='tahoe-dev@allmydata.org',
399       url='http://allmydata.org/',
400       license='GNU GPL',
401       cmdclass={"show_supportlib": ShowSupportLib,
402                 "show_pythonpath": ShowPythonPath,
403                 "run_with_pythonpath": RunWithPythonPath,
404                 "check_auto_deps": CheckAutoDeps,
405                 "build_tahoe": BuildTahoe,
406                 "trial": Trial,
407                 "sdist": MySdist,
408                 },
409       package_dir = {'':'src'},
410       packages=find_packages("src"),
411       classifiers=trove_classifiers,
412       test_suite="allmydata.test",
413       install_requires=install_requires,
414       include_package_data=True,
415       setup_requires=setup_requires,
416       entry_points = { 'console_scripts': [ 'tahoe = allmydata.scripts.runner:run' ] },
417       zip_safe=False, # We prefer unzipped for easier access.
418       )