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 # dynamic relocation, for GUB binaries.
26 bindir = os.path.abspath (os.path.split (sys.argv[0])[0])
27 for p in ['share', 'lib']:
28 datadir = os.path.abspath (bindir + '/../%s/lilypond/current/python/' % p)
29 sys.path.insert (0, datadir)
36 lilypond_version_re_str = '\\\\version *\"([0-9.]+)"'
37 lilypond_version_re = re.compile (lilypond_version_re_str)
41 '''Update LilyPond input to newer version. By default, update from the
42 version taken from the \\version command, to the current LilyPond version.
47 convert-ly --from=2.3.28 --to 2.5.21 foobar.ly
50 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>',
51 'Han-Wen Nienhuys <hanwen@cs.uu.nl>')
53 program_name = os.path.basename (sys.argv[0])
54 program_version = '@TOPLEVEL_VERSION@'
57 sys.stderr.write (program_name + ": " + _ ("warning: %s") % s + '\n')
60 sys.stderr.write (program_name + ": " + _ ("error: %s") % s + '\n')
62 def identify (port=sys.stderr):
63 port.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
76 _('Distributed under terms of the GNU General Public License.'),
77 _('It comes with NO WARRANTY.')))
81 def get_option_parser ():
82 p = ly.get_option_parser (usage='convert-ly [OPTIONS] FILE',
83 version="@TOPLEVEL_VERSION@",
84 description=help_summary)
86 p.add_option ('-f', '--from',
88 metavar=_ ("VERSION"),
90 help=_('start from VERSION [default: \\version found in file]'),
93 p.add_option ('-e', '--edit', help=_('edit in place'),
95 p.add_option ('-n', '--no-version',
96 help=_ ('do not add \\version command if missing'),
98 dest='skip_version_add',
101 p.add_option ("-s", '--show-rules',
102 help=_('print rules [default: --from=0, --to=@TOPLEVEL_VERSION@]'),
104 action='store_true', default=False)
106 p.add_option ('-t', '--to',
107 help=_('convert to VERSION [default: @TOPLEVEL_VERSION@]'),
108 metavar=_('VERSION'),
113 p.add_option_group ('bugs',
114 description='''Report bugs via http://post.gmane.org/post.php'''
115 '''?group=gmane.comp.gnu.lilypond.bugs\n''')
121 def str_to_tuple (s):
122 return tuple (map (int, string.split (s, '.')))
125 return string.join (map (lambda x: '%s' % x, list (t)), '.')
127 def version_cmp (t1, t2):
133 def get_conversions (from_version, to_version):
134 def is_applicable (v, f = from_version, t = to_version):
135 return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
136 return filter (is_applicable, convertrules.conversions)
138 def latest_version ():
139 return convertrules.conversions[-1][0]
141 def show_rules (file, from_version, to_version):
142 for x in convertrules.conversions:
143 if (not from_version or x[0] > from_version) \
144 and (not to_version or x[0] <= to_version):
145 file.write ('%s: %s\n' % (tup_to_str (x[0]), x[2]))
147 def do_conversion (str, from_version, to_version):
148 """Apply conversions from FROM_VERSION to TO_VERSION. Return
149 tuple (LAST,STR), with the last succesful conversion and the resulting
151 conv_list = get_conversions (from_version, to_version)
153 if convertrules.error_file:
154 convertrules.error_file.write (_ ("Applying conversion: "))
159 convertrules.error_file.write (tup_to_str (x[0]))
160 if x != conv_list[-1]:
161 convertrules.error_file.write (', ')
163 last_conversion = x[0]
165 except convertrules.FatalConversionError:
166 convertrules.error_file.write ('\n'
167 + _ ("Error while converting")
169 + _ ("Stopping at last succesful rule")
172 return (last_conversion, str)
176 def guess_lilypond_version (filename):
177 s = open (filename).read ()
178 m = lilypond_version_re.search (s)
184 class FatalConversionError:
187 class UnknownVersion:
190 def do_one_file (infile_name):
191 sys.stderr.write (_ ("Processing `%s\'... ") % infile_name)
192 sys.stderr.write ('\n')
196 if global_options.from_version:
197 from_version = global_options.from_version
199 guess = guess_lilypond_version (infile_name)
201 raise UnknownVersion ()
202 from_version = str_to_tuple (guess)
204 if global_options.to_version:
205 to_version = global_options.to_version
207 to_version = latest_version ()
211 infile = open (infile_name, 'r')
216 (last, result) = do_conversion (infile.read (), from_version, to_version)
220 newversion = r'\version "%s"' % tup_to_str (last)
221 if lilypond_version_re.search (result):
222 result = re.sub (lilypond_version_re_str,
223 '\\' + newversion, result)
224 elif not global_options.skip_version_add:
225 result = newversion + '\n' + result
227 convertrules.error_file.write ('\n')
229 if global_options.edit:
231 os.remove(infile_name + '~')
234 os.rename (infile_name, infile_name + '~')
235 outfile = open (infile_name, 'w')
240 outfile.write (result)
245 opt_parser = get_option_parser()
246 (options, args) = opt_parser.parse_args ()
249 if options.from_version:
250 options.from_version = str_to_tuple (options.from_version)
251 if options.to_version:
252 options.to_version = str_to_tuple (options.to_version)
254 options.outfile_name = ''
255 global global_options
256 global_options = options
258 if not args and not options.show_rules:
259 opt_parser.print_help ()
265 files = do_options ()
267 # should parse files[] to read \version?
268 if global_options.show_rules:
269 show_rules (sys.stdout, global_options.from_version, global_options.to_version)
272 identify (sys.stderr)
277 elif not os.path.isfile (f):
278 error (_ ("can't open file: `%s'") % f)
284 except UnknownVersion:
285 error (_ ("can't determine version for `%s'. Skipping") % f)
287 sys.stderr.write ('\n')