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