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