new file. (Thanks Hendrik Maryns)
[lilypond.git] / cygwin / lily-wins.py
blob06d22595e54c277e53bf101dac4f405bfe1f104b
1 #!@PYTHON@
2 # lily-wins.py -- LilyPond command for .ly on Windows
4 import getopt
5 import os
6 import re
7 import sys
8 import time
10 do_debug = 0
12 def usage ():
13 print 'Usage [-h,--help] lily-wins LY-FILE'
15 # print debugging stuff for now
16 def debug (s):
17 if do_debug:
18 print s
20 def read_pipe (command):
21 debug ('command:' + command)
22 s = os.popen (command).read ()
23 if s and s[-1] == '\n':
24 return s[:-1]
25 return s
27 def system (command):
28 debug ('command:' + command)
29 os.system (command)
31 def strip_extension (f, ext):
32 (p, e) = os.path.splitext (f)
33 if e == ext:
34 e = ''
35 return p + e
37 def escape_shell (x):
38 return re.sub ("(\s|[`'\"\\\\])", r'\\\1',x)
39 # return re.sub (r'''([^\\])([`'"\\\s])''', r'\1\\\2', x)
40 # help emacs'" broken python mode
42 def usage ():
43 print '''lily-wins [options] file
45 Options supported:
47 -h, --help this help screen
48 -d, --debug print debugging information
50 '''
52 debug (`sys.argv`)
54 ########
55 # main
56 (opts, files)=getopt.getopt (sys.argv[1:],'dh', ['help', 'debug'])
58 for (o,a) in opts:
59 if o == '-d' or o == '--debug':
60 do_debug = 1
61 elif o == '-h' or o == '--help':
62 usage ()
63 sys.exit (0)
65 if files == []:
66 usage ()
67 sys.exit (1)
69 native_file = files[0]
70 print 'Processing %s ...' % native_file
71 file = read_pipe ('/usr/bin/cygpath -au %s' % escape_shell (native_file))
72 if not file:
73 file = native_file
75 cwd = os.getcwd ()
77 dir = os.path.dirname (file)
78 if not dir:
79 dir = '.'
80 base = os.path.basename (file)
81 stem = strip_extension (base, '.ly')
82 debug ( `vars ()`)
84 native_base = '%(dir)s/%(stem)s' % vars ()
85 native_base = read_pipe ('/usr/bin/cygpath -aw %s' % escape_shell (native_base))
87 if not 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))
93 # hmm
94 native_view = re.sub ('"([^"]*).*"', '\\1', pdfopencommand)
95 if not native_view:
96 native_view = 'acrobat'
98 if native_view:
99 pdfview = read_pipe ('/usr/bin/cygpath -au %s' % escape_shell (native_view))
100 if not pdfview:
101 # message box?
102 sys.stderr.write ('no pdf viewer found\n')
103 pdfview = 'xpdf'
105 os.chdir (dir)
106 pdffile = '%(stem)s.pdf' % vars ()
107 if os.path.exists (pdffile):
108 os.unlink (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):
120 # message box?
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 ()
127 else:
129 # run even if failed, to make sure that error
130 system ('%s %s.pdf' % (escape_shell (pdfview), escape_shell (native_base)))