]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - setup.py
setup.py: stop putting pyutil.version_class/etc in _version.py
[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-2011 Allmydata, Inc.
7 #
8 # This file is part of Tahoe-LAFS.
9 #
10 # See the docs/about.rst file for licensing information.
11
12 import glob, os, stat, subprocess, sys, 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 len(sys.argv) > 1 and sys.argv[1] == '--fakedependency':
67     del sys.argv[1]
68     install_requires += ["fakedependency >= 1.0.0"]
69
70 __requires__ = install_requires[:]
71
72 egg = os.path.realpath(glob.glob('setuptools-*.egg')[0])
73 sys.path.insert(0, egg)
74 egg = os.path.realpath(glob.glob('darcsver-*.egg')[0])
75 sys.path.insert(0, egg)
76 egg = os.path.realpath(glob.glob('setuptools_darcs-*.egg')[0])
77 sys.path.insert(0, egg)
78 import setuptools; setuptools.bootstrap_install_from = egg
79
80 from setuptools import find_packages, setup
81 from setuptools.command import sdist
82 from setuptools import Command
83
84 trove_classifiers=[
85     "Development Status :: 5 - Production/Stable",
86     "Environment :: Console",
87     "Environment :: Web Environment",
88     "License :: OSI Approved :: GNU General Public License (GPL)",
89     "License :: DFSG approved",
90     "License :: Other/Proprietary License",
91     "Intended Audience :: Developers",
92     "Intended Audience :: End Users/Desktop",
93     "Intended Audience :: System Administrators",
94     "Operating System :: Microsoft",
95     "Operating System :: Microsoft :: Windows",
96     "Operating System :: Microsoft :: Windows :: Windows NT/2000",
97     "Operating System :: Unix",
98     "Operating System :: POSIX :: Linux",
99     "Operating System :: POSIX",
100     "Operating System :: MacOS :: MacOS X",
101     "Operating System :: OS Independent",
102     "Natural Language :: English",
103     "Programming Language :: C",
104     "Programming Language :: Python",
105     "Programming Language :: Python :: 2",
106     "Programming Language :: Python :: 2.4",
107     "Programming Language :: Python :: 2.5",
108     "Programming Language :: Python :: 2.6",
109     "Programming Language :: Python :: 2.7",
110     "Topic :: Utilities",
111     "Topic :: System :: Systems Administration",
112     "Topic :: System :: Filesystems",
113     "Topic :: System :: Distributed Computing",
114     "Topic :: Software Development :: Libraries",
115     "Topic :: Communications :: Usenet News",
116     "Topic :: System :: Archiving :: Backup",
117     "Topic :: System :: Archiving :: Mirroring",
118     "Topic :: System :: Archiving",
119     ]
120
121
122 setup_requires = []
123
124 # The darcsver command from the darcsver plugin is needed to initialize the
125 # distribution's .version attribute correctly. (It does this either by
126 # examining darcs history, or if that fails by reading the
127 # src/allmydata/_version.py file). darcsver will also write a new version
128 # stamp in src/allmydata/_version.py, with a version number derived from
129 # darcs history. Note that the setup.cfg file has an "[aliases]" section
130 # which enumerates commands that you might run and specifies that it will run
131 # darcsver before each one. If you add different commands (or if I forgot
132 # some that are already in use), you may need to add it to setup.cfg and
133 # configure it to run darcsver before your command, if you want the version
134 # number to be correct when that command runs.
135 # http://pypi.python.org/pypi/darcsver
136 setup_requires.append('darcsver >= 1.7.2')
137
138 # Nevow imports itself when building, which causes Twisted and zope.interface
139 # to be imported. We need to make sure that the versions of Twisted and
140 # zope.interface used at build time satisfy Nevow's requirements. If not
141 # then there are two problems:
142 #  - prior to Nevow v0.9.33, Nevow didn't declare its dependency on Twisted
143 #    in a way that enabled setuptools to satisfy that requirement at
144 #    build time.
145 #  - some versions of zope.interface, e.g. v3.6.4, are incompatible with
146 #    Nevow, and we need to avoid those both at build and run-time.
147 #
148 # This only matters when compatible versions of Twisted and zope.interface
149 # are not already installed. Retire this hack when
150 # https://bugs.launchpad.net/nevow/+bug/812537 has been fixed.
151 setup_requires += [req for req in install_requires if req.startswith('Twisted') or req.startswith('zope.interface')]
152
153 # setuptools_darcs is required to produce complete distributions (such
154 # as with "sdist" or "bdist_egg"), unless there is a
155 # src/allmydata_tahoe.egg-info/SOURCE.txt file present which contains
156 # a complete list of files that should be included.
157
158 # http://pypi.python.org/pypi/setuptools_darcs
159
160 # However, requiring it runs afoul of a bug in Distribute, which was
161 # shipped in Ubuntu Lucid, so for now you have to manually install it
162 # before building sdists or eggs:
163 # http://bitbucket.org/tarek/distribute/issue/55/revision-control-plugin-automatically-installed-as-a-build-dependency-is-not-present-when-another-build-dependency-is-being
164
165 # Note that we explicitly inject setuptools_darcs at the beginning of
166 # this setup.py file, so it is still in effect when building dists
167 # using this setup.py file even when the following requirement is
168 # disabled.
169 if False:
170     setup_requires.append('setuptools_darcs >= 1.1.0')
171
172 # trialcoverage is required if you want the "trial" unit test runner to have a
173 # "--reporter=bwverbose-coverage" option which produces code-coverage results.
174 # The required version is 0.3.3, because that is the latest version that only
175 # depends on a version of pycoverage for which binary packages are available.
176 if "--reporter=bwverbose-coverage" in sys.argv:
177     setup_requires.append('trialcoverage >= 0.3.3')
178
179 # stdeb is required to produce Debian files with the "sdist_dsc" command.
180 if "sdist_dsc" in sys.argv:
181     setup_requires.append('stdeb >= 0.3')
182
183 # We no longer have any requirements specific to tests.
184 tests_require=[]
185
186
187 class Trial(Command):
188     description = "run trial (use 'bin%stahoe debug trial' for the full set of trial options)" % (os.sep,)
189     # This is just a subset of the most useful options, for compatibility.
190     user_options = [ ("rterrors", "e", "Print out tracebacks as soon as they occur."),
191                      ("reporter=", None, "The reporter to use for this test run."),
192                      ("suite=", "s", "Specify the test suite."),
193                      ("quiet", None, "Don't display version numbers and paths of Tahoe dependencies."),
194                    ]
195
196     def initialize_options(self):
197         self.rterrors = False
198         self.reporter = None
199         self.suite = "allmydata"
200         self.quiet = False
201
202     def finalize_options(self):
203         pass
204
205     def run(self):
206         args = [sys.executable, os.path.join('bin', 'tahoe')]
207         if not self.quiet:
208             args.append('--version-and-path')
209         args += ['debug', 'trial']
210         if self.rterrors:
211             args.append('--rterrors')
212         if self.reporter:
213             args.append('--reporter=' + self.reporter)
214         if self.suite:
215             args.append(self.suite)
216         rc = subprocess.call(args)
217         sys.exit(rc)
218
219
220 class MakeExecutable(Command):
221     description = "make the 'bin%stahoe' scripts" % (os.sep,)
222     user_options = []
223
224     def initialize_options(self):
225         pass
226     def finalize_options(self):
227         pass
228     def run(self):
229         bin_tahoe_template = os.path.join("bin", "tahoe-script.template")
230
231         # tahoe.pyscript is really only necessary for Windows, but we also
232         # create it on Unix for consistency.
233         script_names = ["tahoe.pyscript", "tahoe"]
234
235         # Create the tahoe script file under the 'bin' directory. This
236         # file is exactly the same as the 'tahoe-script.template' script
237         # except that the shebang line is rewritten to use our sys.executable
238         # for the interpreter.
239         f = open(bin_tahoe_template, "rU")
240         script_lines = f.readlines()
241         f.close()
242         script_lines[0] = '#!%s\n' % (sys.executable,)
243         for script_name in script_names:
244             tahoe_script = os.path.join("bin", script_name)
245             try:
246                 os.remove(tahoe_script)
247             except Exception:
248                 if os.path.exists(tahoe_script):
249                    raise
250             f = open(tahoe_script, "wb")
251             for line in script_lines:
252                 f.write(line)
253             f.close()
254
255         # chmod +x
256         unix_script = os.path.join("bin", "tahoe")
257         old_mode = stat.S_IMODE(os.stat(unix_script)[stat.ST_MODE])
258         new_mode = old_mode | (stat.S_IXUSR | stat.S_IRUSR |
259                                stat.S_IXGRP | stat.S_IRGRP |
260                                stat.S_IXOTH | stat.S_IROTH )
261         os.chmod(unix_script, new_mode)
262
263         old_tahoe_exe = os.path.join("bin", "tahoe.exe")
264         try:
265             os.remove(old_tahoe_exe)
266         except Exception:
267             if os.path.exists(old_tahoe_exe):
268                 raise
269
270
271 DARCS_VERSION_BODY = '''
272 # This _version.py is generated from darcs metadata by the tahoe setup.py
273 # and the "darcsver" package.
274
275 __pkgname__ = "%(pkgname)s"
276 verstr = "%(pkgversion)s"
277 __version__ = verstr
278 '''
279
280 GIT_VERSION_BODY = '''
281 # This _version.py is generated from git metadata by the tahoe setup.py.
282
283 __pkgname__ = "%(pkgname)s"
284 real_version = "%(version)s"
285 full_version = "%(full)s"
286 verstr = "%(normalized)s"
287 __version__ = verstr
288 '''
289
290 def run_command(args, cwd=None, verbose=False):
291     try:
292         # remember shell=False, so use git.cmd on windows, not just git
293         p = subprocess.Popen(args, stdout=subprocess.PIPE, cwd=cwd)
294     except EnvironmentError, e:
295         if verbose:
296             print "unable to run %s" % args[0]
297             print e
298         return None
299     stdout = p.communicate()[0].strip()
300     if p.returncode != 0:
301         if verbose:
302             print "unable to run %s (error)" % args[0]
303         return None
304     return stdout
305
306
307 def versions_from_git(tag_prefix, verbose=False):
308     # this runs 'git' from the directory that contains this file. That either
309     # means someone ran a setup.py command (and this code is in
310     # versioneer.py, thus the containing directory is the root of the source
311     # tree), or someone ran a project-specific entry point (and this code is
312     # in _version.py, thus the containing directory is somewhere deeper in
313     # the source tree). This only gets called if the git-archive 'subst'
314     # variables were *not* expanded, and _version.py hasn't already been
315     # rewritten with a short version string, meaning we're inside a checked
316     # out source tree.
317
318     # versions_from_git (as copied from python-versioneer) returns strings
319     # like "1.9.0-25-gb73aba9-dirty", which means we're in a tree with
320     # uncommited changes (-dirty), the latest checkin is revision b73aba9,
321     # the most recent tag was 1.9.0, and b73aba9 has 25 commits that weren't
322     # in 1.9.0 . The narrow-minded NormalizedVersion parser that takes our
323     # output (meant to enable sorting of version strings) refuses most of
324     # that. Tahoe uses a function named suggest_normalized_version() that can
325     # handle "1.9.0.post25", so dumb down our output to match.
326
327     try:
328         source_dir = os.path.dirname(os.path.abspath(__file__))
329     except NameError:
330         # some py2exe/bbfreeze/non-CPython implementations don't do __file__
331         return {} # not always correct
332     GIT = "git"
333     if sys.platform == "win32":
334         GIT = "git.cmd"
335     stdout = run_command([GIT, "describe", "--tags", "--dirty", "--always"],
336                          cwd=source_dir)
337     if stdout is None:
338         return {}
339     if not stdout.startswith(tag_prefix):
340         if verbose:
341             print "tag '%s' doesn't start with prefix '%s'" % (stdout, tag_prefix)
342         return {}
343     version = stdout[len(tag_prefix):]
344     pieces = version.split("-")
345     if len(pieces) == 1:
346         normalized_version = pieces[0]
347     else:
348         normalized_version = "%s.post%s" % (pieces[0], pieces[1])
349     stdout = run_command([GIT, "rev-parse", "HEAD"], cwd=source_dir)
350     if stdout is None:
351         return {}
352     full = stdout.strip()
353     if version.endswith("-dirty"):
354         full += "-dirty"
355         normalized_version += ".dev0"
356     return {"version": version, "normalized": normalized_version, "full": full}
357
358
359 class UpdateVersion(Command):
360     description = "update _version.py from revision-control metadata"
361     user_options = []
362
363     def initialize_options(self):
364         pass
365     def finalize_options(self):
366         pass
367     def run(self):
368         target = self.distribution.versionfiles[0]
369         if os.path.isdir(os.path.join(basedir, "_darcs")):
370             verstr = self.try_from_darcs(target)
371         elif os.path.isdir(os.path.join(basedir, ".git")):
372             verstr = self.try_from_git(target)
373         else:
374             print "no version-control data found, leaving _version.py alone"
375             return
376         if verstr:
377             self.distribution.metadata.version = verstr
378
379     def try_from_darcs(self, target):
380         from darcsver.darcsvermodule import update
381         (rc, verstr) = update(pkgname=self.distribution.get_name(),
382                               verfilename=self.distribution.versionfiles,
383                               revision_number=True,
384                               version_body=DARCS_VERSION_BODY)
385         if rc == 0:
386             return verstr
387
388     def try_from_git(self, target):
389         versions = versions_from_git("allmydata-tahoe-", verbose=True)
390         if versions:
391             for fn in self.distribution.versionfiles:
392                 f = open(fn, "wb")
393                 f.write(GIT_VERSION_BODY %
394                         { "pkgname": self.distribution.get_name(),
395                           "version": versions["version"],
396                           "normalized": versions["normalized"],
397                           "full": versions["full"] })
398                 f.close()
399                 print "git-version: wrote '%s' into '%s'" % (versions["version"], fn)
400         return versions.get("normalized", None)
401
402
403 class MySdist(sdist.sdist):
404     """ A hook in the sdist command so that we can determine whether this the
405     tarball should be 'SUMO' or not, i.e. whether or not to include the
406     external dependency tarballs. Note that we always include
407     misc/dependencies/* in the tarball; --sumo controls whether tahoe-deps/*
408     is included as well.
409     """
410
411     user_options = sdist.sdist.user_options + \
412         [('sumo', 's',
413           "create a 'sumo' sdist which includes the contents of tahoe-deps/*"),
414          ]
415     boolean_options = ['sumo']
416
417     def initialize_options(self):
418         sdist.sdist.initialize_options(self)
419         self.sumo = False
420
421     def make_distribution(self):
422         # add our extra files to the list just before building the
423         # tarball/zipfile. We override make_distribution() instead of run()
424         # because setuptools.command.sdist.run() does not lend itself to
425         # easy/robust subclassing (the code we need to add goes right smack
426         # in the middle of a 12-line method). If this were the distutils
427         # version, we'd override get_file_list().
428
429         if self.sumo:
430             # If '--sumo' was specified, include tahoe-deps/* in the sdist.
431             # We assume that the user has fetched the tahoe-deps.tar.gz
432             # tarball and unpacked it already.
433             self.filelist.extend([os.path.join("tahoe-deps", fn)
434                                   for fn in os.listdir("tahoe-deps")])
435             # In addition, we want the tarball/zipfile to have -SUMO in the
436             # name, and the unpacked directory to have -SUMO too. The easiest
437             # way to do this is to patch self.distribution and override the
438             # get_fullname() method. (an alternative is to modify
439             # self.distribution.metadata.version, but that also affects the
440             # contents of PKG-INFO).
441             fullname = self.distribution.get_fullname()
442             def get_fullname():
443                 return fullname + "-SUMO"
444             self.distribution.get_fullname = get_fullname
445
446         try:
447             old_mask = os.umask(int("022", 8))
448             return sdist.sdist.make_distribution(self)
449         finally:
450             os.umask(old_mask)
451
452
453 setup_args = {}
454 if version:
455     setup_args["version"] = version
456
457 setup(name=APPNAME,
458       description='secure, decentralized, fault-tolerant filesystem',
459       long_description=open('README.txt', 'rU').read(),
460       author='the Tahoe-LAFS project',
461       author_email='tahoe-dev@tahoe-lafs.org',
462       url='https://tahoe-lafs.org/',
463       license='GNU GPL', # see README.txt -- there is an alternative licence
464       cmdclass={"trial": Trial,
465                 "make_executable": MakeExecutable,
466                 "update_version": UpdateVersion,
467                 "sdist": MySdist,
468                 },
469       package_dir = {'':'src'},
470       packages=find_packages("src"),
471       classifiers=trove_classifiers,
472       test_suite="allmydata.test",
473       install_requires=install_requires,
474       tests_require=tests_require,
475       include_package_data=True,
476       setup_requires=setup_requires,
477       entry_points = { 'console_scripts': [ 'tahoe = allmydata.scripts.runner:run' ] },
478       zip_safe=False, # We prefer unzipped for easier access.
479       versionfiles=['src/allmydata/_version.py',],
480       **setup_args
481       )