2 # lily-wins.py -- LilyPond command for .ly on Windows
13 print 'Usage [-h,--help] lily-wins LY-FILE'
15 # print debugging stuff for now
20 def read_pipe (command):
21 debug ('command:' + command)
22 s = os.popen (command).read ()
23 if s and s[-1] == '\n':
28 debug ('command:' + command)
31 def strip_extension (f, ext):
32 (p, e) = os.path.splitext (f)
38 return re.sub ("(\s|[`'\"\\\\])", r'\\\1',x)
39 # return re.sub (r'''([^\\])([`'"\\\s])''', r'\1\\\2', x)
40 # help emacs'" broken python mode
43 print '''lily-wins [options] file
47 -h, --help this help screen
48 -d, --debug print debugging information
56 (opts, files)=getopt.getopt (sys.argv[1:],'dh', ['help', 'debug'])
59 if o == '-d' or o == '--debug':
61 elif o == '-h' or o == '--help':
69 native_file = files[0]
70 print 'Processing %s ...' % native_file
71 file = read_pipe ('/usr/bin/cygpath -au %s' % escape_shell (native_file))
77 dir = os.path.dirname (file)
80 base = os.path.basename (file)
81 stem = strip_extension (base, '.ly')
84 native_base = '%(dir)s/%(stem)s' % vars ()
85 native_base = read_pipe ('/usr/bin/cygpath -aw %s' % escape_shell (native_base))
88 native_base = '%(dir)s/%(stem)s' % vars ()
90 pdfname = read_pipe ('/usr/bin/regtool get /root/.pdf/')
91 pdfopencommand = read_pipe ('/usr/bin/regtool get /root/%s/shell/open/command/' % escape_shell (pdfname))
94 native_view = re.sub ('"([^"]*).*"', '\\1', pdfopencommand)
96 native_view = 'acrobat'
99 pdfview = read_pipe ('/usr/bin/cygpath -au %s' % escape_shell (native_view))
102 sys.stderr.write ('no pdf viewer found\n')
106 pdffile = '%(stem)s.pdf' % vars ()
107 if os.path.exists (pdffile):
111 script = '/usr/bin/lilypond'
113 if os.path.exists ('/usr/bin/ly2dvi'):
114 script = '/usr/bin/ly2dvi'
116 stat = system ('%s -p %s > %s.log 2>&1' % (script, escape_shell (base),
117 escape_shell (stem)))
119 if not os.path.exists (pdffile):
121 sys.stderr.write ('PDF output not found. Error log: \n')
123 map (sys.stderr.write, open (stem + '.log').readlines ()[-20:])
124 sys.stderr.write ('A full log is in the file %s.log\n' % stem)
125 sys.stderr.write ('\n\nPress enter to close window\n')
126 sys.stdin.readline ()
129 # run even if failed, to make sure that error
130 system ('%s %s.pdf' % (escape_shell (pdfview), escape_shell (native_base)))