Code

documentation cleanup
[roundup.git] / roundup / init.py
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.29 2004-02-11 23:55:08 richard Exp $
20 """Init (create) a roundup instance.
21 """
22 __docformat__ = 'restructuredtext'
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'
61        the directory to place the instance data in
62     'template'
63        the directory holding the template to use in creating the instance data
65     The instance_home directory will be created using the files found in
66     the named template (roundup.templates.<name>). A standard instance_home
67     contains:
69     config.py
70       simple configuration of things like the email address for the
71       mail gateway, the mail domain, the mail host, ...
72     dbinit.py and select_db.py
73       defines the schema for the hyperdatabase and indicates which
74       backend to use.
75     interfaces.py
76       defines the CGI Client and mail gateway MailGW classes that are
77       used by roundup.cgi, roundup-server and roundup-mailgw.
78     __init__.py
79       ties together all the instance information into one interface
80     db/
81       the actual database that stores the instance's data
82     html/
83       the html templates that are used by the CGI Client
84     detectors/
85       the auditor and reactor modules for this instance
86     '''
87     # At the moment, it's just a copy
88     copytree(template, instance_home)
90     # rename the tempate in the TEMPLATE-INFO.txt file
91     ti = loadTemplateInfo(instance_home)
92     ti['name'] = ti['name'] + '-' + os.path.split(instance_home)[1]
93     saveTemplateInfo(instance_home, ti)
96 def listTemplates(dir):
97     ''' List all the Roundup template directories in a given directory.
99         Find all the dirs that contain a TEMPLATE-INFO.txt and parse it.
101         Return a list of dicts of info about the templates.
102     '''
103     ret = {}
104     for idir in os.listdir(dir):
105         idir = os.path.join(dir, idir)
106         ti = loadTemplateInfo(idir)
107         if ti:
108             ret[ti['name']] = ti
109     return ret
111 def loadTemplateInfo(dir):
112     ''' Attempt to load a Roundup template from the indicated directory.
114         Return None if there's no template, otherwise a template info
115         dictionary.
116     '''
117     ti = os.path.join(dir, 'TEMPLATE-INFO.txt')
118     if not os.path.exists(ti):
119         return None
121     # load up the template's information
122     f = open(ti)
123     try:
124         m = rfc822.Message(open(ti))
125         ti = {}
126         ti['name'] = m['name']
127         ti['description'] = m['description']
128         ti['intended-for'] = m['intended-for']
129         ti['path'] = dir
130     finally:
131         f.close()
132     return ti
134 def writeHeader(name, value):
135     ''' Write an rfc822-compatible header line, making it wrap reasonably
136     '''
137     out = [name.capitalize() + ':']
138     n = len(out[0])
139     for word in value.split():
140         if len(word) + n > 74:
141             out.append('\n')
142             n = 0
143         out.append(' ' + word)
144         n += len(out[-1])
145     return ''.join(out) + '\n'
147 def saveTemplateInfo(dir, info):
148     ''' Save the template info (dict of values) to the TEMPLATE-INFO.txt
149         file in the indicated directory.
150     '''
151     ti = os.path.join(dir, 'TEMPLATE-INFO.txt')
152     f = open(ti, 'w')
153     try:
154         for name in 'name description intended-for path'.split():
155             f.write(writeHeader(name, info[name]))
156     finally:
157         f.close()
159 def write_select_db(instance_home, backend):
160     ''' Write the file that selects the backend for the tracker
161     '''
162     # now select database
163     db = '''# WARNING: DO NOT EDIT THIS FILE!!!
164 from roundup.backends.back_%s import Database, Class, FileClass, IssueClass
165 '''%backend
166     open(os.path.join(instance_home, 'select_db.py'), 'w').write(db)
169 def initialise(instance_home, adminpw):
170     '''Initialise an instance's database
172     adminpw    - the password for the "admin" user
173     '''
174     # now import the instance and call its init
175     instance = roundup.instance.open(instance_home)
176     instance.init(password.Password(adminpw))
178 # vim: set filetype=python ts=4 sw=4 et si