#!/bin/python # lilytex-wins.py -- LilyPond command for .lytex on Windows # By Joshua Leung (address@hidden), based on lily-wins.py # useful for LilyPond 2.4.2 - Recent lower versions may also work. # To make it work with a windows right-click, add this to file-type associations # for the .lytex extension. Change the cygwin directory to the correct place for # your system. Also, place this in the right place. # C:\cygwin\bin\bash.exe --login -c '/bin/lilytex-wins "%1"' # Coded 12 April 2005. # 12/04/05 - It works with files in a user's home dir. Not sure about other places. # - EDIT: it falls down when path has spaces. why, I don't know. how to fix, I don't know. # - after a short break, it works! # importing - removed import time import getopt import os import re import sys def usage (): print 'Usage [-h,--help] lilytex-wins LYTEX-FILE' def read_pipe (command): s = os.popen (command).read () if s and s[-1] == '\n': return s[:-1] return s def system (command): os.system (command) def strip_extension (f, ext): (p, e) = os.path.splitext (f) if e == ext: e = '' return p + e def escape_shell (x): return re.sub ("(\s|[`'\"\\\\])", r'\\\1',x) ######## # main # the code around here could do with a shake up! - but it is currently working. filea = sys.argv[1] native_file = filea print 'Processing %s ...' % native_file file = read_pipe ('/usr/bin/cygpath -au %s' % escape_shell (native_file)) if not file: file = native_file cwd = os.getcwd () dir = os.path.dirname (file) if not dir: dir = '.' base = os.path.basename (file) # just a few changes here if base.find('.lytex') == -1: stem = strip_extension (base, '.LYTEX') else: stem = strip_extension (base, '.lytex') native_base = '%(dir)s/%(stem)s' % vars () native_base = read_pipe ('/usr/bin/cygpath -aw %s' % escape_shell (native_base)) pdf_base = '%(dir)s/out/%(stem)s' % vars () pdf_base = read_pipe ('/usr/bin/cygpath -aw %s' % escape_shell (pdf_base)) if not native_base: native_base = '%(dir)s/%(stem)s' % vars () pdfname = read_pipe ('/usr/bin/regtool get /root/.pdf/') pdfopencommand = read_pipe ('/usr/bin/regtool get /root/%s/shell/open/command/' % escape_shell (pdfname)) # hmm - pdf stuff native_view = re.sub ('"([^"]*).*"', '\\1', pdfopencommand) if not native_view: native_view = 'acrobat' if native_view: pdfview = read_pipe ('/usr/bin/cygpath -au %s' % escape_shell (native_view)) if not pdfview: # message box? sys.stderr.write ('no pdf viewer found\n') pdfview = 'xpdf' os.chdir (dir) pdffile = '%(stem)s.pdf' % vars () if os.path.exists ('out'): os.chdir ('out') if os.path.exists (pdffile): # why do we have this functionality anyway??? os.unlink (pdffile) os.chdir (dir) # reset to # my major changes from this point onwards script = '/usr/bin/lilypond-book --output=out' exeA = '/usr/bin/latex' exeB = '/usr/bin/dvips -u+lilypond -u+ec-mftrace' scriptB = '/usr/bin/ps2pdf' if os.path.exists ('/usr/bin/lilypond-book'): script = '/usr/bin/lilypond-book --output=out' # run lilypond-book on the lytex file statA = system ('%s %s' % (script, escape_shell (base))) os.chdir ('out') # change the working directory statC = system ('%s %s.tex' % (exeA, escape_shell (stem))) statD = system ('%s %s.dvi' % (exeB, escape_shell (stem))) statE = system ('%s %s.ps' % (scriptB, escape_shell (stem))) # later, we might to make a copy of this PDF, and place it with the .lytex if not os.path.exists (pdffile): print 'PDF output not found. An error occured.' print 'Press enter to close window' sys.stdin.readline () else: system ('%s %s.pdf' % (escape_shell (pdfview), escape_shell (pdf_base)))