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