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