]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/makelsr.py
Add support for LSR snippets texidocs translation
[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-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'])
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 notags_files = []
53
54 # mark the section that will be printed verbatim by lilypond-book
55 end_header_re = re.compile ('(\\header {.+?(?:"\\s*|\\s+)}\n)\n', re.M | re.S)
56
57 def mark_verbatim_section (ly_code):
58         return end_header_re.sub ('\\1% begin verbatim\n', ly_code, 1)
59
60 begin_header_re = re.compile ('\\header\\s*{', re.M)
61
62 # add tags to ly files from LSR
63 def add_tags (ly_code, tags):
64         return begin_header_re.sub ('\\g<0>\n  lsrtags = "' + tags + '"\n', ly_code, 1)
65
66 def copy_ly (srcdir, name, tags):
67         global unsafe
68         global unconverted
69         dest = os.path.join (DEST, name)
70         tags = ', '.join (tags)
71         s = open (os.path.join (srcdir, name)).read ()
72
73         texidoc_translations_path = os.path.join (TEXIDOCS,
74                                                   os.path.splitext (name)[0] + '.texidoc')
75         if os.path.exists (texidoc_translations_path):
76                 texidoc_translations = open (texidoc_translations_path).read ()
77                 s = begin_header_re.sub ('\\g<0>\n' + texidoc_translations, s, 1)
78
79         if in_dir in srcdir:
80                 s = LY_HEADER_LSR + add_tags (s, tags)
81         else:
82                 s = LY_HEADER_NEW + s
83
84         s = mark_verbatim_section (s)
85         open (dest, 'w').write (s)
86
87         e = os.system ("convert-ly -e '%s'" % dest)
88         if e:
89                 unconverted.append (dest)
90         if os.path.exists (dest + '~'):
91                 os.remove (dest + '~')
92         # -V seems to make unsafe snippets fail nicer/sooner
93         e = os.system ("lilypond -V -dno-print-pages -dsafe -o /tmp/lsrtest '%s'" % dest)
94         if e:
95                 unsafe.append (dest)
96
97 def read_source_with_dirs (src):
98         s = {}
99         l = {}
100         for tag in TAGS:
101                 srcdir = os.path.join (src, tag)
102                 l[tag] = set (map (os.path.basename, glob.glob (os.path.join (srcdir, '*.ly'))))
103                 for f in l[tag]:
104                         if f in s.keys ():
105                                 s[f][1].append (tag)
106                         else:
107                                 s[f] = (srcdir, [tag])
108         return s, l
109
110
111 tags_re = re.compile ('lsrtags\\s*=\\s*"(.+?)"')
112
113 def read_source (src):
114         s = {}
115         l = dict ([(tag, set()) for tag in TAGS])
116         for f in glob.glob (os.path.join (src, '*.ly')):
117                 basename = os.path.basename (f)
118                 m = tags_re.search (open (f, 'r').read ())
119                 if m:
120                         file_tags = [tag.strip() for tag in m.group (1). split(',')]
121                         s[basename] = (src, file_tags)
122                         [l[tag].add (basename) for tag in file_tags if tag in TAGS]
123                 else:
124                         notags_files.append (f)
125         return s, l
126
127
128 def dump_file_list (file, list):
129         f = open (file, 'w')
130         f.write ('\n'.join (list) + '\n')
131
132 ## clean out existing lys and generated files
133 map (os.remove, glob.glob (os.path.join (DEST, '*.ly')) +
134      glob.glob (os.path.join (DEST, '*.snippet-list')))
135
136 # read LSR source where tags are defined by subdirs
137 snippets, tag_lists = read_source_with_dirs (in_dir)
138 # read input/new where tags are directly
139 s, l = read_source (NEW_LYS)
140 snippets.update (s)
141 for t in TAGS:
142         tag_lists[t].update (l[t])
143
144 for (name, (srcdir, tags)) in snippets.items ():
145         copy_ly (srcdir, name, tags)
146
147 for (tag, file_set) in tag_lists.items ():
148         dump_file_list (os.path.join (DEST, tag + '.snippet-list'), file_set)
149
150 if unconverted:
151         sys.stderr.write ('These files could not be converted successfully by convert-ly:\n')
152         sys.stderr.write ('\n'.join (unconverted) + '\n\n')
153
154 if notags_files:
155         sys.stderr.write ('No tags could be found in these files:\n')
156         sys.stderr.write ('\n'.join (notags_files) + '\n\n')
157
158 dump_file_list ('lsr-unsafe.txt', unsafe)
159 sys.stderr.write ('''
160
161 Unsafe files printed in lsr-unsafe.txt: CHECK MANUALLY!
162   git add input/lsr
163   xargs git-diff HEAD < lsr-unsafe.txt
164
165 ''')
166