]> git.donarmstrong.com Git - lilypond.git/blob - scripts/convert-ly.py
1f0ef120f1a1250dc9ce8baf87c2ca3bddadf762
[lilypond.git] / scripts / convert-ly.py
1 #!@TARGET_PYTHON@
2 #
3 # convert-ly.py -- Update old LilyPond input files (fix name?)
4 #
5 # source file of the GNU LilyPond music typesetter
6 #
7 # (c) 1998--2006  Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 #                 Jan Nieuwenhuizen <janneke@gnu.org>
9 #
10 # converting rules are found in python/convertrules.py
11 #
12
13 import os
14 import sys
15 import string
16 import re
17
18
19 datadir = '@local_lilypond_datadir@'
20 if not os.path.isdir (datadir):
21     datadir = '@lilypond_datadir@'
22
23 sys.path.insert (0, os.path.join (datadir, 'python'))
24
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)
30
31 import lilylib as ly
32 global _;_=ly._
33
34 from convertrules import *
35
36 lilypond_version_re_str = '\\\\version *\"([0-9.]+)"'
37 lilypond_version_re = re.compile (lilypond_version_re_str)
38
39
40 help_summary = _ (
41 '''Update LilyPond input to newer version.  By default, update from the
42 version taken from the \\version command, to the current LilyPond version.
43
44 Examples:
45
46   convert-ly -e old.ly
47   convert-ly --from=2.3.28 --to 2.5.21 foobar.ly
48 ''')
49
50 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>',
51          'Han-Wen Nienhuys <hanwen@cs.uu.nl>')
52
53 program_name = os.path.basename (sys.argv[0])
54 program_version = '@TOPLEVEL_VERSION@'
55
56 def warning (s):
57     sys.stderr.write (program_name + ": " + _ ("warning: %s") % s + '\n')
58
59 def error (s):
60     sys.stderr.write (program_name + ": " + _ ("error: %s") % s + '\n')
61
62 def identify (port=sys.stderr):
63     port.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
64
65 def warranty ():
66     identify ()
67     sys.stdout.write ('''
68 Copyright (c) %s by
69
70   Han-Wen Nienhuys
71   Jan Nieuwenhuizen
72
73 %s
74 %s
75 '''  ( '2001--2006',
76        _('Distributed under terms of the GNU General Public License.'),
77        _('It comes with NO WARRANTY.')))
78
79
80
81 def get_option_parser ():
82     p = ly.get_option_parser (usage='convert-ly [OPTIONS] FILE',
83                   version="@TOPLEVEL_VERSION@",
84                   description=help_summary)
85
86     p.add_option ('-f', '--from', 
87               action="store",
88               metavar=_ ("VERSION"),
89               dest="from_version",
90               help=_('start from VERSION [default: \\version found in file]'),
91               default='')
92     
93     p.add_option ('-e', '--edit', help=_('edit in place'),
94               action='store_true')
95     p.add_option ('-n', '--no-version',
96               help=_ ('do not add \\version command if missing'),
97               action='store_true',
98               dest='skip_version_add',
99               default=False)
100     
101     p.add_option ("-s", '--show-rules',
102               help=_('print rules [default: --from=0, --to=@TOPLEVEL_VERSION@]'),
103               dest='show_rules',
104               action='store_true', default=False)
105     
106     p.add_option ('-t', '--to',
107               help=_('convert to VERSION [default: @TOPLEVEL_VERSION@]'),
108               metavar=_('VERSION'),
109               action='store',
110               dest="to_version",
111               default='')
112
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''')
116     
117     return p
118
119
120
121 def str_to_tuple (s):
122     return tuple (map (int, string.split (s, '.')))
123
124 def tup_to_str (t):
125     return string.join (map (lambda x: '%s' % x, list (t)), '.')
126
127 def version_cmp (t1, t2):
128     for x in [0, 1, 2]:
129         if t1[x] - t2[x]:
130             return t1[x] - t2[x]
131     return 0
132
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, conversions)
137
138 def latest_version ():
139     return conversions[-1][0]
140
141 def show_rules (file, from_version, to_version):
142     for x in 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]))
146
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
150 string."""
151     conv_list = get_conversions (from_version, to_version)
152
153     if error_file:
154         error_file.write (_ ("Applying conversion: "))
155         
156     last_conversion = ()
157     try:
158         for x in conv_list:
159             error_file.write (tup_to_str (x[0]))
160             if x != conv_list[-1]:
161                 error_file.write (', ')
162             str = x[1] (str)
163             last_conversion = x[0]
164
165     except FatalConversionError:
166         error_file.write (_ ("error while converting")) 
167         error_file.write ('\n')
168         error_file.write (_ ("Aborting"))
169         error_file.write ('\n')
170
171
172
173     return (last_conversion, str)
174
175
176
177 def guess_lilypond_version (filename):
178     s = open (filename).read ()
179     m = lilypond_version_re.search (s)
180     if m:
181         return m.group (1)
182     else:
183         return ''
184
185 class FatalConversionError:
186     pass
187
188 class UnknownVersion:
189     pass
190
191 def do_one_file (infile_name):
192     sys.stderr.write (_ ("Processing `%s\'... ") % infile_name)
193     sys.stderr.write ('\n')
194
195     from_version = None
196     to_version = None
197     if global_options.from_version:
198         from_version = global_options.from_version
199     else:
200         guess = guess_lilypond_version (infile_name)
201         if not guess:
202             raise UnknownVersion ()
203         from_version = str_to_tuple (guess)
204
205     if global_options.to_version:
206         to_version = global_options.to_version
207     else:
208         to_version = latest_version ()
209
210
211     if infile_name:
212         infile = open (infile_name, 'r')
213     else:
214         infile = sys.stdin
215
216
217     (last, result) = do_conversion (infile.read (), from_version, to_version)
218     infile.close ()
219
220     if last:
221         newversion = r'\version "%s"' % tup_to_str (last)
222         if lilypond_version_re.search (result):
223             result = re.sub (lilypond_version_re_str,
224                      '\\' + newversion, result)
225         elif not global_options.skip_version_add:
226             result = newversion + '\n' + result
227             
228         error_file.write ('\n')            
229     
230         if global_options.edit:
231             try:
232                 os.remove(infile_name + '~')
233             except:
234                 pass
235             os.rename (infile_name, infile_name + '~')
236             outfile = open (infile_name, 'w')
237         else:
238             outfile = sys.stdout
239
240
241         outfile.write (result)
242
243     sys.stderr.flush ()
244
245 def do_options ():
246     opt_parser = get_option_parser()
247     (options, args) = opt_parser.parse_args ()
248
249
250     if options.from_version:
251         options.from_version = str_to_tuple (options.from_version)
252     if options.to_version:
253         options.to_version = str_to_tuple (options.to_version)
254
255     options.outfile_name = ''
256     global global_options
257     global_options = options
258
259     if not args and not options.show_rules:
260         opt_parser.print_help ()
261         sys.exit (2)
262
263     return args
264
265 def main ():
266     files = do_options ()
267
268     # should parse files[] to read \version?
269     if global_options.show_rules:
270         show_rules (sys.stdout, global_options.from_version, global_options.to_version)
271         sys.exit (0)
272
273     identify (sys.stderr)
274
275     for f in files:
276         if f == '-':
277             f = ''
278         elif not os.path.isfile (f):
279             error (_ ("can't open file: `%s'") % f)
280             if len (files) == 1:
281                 sys.exit (1)
282             continue
283         try:
284             do_one_file (f)
285         except UnknownVersion:
286             error (_ ("can't determine version for `%s'. Skipping") % f)
287
288     sys.stderr.write ('\n')
289
290 main ()