*** empty log message ***
[lilypond/patrick.git] / buildscripts / mutopia-index.py
blob977226317d68daa2aaed526f68b37a758d2428d1
1 #!@PYTHON@
2 # mutopia-index.py
4 import fnmatch
5 import getopt
6 import os
7 import os
8 import re
9 import stat
10 import sys
12 def find (pat, dir):
13 f = os.popen ('find %s -name "%s"'% (dir, pat))
14 lst = []
15 for a in f.readlines():
16 a = a[:-1]
17 lst.append (a)
18 return lst
22 headertext= r"""
24 <h1>LilyPond samples</h1>
27 <p>You're looking at a page with some LilyPond samples. These files
28 are also included in the distribution. The output is completely
29 generated from the source file, without any further touch up.
31 <p>
33 The pictures are 90 dpi anti-aliased snapshots of the printed output.
34 For a good impression of the quality print out the PDF file.
35 """
37 headertext_nopics= r"""
38 <p>Nothing to be seen here, move along.
39 """
42 # FIXME breaks on multiple strings.
44 def read_lilypond_header (fn):
45 s = open (fn).read ()
46 s = re.sub ('%.*$', '', s)
47 s = re.sub ('\n', ' ', s)
49 dict = {}
50 m = re.search (r"""\\header\s*{([^}]*)}""", s)
52 if m:
53 s = m.group (1)
54 else:
55 return dict
57 while s:
58 m = re.search (r'''\s*(\S+)\s*=\s*"([^"]+)"''', s)
59 if m == None:
60 s = ''
61 else:
62 s = s[m.end (0):]
63 left = m.group (1)
64 right = m.group (2)
66 left = re.sub ('"', '', left)
67 right = re.sub ('"', '', right)
68 dict[left] = right
70 return dict
72 def help ():
73 sys.stdout.write (r'''Usage: mutopia-index [OPTIONS] INFILE OUTFILE
74 Generate index for mutopia.
76 Options:
77 -h, --help print this help
78 -o, --output=FILE write output to file
79 -s, --subdirs=DIR add subdir
80 --suffix=SUF specify suffix
82 ''')
83 sys.exit (0)
85 # ugh.
86 def gen_list (inputs, file_name):
87 sys.stderr.write ("generating HTML list %s" % file_name)
88 sys.stderr.write ('\n')
89 if file_name:
90 list = open (file_name, 'w')
91 else:
92 list = sys.stdout
93 list.write ('''<html><head><title>Rendered Examples</title>
94 <style type="text/css">
95 hr { border:0; height:1; color: #000000; background-color: #000000; }\n
96 </style>
97 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
98 </head>''')
100 list.write ('<body bgcolor=white>\n')
102 if inputs:
103 list.write (headertext)
104 else:
105 list.write (headertext_nopics)
107 for ex in inputs:
108 print ex
110 (base, ext) = os.path.splitext (ex)
111 (base, ext2) = os.path.splitext (base)
112 ext = ext2 + ext
114 header = read_lilypond_header (ex)
115 def read_dict (s, default, h = header):
116 try:
117 ret = h[s]
118 except KeyError:
119 ret = default
120 return ret
121 head = read_dict ('title', os.path.basename (base))
122 composer = read_dict ('composer', '')
123 desc = read_dict ('description', '')
124 list.write ('<hr>\n')
125 list.write ('<h1>%s</h1>\n' % head);
126 if composer:
127 list.write ('<h2>%s</h2>\n' % composer)
128 if desc:
129 list.write ('%s<p>' % desc)
130 list.write ('<ul>\n')
132 def list_item (file_name, desc, type, lst = list):
133 if os.path.isfile (file_name):
134 lst.write ('<li><a href="%s">%s</a>'
135 % (file_name, desc))
137 # FIXME: include warning if it uses \include
138 # files.
140 size = os.stat (file_name)[stat.ST_SIZE]
141 kB = (size + 512) / 1024
142 if kB:
143 lst.write (' (%s %d kB)' % (type, kB))
144 else:
145 lst.write (' (%s %d characters)'
146 % (type, size))
147 pictures = ['jpeg', 'png', 'xpm']
148 lst.write ('\n')
149 else:
150 print "can't find" , `file_name`
152 list_item (base + ext, 'The input', 'ASCII')
154 pages_found = 0
155 for page in range (1, 100):
156 f = base + '-page%d.png' % page
158 if not os.path.isfile (f):
159 break
160 pages_found += 1
161 list_item (f, 'See a picture of page %d' % page, 'png')
163 if pages_found == 0 and os.path.exists (base + '.png'):
164 list_item (base + ".png",
165 'See a picture', 'png')
168 list_item (base + '.pdf', 'Print', 'PDF')
169 list_item (base + '.midi', 'Listen', 'MIDI')
170 list.write ('</ul>\n');
172 list.write ('</body></html>\n');
173 list.close ()
175 (options, files) = getopt.getopt (sys.argv[1:],
176 'ho:', ['help', 'output='])
177 outfile = 'examples.html'
179 subdirs = []
180 for opt in options:
181 o = opt[0]
182 a = opt[1]
183 if o == '--help' or o == '-h':
184 help ()
185 elif o == '--output' or o == '-o':
186 outfile = a
188 dirs = []
189 for f in files:
190 dirs = dirs + find ('out-www', f)
192 if not dirs:
193 dirs = ['.']
195 allfiles = []
197 for d in dirs:
198 allfiles = allfiles + find ('*.ly.txt', d)
200 gen_list (allfiles, outfile)