]> git.donarmstrong.com Git - lilypond.git/blob - stepmake/bin/add-html-footer.py
* GNUmakefile.in (local-WWW-post): footify build fix.
[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
13 gcos = "unknown"
14 index_url=''
15 top_url=''
16 changelog_file=''
17 package_name = ''
18 package_version = ''
19
20 mail_address = '(address unknown)'
21 try:
22         mail_address= os.environ['MAILADDRESS']
23 except KeyError:
24         pass
25
26 webmaster= mail_address
27 try:
28         webmaster= os.environ['WEBMASTER']
29 except KeyError:
30         pass
31
32 header_file = ''
33 footer_file = ''
34 default_header = r"""
35 """
36
37 default_footer = r"""<hr>Please take me <a href=@INDEX@>back to the index</a>
38 of @PACKAGE_NAME@
39 """
40
41 built = r"""<hr>
42 <p><font size="-1">
43 This page is for @PACKAGE_NAME@-@PACKAGE_VERSION@ (@BRANCH@). <br>
44 </font>
45 <address><font size="-1">
46 Report errors to &lt;<a href="mailto:@MAILADDRESS@">@MAILADDRESS@</a>&gt;</font></address>"""
47
48
49 def gulp_file (f):
50         try:
51                 i = open(f)
52                 i.seek (0, 2)
53                 n = i.tell ()
54                 i.seek (0,0)
55         except:
56                 sys.stderr.write ("can't open file: %s\n" % f)
57                 return ''
58         s = i.read (n)
59         if len (s) <= 0:
60                 sys.stderr.write ("gulped empty file: %s\n" % f)
61         i.close ()
62         return s
63
64 def help ():
65         sys.stdout.write (r"""Usage: add-html-footer [OPTIONS]... HTML-FILE
66 Add header, footer and top of ChangLog file (up to the ********) to HTML-FILE
67
68 Options:
69   --changelog=FILE          use FILE as ChangeLog [ChangeLog]
70   --footer=FILE             use FILE as footer
71   --header=FILE             use FILE as header
72   -h, --help                print this help
73   --index=URL               set homepage to URL
74   --name=NAME               set package_name to NAME
75   --version=VERSION         set package version to VERSION
76
77 """)
78         sys.exit (0)
79
80 (options, files) = getopt.getopt(sys.argv[1:], 'h', [
81         'changelog=', 'footer=', 'header=', 'help', 'index=',
82         'name=', 'version=']) 
83
84 for opt in options:
85         o = opt[0]
86         a = opt[1]
87         if o == '--changelog':
88                 changelog_file = a
89         elif o == '--footer':
90                 footer_file = a
91         elif o == '--header':
92                 header_file = a
93         elif o == '-h' or o == '--help':
94                 help ()
95         # urg, this is top!
96         elif o == '--index':
97                 index_url = a
98         elif o == '--name':
99                 package_name = a
100         elif o == '--version':
101                 package_version = a
102         else:
103                 raise 'unknown opt ', o
104
105 #burp?
106 def set_gcos ():
107         global gcos
108         os.environ["CONFIGSUFFIX"] = 'www';
109         if os.name == 'nt':
110                 import ntpwd
111                 pw = ntpwd.getpwname(os.environ['USERNAME'])
112         else:
113                 import pwd
114                 if os.environ.has_key('FAKEROOTKEY') and os.environ.has_key('LOGNAME'):
115                         pw = pwd.getpwnam (os.environ['LOGNAME'])
116                 else:
117                         pw = pwd.getpwuid (os.getuid())
118
119         f = pw[4]
120         f = string.split (f, ',')[0]
121         gcos = f 
122
123 def compose (default, file):
124         s = default
125         if file:
126                 s = gulp_file (file)
127         return s
128
129 set_gcos ()
130 localtime = time.strftime ('%c %Z', time.localtime (time.time ()))
131
132 if os.path.basename (index_url) != "index.html":
133         index_url = os.path.join (index_url , "index.html")
134 top_url = os.path.dirname (index_url) + "/"
135
136 header = compose (default_header, header_file)
137
138 # compose (default_footer, footer_file)
139 footer =  built
140 header_tag = '<!-- header_tag -->'
141 footer_tag = '<!-- footer_tag -->'
142
143 # Python < 1.5.2 compatibility
144 #
145 # On most platforms, this is equivalent to
146 #`normpath(join(os.getcwd()), PATH)'.  *Added in Python version 1.5.2*
147 if os.path.__dict__.has_key ('abspath'):
148         abspath = os.path.abspath
149 else:
150         def abspath (path):
151                 return os.path.normpath (os.path.join (os.getcwd (), path))
152
153
154 def remove_self_ref (s):        
155         self_url = abspath (os.getcwd () + '/' + f)
156         #sys.stderr.write ('url0: %s\n' % self_url)
157
158         # self_url = re.sub ('.*?' + string.lower (package_name) + '[^/]*/',
159         #                '', self_url)
160         # URG - this only works when source tree is unpacked in `src/' dir
161         # For some reason, .*? still eats away
162         #     /home/fred/usr/src/lilypond-1.5.14/Documentation/user/out-www/lilypond/
163         # instead of just
164         #
165         #     /home/fred/usr/src/lilypond-1.5.14/
166         #
167         #     Tutorial.html
168         self_url = re.sub ('.*?src/' + string.lower (package_name) + '[^/]*/',
169                          '', self_url)
170
171         #sys.stderr.write ('url1: %s\n' % self_url)
172         
173         #urg, ugly lily-specific toplevel index hack
174         self_url = re.sub ('.*topdocs/out-www/index.html', 'index.html', self_url)
175         #sys.stderr.write ('url2: %s\n' % self_url)
176
177         # ugh, python2.[12] re is broken.
178         ## pat = re.compile ('.*?(<a href="[\./]*' + self_url + '#?[^"]*">)([^<]*)(</a>)', re.DOTALL)
179         pat = re.compile ('[.\n]*?(<a href="[\./]*' + self_url + '#?[^"]*">)([^<]*)(</a>)')
180         m = pat.search (s)
181         while m:
182                 #sys.stderr.write ('self: %s\n' % m.group (2))
183                 s = s[:m.start (1)] + m.group (2) + s[m.end (3):]
184                 m = pat.search (s)
185         return s
186
187 def do_file (f):
188         s = gulp_file (f)
189
190         if changelog_file:
191                 changes = gulp_file (changelog_file)
192                 # urg?
193                 #m = re.search ('^\\\\*\\\\*', changes)
194                 m = re.search (r'\*\*\*', changes)
195                 if m:
196                         changes = changes[:m.start (0)]
197                 s = re.sub ('top_of_ChangeLog', '<pre>\n'+ changes  + '\n</pre>\n', s)
198
199         if re.search (header_tag, s) == None:
200                 body = '<BODY BGCOLOR=WHITE TEXT=BLACK>'
201                 s = re.sub ('(?i)<body>', body, s)
202                 if re.search ('(?i)<BODY', s):
203                         s = re.sub ('(?i)<body[^>]*>', body + header, s, 1)
204                 elif re.search ('(?i)<html', s):                
205                         s = re.sub ('(?i)<html>', '<HTML>' + header, s, 1)
206                 else:
207                         s = header + s
208
209                 s = header_tag + '\n' + s
210
211                 if re.search ('(?i)<!DOCTYPE', s) == None:
212                         doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n'
213                         s = doctype + s
214
215         if re.search (footer_tag, s) == None:
216                 s = s + footer_tag + '\n'
217
218                 if re.search ('(?i)</body', s):
219                         s = re.sub ('(?i)</body>', footer + '</BODY>', s, 1)
220                 elif re.search ('(?i)</html', s):               
221                         s = re.sub ('(?i)</html>', footer + '</HTML>', s, 1)
222                 else:
223                         s = s + footer
224
225
226         #URUGRGOUSNGUOUNRIU
227         index = index_url
228         top = top_url
229         if os.path.basename (f) == "index.html":
230                 cwd = os.getcwd ()
231                 if os.path.basename (cwd) == "topdocs":
232                         index = "index.html"
233                         top = ""
234
235                 # don't cause ///////index.html entries in log files.
236                 #       index = "./index.html"
237                 #       top = "./"
238
239
240         versiontup = string.split(package_version, '.')
241         branch_str = 'stable-branch'
242         if string.atoi ( versiontup[1]) %  2:
243                 branch_str = 'development-branch'
244                 
245         s = re.sub ('@INDEX@', index, s)
246         s = re.sub ('@TOP@', top, s)
247         s = re.sub ('@PACKAGE_NAME@', package_name, s)
248         s = re.sub ('@PACKAGE_VERSION@', package_version, s)
249         s = re.sub ('@WEBMASTER@', webmaster, s)
250         s = re.sub ('@GCOS@', gcos, s)
251         s = re.sub ('@LOCALTIME@', localtime, s)
252         s = re.sub ('@MAILADDRESS@', mail_address, s)
253         s = re.sub ('@BRANCH@', branch_str, s)  
254
255         # ugh, python2.[12] re is broken.
256         #pat = re.compile ('.*?<!--\s*(@[^@]*@)\s*=\s*([^>]*)\s*-->', re.DOTALL)
257         pat = re.compile ('[.\n]*?<!--\s*(@[^@]*@)\s*=\s*([^>]*)\s*-->')
258         m = pat.search (s)
259         while m:
260                 at_var = m.group (1)
261                 at_val = m.group (2)
262                 sys.stderr.write ('at: %s -> %s\n' % (at_var, at_val))
263                 s = re.sub (at_var, at_val, s)
264                 m = pat.search (s)
265
266         # urg
267         # maybe find first node?
268         fallback_web_title = '-- --'
269
270         # ugh, python2.[12] re is broken.
271         #m = re.match ('.*?<title>\(.*?\)</title>', s, re.DOTALL)
272         m = re.match ('[.\n]*?<title>([.\n]*?)</title>', s)
273         if m:
274                 fallback_web_title = m.group (1)
275         s = re.sub ('@WEB-TITLE@', fallback_web_title, s)
276         
277         s = remove_self_ref (s)
278
279         open (f, 'w').write (s)
280
281
282 for f in files:
283         do_file (f)
284