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
21 datadir = '@local_lilypond_datadir@'
22 if not os.path.isdir (datadir):
23 datadir = '@lilypond_datadir@'
25 sys.path.insert (0, os.path.join (datadir, 'python'))
27 if os.environ.has_key ('LILYPONDPREFIX'):
28 datadir = os.environ['LILYPONDPREFIX']
29 while datadir[-1] == os.sep:
32 datadir = os.path.join (datadir, "share/lilypond/current/")
33 sys.path.insert (0, os.path.join (datadir, 'python'))
35 # dynamic relocation, for GUB binaries.
36 bindir = os.path.split (sys.argv[0])[0]
37 for p in ['share', 'lib']:
38 datadir = os.path.abspath (bindir + '/../%s/lilypond/current/python/' % p)
39 sys.path.insert (0, datadir)
45 from convertrules import *
47 lilypond_version_re_str = '\\\\version *\"([0-9.]+)"'
48 lilypond_version_re = re.compile (lilypond_version_re_str)
52 '''Update LilyPond input to newer version. By default, update from the
53 version taken from the \\version command, to the current LilyPond version.
58 convert-ly --from=2.3.28 --to 2.5.21 foobar.ly
61 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>',
62 'Han-Wen Nienhuys <hanwen@cs.uu.nl>')
64 program_name = os.path.basename (sys.argv[0])
65 program_version = '@TOPLEVEL_VERSION@'
70 def get_option_parser ():
71 p = ly.get_option_parser (usage='lilypond-book [OPTIONS] FILE',
72 version="@TOPLEVEL_VERSION@",
73 description=help_summary)
75 p.add_option ('-f', '--from',
77 metavar=_ ("VERSION"),
79 help=_('start from VERSION [default: \\version found in file]'),
82 p.add_option ('-e', '--edit', help=_('edit in place'),
84 p.add_option ('-n', '--no-version',
85 help=_ ('do not add \\version command if missing'),
87 dest='skip_version_add',
90 p.add_option ("-s", '--show-rules',
91 help=_('print rules [default: --from=0, --to=@TOPLEVEL_VERSION@]'),
92 action='store_true', default=False)
94 p.add_option ('-t', '--to',
95 help=_('convert to VERSION [default: @TOPLEVEL_VERSION@]'),
101 p.add_option_group ('bugs',
102 description='''Report bugs via http://post.gmane.org/post.php'''
103 '''?group=gmane.comp.gnu.lilypond.bugs\n''')
109 def str_to_tuple (s):
110 return tuple (map (string.atoi, string.split (s, '.')))
113 return string.join (map (lambda x: '%s' % x, list (t)), '.')
115 def version_cmp (t1, t2):
121 def get_conversions (from_version, to_version):
122 def is_applicable (v, f = from_version, t = to_version):
123 return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
124 return filter (is_applicable, conversions)
126 def latest_version ():
127 return conversions[-1][0]
129 def show_rules (file, from_version, to_version):
130 for x in conversions:
131 if (not from_version or x[0] > from_version) \
132 and (not to_version or x[0] <= to_version):
133 file.write ('%s: %s\n' % (tup_to_str (x[0]), x[2]))
135 def do_conversion (str, from_version, to_version):
136 """Apply conversions from FROM_VERSION to TO_VERSION. Return
137 tuple (LAST,STR), with the last succesful conversion and the resulting
139 conv_list = get_conversions (from_version, to_version)
142 error_file.write (_ ("Applying conversion: "))
147 error_file.write (tup_to_str (x[0]))
148 if x != conv_list[-1]:
149 error_file.write (', ')
151 last_conversion = x[0]
153 except FatalConversionError:
154 error_file.write (_ ("error while converting"))
155 error_file.write ('\n')
156 error_file.write (_ ("Aborting"))
157 error_file.write ('\n')
161 return (last_conversion, str)
165 def guess_lilypond_version (filename):
166 s = open (filename).read ()
167 m = lilypond_version_re.search (s)
173 class FatalConversionError:
176 class UnknownVersion:
179 def do_one_file (infile_name):
180 sys.stderr.write (_ ("Processing `%s\'... ") % infile_name)
181 sys.stderr.write ('\n')
183 if global_options.edit:
184 outfile_name = infile_name + '.NEW'
185 elif global_options.outfile_name:
186 outfile_name = global_options.outfile_name
188 if global_options.from_version:
189 from_version = global_options.from_version
191 guess = guess_lilypond_version (infile_name)
193 raise UnknownVersion ()
194 from_version = str_to_tuple (guess)
196 if global_options.to_version:
197 to_version = global_options.to_version
199 to_version = latest_version ()
203 infile = open (infile_name, 'r')
208 (last, result) = do_conversion (infile.read (), from_version, to_version)
212 newversion = r'\version "%s"' % tup_to_str (last)
213 if lilypond_version_re.search (result):
214 result = re.sub (lilypond_version_re_str,
215 '\\' + newversion, result)
217 result = newversion + '\n' + result
219 error_file.write ('\n')
221 if global_options.edit:
223 os.remove(infile_name + '~')
226 os.rename (infile_name, infile_name + '~')
227 outfile = open (infile_name, 'w')
232 outfile.write (result)
237 opt_parser = get_option_parser()
238 (options, args) = opt_parser.parse_args ()
241 if options.from_version:
242 options.from_version = str_to_tuple (options.from_version)
243 if options.to_version:
244 options.to_version = str_to_tuple (options.to_version)
246 options.outfile_name = ''
247 global global_options
248 global_options = options
251 opt_parser.print_help ()
257 files = do_options ()
259 # should parse files[] to read \version?
260 if global_options.show_rules:
261 show_rules (sys.stdout, from_version, to_version)
264 ly.identify (sys.stderr)
269 elif not os.path.isfile (f):
270 ly.error (_ ("can't open file: `%s'") % f)
276 except UnknownVersion:
277 ly.error (_ ("can't determine version for `%s'. Skipping") % f)
279 sys.stderr.write ('\n')