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