]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/makelsr.py
Deeply revise input/lsr structure, generation and compilation
[lilypond.git] / buildscripts / makelsr.py
1 #!/usr/bin/python
2
3 import sys
4 import os
5 import glob
6
7 USAGE = '''  Usage: makelsr.py LSR_SNIPPETS_DIR
8 This script must be run from top of the source tree;
9 it updates snippets input/lsr with snippets in input/new or LSR_SNIPPETS_DIR.
10 '''
11
12 LY_HEADER = '''%%%% Do not edit this file; it is auto-generated from LSR!
13 %%%% Tags: %s
14 '''
15
16 DEST = os.path.join ('input', 'lsr')
17 NEW_LYS = os.path.join ('input', 'new')
18
19 TAGS = []
20 # NR 1
21 TAGS.extend (['pitches', 'rhythms', 'expressive-marks',
22 'repeats', 'simultaneous-notes', 'staff-notation', 'editorial-and-educational-use', 'text'])
23 # NR 2
24 TAGS.extend (['vocal-music', 'chords', 'piano-music',
25 'percussion', 'guitar', 'strings', 'bagpipes', 'ancient-notation'])
26
27 TAGS.append ('other')
28
29 def exit_with_usage (n=0):
30         sys.stderr.write (USAGE)
31         sys.exit (n)
32
33 try:
34         in_dir = sys.argv[1]
35 except:
36         exit_with_usage (2)
37
38 if not (os.path.isdir (DEST) and os.path.isdir (NEW_LYS)):
39         exit_with_usage (3)
40
41 unsafe = []
42 unconverted = []
43
44 def copy_ly (srcdir, name, tags):
45         global unsafe
46         global unconverted
47         dest = os.path.join (DEST, name)
48         f = open (dest, 'w')
49         f.write (LY_HEADER % ', '.join (tags))
50         f.write (open (os.path.join (srcdir, name)).read ())
51         f.close ()
52         e = os.system('convert-ly -e ' + dest)
53         if e:
54                 unconverted.append (dest)
55         if os.path.exists (dest + '~'):
56                 os.remove (dest + '~')
57         # -V seems to make unsafe snippets fail nicer/sooner
58         e = os.system ('nice lilypond -V -dno-print-pages -dsafe -o /tmp/lsrtest ' + dest)
59         if e:
60                 unsafe.append (dest)
61
62 def read_source (src):
63         s = {}
64         l = {}
65         for tag in TAGS:
66                 srcdir = os.path.join (src, tag)
67                 l[tag] = set (map (os.path.basename, glob.glob (os.path.join (srcdir, '*.ly'))))
68                 for f in l[tag]:
69                         if f in s.keys ():
70                                 s[f][1].append (tag)
71                         else:
72                                 s[f] = (srcdir, [tag])
73         return s, l
74
75 def dump_file_list (file, list):
76         f = open (file, 'w')
77         f.write ('\n'.join (list) + '\n')
78
79 ## clean out existing lys and generated files
80 map (os.remove, glob.glob (os.path.join (DEST, '*.ly')) +
81      glob.glob (os.path.join (DEST, '*.snippet-list')))
82
83 # read LSR source
84 snippets, tag_lists = read_source (in_dir)
85 # read input/new
86 s, l = read_source (NEW_LYS)
87 snippets.update (s)
88 for t in TAGS:
89         tag_lists[t].update (l[t])
90
91 for (name, (srcdir, tags)) in snippets.items ():
92         copy_ly (srcdir, name, tags)
93
94 for (tag, file_set) in tag_lists.items ():
95         dump_file_list (os.path.join (DEST, tag + '.snippet-list'), file_set)
96
97 if unconverted:
98         sys.stderr.write ('These files could not be converted successfully by convert-ly:\n')
99         sys.stderr.write ('\n'.join (unconverted))
100
101 dump_file_list ('lsr-unsafe.txt', unsafe)
102 sys.stderr.write ('''
103
104 Unsafe files printed in lsr-unsafe.txt: CHECK MANUALLY!
105   xargs git-diff < lsr-unsafe.txt
106
107 ''')
108