3 # convert-ly.py -- Update old LilyPond input files (fix name?)
5 # source file of the GNU LilyPond music typesetter
7 # (c) 1998--2006 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 # Jan Nieuwenhuizen <janneke@gnu.org>
10 # converting rules are found in python/convertrules.py
19 datadir = '@local_lilypond_datadir@'
20 if not os.path.isdir (datadir):
21 datadir = '@lilypond_datadir@'
23 sys.path.insert (0, os.path.join (datadir, 'python'))
25 if os.environ.has_key ('LILYPONDPREFIX'):
26 datadir = os.environ['LILYPONDPREFIX']
27 while datadir[-1] == os.sep:
30 datadir = os.path.join (datadir, "share/lilypond/current/")
31 sys.path.insert (0, os.path.join (datadir, 'python'))
33 # dynamic relocation, for GUB binaries.
34 bindir = os.path.split (sys.argv[0])[0]
35 for p in ['share', 'lib']:
36 datadir = os.path.abspath (bindir + '/../%s/lilypond/current/python/' % p)
37 sys.path.insert (0, datadir)
42 from convertrules import *
44 lilypond_version_re_str = '\\\\version *\"([0-9.]+)"'
45 lilypond_version_re = re.compile (lilypond_version_re_str)
49 '''Update LilyPond input to newer version. By default, update from the
50 version taken from the \\version command, to the current LilyPond version.
55 convert-ly --from=2.3.28 --to 2.5.21 foobar.ly
58 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>',
59 'Han-Wen Nienhuys <hanwen@cs.uu.nl>')
61 program_name = os.path.basename (sys.argv[0])
62 program_version = '@TOPLEVEL_VERSION@'
67 sys.stderr.write (program_name + ": " + _ ("warning: %s") % s + '\n')
70 sys.stderr.write (program_name + ": " + _ ("error: %s") % s + '\n')
72 def identify (port=sys.stderr):
73 port.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
86 _('Distributed under terms of the GNU General Public License.'),
87 _('It comes with NO WARRANTY.')))
91 def get_option_parser ():
92 p = ly.get_option_parser (usage='lilypond-book [OPTIONS] FILE',
93 version="@TOPLEVEL_VERSION@",
94 description=help_summary)
96 p.add_option ('-f', '--from',
98 metavar=_ ("VERSION"),
100 help=_('start from VERSION [default: \\version found in file]'),
103 p.add_option ('-e', '--edit', help=_('edit in place'),
105 p.add_option ('-n', '--no-version',
106 help=_ ('do not add \\version command if missing'),
108 dest='skip_version_add',
111 p.add_option ("-s", '--show-rules',
112 help=_('print rules [default: --from=0, --to=@TOPLEVEL_VERSION@]'),
113 action='store_true', default=False)
115 p.add_option ('-t', '--to',
116 help=_('convert to VERSION [default: @TOPLEVEL_VERSION@]'),
117 metavar=_('VERSION'),
122 p.add_option_group ('bugs',
123 description='''Report bugs via http://post.gmane.org/post.php'''
124 '''?group=gmane.comp.gnu.lilypond.bugs\n''')
130 def str_to_tuple (s):
131 return tuple (map (int, string.split (s, '.')))
134 return string.join (map (lambda x: '%s' % x, list (t)), '.')
136 def version_cmp (t1, t2):
142 def get_conversions (from_version, to_version):
143 def is_applicable (v, f = from_version, t = to_version):
144 return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
145 return filter (is_applicable, conversions)
147 def latest_version ():
148 return conversions[-1][0]
150 def show_rules (file, from_version, to_version):
151 for x in conversions:
152 if (not from_version or x[0] > from_version) \
153 and (not to_version or x[0] <= to_version):
154 file.write ('%s: %s\n' % (tup_to_str (x[0]), x[2]))
156 def do_conversion (str, from_version, to_version):
157 """Apply conversions from FROM_VERSION to TO_VERSION. Return
158 tuple (LAST,STR), with the last succesful conversion and the resulting
160 conv_list = get_conversions (from_version, to_version)
163 error_file.write (_ ("Applying conversion: "))
168 error_file.write (tup_to_str (x[0]))
169 if x != conv_list[-1]:
170 error_file.write (', ')
172 last_conversion = x[0]
174 except FatalConversionError:
175 error_file.write (_ ("error while converting"))
176 error_file.write ('\n')
177 error_file.write (_ ("Aborting"))
178 error_file.write ('\n')
182 return (last_conversion, str)
186 def guess_lilypond_version (filename):
187 s = open (filename).read ()
188 m = lilypond_version_re.search (s)
194 class FatalConversionError:
197 class UnknownVersion:
200 def do_one_file (infile_name):
201 sys.stderr.write (_ ("Processing `%s\'... ") % infile_name)
202 sys.stderr.write ('\n')
206 if global_options.from_version:
207 from_version = global_options.from_version
209 guess = guess_lilypond_version (infile_name)
211 raise UnknownVersion ()
212 from_version = str_to_tuple (guess)
214 if global_options.to_version:
215 to_version = global_options.to_version
217 to_version = latest_version ()
221 infile = open (infile_name, 'r')
226 (last, result) = do_conversion (infile.read (), from_version, to_version)
230 newversion = r'\version "%s"' % tup_to_str (last)
231 if lilypond_version_re.search (result):
232 result = re.sub (lilypond_version_re_str,
233 '\\' + newversion, result)
235 result = newversion + '\n' + result
237 error_file.write ('\n')
239 if global_options.edit:
241 os.remove(infile_name + '~')
244 os.rename (infile_name, infile_name + '~')
245 outfile = open (infile_name, 'w')
250 outfile.write (result)
255 opt_parser = get_option_parser()
256 (options, args) = opt_parser.parse_args ()
259 if options.from_version:
260 options.from_version = str_to_tuple (options.from_version)
261 if options.to_version:
262 options.to_version = str_to_tuple (options.to_version)
264 options.outfile_name = ''
265 global global_options
266 global_options = options
269 opt_parser.print_help ()
275 files = do_options ()
277 # should parse files[] to read \version?
278 if global_options.show_rules:
279 show_rules (sys.stdout, global_options.from_version, global_options.to_version)
282 identify (sys.stderr)
287 elif not os.path.isfile (f):
288 error (_ ("can't open file: `%s'") % f)
294 except UnknownVersion:
295 error (_ ("can't determine version for `%s'. Skipping") % f)
297 sys.stderr.write ('\n')