lilypond-1.5.12
[lilypond.git] / buildscripts / mutopia-index.py
blob794e59929ec1b6fac3589edf0c908ad8e277eb1d
1 #!@PYTHON@
2 # mutopia-index.py
4 name = 'mutopia-index'
6 # find.py -- deprecated in python 2.0
7 import fnmatch
8 import os
10 _debug = 0
12 _prune = ['(*)']
14 def find(pattern, dir = os.curdir):
15 list = []
16 names = os.listdir(dir)
17 names.sort()
18 for name in names:
19 if name in (os.curdir, os.pardir):
20 continue
21 fullname = os.path.join(dir, name)
22 if fnmatch.fnmatch(name, pattern):
23 list.append(fullname)
24 if os.path.isdir(fullname) and not os.path.islink(fullname):
25 for p in _prune:
26 if fnmatch.fnmatch(name, p):
27 if _debug: print "skip", `fullname`
28 break
29 else:
30 if _debug: print "descend into", `fullname`
31 list = list + find(pattern, fullname)
32 return list
35 import re
36 import os
37 import sys
38 import stat
40 def gulp_file (fn):
41 try:
42 f = open (fn)
43 except:
44 raise 'not there' , fn
45 return f.read ()
47 def file_exist_b (fn):
48 try:
49 f = open (fn)
50 return 1
51 except:
52 return 0
55 headertext= r"""
56 <p>You're looking at a page with some LilyPond samples.
57 These files are also included in the distribution. The output is
58 completely generated by LilyPond, without any touch up by humans.
60 <p>The PostScript files were generated using TeX and dvips. The pictures
61 are 90dpi anti-aliased snapshots of the printed output. The images
62 are in PNG format, and should be viewable with any current browser.
64 <p>If you want a better impression of the appearance, do print out one of
65 the postscript files.
66 """
70 # FIXME breaks on multiple strings.
72 def read_lilypond_header (fn):
73 s = open(fn).read ()
74 s = re.sub('%.*$', '', s)
75 s = re.sub('\n', ' ', s)
77 dict = {}
78 m = re.search (r"""\\header\s*{([^}]*)}""", s)
80 if m:
81 s = m.group(1)
82 else:
83 return dict
85 while s:
86 m = re.search (r"""\s*(\S+)\s*=\s*([^;]+)\s*;""", s)
87 if m == None:
88 s = ''
89 else:
90 s = s[m.end (0):]
91 left = m.group (1)
92 right = m.group (2)
94 left = re.sub ('"', '', left)
95 right = re.sub ('"', '', right)
96 dict[left] = right
98 return dict
100 def help ():
101 sys.stdout.write (r"""Usage: mutopia-index [options] INFILE OUTFILE
102 Generate index for mutopia\n
103 Options:
104 -h, --help print this help
105 -o,-output=FILE write output to file.
106 -s, --subdirs=DIR add subdir
107 --suffix=SUF specify suffix"""
109 sys.exit (0)
111 # ugh.
112 def gen_list(inputs, filename):
113 print "generating HTML list %s\n" % filename
114 if filename:
115 list = open(filename, 'w')
116 else:
117 list = sys.stdout
118 list.write ('<html><title>Rendered Examples</title>\n')
119 list.write ('<body bgcolor=white>\n')
121 if inputs:
122 list.write (headertext)
123 else:
124 list.write (headertext_nopics)
127 for ex in inputs:
128 (base, ext) = os.path.splitext (ex)
129 (base, ext2) = os.path.splitext (base)
130 ext = ext2 + ext
132 print '%s, ' % ex
133 header = read_lilypond_header(ex)
135 def read_dict(s, default, h =header):
136 try:
137 ret = h[s]
138 except KeyError:
139 ret = default
140 return ret
141 head = read_dict('title', os.path.basename (base))
142 composer = read_dict('composer', '')
143 desc = read_dict('description', '')
144 list.write('<hr>\n')
145 list.write('<h1>example file: %s</h1>\n' % head);
146 if composer <> '':
147 list.write('<h2>%s</h2>\n' % composer)
148 if desc <> '':
149 list.write('%s<p>' % desc)
150 list.write ('<ul>\n')
151 def list_item(filename, desc, type, l = list):
152 if file_exist_b(filename):
153 l.write ('<li><a href="%s">%s</a>' % (filename, desc))
154 size=os.stat(filename)[stat.ST_SIZE]
155 l.write (' (%s %dk)' % (type, (size + 512) / 1024))
156 pictures = ['jpeg', 'png', 'xpm']
157 l.write ('\n')
159 list_item(base + ext, 'The input', 'ASCII')
160 for pageno in range(1,100):
161 f = base + '-page%d.png' % pageno
162 if not file_exist_b (f):
163 break
164 list_item(f, 'See a picture of page %d' % pageno, 'png')
165 list_item(base + '.ps.gz', 'Print ', 'gzipped PostScript')
166 list_item(base + '.midi', 'Listen', 'MIDI')
167 list.write ("</ul>\n");
169 list.write('</body></html>\n');
170 list.close()
172 import getopt
174 (options, files) = getopt.getopt(sys.argv[1:],
175 'ho:', ['help', 'output='])
176 outfile = 'examples.html'
178 subdirs =[]
179 for opt in options:
180 o = opt[0]
181 a = opt[1]
182 if o == '--help' or o == '-h':
183 help()
184 elif o == '--output' or o == '-o':
185 outfile = a
187 dirs = []
188 for f in files:
189 dirs = dirs + find ('out-www', f)
191 if not dirs:
192 dirs = ['.']
194 allfiles = []
196 for d in dirs:
197 allfiles = allfiles + find ('*.ly.txt', d)
199 print allfiles
201 gen_list (allfiles, outfile)