release commit
[lilypond.git] / buildscripts / mutopia-index.py
blob1e0d594394192edd99f64dc0058e41eff081cb5a
1 #!@PYTHON@
2 # mutopia-index.py
4 name = 'mutopia-index'
6 import fnmatch
7 import os
9 _debug = 0
11 _prune = ['(*)']
13 def find(pattern, dir = os.curdir):
14 list = []
15 names = os.listdir(dir)
16 names.sort()
17 for name in names:
18 if name in (os.curdir, os.pardir):
19 continue
20 fullname = os.path.join(dir, name)
21 if fnmatch.fnmatch(name, pattern):
22 list.append(fullname)
23 if os.path.isdir(fullname) and not os.path.islink(fullname):
24 for p in _prune:
25 if fnmatch.fnmatch(name, p):
26 if _debug: print "skip", `fullname`
27 break
28 else:
29 if _debug: print "descend into", `fullname`
30 list = list + find(pattern, fullname)
31 return list
34 import re
35 import os
36 import sys
37 import stat
39 def gulp_file (fn):
40 try:
41 f = open (fn)
42 except:
43 raise 'not there' , fn
44 return f.read ()
46 def file_exist_b (fn):
47 try:
48 f = open (fn)
49 return 1
50 except:
51 return 0
54 headertext= r"""
56 <p>You're looking at a page with some LilyPond samples. These files
57 are also included in the distribution. The output is completely
58 generated from the <tt>.ly</tt> source file, without any further touch
59 up.
61 <p>
63 The pictures are 90 dpi anti-aliased snapshots of the printed output.
64 If you want a better impression of the appearance, do print out one of
65 the PDF or PostScript files; they use scalable fonts, and should look
66 good at any resolution.
68 """
70 headertext_nopics= r"""
71 <p>Nothing to be seen here, move along.
72 """
75 # FIXME breaks on multiple strings.
77 def read_lilypond_header (fn):
78 s = open(fn).read ()
79 s = re.sub('%.*$', '', s)
80 s = re.sub('\n', ' ', s)
82 dict = {}
83 m = re.search (r"""\\header\s*{([^}]*)}""", s)
85 if m:
86 s = m.group(1)
87 else:
88 return dict
90 while s:
91 m = re.search (r'''\s*(\S+)\s*=\s*"([^"]+)"''', s)
92 if m == None:
93 s = ''
94 else:
95 s = s[m.end (0):]
96 left = m.group (1)
97 right = m.group (2)
99 left = re.sub ('"', '', left)
100 right = re.sub ('"', '', right)
101 dict[left] = right
103 return dict
105 def help ():
106 sys.stdout.write (r"""Usage: mutopia-index [OPTIONS] INFILE OUTFILE
107 Generate index for mutopia.
109 Options:
110 -h, --help print this help
111 -o, --output=FILE write output to file
112 -s, --subdirs=DIR add subdir
113 --suffix=SUF specify suffix
117 sys.exit (0)
119 # ugh.
120 def gen_list(inputs, filename):
121 print "generating HTML list %s\n" % filename
122 if filename:
123 list = open(filename, 'w')
124 else:
125 list = sys.stdout
126 list.write ('<html><title>Rendered Examples</title>\n')
127 list.write ('<body bgcolor=white>\n')
129 if inputs:
130 list.write (headertext)
131 else:
132 list.write (headertext_nopics)
135 for ex in inputs:
136 (base, ext) = os.path.splitext (ex)
137 (base, ext2) = os.path.splitext (base)
138 ext = ext2 + ext
140 header = read_lilypond_header(ex)
141 def read_dict(s, default, h =header):
142 try:
143 ret = h[s]
144 except KeyError:
145 ret = default
146 return ret
147 head = read_dict('title', os.path.basename (base))
148 composer = read_dict('composer', '')
149 desc = read_dict('description', '')
150 list.write('<hr>\n')
151 list.write('<h1>%s</h1>\n' % head);
152 if composer:
153 list.write('<h2>%s</h2>\n' % composer)
154 if desc:
155 list.write('%s<p>' % desc)
156 list.write ('<ul>\n')
157 def list_item(filename, desc, type, l = list):
158 if file_exist_b(filename):
160 l.write ('<li><a href="%s">%s</a>' % (filename, desc))
162 # todo: include warning if it uses \include
163 # files.
165 size=os.stat(filename)[stat.ST_SIZE]
166 kB=(size + 512) / 1024
167 if kB:
168 l.write (' (%s %d kB)' % (type, kB))
169 else:
170 l.write (' (%s %d characters)' % (type, size))
171 pictures = ['jpeg', 'png', 'xpm']
172 l.write ('\n')
174 list_item(base + ext, 'The input', 'ASCII')
175 for pageno in range(1,100):
176 f = base + '-page%d.png' % pageno
177 if not file_exist_b (f):
178 break
179 list_item(f, 'See a picture of page %d' % pageno, 'png')
180 list_item(base + '.pdf', 'Print', 'PDF')
181 list_item(base + '.ps.gz', 'Print', 'gzipped PostScript')
182 list_item(base + '.midi', 'Listen', 'MIDI')
183 list.write ("</ul>\n");
185 list.write('</body></html>\n');
186 list.close()
188 import getopt
190 (options, files) = getopt.getopt(sys.argv[1:],
191 'ho:', ['help', 'output='])
192 outfile = 'examples.html'
194 subdirs =[]
195 for opt in options:
196 o = opt[0]
197 a = opt[1]
198 if o == '--help' or o == '-h':
199 help()
200 elif o == '--output' or o == '-o':
201 outfile = a
203 dirs = []
204 for f in files:
205 dirs = dirs + find ('out-www', f)
207 if not dirs:
208 dirs = ['.']
210 allfiles = []
212 for d in dirs:
213 allfiles = allfiles + find ('*.ly.txt', d)
215 gen_list (allfiles, outfile)