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@xs4all.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.''')
46 convert-ly --from=2.3.28 --to 2.5.21 foobar.ly
49 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>',
50 'Han-Wen Nienhuys <hanwen@xs4all.nl>')
52 program_name = os.path.basename (sys.argv[0])
53 program_version = '@TOPLEVEL_VERSION@'
56 sys.stderr.write (program_name + ": " + _ ("warning: %s") % s + '\n')
59 sys.stderr.write (program_name + ": " + _ ("error: %s") % s + '\n')
61 def identify (port=sys.stderr):
62 port.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
75 _ ("Distributed under terms of the GNU General Public License."),
76 _ ('It comes with NO WARRANTY.')))
79 def get_option_parser ():
80 p = ly.get_option_parser (usage=_ ("%s [OPTION]... FILE") % 'convert-ly',
81 version="@TOPLEVEL_VERSION@",
82 description=help_summary)
84 p.add_option ('-f', '--from',
86 metavar=_ ("VERSION"),
88 help=_ ("start from VERSION [default: \\version found in file]"),
91 p.add_option ('-e', '--edit', help=_ ("edit in place"),
93 p.add_option ('-n', '--no-version',
94 help=_ ("do not add \\version command if missing"),
96 dest='skip_version_add',
99 p.add_option ("-s", '--show-rules',
100 help=_ ("show rules [default: --from=0, --to=@TOPLEVEL_VERSION@]"),
102 action='store_true', default=False)
104 p.add_option ('-t', '--to',
105 help=_ ("convert to VERSION [default: @TOPLEVEL_VERSION@]"),
106 metavar=_ ('VERSION'),
111 p.add_option_group ('bugs',
112 description=(_ ("Report bugs via")
113 + ''' http://post.gmane.org/post.php'''
114 '''?group=gmane.comp.gnu.lilypond.bugs\n'''))
120 def str_to_tuple (s):
121 return tuple (map (int, string.split (s, '.')))
124 return string.join (map (lambda x: '%s' % x, list (t)), '.')
126 def version_cmp (t1, t2):
132 def get_conversions (from_version, to_version):
133 def is_applicable (v, f = from_version, t = to_version):
134 return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
135 return filter (is_applicable, convertrules.conversions)
137 def latest_version ():
138 return convertrules.conversions[-1][0]
140 def show_rules (file, from_version, to_version):
141 for x in convertrules.conversions:
142 if (not from_version or x[0] > from_version) \
143 and (not to_version or x[0] <= to_version):
144 file.write ('%s: %s\n' % (tup_to_str (x[0]), x[2]))
146 def do_conversion (str, from_version, to_version):
147 """Apply conversions from FROM_VERSION to TO_VERSION. Return
148 tuple (LAST,STR), with the last succesful conversion and the resulting
150 conv_list = get_conversions (from_version, to_version)
152 if convertrules.error_file:
153 convertrules.error_file.write (_ ("Applying conversion: "))
158 convertrules.error_file.write (tup_to_str (x[0]))
159 if x != conv_list[-1]:
160 convertrules.error_file.write (', ')
162 last_conversion = x[0]
164 except convertrules.FatalConversionError:
165 convertrules.error_file.write ('\n'
166 + _ ("Error while converting")
168 + _ ("Stopping at last succesful rule")
171 return (last_conversion, str)
175 def guess_lilypond_version (filename):
176 s = open (filename).read ()
177 m = lilypond_version_re.search (s)
183 class FatalConversionError:
186 class UnknownVersion:
189 def do_one_file (infile_name):
190 sys.stderr.write (_ ("Processing `%s\'... ") % infile_name)
191 sys.stderr.write ('\n')
195 if global_options.from_version:
196 from_version = global_options.from_version
198 guess = guess_lilypond_version (infile_name)
200 raise UnknownVersion ()
201 from_version = str_to_tuple (guess)
203 if global_options.to_version:
204 to_version = global_options.to_version
206 to_version = latest_version ()
210 infile = open (infile_name, 'r')
215 (last, result) = do_conversion (infile.read (), from_version, to_version)
219 newversion = r'\version "%s"' % tup_to_str (last)
220 if lilypond_version_re.search (result):
221 result = re.sub (lilypond_version_re_str,
222 '\\' + newversion, result)
223 elif not global_options.skip_version_add:
224 result = newversion + '\n' + result
226 convertrules.error_file.write ('\n')
228 if global_options.edit:
230 os.remove(infile_name + '~')
233 os.rename (infile_name, infile_name + '~')
234 outfile = open (infile_name, 'w')
239 outfile.write (result)
244 opt_parser = get_option_parser()
245 (options, args) = opt_parser.parse_args ()
248 if options.from_version:
249 options.from_version = str_to_tuple (options.from_version)
250 if options.to_version:
251 options.to_version = str_to_tuple (options.to_version)
253 options.outfile_name = ''
254 global global_options
255 global_options = options
257 if not args and not options.show_rules:
258 opt_parser.print_help ()
264 files = do_options ()
266 # should parse files[] to read \version?
267 if global_options.show_rules:
268 show_rules (sys.stdout, global_options.from_version, global_options.to_version)
271 identify (sys.stderr)
276 elif not os.path.isfile (f):
277 error (_ ("cannot open file: `%s'") % f)
283 except UnknownVersion:
284 error (_ ("cannot determine version for `%s'. Skipping") % f)
286 sys.stderr.write ('\n')