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