1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 #
18 # $Id: init.py,v 1.28 2003-11-13 04:12:10 richard Exp $
20 __doc__ = """
21 Init (create) a roundup instance.
22 """
24 import os, sys, errno, rfc822
26 import roundup.instance, password
27 from roundup import install_util
29 def copytree(src, dst, symlinks=0):
30 """Recursively copy a directory tree using copyDigestedFile().
32 The destination directory os allowed to exist.
34 If the optional symlinks flag is true, symbolic links in the
35 source tree result in symbolic links in the destination tree; if
36 it is false, the contents of the files pointed to by symbolic
37 links are copied.
39 This was copied from shutil.py in std lib.
40 """
41 names = os.listdir(src)
42 try:
43 os.mkdir(dst)
44 except OSError, error:
45 if error.errno != errno.EEXIST: raise
46 for name in names:
47 srcname = os.path.join(src, name)
48 dstname = os.path.join(dst, name)
49 if symlinks and os.path.islink(srcname):
50 linkto = os.readlink(srcname)
51 os.symlink(linkto, dstname)
52 elif os.path.isdir(srcname):
53 copytree(srcname, dstname, symlinks)
54 else:
55 install_util.copyDigestedFile(srcname, dstname)
57 def install(instance_home, template):
58 '''Install an instance using the named template and backend.
60 instance_home - the directory to place the instance data in
61 template - the directory holding the template to use in creating
62 the instance data
64 The instance_home directory will be created using the files found in
65 the named template (roundup.templates.<name>). A standard instance_home
66 contains:
67 . config.py
68 - simple configuration of things like the email address for the
69 mail gateway, the mail domain, the mail host, ...
70 . dbinit.py and select_db.py
71 - defines the schema for the hyperdatabase and indicates which
72 backend to use.
73 . interfaces.py
74 - defines the CGI Client and mail gateway MailGW classes that are
75 used by roundup.cgi, roundup-server and roundup-mailgw.
76 . __init__.py
77 - ties together all the instance information into one interface
78 . db/
79 - the actual database that stores the instance's data
80 . html/
81 - the html templates that are used by the CGI Client
82 . detectors/
83 - the auditor and reactor modules for this instance
85 '''
86 # At the moment, it's just a copy
87 copytree(template, instance_home)
89 # rename the tempate in the TEMPLATE-INFO.txt file
90 ti = loadTemplateInfo(instance_home)
91 ti['name'] = ti['name'] + '-' + os.path.split(instance_home)[1]
92 saveTemplateInfo(instance_home, ti)
95 def listTemplates(dir):
96 ''' List all the Roundup template directories in a given directory.
98 Find all the dirs that contain a TEMPLATE-INFO.txt and parse it.
100 Return a list of dicts of info about the templates.
101 '''
102 ret = {}
103 for idir in os.listdir(dir):
104 idir = os.path.join(dir, idir)
105 ti = loadTemplateInfo(idir)
106 if ti:
107 ret[ti['name']] = ti
108 return ret
110 def loadTemplateInfo(dir):
111 ''' Attempt to load a Roundup template from the indicated directory.
113 Return None if there's no template, otherwise a template info
114 dictionary.
115 '''
116 ti = os.path.join(dir, 'TEMPLATE-INFO.txt')
117 if not os.path.exists(ti):
118 return None
120 # load up the template's information
121 f = open(ti)
122 try:
123 m = rfc822.Message(open(ti))
124 ti = {}
125 ti['name'] = m['name']
126 ti['description'] = m['description']
127 ti['intended-for'] = m['intended-for']
128 ti['path'] = dir
129 finally:
130 f.close()
131 return ti
133 def writeHeader(name, value):
134 ''' Write an rfc822-compatible header line, making it wrap reasonably
135 '''
136 out = [name.capitalize() + ':']
137 n = len(out[0])
138 for word in value.split():
139 if len(word) + n > 74:
140 out.append('\n')
141 n = 0
142 out.append(' ' + word)
143 n += len(out[-1])
144 return ''.join(out) + '\n'
146 def saveTemplateInfo(dir, info):
147 ''' Save the template info (dict of values) to the TEMPLATE-INFO.txt
148 file in the indicated directory.
149 '''
150 ti = os.path.join(dir, 'TEMPLATE-INFO.txt')
151 f = open(ti, 'w')
152 try:
153 for name in 'name description intended-for path'.split():
154 f.write(writeHeader(name, info[name]))
155 finally:
156 f.close()
158 def write_select_db(instance_home, backend):
159 ''' Write the file that selects the backend for the tracker
160 '''
161 # now select database
162 db = '''# WARNING: DO NOT EDIT THIS FILE!!!
163 from roundup.backends.back_%s import Database, Class, FileClass, IssueClass
164 '''%backend
165 open(os.path.join(instance_home, 'select_db.py'), 'w').write(db)
168 def initialise(instance_home, adminpw):
169 '''Initialise an instance's database
171 adminpw - the password for the "admin" user
172 '''
173 # now import the instance and call its init
174 instance = roundup.instance.open(instance_home)
175 instance.init(password.Password(adminpw))
177 # vim: set filetype=python ts=4 sw=4 et si