]> git.donarmstrong.com Git - lilypond.git/blob - scripts/auxiliar/makelsr.py
Reformat Python code of makelsr
[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,
115                            glob.glob (os.path.join (srcdir, '*.ly'))))
116         for f in l[tag]:
117             if f in s:
118                 s[f][1].append (tag)
119             else:
120                 s[f] = (srcdir, [tag])
121     return s, l
122
123
124 tags_re = re.compile ('lsrtags\\s*=\\s*"(.+?)"')
125
126 def read_source (src):
127     s = {}
128     l = dict ([(tag, set()) for tag in TAGS])
129     for f in glob.glob (os.path.join (src, '*.ly')):
130         basename = os.path.basename (f)
131         m = tags_re.search (open (f, 'r').read ())
132         if m:
133             file_tags = [tag.strip() for tag in m.group (1). split(',')]
134             s[basename] = (src, file_tags)
135             [l[tag].add (basename) for tag in file_tags if tag in TAGS]
136         else:
137             notags_files.append (f)
138     return s, l
139
140
141 def dump_file_list (file, file_list, update=False):
142     if update:
143         old_list = set (open (file, 'r').read ().splitlines ())
144         old_list.update (file_list)
145         new_list = list (old_list)
146     else:
147         new_list = file_list
148     f = open (file, 'w')
149     f.write ('\n'.join (sorted (new_list)) + '\n')
150
151 if in_dir:
152     ## clean out existing lys and generated files
153     map (os.remove, glob.glob (os.path.join (DEST, '*.ly')) +
154          glob.glob (os.path.join (DEST, '*.snippet-list')))
155
156     # read LSR source where tags are defined by subdirs
157     snippets, tag_lists = read_source_with_dirs (in_dir)
158
159     # read input/new where tags are directly defined
160     s, l = read_source (NEW_LYS)
161     snippets.update (s)
162     for t in TAGS:
163         tag_lists[t].update (l[t])
164 else:
165     snippets, tag_lists = read_source (NEW_LYS)
166
167 for (name, (srcdir, tags)) in snippets.items ():
168     copy_ly (srcdir, name, tags)
169 for (tag, file_set) in tag_lists.items ():
170     dump_file_list (os.path.join (DEST, tag + '.snippet-list'),
171                     file_set, update=not(in_dir))
172 if unconverted:
173     sys.stderr.write ('These files could not be converted successfully by convert-ly:\n')
174     sys.stderr.write ('\n'.join (unconverted) + '\n\n')
175 if notags_files:
176     sys.stderr.write ('No tags could be found in these files:\n')
177     sys.stderr.write ('\n'.join (notags_files) + '\n\n')
178
179 dump_file_list ('lsr-unsafe.txt', unsafe)
180 sys.stderr.write ('''
181
182 Unsafe files printed in lsr-unsafe.txt: CHECK MANUALLY!
183   git add input/lsr/*.ly
184   xargs git diff HEAD < lsr-unsafe.txt
185
186 ''')
187