Code

Fixed instance installation ... moved the htmlbase module into templates
[roundup.git] / roundup / templates / builder.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: builder.py,v 1.2 2002-09-05 23:39:13 richard Exp $
19 import os, sys, glob, errno, re
21 __doc__ = """
22 Collect template parts and create instance template files.
23 """
25 preamble = """ 
26 # Do Not Edit (Unless You Want To)
27 # This file automagically generated by roundup.templatebuilder.makeHtmlBase
28
29 """
31 def makeHtmlBase(templateDir):
32     ''' make a <template>_htmlbase.py file in rondup.tempaltes, from the
33         contents of templateDir/html
34     '''
35     print "packing up templates in", templateDir
37     filelist = glob.glob(os.path.join(templateDir, 'html', '*'))
38     filelist = filter(os.path.isfile, filelist) # only want files
39     filelist.sort()
41     # ok, figure the template name and templates dir
42     dir, name = os.path.split(templateDir)
44     fd = open(os.path.join(dir, '%s_htmlbase.py'%name), 'w')
45     fd.write(preamble)
46     for file in filelist:
47         # skip the backup files created by richard's vim
48         if file[-1] == '~': continue
49         mangled_name = os.path.basename(file).replace('.','DOT')
50         fd.write('%s = """'%mangled_name)
51         fd.write(re.sub(r'\$((Id|File|Log).*?)\$', r'dollar\1dollar',
52             open(file).read(), re.I))
53         fd.write('"""\n\n')
54     fd.close()
56 def installHtmlBase(template, installDir):
57     ''' passed a template name and an installDir, unpacks the html files into
58         the installdir
59     '''
60     tmod = '%s_htmlbase'%template
61     tdir = __import__('roundup.templates.'+tmod).templates
62     if hasattr(tdir, tmod):
63         htmlbase = getattr(tdir, tmod)
64     else:
65         raise "TemplateError", \
66             "couldn't find roundup.templates.%s_htmlbase"%template
67     installDir = os.path.join(installDir, 'html')
68     try:
69         os.makedirs(installDir)
70     except OSError, error:
71         if error.errno != errno.EEXIST: raise
73 #    print "installing from", htmlbase.__file__, "into", installDir
74     modulecontents = dir(htmlbase)
75     for mangledfile in modulecontents:
76         if mangledfile[0] == "_": 
77             continue
78         filename = re.sub('DOT', '.', mangledfile)
79         outfile = os.path.join(installDir, filename)
80         outfd = open(outfile, 'w')
81         data = getattr(htmlbase, mangledfile)
82         outfd.write(data)
83     
84 if __name__ == "__main__":
85     if len(sys.argv) == 2:
86         makeHtmlBase(sys.argv[1])
87     elif len(sys.argv) == 3:
88         installHtmlBase(sys.argv[1], sys.argv[2])
89     else:
90         print "Usage: %s <template directory>"%sys.argv[0]
92 #
93 # $Log: not supported by cvs2svn $
94 # Revision 1.1  2002/08/16 04:25:03  richard
95 # cleanup: moved templatebuilder into templates.builder
96 #
97 # Revision 1.14  2002/02/05 09:59:05  grubert
98 #  . makeHtmlBase: re.sub under python 2.2 did not replace '.', string.replace does it.
99 #
100 # Revision 1.13  2001/11/22 15:46:42  jhermann
101 # Added module docstrings to all modules.
103 # Revision 1.12  2001/11/14 21:35:21  richard
104 #  . users may attach files to issues (and support in ext) through the web now
106 # Revision 1.11  2001/08/07 00:24:42  richard
107 # stupid typo
109 # Revision 1.10  2001/08/07 00:15:51  richard
110 # Added the copyright/license notice to (nearly) all files at request of
111 # Bizar Software.
113 # Revision 1.9  2001/08/01 05:06:10  richard
114 # htmlbase doesn't have extraneous $Foo$ in it any more
116 # Revision 1.8  2001/07/30 08:12:17  richard
117 # Added time logging and file uploading to the templates.
119 # Revision 1.7  2001/07/30 00:06:52  richard
120 # Hrm - had IOError instead of OSError. Not sure why there's two. Ho hum.
122 # Revision 1.6  2001/07/29 07:01:39  richard
123 # Added vim command to all source so that we don't get no steenkin' tabs :)
127 # vim: set filetype=python ts=4 sw=4 et si