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.36 2005-12-03 11:22:50 a1s Exp $
20 """Init (create) a roundup instance.
21 """
22 __docformat__ = 'restructuredtext'
24 import os, errno, rfc822
26 from roundup import install_util, password
27 from roundup.configuration import CoreConfig
28 from roundup.i18n import _
30 def copytree(src, dst, symlinks=0):
31 """Recursively copy a directory tree using copyDigestedFile().
33 The destination directory is allowed to exist.
35 If the optional symlinks flag is true, symbolic links in the
36 source tree result in symbolic links in the destination tree; if
37 it is false, the contents of the files pointed to by symbolic
38 links are copied.
40 This was copied from shutil.py in std lib.
41 """
43 # Prevent 'hidden' files (those starting with '.') from being considered.
44 names = [f for f in os.listdir(src) if not f.startswith('.')]
45 try:
46 os.mkdir(dst)
47 except OSError, error:
48 if error.errno != errno.EEXIST: raise
49 for name in names:
50 srcname = os.path.join(src, name)
51 dstname = os.path.join(dst, name)
52 if symlinks and os.path.islink(srcname):
53 linkto = os.readlink(srcname)
54 os.symlink(linkto, dstname)
55 elif os.path.isdir(srcname):
56 copytree(srcname, dstname, symlinks)
57 else:
58 install_util.copyDigestedFile(srcname, dstname)
60 def install(instance_home, template, settings={}):
61 '''Install an instance using the named template and backend.
63 'instance_home'
64 the directory to place the instance data in
65 'template'
66 the directory holding the template to use in creating the instance data
67 'settings'
68 config.ini setting overrides (dictionary)
70 The instance_home directory will be created using the files found in
71 the named template (roundup.templates.<name>). A usual instance_home
72 contains:
74 config.ini
75 tracker configuration file
76 schema.py
77 database schema definition
78 initial_data.py
79 database initialization script, used to populate the database
80 with 'roundup-admin init' command
81 interfaces.py
82 (optional, not installed from standard templates) defines
83 the CGI Client and mail gateway MailGW classes that are
84 used by roundup.cgi, roundup-server and roundup-mailgw.
85 db/
86 the actual database that stores the instance's data
87 html/
88 the html templates that are used by the CGI Client
89 detectors/
90 the auditor and reactor modules for this instance
91 extensions/
92 code extensions to Roundup
93 '''
94 # At the moment, it's just a copy
95 copytree(template, instance_home)
97 # rename the tempate in the TEMPLATE-INFO.txt file
98 ti = loadTemplateInfo(instance_home)
99 ti['name'] = ti['name'] + '-' + os.path.split(instance_home)[1]
100 saveTemplateInfo(instance_home, ti)
102 # if there is no config.ini or old-style config.py
103 # installed from the template, write default config text
104 config_ini_file = os.path.join(instance_home, CoreConfig.INI_FILE)
105 if not os.path.isfile(config_ini_file):
106 config = CoreConfig(settings=settings)
107 config.save(config_ini_file)
110 def listTemplates(dir):
111 ''' List all the Roundup template directories in a given directory.
113 Find all the dirs that contain a TEMPLATE-INFO.txt and parse it.
115 Return a list of dicts of info about the templates.
116 '''
117 ret = {}
118 for idir in os.listdir(dir):
119 idir = os.path.join(dir, idir)
120 ti = loadTemplateInfo(idir)
121 if ti:
122 ret[ti['name']] = ti
123 return ret
125 def loadTemplateInfo(dir):
126 ''' Attempt to load a Roundup template from the indicated directory.
128 Return None if there's no template, otherwise a template info
129 dictionary.
130 '''
131 ti = os.path.join(dir, 'TEMPLATE-INFO.txt')
132 if not os.path.exists(ti):
133 return None
135 if os.path.exists(os.path.join(dir, 'config.py')):
136 print _("WARNING: directory '%s'\n"
137 "\tcontains old-style template - ignored"
138 ) % os.path.abspath(dir)
139 return None
141 # load up the template's information
142 f = open(ti)
143 try:
144 m = rfc822.Message(open(ti))
145 ti = {}
146 ti['name'] = m['name']
147 ti['description'] = m['description']
148 ti['intended-for'] = m['intended-for']
149 ti['path'] = dir
150 finally:
151 f.close()
152 return ti
154 def writeHeader(name, value):
155 ''' Write an rfc822-compatible header line, making it wrap reasonably
156 '''
157 out = [name.capitalize() + ':']
158 n = len(out[0])
159 for word in value.split():
160 if len(word) + n > 74:
161 out.append('\n')
162 n = 0
163 out.append(' ' + word)
164 n += len(out[-1])
165 return ''.join(out) + '\n'
167 def saveTemplateInfo(dir, info):
168 ''' Save the template info (dict of values) to the TEMPLATE-INFO.txt
169 file in the indicated directory.
170 '''
171 ti = os.path.join(dir, 'TEMPLATE-INFO.txt')
172 f = open(ti, 'w')
173 try:
174 for name in 'name description intended-for path'.split():
175 f.write(writeHeader(name, info[name]))
176 finally:
177 f.close()
179 def write_select_db(instance_home, backend):
180 ''' Write the file that selects the backend for the tracker
181 '''
182 dbdir = os.path.join(instance_home, 'db')
183 if not os.path.exists(dbdir):
184 os.makedirs(dbdir)
185 f = open(os.path.join(dbdir, 'backend_name'), 'w')
186 f.write(backend+'\n')
187 f.close()
191 # vim: set filetype=python sts=4 sw=4 et si :