]> git.donarmstrong.com Git - lilypond.git/blob - cygwin/lily-wins.py
Merge branch 'cvs-head' of http://lilypond.org/vc/lilypond into master-hanwen
[lilypond.git] / cygwin / lily-wins.py
1 #!@PYTHON@
2 # lily-wins.py -- LilyPond command for .ly on Windows
3
4 import getopt
5 import os
6 import re
7 import sys
8 import time
9
10 do_debug = 0
11
12 def usage ():
13         print 'Usage [-h,--help] lily-wins LY-FILE'
14
15 # print debugging stuff for now
16 def debug (s):
17         if do_debug:
18                 print s
19
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 
26
27 def system (command):
28         debug ('command:' + command)
29         os.system (command)
30
31 def strip_extension (f, ext):
32         (p, e) = os.path.splitext (f)
33         if e == ext:
34                 e = ''
35         return p + e
36
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
41
42 def usage ():
43         print '''lily-wins [options] file
44
45 Options supported:
46
47   -h, --help      this help screen
48   -d, --debug     print debugging information
49   
50 '''
51         
52 debug (`sys.argv`)
53
54 ########
55 # main
56 (opts, files)=getopt.getopt (sys.argv[1:],'dh', ['help', 'debug'])
57
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)
64
65 if files == []:
66         usage ()
67         sys.exit (1)
68         
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
74
75 cwd = os.getcwd ()
76
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 ()`)
83
84 native_base = '%(dir)s/%(stem)s' % vars ()
85 native_base = read_pipe ('/usr/bin/cygpath -aw %s' % escape_shell (native_base))
86                          
87 if not native_base:
88         native_base = '%(dir)s/%(stem)s' % vars ()
89
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))
92
93 # hmm
94 native_view = re.sub ('"([^"]*).*"', '\\1', pdfopencommand)
95 if not native_view:
96         native_view = 'acrobat'
97         
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'
104
105 os.chdir (dir)
106 pdffile = '%(stem)s.pdf' % vars ()
107 if os.path.exists (pdffile):
108         os.unlink (pdffile)
109
110
111 script = '/usr/bin/lilypond'
112
113 if os.path.exists ('/usr/bin/ly2dvi'):
114         script = '/usr/bin/ly2dvi'
115
116 stat = system ('%s %s > %s.lylog 2>&1' % (script, escape_shell (base),
117                                     escape_shell (stem)))
118
119 if not os.path.exists (pdffile):
120         # message box?
121         sys.stderr.write ('PDF output not found. Error log: \n')
122
123         map (sys.stderr.write, open (stem + '.lylog').readlines ()[-20:])
124         sys.stderr.write ('A full log is in the file %s.lylog\n' % stem)
125         sys.stderr.write ('\n\nPress enter to close window\n')
126         sys.stdin.readline ()
127 else:
128         
129         # run even if failed, to make sure that error is visible.
130         system ('%s %s.pdf' % (escape_shell (pdfview), escape_shell (native_base)))