Code

I thought I already made this change...
[roundup.git] / setup.py
1 #! /usr/bin/env python
2 #
3 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
4 # This module is free software, and you may redistribute it and/or modify
5 # under the same terms as Python, so long as this copyright message and
6 # disclaimer are retained in their original form.
7 #
8 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
9 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
10 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
11 # POSSIBILITY OF SUCH DAMAGE.
12 #
13 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
14 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
15 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
16 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
17 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
18
19 # $Id: setup.py,v 1.50 2003-04-25 02:09:20 richard Exp $
21 from distutils.core import setup, Extension
22 from distutils.util import get_platform
23 from distutils.command.build_scripts import build_scripts
25 import sys, os, string
26 from glob import glob
28 # patch distutils if it can't cope with the "classifiers" keyword
29 if sys.version < '2.2.3':
30     from distutils.dist import DistributionMetadata
31     DistributionMetadata.classifiers = None
32     DistributionMetadata.download_url = None
35 #############################################################################
36 ### Build script files
37 #############################################################################
39 class build_scripts_create(build_scripts):
40     """ Overload the build_scripts command and create the scripts
41         from scratch, depending on the target platform.
43         You have to define the name of your package in an inherited
44         class (due to the delayed instantiation of command classes
45         in distutils, this cannot be passed to __init__).
47         The scripts are created in an uniform scheme: they start the
48         run() function in the module
50             <packagename>.scripts.<mangled_scriptname>
52         The mangling of script names replaces '-' and '/' characters
53         with '-' and '.', so that they are valid module paths. 
54     """
55     package_name = None
57     def copy_scripts(self):
58         """ Create each script listed in 'self.scripts'
59         """
60         if not self.package_name:
61             raise Exception("You have to inherit build_scripts_create and"
62                 " provide a package name")
63         
64         to_module = string.maketrans('-/', '_.')
66         self.mkpath(self.build_dir)
67         for script in self.scripts:
68             outfile = os.path.join(self.build_dir, os.path.basename(script))
70             #if not self.force and not newer(script, outfile):
71             #    self.announce("not copying %s (up-to-date)" % script)
72             #    continue
74             if self.dry_run:
75                 self.announce("would create %s" % outfile)
76                 continue
78             module = os.path.splitext(os.path.basename(script))[0]
79             module = string.translate(module, to_module)
80             cmdopt=self.distribution.command_options
81             if cmdopt['install'].has_key('prefix'):
82                 prefix = cmdopt['install']['prefix'][1]
83                 version = '%d.%d'%sys.version_info[:2]
84                 prefix = '''
85 import sys
86 sys.path.insert(1, "%s/lib/python%s/site-packages")
87 '''%(prefix, version)
88             else:
89                 prefix = ''
90             script_vars = {
91                 'python': os.path.normpath(sys.executable),
92                 'package': self.package_name,
93                 'module': module,
94                 'prefix': prefix,
95             }
97             self.announce("creating %s" % outfile)
98             file = open(outfile, 'w')
100             try:
101                 if sys.platform == "win32":
102                     file.write('@echo off\n'
103                         'if NOT "%%_4ver%%" == "" "%(python)s" -O -c "from %(package)s.scripts.%(module)s import run; run()" %%$\n'
104                         'if     "%%_4ver%%" == "" "%(python)s" -O -c "from %(package)s.scripts.%(module)s import run; run()" %%*\n'
105                         % script_vars)
106                 else:
107                     file.write('#! %(python)s -O\n%(prefix)s'
108                         'from %(package)s.scripts.%(module)s import run\n'
109                         'run()\n'
110                         % script_vars)
111             finally:
112                 file.close()
113                 os.chmod(outfile, 0755)
116 class build_scripts_roundup(build_scripts_create):
117     package_name = 'roundup'
120 def scriptname(path):
121     """ Helper for building a list of script names from a list of
122         module files.
123     """
124     script = os.path.splitext(os.path.basename(path))[0]
125     script = string.replace(script, '_', '-')
126     if sys.platform == "win32":
127         script = script + ".bat"
128     return script
132 #############################################################################
133 ### Main setup stuff
134 #############################################################################
136 def main():
137     # build list of scripts from their implementation modules
138     roundup_scripts = map(scriptname, glob('roundup/scripts/[!_]*.py'))
140     # template munching
141     packagelist = [
142         'roundup',
143         'roundup.cgi',
144         'roundup.cgi.PageTemplates',
145         'roundup.cgi.TAL',
146         'roundup.cgi.ZTUtils',
147         'roundup.backends',
148         'roundup.scripts'
149     ]
150     installdatafiles = [
151         ('share/roundup/cgi-bin', ['cgi-bin/roundup.cgi']),
152     ] 
154     # install man pages on POSIX platforms
155     if os.name == 'posix':
156         installdatafiles.append(('man/man1', ['doc/roundup-admin.1',
157             'doc/roundup-mailgw.1', 'doc/roundup-server.1']))
159     # add the templates to the data files lists
160     from roundup.admin import listTemplates
161     templates = [t['path'] for t in listTemplates('templates').values()]
162     for tdir in templates:
163         # scan for data files
164         for idir in '. detectors html'.split():
165             idir = os.path.join(tdir, idir)
166             tfiles = []
167             for f in os.listdir(idir):
168                 if f.startswith('.'):
169                     continue
170                 ifile = os.path.join(idir, f)
171                 if os.path.isfile(ifile):
172                     tfiles.append(ifile)
173             installdatafiles.append(
174                 (os.path.join('share', 'roundup', idir), tfiles)
175             )
177     # perform the setup action
178     from roundup import __version__
179     setup(
180         name = "roundup", 
181         version = __version__,
182         description = "Roundup issue tracking system.",
183         author = "Richard Jones",
184         author_email = "richard@users.sourceforge.net",
185         url = 'http://sourceforge.net/projects/roundup/',
186         download_url = 'http://sourceforge.net/project/showfiles.php?group_id=31577',
187         packages = packagelist,
188         classifiers = [
189             'Development Status :: 4 - Beta',
190             'Environment :: Console',
191             'Environment :: Web Environment',
192             'Intended Audience :: End Users/Desktop',
193             'Intended Audience :: Developers',
194             'Intended Audience :: System Administrators',
195             'License :: OSI Approved :: Python Software Foundation License',
196             'Operating System :: MacOS :: MacOS X',
197             'Operating System :: Microsoft :: Windows',
198             'Operating System :: POSIX',
199             'Programming Language :: Python',
200             'Topic :: Communications :: Email',
201             'Topic :: Office/Business',
202             'Topic :: Software Development :: Bug Tracking',
203         ],
205         # Override certain command classes with our own ones
206         cmdclass = {
207             'build_scripts': build_scripts_roundup,
208         },
209         scripts = roundup_scripts,
211         data_files =  installdatafiles
212     )
214 def install_demo():
215     ''' Install a demo server for users to play with for instant gratification.
217         Sets up the web service on localhost port 8080. Disables nosy lists.
218     '''
219     import shutil, socket, errno, BaseHTTPServer
221     # create the instance
222     home = os.path.abspath('demo')
223     try:
224         shutil.rmtree(home)
225     except os.error, error:
226         if error.errno != errno.ENOENT:
227             raise
228     from roundup import init, instance, password
229     init.install(home, os.path.join('templates', 'classic'))
230     # don't have email flying around
231     os.remove(os.path.join(home, 'detectors', 'nosyreaction.py'))
232     init.write_select_db(home, 'anydbm')
234     # figure basic params for server
235     hostname = socket.gethostname()
236     # pick a fairly odd, random port
237     port = 8917
238     while 1:
239         print 'Trying to set up web server on port %d ...'%port,
240         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
241         s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
242         try:
243             s.connect((hostname, port))
244         except socket.error, e:
245             if not hasattr(e, 'args') or e.args[0] != errno.ECONNREFUSED:
246                 raise
247             print 'should be ok.'
248             break
249         else:
250             s.close()
251             print 'already in use.'
252             port += 100
253     url = 'http://%s:%s/demo/'%(hostname, port)
255     # write the config
256     f = open(os.path.join(home, 'config.py'), 'r')
257     s = f.read().replace('http://tracker.example/cgi-bin/roundup.cgi/bugs/',
258         url)
259     f.close()
260     f = open(os.path.join(home, 'config.py'), 'w')
261     f.write(s)
262     f.close()
264     # initialise the database
265     init.initialise(home, 'admin')
267     # add the "demo" user
268     tracker = instance.open(home)
269     db = tracker.open('admin')
270     db.user.create(username='demo', password=password.Password('demo'),
271         realname='Demo User', roles='User')
272     db.commit()
273     db.close()
275     # ok, so start up the server
276     from roundup.scripts.roundup_server import RoundupRequestHandler
277     RoundupRequestHandler.TRACKER_HOMES = {'demo': home}
278     httpd = BaseHTTPServer.HTTPServer((hostname, port), RoundupRequestHandler)
279     print 'Server running - connect to:\n  %s'%url
280     print 'You may log in as "demo"/"demo" or "admin"/"admin".'
281     print 'Hit Control-C to stop the server.'
282     try:
283         httpd.serve_forever()
284     except KeyboardInterrupt:
285         print 'Keyboard Interrupt: exiting'
287 if __name__ == '__main__':
288     if len(sys.argv) > 1 and sys.argv[1] == 'demo':
289         install_demo()
290     else:
291         main()
293 # vim: set filetype=python ts=4 sw=4 et si