Code

*** empty log message ***
[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.4 2002-09-13 04:39:12 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.startswith('__') and mangledfile.endswith('__'):
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 # vim: set filetype=python ts=4 sw=4 et si