]> git.donarmstrong.com Git - lilypond.git/blob - stepmake/bin/add-html-footer.py
release: 1.3.141
[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><font size=-1>
42 This page was built from @PACKAGE_NAME@-@PACKAGE_VERSION@ by 
43 <address><br>@GCOS@ &lt<a href="mailto:%s">@MAIL_ADDRESS@</a>&gt,
44 @LOCALTIME@.</address><p></font>"""
45
46
47 def gulp_file (f):
48         try:
49                 i = open(f)
50                 i.seek (0, 2)
51                 n = i.tell ()
52                 i.seek (0,0)
53         except:
54                 sys.stderr.write ("can't open file: %s\n" % f)
55                 return ''
56         s = i.read (n)
57         if len (s) <= 0:
58                 sys.stderr.write ("gulped empty file: %s\n" % f)
59         i.close ()
60         return s
61
62 def help ():
63         sys.stdout.write (r"""Usage: add-html-footer [OPTION]... HTML-FILE
64 Add header, footer and top of ChangLog file (up to the ********) to HTML-FILE
65
66 Options:
67   --changelog=FILE          use FILE as ChangeLog [ChangeLog]
68   --footer=FILE             use FILE as footer
69   --header=FILE             use FILE as header
70   -h, --help                print this help
71   --index=URL               set homepage to URL
72   --name=NAME               set package_name to NAME
73   --version=VERSION         set package version to VERSION
74 """)
75         sys.exit (0)
76
77 (options, files) = getopt.getopt(sys.argv[1:], 'h', [
78         'changelog=', 'footer=', 'header=', 'help', 'index=',
79         'name=', 'version=']) 
80
81 for opt in options:
82         o = opt[0]
83         a = opt[1]
84         if o == '--changelog':
85                 changelog_file = a
86         elif o == '--footer':
87                 footer_file = a
88         elif o == '--header':
89                 header_file = a
90         elif o == '-h' or o == '--help':
91                 help ()
92         # urg, this is top!
93         elif o == '--index':
94                 index_url = a
95         elif o == '--name':
96                 package_name = a
97         elif o == '--version':
98                 package_version = a
99         else:
100                 raise 'unknown opt ', o
101
102 #burp?
103 def set_gcos ():
104         global gcos
105         os.environ["CONFIGSUFFIX"] = 'www';
106         if os.name == 'nt':
107                 import ntpwd
108                 pw = ntpwd.getpwname(os.environ['USERNAME'])
109         else:
110                 import pwd
111                 pw = pwd.getpwuid (os.getuid());
112
113         f = pw[4]
114         f = string.split (f, ',')[0]
115         gcos = f 
116
117 def compose (default, file):
118         s = default
119         if file:
120                 s = gulp_file (file)
121         return s
122
123 set_gcos ()
124 localtime = time.strftime ('%c %Z', time.localtime (time.time ()))
125
126 if os.path.basename (index_url) != "index.html":
127         index_url = os.path.join (index_url , "index.html")
128 top_url = os.path.dirname (index_url) + "/"
129
130 header = compose (default_header, header_file)
131 footer = compose (default_footer, footer_file) + built
132 header_tag = '<! header_tag >'
133 footer_tag = '<! footer_tag >'
134
135 def do_file (f):
136         s = gulp_file (f)
137
138         if changelog_file:
139                 changes = gulp_file (changelog_file)
140                 # urg?
141                 #m = re.search ('^\\\\*\\\\*', changes)
142                 m = re.search (r'\*\*\*', changes)
143                 if m:
144                         changes = changes[:m.start (0)]
145                 s = re.sub ('top_of_ChangeLog', '<XMP>\n'+ changes  + '\n</XMP>\n', s)
146
147         if re.search (header_tag, s) == None:
148                 body = '<BODY BGCOLOR=WHITE><FONT COLOR=BLACK>'
149                 s = re.sub ('(?i)<body>', body, s)
150                 if re.search ('(?i)<BODY', s):
151                         s = re.sub ('(?i)<body[^>]*>', body + header, s, 1)
152                 elif re.search ('(?i)<html', s):                
153                         s = re.sub ('(?i)<html>', '<HTML>' + header, s, 1)
154                 else:
155                         s = header + s
156
157                 s = header_tag + s
158
159         if re.search (footer_tag, s) == None:
160                 s = s + footer_tag
161
162                 if re.search ('(?i)</body', s):
163                         s = re.sub ('(?i)</body>', footer + '</BODY>', s, 1)
164                 elif re.search ('(?i)</html', s):               
165                         s = re.sub ('(?i)</html>', footer + '</HTML>', s, 1)
166                 else:
167                         s = s + footer
168
169         #URUGRGOUSNGUOUNRIU
170         index = index_url
171         top = top_url
172         if os.path.basename (f) == "index.html":
173                 cwd = os.getcwd ()
174                 if os.path.basename (cwd) == "topdocs":
175                         index = "index.html"
176                         top = ""
177
178                 # don't cause ///////index.html entries in log files.
179                 #       index = "./index.html"
180                 #       top = "./"
181
182         s = re.sub ('@INDEX@', index, s)
183         s = re.sub ('@TOP@', top, s)
184         s = re.sub ('@PACKAGE_NAME@', package_name, s)
185         s = re.sub ('@PACKAGE_VERSION@', package_version, s)
186         s = re.sub ('@WEBMASTER@', webmaster, s)
187         s = re.sub ('@GCOS@', gcos, s)
188         s = re.sub ('@LOCALTIME@', localtime, s)
189         s = re.sub ('@MAIL_ADDRESS@', mail_address, s)
190
191         open (f, 'w').write (s)
192
193
194 for f in files:
195         do_file (f)
196