]> git.donarmstrong.com Git - lilypond.git/blob - scripts/convert-ly.py
Add '-dcrop' option to ps and svg backends
[lilypond.git] / scripts / convert-ly.py
1 #!@TARGET_PYTHON@
2
3 # convert-ly.py -- Update old LilyPond input files (fix name?)
4 # converting rules are found in python/convertrules.py
5
6 # This file is part of LilyPond, the GNU music typesetter.
7 #
8 # Copyright (C) 1998--2015  Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #                 Jan Nieuwenhuizen <janneke@gnu.org>
10 #
11 # LilyPond is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation, either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # LilyPond is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
23
24 import os
25 import sys
26 import re
27 import shutil
28
29 """
30 @relocate-preamble@
31 """
32
33 import lilylib as ly
34 global _;_=ly._
35
36 ly.require_python_version ()
37
38 import convertrules
39
40 lilypond_version_re_str = '\\\\version *\"([0-9.]+)"'
41 lilypond_version_re = re.compile (lilypond_version_re_str)
42
43 lilypond_version_strict_re_str = '\\\\version *\"([0-9]+[.][0-9]+[.][0-9]+)"'
44 lilypond_version_strict_re = re.compile (lilypond_version_strict_re_str)
45
46 help_summary = (
47 _ ('''Update LilyPond input to newer version.  By default, update from the
48 version taken from the \\version command, to the current LilyPond version.''')
49 + "\n"
50 + _ ("If FILE is `-', read from standard input.")
51 + "\n\n"
52 + _ ("Examples:")
53 + '''
54   $ convert-ly -e old.ly
55   $ convert-ly --from=2.3.28 --to=2.5.21 foobar.ly > foobar-new.ly
56 ''')
57
58 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>',
59              'Han-Wen Nienhuys <hanwen@xs4all.nl>')
60
61 program_name = os.path.basename (sys.argv[0])
62 program_version = '@TOPLEVEL_VERSION@'
63
64 authors = ('Jan Nieuwenhuizen <janneke@gnu.org>',
65            'Han-Wen Nienhuys <hanwen@xs4all.nl>')
66
67 def identify ():
68     ly.progress ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
69
70 def warranty ():
71     identify ()
72     ly.encoded_write (sys.stdout, '''
73 %s
74
75 %s
76
77 %s
78 %s
79 ''' % ( _ ('Copyright (c) %s by') % '2001--2015',
80         ' '.join (authors),
81         _ ('Distributed under terms of the GNU General Public License.'),
82         _ ('It comes with NO WARRANTY.')))
83
84 def get_option_parser ():
85     p = ly.get_option_parser (usage=_ ("%s [OPTION]... FILE") % 'convert-ly',
86                   description=help_summary,
87                   add_help_option=False)
88
89     p.version="@TOPLEVEL_VERSION@"
90     p.add_option("--version",
91                  action="version",
92                  help=_ ("show version number and exit"))
93
94     p.add_option("-h", "--help",
95                  action="help",
96                  help=_ ("show this help and exit"))
97
98     p.add_option ('-f', '--from', 
99               action="store",
100               metavar=_ ("VERSION"),
101               dest="from_version",
102               help=_ ("start from VERSION [default: \\version found in file]"),
103               default='')
104     
105     p.add_option ('-e', '--edit', help=_ ("edit in place"),
106               action='store_true')
107
108     p.add_option ("-l", "--loglevel",
109                   help=_ ("Print log messages according to LOGLEVEL "
110                           "(NONE, ERROR, WARNING, PROGRESS (default), DEBUG)"),
111                   metavar=_ ("LOGLEVEL"),
112                   action='callback',
113                   callback=ly.handle_loglevel_option,
114                   type='string')
115
116     p.add_option ('-n', '--no-version',
117               help=_ ("do not add \\version command if missing"),
118               action='store_true',
119               dest='skip_version_add',
120               default=False)
121
122     p.add_option ('-c', '--current-version',
123               help=_ ("force updating \\version number to %s") % program_version,
124               action='store_true',
125               dest='force_current_version',
126               default=False)
127
128     p.add_option ('-d', '--diff-version-update',
129               help=_ ("only update \\version number if file is modified"),
130               action='store_true',
131               dest='diff_version_update',
132               default=False)
133
134     p.add_option ("-s", '--show-rules',
135               help=_ ("show rules [default: -f 0, -t %s]") % program_version,
136               dest='show_rules',
137               action='store_true', default=False)
138
139     p.add_option ('-t', '--to',
140               help=_ ("convert to VERSION [default: %s]") % program_version,
141               metavar=_ ('VERSION'),
142               action='store',
143               dest="to_version",
144               default='')
145
146     p.add_option ('-b', '--backup-numbered',
147               help=_ ("make a numbered backup [default: filename.ext~]"),
148               action='store_true',
149               dest="backup_numbered",
150               default='')
151
152     p.add_option ('-w', '--warranty', help=_ ("show warranty and copyright"),
153            action='store_true',
154            ),
155     p.add_option_group ('',
156                         description=(
157             _ ("Report bugs via %s")
158             % 'http://post.gmane.org/post.php'
159             '?group=gmane.comp.gnu.lilypond.bugs') + '\n')
160     
161     return p
162
163 def str_to_tuple (s):
164     return tuple ([int(n) for n in s.split ('.')])
165
166 def tup_to_str (t):
167     return '.'.join (['%s' % x for x in t])
168
169 def version_cmp (t1, t2):
170     for x in [0, 1, 2]:
171         if t1[x] - t2[x]:
172             return t1[x] - t2[x]
173     return 0
174
175 def get_conversions (from_version, to_version):
176     def is_applicable (v, f = from_version, t = to_version):
177         return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
178     return filter (is_applicable, convertrules.conversions)
179
180 def latest_version ():
181     return convertrules.conversions[-1][0]
182
183 def show_rules (file, from_version, to_version):
184     for x in convertrules.conversions:
185         if (not from_version or x[0] > from_version) \
186            and (not to_version or x[0] <= to_version):
187             ly.encoded_write  (file, '%s: %s\n' % (tup_to_str (x[0]), x[2]))
188
189 def do_conversion (str, from_version, to_version):
190     """Apply conversions from FROM_VERSION to TO_VERSION.  Return
191 tuple (LAST,LASTCHANGED,STR,ERRORS), with the last applied conversion,
192 the last conversion resulting in a change, the resulting
193 string and the number of errors."""
194     conv_list = get_conversions (from_version, to_version)
195
196     ly.progress (_ ("Applying conversion: "), newline = False)
197
198     last_conversion = None
199     last_change = None
200     errors = 0
201     try:
202         for x in conv_list:
203             if x != conv_list[-1]:
204                 ly.progress (tup_to_str (x[0]), newline = False)
205                 ly.progress (', ', newline = False)
206             else:
207                 ly.progress (tup_to_str (x[0]))
208             newstr = x[1] (str)
209             last_conversion = x[0]
210             if (newstr != str):
211                 last_change = last_conversion
212             str = newstr
213
214     except convertrules.FatalConversionError:
215         ly.error (_ ("Error while converting")
216                   + '\n'
217                   + _ ("Stopping at last successful rule"))
218         errors += 1
219
220     return (last_conversion, last_change, str, errors)
221
222 def guess_lilypond_version (input):
223     m = lilypond_version_strict_re.search (input)
224     if m:
225         return m.group (1)
226     m = lilypond_version_re.search (input)
227     if m:
228         raise InvalidVersion (m.group (1))
229     else:
230         return ''
231
232 class FatalConversionError (Exception):
233     pass
234
235 class UnknownVersion (Exception):
236     pass
237
238 class InvalidVersion (Exception):
239     def __init__ (self, version):
240       self.version = version
241
242 def back_up (file, numbered):
243     if numbered:
244       n = 0
245       while True:
246         n = n + 1
247         back_up = file + '.~' + str(n) + '~'
248         if not os.path.exists (back_up):
249             break
250     else:
251       back_up = file + '~'
252     shutil.copy2 (file, back_up)
253     return back_up
254
255 def do_one_file (infile_name):
256     ly.progress (_ (u"Processing `%s\'... ") % infile_name, True)
257
258     if infile_name:
259         infile = open (infile_name, 'r')
260         input = infile.read ()
261         infile.close ()
262     else:
263         input = sys.stdin.read ()
264
265     to_version = None
266     org_version = None
267     guess = guess_lilypond_version (input)
268     org_version = guess and str_to_tuple (guess)
269     from_version = global_options.from_version or org_version
270     if not from_version:
271         raise UnknownVersion ()
272
273     if global_options.to_version:
274         to_version = global_options.to_version
275     else:
276         to_version = latest_version ()
277
278     if len (from_version) != 3:
279         raise InvalidVersion (".".join ([str(n) for n in from_version]))
280
281
282     (last, last_change, result, errors) = \
283         do_conversion (input, from_version, to_version)
284
285     if global_options.force_current_version and \
286             (last is None or last == to_version):
287         last = str_to_tuple (program_version)
288     if last:
289         if global_options.diff_version_update:
290             # Note that last_change can be set even if the result is
291             # the same if two conversion rules cancelled out
292             if result == input:
293                 # make no (actual) change to the version number
294                 last = org_version or from_version
295             else:
296                 last = last_change
297                 # If the last update was to an unstable version
298                 # number, and the final update target is no longer in
299                 # the same unstable series, we update to the stable
300                 # series following the unstable version.
301                 if last[1]%2: # unstable
302                     next_stable = (last[0], last[1]+1, 0)
303                     if next_stable <= to_version:
304                         last = next_stable
305
306         newversion = r'\version "%s"' % tup_to_str (last)
307         if lilypond_version_re.search (result):
308             result = re.sub (lilypond_version_re_str,
309                      '\\' + newversion, result)
310         elif not global_options.skip_version_add:
311             result = newversion + '\n' + result
312
313     ly.progress ('\n')
314
315     if global_options.edit:
316         backup = back_up (infile_name, global_options.backup_numbered)
317         outfile = open (infile_name, 'w')
318     else:
319         outfile = sys.stdout
320
321     outfile.write (result)
322     
323     sys.stderr.flush ()
324
325     return errors
326
327 def do_options ():
328     opt_parser = get_option_parser()
329     (options, args) = opt_parser.parse_args ()
330
331     if options.warranty:
332         warranty ()
333         sys.exit (0)
334
335     if options.from_version:
336         options.from_version = str_to_tuple (options.from_version)
337     if options.to_version:
338         options.to_version = str_to_tuple (options.to_version)
339
340     options.outfile_name = ''
341     global global_options
342     global_options = options
343
344     if not args and not options.show_rules:
345         opt_parser.print_help ()
346         sys.exit (2)
347
348     return args
349
350 def main ():
351     files = do_options ()
352
353     # should parse files[] to read \version?
354     if global_options.show_rules:
355         show_rules (sys.stdout, global_options.from_version, global_options.to_version)
356         sys.exit (0)
357
358     identify ()
359
360     errors = 0
361     for f in files:
362         f = f.decode (sys.stdin.encoding or "utf-8")
363         if f == '-':
364             f = ''
365         elif not os.path.isfile (f):
366             ly.error (_ (u"%s: Unable to open file") % f)
367             errors += 1
368             continue
369         try:
370             errors += do_one_file (f)
371         except UnknownVersion:
372             ly.error (_ (u"%s: Unable to determine version.  Skipping") % f)
373             errors += 1
374         except InvalidVersion:
375             # Compat code for 2.x and 3.0 syntax ("except .. as v" doesn't 
376             # work in python 2.4!):
377             t, v, b = sys.exc_info ()
378             ly.error (_ (u"%s: Invalid version string `%s' \n"
379                          "Valid version strings consist of three numbers, "
380                          "separated by dots, e.g. `2.8.12'") % (f, v.version) )
381             errors += 1
382
383     if errors:
384         ly.warning (ly.ungettext ("There was %d error.",
385             "There were %d errors.", errors) % errors)
386         sys.exit (1)
387
388 main ()