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.
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.
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.
18 # $Id: init.py,v 1.36 2005-12-03 11:22:50 a1s Exp $
20 """Init (create) a roundup instance.
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
40 This was copied from shutil.py in std lib.
43 # Prevent 'hidden' files (those starting with '.') from being considered.
44 names
= [f
for f
in os
.listdir(src
) if not f
.startswith('.')]
47 except OSError, error
:
48 if error
.errno
!= errno
.EEXIST
: raise
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
)
58 install_util
.copyDigestedFile(srcname
, dstname
)
60 def install(instance_home
, template
, settings
={}):
61 '''Install an instance using the named template and backend.
64 the directory to place the instance data in
66 the directory holding the template to use in creating the instance data
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
75 tracker configuration file
77 database schema definition
79 database initialization script, used to populate the database
80 with 'roundup-admin init' command
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.
86 the actual database that stores the instance's data
88 the html templates that are used by the CGI Client
90 the auditor and reactor modules for this instance
92 code extensions to Roundup
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.
118 for idir
in os
.listdir(dir):
119 idir
= os
.path
.join(dir, idir
)
120 ti
= loadTemplateInfo(idir
)
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
131 ti
= os
.path
.join(dir, 'TEMPLATE-INFO.txt')
132 if not os
.path
.exists(ti
):
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)
141 # load up the template's information
144 m
= rfc822
.Message(open(ti
))
146 ti
['name'] = m
['name']
147 ti
['description'] = m
['description']
148 ti
['intended-for'] = m
['intended-for']
154 def writeHeader(name
, value
):
155 ''' Write an rfc822-compatible header line, making it wrap reasonably
157 out
= [name
.capitalize() + ':']
159 for word
in value
.split():
160 if len(word
) + n
> 74:
163 out
.append(' ' + word
)
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.
171 ti
= os
.path
.join(dir, 'TEMPLATE-INFO.txt')
174 for name
in 'name description intended-for path'.split():
175 f
.write(writeHeader(name
, info
[name
]))
179 def write_select_db(instance_home
, backend
, dbdir
= 'db'):
180 ''' Write the file that selects the backend for the tracker
182 # dbdir may be a relative pathname, os.path.join does the right
183 # thing when the second component of a join is an absolute path
184 dbdir
= os
.path
.join (instance_home
, dbdir
)
185 if not os
.path
.exists(dbdir
):
187 f
= open(os
.path
.join(dbdir
, 'backend_name'), 'w')
188 f
.write(backend
+'\n')
193 # vim: set filetype=python sts=4 sw=4 et si :