]> git.donarmstrong.com Git - lilypond.git/blob - stepmake/bin/add-html-footer.py
1ab9987e84319aa2ee6b9a7694bb5ff2664a26a7
[lilypond.git] / stepmake / bin / add-html-footer.py
1 #!@PYTHON@
2
3 """
4 Print a nice footer.  add the top of the ChangeLog file (up to the ********)
5 """
6 import re
7 import sys
8 import os
9 import time
10 import string 
11 import getopt
12 import __main__
13
14 fullname = "unknown"
15 changelog_file = ''
16 index_file=''
17 banner_file = ''
18 changelog_file=''
19 changes =''
20 package_version = ''
21
22 mail_address = '(address unknown)'
23 try:
24         mail_address= os.environ['MAILADDRESS']
25 except KeyError:
26         pass
27
28
29 webmaster= ''
30 try:
31         webmaster= os.environ['WEBMASTER']
32 except KeyError:
33         pass
34
35
36
37 footer_fn = ''
38 footer = r"""<hr>Please take me <a href=%s>back to the index</a>
39 of %s
40 <!-- package name %s>
41  <!-- webmaster fields. %s %s>
42 """
43
44 builtstr = r"""<hr><font size=-1>
45 This page was built from %s-%s by 
46 <address><br>%s &lt<a href=mailto:%s>%s</a>&gt,  %s.</address><p></font>"""
47
48 package_name = ''
49
50 (options, files) = getopt.getopt(sys.argv[1:], 'c:hp:', [
51         'name=', 'footer=', 'version=',
52         'changelog=', 'help', 'news=', 'index=']) 
53
54 def gulp_file(f):
55         try:
56                         i = open(f)
57                         i.seek (0, 2)
58                         n = i.tell ()
59                         i.seek (0,0)
60         except:
61                         sys.stderr.write ("can't open file: %s\n" % f)
62                         return ''
63         s = i.read (n)
64         if len (s) <= 0:
65                         sys.stderr.write ("gulped empty file: %s\n" % f)
66         i.close ()
67         return s
68
69 def help ():
70         sys.stdout.write (r"""Usage: add-html-footer [OPTION]... HTML-FILE
71 Add a nice footer, add the top of the ChangLog file (up to the ********)
72 Options:
73 -h, --help                       print this help
74 --version               package version
75 --name                           package_name
76 --footer                           footer file.
77 """)
78         sys.exit (0)
79
80 for opt in options:
81         o = opt[0]
82         a = opt[1]
83         if o == '--news' or o == '--changelog' or o == '-c':
84                 changelog_file = a
85         elif o == '--index':
86                 index_file = a
87         elif o == '--footer':
88                 footer_fn = a
89         elif o == '--name':
90                 package_name = a
91         elif o == '-h' or o == '--help':
92                 help ()
93         elif o == '--version':
94                 package_version = a
95         else:
96                 raise 'unknown opt ', o
97 def set_vars():
98         os.environ["CONFIGSUFFIX"] = 'www';
99         if os.name == 'nt':
100                 import ntpwd
101                 pw = ntpwd.getpwname(os.environ['USERNAME'])
102         else:
103                 import pwd
104                 pw = pwd.getpwuid (os.getuid());
105
106         f =pw[4]
107         f = string.split (f, ',')[0]
108         __main__.fullname=f 
109 set_vars ()
110
111
112 def footstr(index):
113         ft = __main__.footer
114
115         if footer_fn:
116                 try:
117                         ft = open (footer_fn).read ()
118                 except:
119                         raise 'oops: ' , footer_fn
120
121
122         s = ft % (index, package_name, package_name, webmaster, webmaster)
123         s = s + builtstr % (package_name, package_version, fullname,
124                             mail_address, mail_address, 
125                             time.strftime ('%c %Z', time.localtime (time.time ())))
126         return s
127
128 banner = footstr (index_file)
129 banner_id = '<! banner_id >'
130
131
132 if changelog_file:
133         changes = gulp_file (changelog_file)
134         m = re.search ('^\*\*', changes)
135         if m:
136                 changes = changes[:m.start (0)]
137
138
139
140 def do_file (s):
141         if changelog_file:
142                 s = re.sub ('top_of_ChangeLog', '<XMP>\n'+ changes  + '\n</XMP>\n', s)
143
144
145         if re.search (banner_id, s) == None:
146                 s = banner_id + s
147         else:
148                 return s
149
150         s = re.sub ('(?i)<body>', '<BODY BGCOLOR=WHITE><FONT COLOR=BLACK>', s)
151         # do title.
152         #s = check_tag ('<body', '', s, 0)
153         if re.search ('(?i)</body', s):
154                 s = re.sub ('(?i)</body>', banner + '</BODY>', s)
155         elif re.search ('(?i)</html', s):               
156                 s = re.sub ('(?i)</html>', banner + '</HTML>', s)
157         else:
158                 s = s + banner
159
160         return s
161
162 for f in files:
163         s = gulp_file (f)
164         s = do_file (s)
165         open (f, 'w').write (s)
166
167 if 0:
168         title = '<HEAD><TITLE>' \
169                 + package_name + ' -- ' + os.path.basename (os.path.splitext(f)[0]) \
170                 + '</TITLE></HEAD>'
171         s = check_tag ('<title>', title, s, 0)
172
173         s = check_tag ('<html', '', s, 0)
174         if regex.search ('<HTML', s) == -1:
175                 s = '<HTML>\n' + s
176         s = check_tag ('</html>', '</HTML>', s, 1)
177
178         dump_file (f, s)
179
180