3 # convert-ly.py -- convertor for lilypond versions
5 # source file of the GNU LilyPond music typesetter
10 # use -f and -t for -s output
16 program_name = 'convert-ly'
17 version = '@TOPLEVEL_VERSION@'
27 # Did we ever have \mudela-version? I doubt it.
28 # lilypond_version_re_str = '\\\\version *\"(.*)\"'
29 lilypond_version_re_str = '\\\\(mudela-)?version *\"(.*)\"'
30 lilypond_version_re = re.compile (lilypond_version_re_str)
33 return '%s (GNU LilyPond) %s' %(program_name, version);
36 sys.stderr.write (program_id () + '\n')
40 r"""Usage: %s [OPTION]... [FILE]...
41 Try to convert to newer lilypond-versions. The version number of the
42 input is guessed by default from \version directive
45 -a, --assume-old apply all conversions to unversioned files
46 -h, --help print this help
47 -e, --edit in place edit
48 -f, --from=VERSION start from version
49 -s, --show-rules print all rules.
50 -t, --to=VERSION target version
51 --version print program version
53 Report bugs to bugs-gnu-music@gnu.org
62 sys.stdout.write (r"""%s
64 This is free software. It is covered by the GNU General Public
65 License, and you are welcome to change it and/or distribute copies of
66 it under certain conditions. invoke as `%s --warranty' for more
69 """ % (program_id() , program_name))
78 print 'can\'t open file: ' + f + '\n'
82 print 'gulped empty file: ' + f + '\n'
87 return tuple (map (string.atoi, string.split (s,'.')))
90 return string.join (map (lambda x: '%s' % x, list (t)), '.')
92 def version_cmp (t1, t2):
98 def guess_lilypond_version (filename):
99 s = gulp_file (filename)
100 m = lilypond_version_re.search (s)
106 class FatalConversionError:
111 def show_rules (file):
112 for x in conversions:
113 file.write ('%s: %s\n' % (tup_to_str (x[0]), x[2]))
115 ############################
119 if re.search ('\\\\multi', str):
120 sys.stderr.write ('\nNot smart enough to convert \\multi')
123 conversions.append (((0,1,9), conv, '\\header { key = concat + with + operator }'))
125 if 1: # need new a namespace
127 if re.search ('\\\\octave', str):
128 sys.stderr.write ('\nNot smart enough to convert \\octave')
129 # raise FatalConversionError()
133 conversions.append ((
134 ((0,1,19), conv, 'deprecated \\octave; can\'t convert automatically')))
137 if 1: # need new a namespace
139 str = re.sub ('\\\\textstyle([^;]+);',
140 '\\\\property Lyrics . textstyle = \\1', str)
141 # harmful to current .lys
142 # str = re.sub ('\\\\key([^;]+);', '\\\\accidentals \\1;', str)
146 conversions.append ((
147 ((0,1,20), conv, 'deprecated \\textstyle, new \key syntax')))
152 str = re.sub ('\\\\musical_pitch', '\\\\musicalpitch',str)
153 str = re.sub ('\\\\meter', '\\\\time',str)
157 conversions.append ((
158 ((0,1,21), conv, '\\musical_pitch -> \\musicalpitch, '+
159 '\\meter -> \\time')))
165 conversions.append ((
166 ((1,0,0), conv, '0.1.21 -> 1.0.0 ')))
171 str = re.sub ('\\\\accidentals', '\\\\keysignature',str)
172 str = re.sub ('specialaccidentals *= *1', 'keyoctaviation = 0',str)
173 str = re.sub ('specialaccidentals *= *0', 'keyoctaviation = 1',str)
177 conversions.append ((
178 ((1,0,1), conv, '\\accidentals -> \\keysignature, ' +
179 'specialaccidentals -> keyoctaviation')))
183 if re.search ('\\\\header', str):
184 sys.stderr.write ('\nNot smart enough to convert to new \\header format')
187 conversions.append (((1,0,2), conv, '\\header { key = concat + with + operator }'))
191 str = re.sub ('\\\\melodic([^a-zA-Z])', '\\\\notes\\1',str)
194 conversions.append (((1,0,3), conv, '\\melodic -> \\notes'))
198 str = re.sub ('default_paper *=', '',str)
199 str = re.sub ('default_midi *=', '',str)
202 conversions.append (((1,0,4), conv, 'default_{paper,midi}'))
206 str = re.sub ('ChoireStaff', 'ChoirStaff',str)
207 str = re.sub ('\\\\output', 'output = ',str)
211 conversions.append (((1,0,5), conv, 'ChoireStaff -> ChoirStaff'))
215 if re.search ('[a-zA-Z]+ = *\\translator',str):
216 sys.stderr.write ('\nNot smart enough to change \\translator syntax')
217 # raise FatalConversionError()
220 conversions.append (((1,0,6), conv, 'foo = \\translator {\\type .. } ->\\translator {\\type ..; foo; }'))
225 str = re.sub ('\\\\lyrics*', '\\\\lyrics',str)
229 conversions.append (((1,0,7), conv, '\\lyric -> \\lyrics'))
233 str = re.sub ('\\\\\\[/3+', '\\\\times 2/3 { ',str)
234 str = re.sub ('\\[/3+', '\\\\times 2/3 { [',str)
235 str = re.sub ('\\\\\\[([0-9/]+)', '\\\\times \\1 {',str)
236 str = re.sub ('\\[([0-9/]+)', '\\\\times \\1 { [',str)
237 str = re.sub ('\\\\\\]([0-9/]+)', '}', str)
238 str = re.sub ('\\\\\\]', '}',str)
239 str = re.sub ('\\]([0-9/]+)', '] }', str)
242 conversions.append (((1,0,10), conv, '[2/3 ]1/1 -> \\times 2/3 '))
247 conversions.append (((1,0,12), conv, 'Chord syntax stuff'))
254 str = re.sub ('<([^>~]+)~([^>]*)>','<\\1 \\2> ~', str)
258 conversions.append (((1,0,13), conv, '<a ~ b> c -> <a b> ~ c'))
262 str = re.sub ('<\\[','[<', str)
263 str = re.sub ('\\]>','>]', str)
267 conversions.append (((1,0,14), conv, '<[a b> <a b]>c -> [<a b> <a b>]'))
272 str = re.sub ('\\\\type([^\n]*engraver)','\\\\TYPE\\1', str)
273 str = re.sub ('\\\\type([^\n]*performer)','\\\\TYPE\\1', str)
274 str = re.sub ('\\\\type','\\\\context', str)
275 str = re.sub ('\\\\TYPE','\\\\type', str)
276 str = re.sub ('textstyle','textStyle', str)
280 conversions.append (((1,0,16), conv, '\\type -> \\context, textstyle -> textStyle'))
285 if re.search ('\\\\repeat',str):
286 sys.stderr.write ('\nNot smart enough to convert \\repeat')
287 # raise FatalConversionError()
290 conversions.append (((1,0,18), conv,
291 '\\repeat NUM Music Alternative -> \\repeat FOLDSTR Music Alternative'))
295 str = re.sub ('SkipBars','skipBars', str)
296 str = re.sub ('fontsize','fontSize', str)
297 str = re.sub ('midi_instrument','midiInstrument', str)
301 conversions.append (((1,0,19), conv,
302 'fontsize -> fontSize, midi_instrument -> midiInstrument, SkipBars -> skipBars'))
307 str = re.sub ('tieydirection','tieVerticalDirection', str)
308 str = re.sub ('slurydirection','slurVerticalDirection', str)
309 str = re.sub ('ydirection','verticalDirection', str)
313 conversions.append (((1,0,20), conv,
314 '{,tie,slur}ydirection -> {v,tieV,slurV}erticalDirection'))
319 str = re.sub ('hshift','horizontalNoteShift', str)
323 conversions.append (((1,0,21), conv,
324 'hshift -> horizontalNoteShift'))
329 str = re.sub ('\\\\grouping[^;]*;','', str)
333 conversions.append (((1,1,52), conv,
334 'deprecate \\grouping'))
339 str = re.sub ('\\\\wheel','\\\\coda', str)
343 conversions.append (((1,1,55), conv,
344 '\\wheel -> \\coda'))
348 str = re.sub ('keyoctaviation','keyOctaviation', str)
349 str = re.sub ('slurdash','slurDash', str)
353 conversions.append (((1,1,65), conv,
354 'slurdash -> slurDash, keyoctaviation -> keyOctaviation'))
358 str = re.sub ('\\\\repeat *\"?semi\"?','\\\\repeat "volta"', str)
362 conversions.append (((1,1,66), conv,
368 str = re.sub ('\"?beamAuto\"? *= *\"?0?\"?','noAutoBeaming = "1"', str)
372 conversions.append (((1,1,67), conv,
373 'beamAuto -> noAutoBeaming'))
377 str = re.sub ('automaticMelismas', 'automaticMelismata', str)
381 conversions.append (((1,2,0), conv,
382 'automaticMelismas -> automaticMelismata'))
386 str = re.sub ('dynamicDir\\b', 'dynamicDirection', str)
390 conversions.append (((1,2,1), conv,
391 'dynamicDir -> dynamicDirection'))
395 str = re.sub ('\\\\cadenza *0 *;', '\\\\cadenzaOff', str)
396 str = re.sub ('\\\\cadenza *1 *;', '\\\\cadenzaOn', str)
400 conversions.append (((1,3,4), conv,
401 '\\cadenza -> \cadenza{On|Off}'))
405 str = re.sub ('"?beamAuto([^"=]+)"? *= *"([0-9]+)/([0-9]+)" *;*',
406 'beamAuto\\1 = #(make-moment \\2 \\3)',
410 conversions.append (((1,3,5), conv, 'beamAuto moment properties'))
414 str = re.sub ('stemStyle',
419 conversions.append (((1,3,17), conv, 'stemStyle -> flagStyle'))
423 str = re.sub ('staffLineLeading',
428 conversions.append (((1,3,18), conv, 'staffLineLeading -> staffSpace'))
433 if re.search ('\\\\repetitions',str):
434 sys.stderr.write ('\nNot smart enough to convert \\repetitions')
435 # raise FatalConversionError()
438 conversions.append (((1,3,23), conv,
439 '\\\\repetitions feature dropped'))
444 str = re.sub ('textEmptyDimension *= *##t',
445 'textNonEmpty = ##f',
447 str = re.sub ('textEmptyDimension *= *##f',
448 'textNonEmpty = ##t',
452 conversions.append (((1,3,35), conv, 'textEmptyDimension -> textNonEmpty'))
456 str = re.sub ("([a-z]+)[ \t]*=[ \t]*\\\\musicalpitch *{([- 0-9]+)} *\n",
457 "(\\1 . (\\2))\n", str)
458 str = re.sub ("\\\\musicalpitch *{([0-9 -]+)}",
459 "\\\\musicalpitch #'(\\1)", str)
460 if re.search ('\\\\notenames',str):
461 sys.stderr.write ('\nNot smart enough to convert to new \\notenames format')
464 conversions.append (((1,3,38), conv, '\musicalpitch { a b c } -> #\'(a b c)'))
469 return '\\key %s;' % string.lower (match.group (1))
471 str = re.sub ("\\\\key ([^;]+);", replace, str)
474 conversions.append (((1,3,39), conv, '\\key A ; ->\\key a;'))
478 if re.search ('\\[:',str):
479 sys.stderr.write ('\nNot smart enough to convert to new tremolo format')
482 conversions.append (((1,3,41), conv,
483 '[:16 c4 d4 ] -> \\repeat "tremolo" 2 { c16 d16 }'))
487 str = re.sub ('Staff_margin_engraver' , 'Instrument_name_engraver', str)
490 conversions.append (((1,3,42), conv,
491 'Staff_margin_engraver deprecated, use Instrument_name_engraver'))
495 str = re.sub ('note[hH]eadStyle\\s*=\\s*"?(\\w+)"?' , "noteHeadStyle = #'\\1", str)
498 conversions.append (((1,3,49), conv,
499 'noteHeadStyle value: string -> symbol'))
503 if re.search ('\\\\keysignature', str):
504 sys.stderr.write ('\nNot smart enough to convert to new tremolo format')
508 conversions.append (((1,3,58), conv,
509 'noteHeadStyle value: string -> symbol'))
513 str = re.sub (r"""\\key *([a-z]+) *;""", r"""\\key \1 \major;""",str);
515 conversions.append (((1,3,59), conv,
516 '\key X ; -> \key X major; '))
520 str = re.sub (r'latexheaders *= *"\\\\input ',
524 conversions.append (((1,3,68), conv, 'latexheaders = "\\input global" -> latexheaders = "global"'))
528 ################ TODO: lots of other syntax change should be done here as well
534 str = re.sub ('basicCollisionProperties', 'NoteCollision', str)
535 str = re.sub ('basicVoltaSpannerProperties' , "VoltaBracket", str)
536 str = re.sub ('basicKeyProperties' , "KeySignature", str)
538 str = re.sub ('basicClefItemProperties' ,"Clef", str)
541 str = re.sub ('basicLocalKeyProperties' ,"Accidentals", str)
542 str = re.sub ('basicMarkProperties' ,"Accidentals", str)
543 str = re.sub ('basic([A-Za-z_]+)Properties', '\\1', str)
545 str = re.sub ('Repeat_engraver' ,'Volta_engraver', str)
548 conversions.append (((1,3,92), conv, 'basicXXXProperties -> XXX, Repeat_engraver -> Volta_engraver'))
552 # Ugh, but meaning of \stemup changed too
553 # maybe we should do \stemup -> \stemUp\slurUp\tieUp ?
554 str = re.sub ('\\\\stemup', '\\\\stemUp', str)
555 str = re.sub ('\\\\stemdown', '\\\\stemDown', str)
556 str = re.sub ('\\\\stemboth', '\\\\stemBoth', str)
558 str = re.sub ('\\\\slurup', '\\\\slurUp', str)
559 str = re.sub ('\\\\slurboth', '\\\\slurBoth', str)
560 str = re.sub ('\\\\slurdown', '\\\\slurDown', str)
561 str = re.sub ('\\\\slurdotted', '\\\\slurDotted', str)
562 str = re.sub ('\\\\slurnormal', '\\\\slurNoDots', str)
564 str = re.sub ('\\\\shiftoff', '\\\\shiftOff', str)
565 str = re.sub ('\\\\shifton', '\\\\shiftOn', str)
566 str = re.sub ('\\\\shiftonn', '\\\\shiftOnn', str)
567 str = re.sub ('\\\\shiftonnn', '\\\\shiftOnnn', str)
569 str = re.sub ('\\\\onevoice', '\\\\oneVoice', str)
570 str = re.sub ('\\\\voiceone', '\\\\voiceOne', str)
571 str = re.sub ('\\\\voicetwo', '\\\\voiceTwo', str)
572 str = re.sub ('\\\\voicethree', '\\\\voiceThree', str)
573 str = re.sub ('\\\\voicefour', '\\\\voiceFour', str)
575 # I don't know exactly when these happened...
576 # ugh, we loose context setting here...
577 str = re.sub ('\\\\property *[^ ]*verticalDirection[^=]*= *#?"?(1|(\\\\up))"?', '\\\\stemUp\\\\slurUp\\\\tieUp', str)
578 str = re.sub ('\\\\property *[^ ]*verticalDirection[^=]*= *#?"?((-1)|(\\\\down))"?', '\\\\stemDown\\\\slurDown\\\\tieDown', str)
579 str = re.sub ('\\\\property *[^ ]*verticalDirection[^=]*= *#?"?(0|(\\\\center))"?', '\\\\stemBoth\\\\slurBoth\\\\tieBoth', str)
581 str = re.sub ('verticalDirection[^=]*= *#?"?(1|(\\\\up))"?', 'Stem \\\\override #\'direction = #0\nSlur \\\\override #\'direction = #0\n Tie \\\\override #\'direction = #1', str)
582 str = re.sub ('verticalDirection[^=]*= *#?"?((-1)|(\\\\down))"?', 'Stem \\\\override #\'direction = #0\nSlur \\\\override #\'direction = #0\n Tie \\\\override #\'direction = #-1', str)
583 str = re.sub ('verticalDirection[^=]*= *#?"?(0|(\\\\center))"?', 'Stem \\\\override #\'direction = #0\nSlur \\\\override #\'direction = #0\n Tie \\\\override #\'direction = #0', str)
585 str = re.sub ('\\\\property *[^ .]*[.]?([a-z]+)VerticalDirection[^=]*= *#?"?(1|(\\\\up))"?', '\\\\\\1Up', str)
586 str = re.sub ('\\\\property *[^ .]*[.]?([a-z]+)VerticalDirection[^=]*= *#?"?((-1)|(\\\\down))"?', '\\\\\\1Down', str)
587 str = re.sub ('\\\\property *[^ .]*[.]?([a-z]+)VerticalDirection[^=]*= *#?"?(0|(\\\\center))"?', '\\\\\\1Both', str)
589 # (lacks capitalisation slur -> Slur)
590 str = re.sub ('([a-z]+)VerticalDirection[^=]*= *#?"?(1|(\\\\up))"?', '\\1 \\\\override #\'direction = #1', str)
591 str = re.sub ('([a-z]+)VerticalDirection[^=]*= *#?"?((-1)|(\\\\down))"?', '\\1 \\override #\'direction = #-1', str)
592 str = re.sub ('([a-z]+)VerticalDirection[^=]*= *#?"?(0|(\\\\center))"?', '\\1 \\\\override #\'direction = #0', str)
595 str = re.sub ('\\\\property *[^ .]*[.]?dynamicDirection[^=]*= *#?"?(1|(\\\\up))"?', '\\\\dynamicUp', str)
596 str = re.sub ('\\\\property *[^ .]*[.]?dyn[^=]*= *#?"?((-1)|(\\\\down))"?', '\\\\dynamicDown', str)
597 str = re.sub ('\\\\property *[^ .]*[.]?dyn[^=]*= *#?"?(0|(\\\\center))"?', '\\\\dynamicBoth', str)
599 str = re.sub ('\\\\property *[^ .]*[.]?([a-z]+)Dash[^=]*= *#?"?(0|(""))"?', '\\\\\\1NoDots', str)
600 str = re.sub ('\\\\property *[^ .]*[.]?([a-z]+)Dash[^=]*= *#?"?([1-9]+)"?', '\\\\\\1Dotted', str)
602 str = re.sub ('\\\\property *[^ .]*[.]?noAutoBeaming[^=]*= *#?"?(0|(""))"?', '\\\\autoBeamOn', str)
603 str = re.sub ('\\\\property *[^ .]*[.]?noAutoBeaming[^=]*= *#?"?([1-9]+)"?', '\\\\autoBeamOff', str)
609 conversions.append (((1,3,93), conv,
610 'property definiton case (eg. onevoice -> oneVoice)'))
615 str = re.sub ('ChordNames*', 'ChordNames', str)
616 if re.search ('\\\\textscript "[^"]* *"[^"]*"', str):
617 sys.stderr.write ('\nNot smart enough to convert to new \\textscript markup text')
619 str = re.sub ('\\textscript +("[^"]*")', '\\textscript #\\1', str)
623 conversions.append (((1,3,97), conv, 'ChordName -> ChordNames'))
626 ## TODO: add lots of these
630 str = re.sub ('\\\\property *"?Voice"? *[.] *"?textStyle"? *= *"([^"]*)"', '\\\\property Voice.TextScript \\\\set #\'font-style = #\'\\1', str)
631 str = re.sub ('\\\\property *"?Lyrics"? *[.] *"?textStyle"? *= *"([^"]*)"', '\\\\property Lyrics.LyricText \\\\set #\'font-style = #\'\\1', str)
633 str = re.sub ('\\\\property *"?([^.]+)"? *[.] *"?timeSignatureStyle"? *= *"([^"]*)"', '\\\\property \\1.TimeSignature \\\\override #\'style = #\'\\2', str)
635 str = re.sub ('"?timeSignatureStyle"? *= *#?""', 'TimeSignature \\\\override #\'style = ##f', str)
637 str = re.sub ('"?timeSignatureStyle"? *= *#?"([^"]*)"', 'TimeSignature \\\\override #\'style = #\'\\1', str)
639 str = re.sub ('#\'style *= #*"([^"])"', '#\'style = #\'\\1', str)
641 str = re.sub ('\\\\property *"?([^.]+)"? *[.] *"?horizontalNoteShift"? *= *"?#?([-0-9]+)"?', '\\\\property \\1.NoteColumn \\\\override #\'horizontal-shift = #\\2', str)
644 str = re.sub ('\\\\property *"?([^.]+)"? *[.] *"?flagStyle"? *= *""', '\\\\property \\1.Stem \\\\override #\'flag-style = ##f', str)
646 str = re.sub ('\\\\property *"?([^.]+)"? *[.] *"?flagStyle"? *= *"([^"]*)"', '\\\\property \\1.Stem \\\\override #\'flag-style = #\'\\2', str)
649 conversions.append (((1,3,98), conv, 'CONTEXT.textStyle -> GROB.#font-style '))
653 str = re.sub ('"?beamAutoEnd_([0-9]*)"? *= *(#\\([^)]*\\))', 'autoBeamSettings \\push #\'(end 1 \\1 * *) = \\2', str)
654 str = re.sub ('"?beamAutoBegin_([0-9]*)"? *= *(#\\([^)]*\))', 'autoBeamSettings \\push #\'(begin 1 \\1 * *) = \\2', str)
655 str = re.sub ('"?beamAutoEnd"? *= *(#\\([^)]*\\))', 'autoBeamSettings \\push #\'(end * * * *) = \\1', str)
656 str = re.sub ('"?beamAutoBegin"? *= *(#\\([^)]*\\))', 'autoBeamSettings \\push #\'(begin * * * *) = \\1', str)
661 conversions.append (((1,3,102), conv, 'beamAutoEnd -> autoBeamSettings \\push (end * * * *)'))
666 str = re.sub ('\\\\push', '\\\\override', str)
667 str = re.sub ('\\\\pop', '\\\\revert', str)
671 conversions.append (((1,3,111), conv, '\\push -> \\override, \\pop -> \\revert'))
675 str = re.sub ('LyricVoice', 'LyricsVoice', str)
677 str = re.sub ('Chord[Nn]ames*.Chord[Nn]ames*', 'ChordNames.ChordName', str)
680 conversions.append (((1,3,113), conv, 'LyricVoice -> LyricsVoice'))
682 def regularize_id (str):
689 elif x in string.digits:
690 x = chr(ord (x) - ord ('0') +ord ('A'))
691 elif x not in string.letters:
693 elif x in string.lowercase and lastx == '_':
702 def regularize_dollar_reference (match):
703 return regularize_id (match.group (1))
704 def regularize_assignment (match):
705 return '\n' + regularize_id (match.group (1)) + ' = '
706 str = re.sub ('\$([^\t\n ]+)', regularize_dollar_reference, str)
707 str = re.sub ('\n([^ \t\n]+) = ', regularize_assignment, str)
710 conversions.append (((1,3,117), conv, 'identifier names: $!foo_bar_123 -> xfooBarABC'))
715 def regularize_paper (match):
716 return regularize_id (match.group (1))
718 str = re.sub ('(paper_[a-z]+)', regularize_paper, str)
719 str = re.sub ('sustainup', 'sustainUp', str)
720 str = re.sub ('nobreak', 'noBreak', str)
721 str = re.sub ('sustaindown', 'sustainDown', str)
722 str = re.sub ('sostenutoup', 'sostenutoUp', str)
723 str = re.sub ('sostenutodown', 'sostenutoDown', str)
724 str = re.sub ('unachorda', 'unaChorda', str)
725 str = re.sub ('trechorde', 'treChorde', str)
729 conversions.append (((1,3,120), conv, 'paper_xxx -> paperXxxx, pedalup -> pedalUp.'))
733 str = re.sub ('drarnChords', 'chordChanges', str)
734 str = re.sub ('\\musicalpitch', '\\pitch', str)
737 conversions.append (((1,3,122), conv, 'drarnChords -> chordChanges, \\musicalpitch -> \\pitch'))
741 str = re.sub ('ly-([sg])et-elt-property', 'ly-\\1et-grob-property', str)
744 conversions.append (((1,3,136), conv, 'ly-X-elt-property -> ly-X-grob-property'))
748 str = re.sub ('point-and-click +#t', 'point-and-click line-column-location', str)
751 conversions.append (((1,3,138), conv, 'point-and-click argument changed to procedure.'))
755 str = re.sub ('followThread', 'followVoice', str)
756 str = re.sub ('Thread.FollowThread', 'Voice.VoiceFollower', str)
757 str = re.sub ('FollowThread', 'VoiceFollower', str)
760 conversions.append (((1,3,138), conv, 'followThread -> followVoice.'))
764 str = re.sub ('font-point-size', 'font-design-size', str)
767 conversions.append (((1,3,139), conv, 'font-point-size -> font-design-size.'))
771 str = re.sub ('([a-zA-Z]*)NoDots', '\\1Solid', str)
774 conversions.append (((1,3,141), conv, 'xNoDots -> xSolid'))
777 ############################
780 def get_conversions (from_version, to_version):
781 def version_b (v, f = from_version, t = to_version):
782 return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
783 return filter (version_b, conversions)
786 def latest_version ():
787 return conversions[-1][0]
789 def do_conversion (infile, from_version, outfile, to_version):
790 conv_list = get_conversions (from_version, to_version)
792 sys.stderr.write ('Applying conversions: ')
797 sys.stderr.write (tup_to_str (x[0]) + ', ')
799 last_conversion = x[0]
801 except FatalConversionError:
802 sys.stderr.write ('Error while converting; I won\'t convert any further')
805 sys.stderr.write ('\n')
806 new_ver = '\\version \"%s\"' % tup_to_str (last_conversion)
808 # ugh: this all really doesn't help
809 # esp. as current conversion rules are soo incomplete
810 if re.search (lilypond_version_re_str, str):
811 str = re.sub (lilypond_version_re_str,'\\'+new_ver , str)
813 # str = new_ver + '\n' + str
817 return last_conversion
819 class UnknownVersion:
822 def do_one_file (infile_name):
823 sys.stderr.write ('Processing `%s\' ... '% infile_name)
826 outfile_name = infile_name + '.NEW'
827 elif __main__.outfile_name:
828 outfile_name = __main__.outfile_name
830 if __main__.from_version:
831 from_version = __main__.from_version
833 guess = guess_lilypond_version (infile_name)
835 raise UnknownVersion()
836 from_version = str_to_tuple (guess)
838 if __main__.to_version:
839 to_version = __main__.to_version
841 to_version = latest_version ()
845 infile = open (infile_name,'r')
850 outfile = open (outfile_name, 'w')
854 touched = do_conversion (infile, from_version, outfile, to_version)
862 if __main__.edit and touched:
864 os.remove(infile_name + '~')
867 os.rename (infile_name, infile_name + '~')
868 os.rename (infile_name + '.NEW', infile_name)
870 sys.stderr.write ('\n')
879 (options, files) = getopt.getopt (
880 sys.argv[1:], 'ao:f:t:seh', ['assume-old', 'version', 'output', 'show-rules', 'help', 'edit', 'from=', 'to='])
885 if o== '--help' or o == '-h':
888 if o == '--version' or o == '-v':
891 elif o== '--from' or o=='-f':
892 from_version = str_to_tuple (a)
893 elif o== '--to' or o=='-t':
894 to_version = str_to_tuple (a)
895 elif o== '--edit' or o == '-e':
897 elif o== '--show-rules' or o == '-s':
898 show_rules (sys.stdout)
900 elif o == '--output' or o == '-o':
902 elif o == '--assume-old' or o == '-a':
914 except UnknownVersion:
915 sys.stderr.write ('\n')
916 sys.stderr.write ("%s: can't determine version for %s" % (program_name, f))
917 sys.stderr.write ('\n')
920 from_version = (0,0,0)
924 sys.stderr.write ("%s: skipping: %s " % (program_name, f))
926 sys.stderr.write ('\n')