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