]> git.donarmstrong.com Git - lilypond.git/blob - scripts/auxiliar/makelsr.py
bbe36342b1222e192f45599ce9052ad34303e477
[lilypond.git] / scripts / auxiliar / makelsr.py
1 #!/usr/bin/env 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 If a snippet is present in both directories, the one from input/new is preferred.
12 '''
13
14 LY_HEADER_LSR = '''%% Do not edit this file; it is auto-generated from LSR http://lsr.dsi.unimi.it
15 %% This file is in the public domain.
16 '''
17
18 LY_HEADER_NEW = '''%% Do not edit this file; it is auto-generated from input/new
19 %% This file is in the public domain.
20 '''
21
22 DEST = os.path.join ('input', 'lsr')
23 NEW_LYS = os.path.join ('input', 'new')
24 TEXIDOCS = os.path.join ('input', 'texidocs')
25
26 TAGS = []
27 # NR 1
28 TAGS.extend (['pitches', 'rhythms', 'expressive-marks',
29 'repeats', 'simultaneous-notes', 'staff-notation',
30 'editorial-annotations', 'text'])
31 # NR 2
32 TAGS.extend (['vocal-music', 'chords', 'keyboards',
33 'percussion', 'fretted-strings', 'unfretted-strings',
34 'ancient-notation', 'winds', 'world-music'
35 ])
36
37 # other
38 TAGS.extend (['contexts-and-engravers', 'tweaks-and-overrides',
39 'paper-and-layout', 'breaks', 'spacing', 'midi', 'titles', 'template'])
40
41 def exit_with_usage (n=0):
42         sys.stderr.write (USAGE)
43         sys.exit (n)
44
45 if len (sys.argv) >= 2:
46         in_dir = sys.argv[1]
47         if len (sys.argv) >= 3:
48                 exit_with_usage (2)
49         if not (os.path.isdir (DEST) and os.path.isdir (NEW_LYS)):
50                 exit_with_usage (3)
51 else:
52         in_dir = ''
53
54 unsafe = []
55 unconverted = []
56 notags_files = []
57
58 # mark the section that will be printed verbatim by lilypond-book
59 end_header_re = re.compile ('(\\header {.+?doctitle = ".+?})\n', re.M | re.S)
60
61 def mark_verbatim_section (ly_code):
62         return end_header_re.sub ('\\1 % begin verbatim\n\n', ly_code, 1)
63
64 # '% LSR' comments are to be stripped
65 lsr_comment_re = re.compile (r'\s*%+\s*LSR.*')
66
67 begin_header_re = re.compile (r'\\header\s*{', re.M)
68
69 # add tags to ly files from LSR
70 def add_tags (ly_code, tags):
71         return begin_header_re.sub ('\\g<0>\n  lsrtags = "' + tags + '"\n', ly_code, 1)
72
73 def copy_ly (srcdir, name, tags):
74         global unsafe
75         global unconverted
76         dest = os.path.join (DEST, name)
77         tags = ', '.join (tags)
78         s = open (os.path.join (srcdir, name)).read ()
79
80         texidoc_translations_path = os.path.join (TEXIDOCS,
81                                                   os.path.splitext (name)[0] + '.texidoc')
82         if os.path.exists (texidoc_translations_path):
83                 texidoc_translations = open (texidoc_translations_path).read ()
84                 # Since we want to insert the translations verbatim using a 
85                 # regexp, \\ is understood as ONE escaped backslash. So we have
86                 # to escape those backslashes once more...
87                 texidoc_translations = texidoc_translations.replace ('\\', '\\\\')
88                 s = begin_header_re.sub ('\\g<0>\n' + texidoc_translations, s, 1)
89
90         if in_dir and in_dir in srcdir:
91                 s = LY_HEADER_LSR + add_tags (s, tags)
92         else:
93                 s = LY_HEADER_NEW + s
94
95         s = mark_verbatim_section (s)
96         s = lsr_comment_re.sub ('', s)
97         open (dest, 'w').write (s)
98
99         e = os.system ("convert-ly -e '%s'" % dest)
100         if e:
101                 unconverted.append (dest)
102         if os.path.exists (dest + '~'):
103                 os.remove (dest + '~')
104         # -V seems to make unsafe snippets fail nicer/sooner
105         e = os.system ("lilypond -V -dno-print-pages -dsafe -o /tmp/lsrtest '%s'" % dest)
106         if e:
107                 unsafe.append (dest)
108
109 def read_source_with_dirs (src):
110         s = {}
111         l = {}
112         for tag in TAGS:
113                 srcdir = os.path.join (src, tag)
114                 l[tag] = set (map (os.path.basename, glob.glob (os.path.join (srcdir, '*.ly'))))
115                 for f in l[tag]:
116                         if f in s:
117                                 s[f][1].append (tag)
118                         else:
119                                 s[f] = (srcdir, [tag])
120         return s, l
121
122
123 tags_re = re.compile ('lsrtags\\s*=\\s*"(.+?)"')
124
125 def read_source (src):
126         s = {}
127         l = dict ([(tag, set()) for tag in TAGS])
128         for f in glob.glob (os.path.join (src, '*.ly')):
129                 basename = os.path.basename (f)
130                 m = tags_re.search (open (f, 'r').read ())
131                 if m:
132                         file_tags = [tag.strip() for tag in m.group (1). split(',')]
133                         s[basename] = (src, file_tags)
134                         [l[tag].add (basename) for tag in file_tags if tag in TAGS]
135                 else:
136                         notags_files.append (f)
137         return s, l
138
139
140 def dump_file_list (file, file_list, update=False):
141         if update:
142                 old_list = set (open (file, 'r').read ().splitlines ())
143                 old_list.update (file_list)
144                 new_list = list (old_list)
145         else:
146                 new_list = file_list
147         f = open (file, 'w')
148         f.write ('\n'.join (sorted (new_list)) + '\n')
149
150 if in_dir:
151         ## clean out existing lys and generated files
152         map (os.remove, glob.glob (os.path.join (DEST, '*.ly')) +
153              glob.glob (os.path.join (DEST, '*.snippet-list')))
154
155         # read LSR source where tags are defined by subdirs
156         snippets, tag_lists = read_source_with_dirs (in_dir)
157
158         # read input/new where tags are directly defined
159         s, l = read_source (NEW_LYS)
160         snippets.update (s)
161         for t in TAGS:
162                 tag_lists[t].update (l[t])
163 else:
164         snippets, tag_lists = read_source (NEW_LYS)
165
166 for (name, (srcdir, tags)) in snippets.items ():
167         copy_ly (srcdir, name, tags)
168 for (tag, file_set) in tag_lists.items ():
169         dump_file_list (os.path.join (DEST, tag + '.snippet-list'), file_set, update=not(in_dir))
170 if unconverted:
171         sys.stderr.write ('These files could not be converted successfully by convert-ly:\n')
172         sys.stderr.write ('\n'.join (unconverted) + '\n\n')
173 if notags_files:
174         sys.stderr.write ('No tags could be found in these files:\n')
175         sys.stderr.write ('\n'.join (notags_files) + '\n\n')
176
177 dump_file_list ('lsr-unsafe.txt', unsafe)
178 sys.stderr.write ('''
179
180 Unsafe files printed in lsr-unsafe.txt: CHECK MANUALLY!
181   git add input/lsr/*.ly
182   xargs git diff HEAD < lsr-unsafe.txt
183
184 ''')
185