Make convert-ly rule for \bigger -> \larger also recognize :bigger
[lilypond.git] / scripts / convert-ly.py
blob93e345f2ae6edf305b7a40481c6de828c978b1ff
1 #!@TARGET_PYTHON@
3 # convert-ly.py -- Update old LilyPond input files (fix name?)
5 # source file of the GNU LilyPond music typesetter
7 # (c) 1998--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
8 # Jan Nieuwenhuizen <janneke@gnu.org>
10 # converting rules are found in python/convertrules.py
13 import os
14 import sys
15 import re
17 """
18 @relocate-preamble@
19 """
21 import lilylib as ly
22 global _;_=ly._
24 ly.require_python_version ()
26 import convertrules
28 lilypond_version_re_str = '\\\\version *\"([0-9.]+)"'
29 lilypond_version_re = re.compile (lilypond_version_re_str)
32 help_summary = (
33 _ ('''Update LilyPond input to newer version. By default, update from the
34 version taken from the \\version command, to the current LilyPond version.''')
35 + _ ("Examples:")
36 + '''
37 $ convert-ly -e old.ly
38 $ convert-ly --from=2.3.28 --to=2.5.21 foobar.ly > foobar-new.ly
39 ''')
41 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>',
42 'Han-Wen Nienhuys <hanwen@xs4all.nl>')
44 program_name = os.path.basename (sys.argv[0])
45 program_version = '@TOPLEVEL_VERSION@'
47 error_file_write = ly.stderr_write
49 def warning (s):
50 ly.stderr_write (program_name + ": " + _ ("warning: %s") % s + '\n')
52 def error (s):
53 ly.stderr_write (program_name + ": " + _ ("error: %s") % s + '\n')
55 def identify (port=sys.stderr):
56 ly.encoded_write (port, '%s (GNU LilyPond) %s\n' % (program_name, program_version))
58 def warranty ():
59 identify ()
60 ly.encoded_write (sys.stdout, '''
61 Copyright (c) %s by
63 Han-Wen Nienhuys
64 Jan Nieuwenhuizen
68 ''' ( '2001--2006',
69 _ ("Distributed under terms of the GNU General Public License."),
70 _ ('It comes with NO WARRANTY.')))
73 def get_option_parser ():
74 p = ly.get_option_parser (usage=_ ("%s [OPTION]... FILE") % 'convert-ly',
75 description=help_summary,
76 add_help_option=False)
78 p.version="@TOPLEVEL_VERSION@"
79 p.add_option("--version",
80 action="version",
81 help=_ ("show version number and exit"))
83 p.add_option("-h", "--help",
84 action="help",
85 help=_ ("show this help and exit"))
87 p.add_option ('-f', '--from',
88 action="store",
89 metavar=_ ("VERSION"),
90 dest="from_version",
91 help=_ ("start from VERSION [default: \\version found in file]"),
92 default='')
94 p.add_option ('-e', '--edit', help=_ ("edit in place"),
95 action='store_true')
97 p.add_option ('-n', '--no-version',
98 help=_ ("do not add \\version command if missing"),
99 action='store_true',
100 dest='skip_version_add',
101 default=False)
103 p.add_option ('-c', '--current-version',
104 help=_ ("force updating \\version number to %s") % program_version,
105 action='store_true',
106 dest='force_current_version',
107 default=False)
109 p.add_option ("-s", '--show-rules',
110 help=_ ("show rules [default: -f 0, -t %s]") % program_version,
111 dest='show_rules',
112 action='store_true', default=False)
114 p.add_option ('-t', '--to',
115 help=_ ("convert to VERSION [default: %s]") % program_version,
116 metavar=_ ('VERSION'),
117 action='store',
118 dest="to_version",
119 default='')
121 p.add_option_group ('',
122 description=(
123 _ ("Report bugs via %s")
124 % 'http://post.gmane.org/post.php'
125 '?group=gmane.comp.gnu.lilypond.bugs') + '\n')
127 return p
131 def str_to_tuple (s):
132 return tuple ([int(n) for n in s.split ('.')])
134 def tup_to_str (t):
135 return '.'.join (['%s' % x for x in t])
137 def version_cmp (t1, t2):
138 for x in [0, 1, 2]:
139 if t1[x] - t2[x]:
140 return t1[x] - t2[x]
141 return 0
143 def get_conversions (from_version, to_version):
144 def is_applicable (v, f = from_version, t = to_version):
145 return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
146 return filter (is_applicable, convertrules.conversions)
148 def latest_version ():
149 return convertrules.conversions[-1][0]
151 def show_rules (file, from_version, to_version):
152 for x in convertrules.conversions:
153 if (not from_version or x[0] > from_version) \
154 and (not to_version or x[0] <= to_version):
155 ly.encoded_write (file, '%s: %s\n' % (tup_to_str (x[0]), x[2]))
157 def do_conversion (str, from_version, to_version):
158 """Apply conversions from FROM_VERSION to TO_VERSION. Return
159 tuple (LAST,STR), with the last succesful conversion and the resulting
160 string."""
161 conv_list = get_conversions (from_version, to_version)
163 error_file_write (_ ("Applying conversion: "))
165 last_conversion = ()
166 try:
167 for x in conv_list:
168 error_file_write (tup_to_str (x[0]))
169 if x != conv_list[-1]:
170 error_file_write (', ')
171 str = x[1] (str)
172 last_conversion = x[0]
174 except convertrules.FatalConversionError:
175 error_file_write ('\n'
176 + _ ("Error while converting")
177 + '\n'
178 + _ ("Stopping at last successful rule")
179 + '\n')
181 return (last_conversion, str)
185 def guess_lilypond_version (input):
186 m = lilypond_version_re.search (input)
187 if m:
188 return m.group (1)
189 else:
190 return ''
192 class FatalConversionError:
193 pass
195 class UnknownVersion:
196 pass
198 def do_one_file (infile_name):
199 ly.stderr_write (_ ("Processing `%s\'... ") % infile_name)
200 sys.stderr.write ('\n')
202 if infile_name:
203 infile = open (infile_name, 'r')
204 input = infile.read ()
205 infile.close ()
206 else:
207 input = sys.stdin.read ()
209 from_version = None
210 to_version = None
211 if global_options.from_version:
212 from_version = global_options.from_version
213 else:
214 guess = guess_lilypond_version (input)
215 if not guess:
216 raise UnknownVersion ()
217 from_version = str_to_tuple (guess)
219 if global_options.to_version:
220 to_version = global_options.to_version
221 else:
222 to_version = latest_version ()
225 (last, result) = do_conversion (input, from_version, to_version)
227 if last:
228 if global_options.force_current_version and last == to_version:
229 last = str_to_tuple (program_version)
231 newversion = r'\version "%s"' % tup_to_str (last)
232 if lilypond_version_re.search (result):
233 result = re.sub (lilypond_version_re_str,
234 '\\' + newversion, result)
235 elif not global_options.skip_version_add:
236 result = newversion + '\n' + result
238 error_file_write ('\n')
240 if global_options.edit:
241 try:
242 os.remove(infile_name + '~')
243 except:
244 pass
245 os.rename (infile_name, infile_name + '~')
246 outfile = open (infile_name, 'w')
247 else:
248 outfile = sys.stdout
251 outfile.write (result)
253 sys.stderr.flush ()
255 def do_options ():
256 opt_parser = get_option_parser()
257 (options, args) = opt_parser.parse_args ()
260 if options.from_version:
261 options.from_version = str_to_tuple (options.from_version)
262 if options.to_version:
263 options.to_version = str_to_tuple (options.to_version)
265 options.outfile_name = ''
266 global global_options
267 global_options = options
269 if not args and not options.show_rules:
270 opt_parser.print_help ()
271 sys.exit (2)
273 return args
275 def main ():
276 files = do_options ()
278 # should parse files[] to read \version?
279 if global_options.show_rules:
280 show_rules (sys.stdout, global_options.from_version, global_options.to_version)
281 sys.exit (0)
283 identify (sys.stderr)
285 for f in files:
286 if f == '-':
287 f = ''
288 elif not os.path.isfile (f):
289 error (_ ("cannot open file: `%s'") % f)
290 if len (files) == 1:
291 sys.exit (1)
292 continue
293 try:
294 do_one_file (f)
295 except UnknownVersion:
296 error (_ ("cannot determine version for `%s'. Skipping") % f)
298 sys.stderr.write ('\n')
300 main ()