]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mutopia-index.py
patch::: 1.5.14.jcn3
[lilypond.git] / buildscripts / mutopia-index.py
1 #!@PYTHON@
2 # mutopia-index.py
3
4 name = 'mutopia-index'
5
6 # find.py -- deprecated in python 2.0
7 import fnmatch
8 import os
9
10 _debug = 0
11
12 _prune = ['(*)']
13
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
33
34
35 import re
36 import os
37 import sys
38 import stat
39
40 def gulp_file (fn):
41         try:
42                 f = open (fn)
43         except:
44                 raise 'not there' , fn
45         return f.read ()
46
47 def file_exist_b (fn):
48         try:
49                 f = open (fn)
50                 return 1
51         except:
52                 return 0
53
54
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.
59
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.
63
64 <p>If you want a better impression of the appearance, do print out one of
65 the postscript files.
66 """
67
68
69 #
70 # FIXME breaks on multiple strings.
71 #
72 def read_lilypond_header (fn):
73         s = open(fn).read ()
74         s = re.sub('%.*$', '', s)
75         s = re.sub('\n', ' ', s)                
76
77         dict = {}
78         m = re.search (r"""\\header\s*{([^}]*)}""", s)
79
80         if m:
81                         s = m.group(1)
82         else:
83                         return dict
84
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)
93
94                         left = re.sub ('"', '', left)
95                         right = re.sub ('"', '', right)
96                         dict[left] = right
97
98         return dict
99
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"""
108                                           )
109         sys.exit (0)
110
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')
120         
121         if inputs:
122                         list.write (headertext)
123         else:
124                         list.write (headertext_nopics)
125
126
127         for ex in inputs:
128                 (base, ext) = os.path.splitext (ex)
129                 (base, ext2) = os.path.splitext (base)          
130                 ext = ext2 + ext
131                 
132                 print '%s, ' % ex
133                 header = read_lilypond_header(ex)
134                 
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                                 kB=(size + 512) / 1024
156                                 if kB:
157                                         l.write (' (%s %dkB)' % (type, kB))
158                                 else:
159                                         l.write (' (%s %dcharacters)' % (type, size))
160                                 pictures = ['jpeg', 'png', 'xpm']
161                                 l.write ('\n')
162
163                 list_item(base + ext, 'The input', 'ASCII')
164                 for pageno in range(1,100):
165                         f  = base + '-page%d.png' % pageno
166                         if not file_exist_b (f):
167                                 break
168                         list_item(f, 'See a picture of page %d' % pageno, 'png')
169                 list_item(base + '.ps.gz', 'Print ', 'gzipped PostScript')
170                 list_item(base + '.midi', 'Listen', 'MIDI')
171                 list.write ("</ul>\n");
172
173         list.write('</body></html>\n');
174         list.close()
175
176 import getopt
177
178 (options, files) = getopt.getopt(sys.argv[1:], 
179   'ho:', ['help', 'output='])
180 outfile = 'examples.html'
181
182 subdirs =[]
183 for opt in options:
184         o = opt[0]
185         a = opt[1]
186         if o == '--help' or o == '-h':
187                 help()
188         elif o == '--output' or o == '-o':
189                 outfile = a
190
191 dirs  = []
192 for f in files:
193         dirs = dirs + find ('out-www', f)
194
195 if not dirs:
196         dirs = ['.']
197
198 allfiles = []
199
200 for d in dirs:
201         allfiles = allfiles + find ('*.ly.txt', d)
202
203 print allfiles
204
205 gen_list (allfiles, outfile)
206