]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mutopia-index.py
release: 1.3.151
[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"""You're looking at a page with some LilyPond samples.
56 These files are also included in the distribution. The output is
57 completely generated by LilyPond, without any touch up by humans.
58
59
60 <p>
61
62 The PostScript files were generated using TeX and dvips. The pictures
63 are 90dpi anti-aliased snapshots of the printed output.  The images
64 are in PNG format, and should be viewable with any current browser.
65
66 <p>
67
68 If you want a better impression of the appearance, do print out one of
69 the postscript files."""
70
71
72 #
73 # FIXME breaks on multiple strings.
74 #
75 def read_lilypond_header (fn):
76         s = open(fn).read ()
77         s = re.sub('%.*$', '', s)
78         s = re.sub('\n', ' ', s)                
79
80         dict = {}
81         m = re.search (r"""\\header\s*{([^}]*)}""", s)
82
83         if m:
84                         s = m.group(1)
85         else:
86                         return dict
87
88         while s:
89                 m = re.search (r"""\s*(\S+)\s*=\s*([^;]+)\s*;""", s)
90                 if m == None:
91                         s = ''
92                 else:
93                         s = s[m.end (0):]
94                         left  = m.group  (1)
95                         right = m.group (2)
96
97                         left = re.sub ('"', '', left)
98                         right = re.sub ('"', '', right)
99                         dict[left] = right
100
101         return dict
102
103 def help ():
104         sys.stdout.write (r"""Usage: mutopia-index [options] INFILE OUTFILE
105 Generate index for mutopia\n
106 Options:
107   -h, --help                 print this help
108   -o,-output=FILE            write output to file.
109   -s, --subdirs=DIR          add subdir
110   --suffix=SUF                   specify suffix"""
111                                           )
112         sys.exit (0)
113
114 # ugh.
115 def gen_list(inputs, filename):
116         print "generating HTML list %s\n" % filename
117         if filename:
118                 list = open(filename, 'w')
119         else:
120                 list = sys.stdout
121         list.write ('<html><TITLE>Rendered Examples</TITLE>\n')
122         list.write ('<body bgcolor=white>')
123         
124         if inputs:
125                         list.write (headertext)
126         else:
127                         list.write (headertext_nopics)
128
129
130         for ex in inputs:
131                 (base, ext) = os.path.splitext (ex)
132                 (base, ext2) = os.path.splitext (base)          
133                 ext = ext2 + ext
134                 
135                 print '%s, ' % ex
136                 header = read_lilypond_header(ex)
137                 
138                 def read_dict(s, default, h =header):
139                                 try:
140                                         ret = h[s]
141                                 except KeyError:
142                                         ret = default
143                                 return ret
144                 head = read_dict('title', os.path.basename (base))
145                 composer = read_dict('composer', '')
146                 desc = read_dict('description', '')
147                 list.write('<hr>')
148                 list.write('<h1>example file: %s</h1>' % head);
149                 if composer <> '':
150                         list.write('<h2>%s</h2>\n' % composer)
151                 if desc <> '':
152                         list.write('%s<p>' % desc)
153                 list.write ('<ul>')
154                 def list_item(filename, desc, type, l = list):
155                         if file_exist_b(filename):
156                                 l.write ('<li><a href=%s>%s</a>' % (filename, desc))
157                                 size=os.stat(filename)[stat.ST_SIZE]
158                                 l.write (' (%s %dk)' % (type, (size + 512) / 1024))
159                                 pictures = ['jpeg', 'png', 'xpm']
160                                 l.write ('\n')
161
162                 list_item(base + ext, 'The input', 'ASCII')
163                 for pageno in range(1,100):
164                         f  = base + '-page%d.png' % pageno
165                         if not file_exist_b (f):
166                                 break
167                         list_item(f, 'See a picture of page %d' % pageno, 'png')
168                 list_item(base + '.ps.gz', 'Print ', 'gzipped PostScript')
169                 list_item(base + '.midi', 'Listen', 'MIDI')
170                 list.write ("</ul>");
171
172         list.write( "</BODY></HTML>");
173         list.close()
174
175 import getopt
176
177 (options, files) = getopt.getopt(sys.argv[1:], 
178   'ho:', ['help', 'output='])
179 outfile = 'examples.html'
180
181 subdirs =[]
182 for opt in options:
183         o = opt[0]
184         a = opt[1]
185         if o == '--help' or o == '-h':
186                 help()
187         elif o == '--output' or o == '-o':
188                 outfile = a
189
190 dirs  = []
191 for f in files:
192         dirs = dirs + find ('out-www', f)
193
194 if not dirs:
195         dirs = ['.']
196
197 allfiles = []
198
199 for d in dirs:
200         allfiles = allfiles + find ('*.ly.txt', d)
201
202 print allfiles
203
204 gen_list (allfiles, outfile)
205